Package org.molgenis.framework.db

Examples of org.molgenis.framework.db.Database


  {
    // System.out.println("FrontControllerAuthenticator LOGIN called");
    try
    {

      Database db = request.getDatabase();

      if (db.getLogin().isAuthenticated())
      {
        return LoginStatus.ALREADY_LOGGED_IN;
      }

      // try to login
      boolean loggedIn = db.getLogin().login(db, username, password);

      // System.out.println("FrontControllerAuthenticator loggedIn: " +
      // loggedIn);

      if (loggedIn)
      {
        // TODO: Missing redirect???
        // Login login = new
        // org.molgenis.auth.DatabaseLogin(request.getDatabase(),
        // "ClusterDemo");

        // store login in session
        request.getRequest().getSession().setAttribute("login", db.getLogin());
        return LoginStatus.SUCCESSFULLY_LOGGED_IN;
      }
      else
      {
        return LoginStatus.AUTHENTICATION_FAILURE;
View Full Code Here


  {
    // System.out.println("FrontControllerAuthenticator LOGOUT called");
    try
    {

      Database db = request.getDatabase();

      if (!db.getLogin().isAuthenticated())
      {
        return LogoutStatus.ALREADY_LOGGED_OUT;
      }

      // logout from database
View Full Code Here

  {
    Tuple tuple0 = mock(Tuple.class);
    when(tuple0.getColNames()).thenReturn(Arrays.asList("Name", "Continent")).getMock();
    when(tuple0.getString("Name")).thenReturn("Netherlands");

    Database db = mock(Database.class);
    String query = "SELECT Name, Continent FROM Country";
    QueryRule queryRule = new QueryRule("Code", Operator.EQUALS, "NLD");
    when(db.sql(query, queryRule)).thenReturn(Arrays.asList(tuple0));
    when(db.sql("SELECT COUNT(*) FROM Country", queryRule)).thenReturn(Arrays.asList(tuple0));

    TupleTable jdbcTable = new JdbcTable(db, query, Arrays.asList(queryRule));
    try
    {
      // check columns
View Full Code Here

    res.getResponse().setBufferSize(10000);
    res.getResponse().setContentType("text/html; charset=UTF-8");

    PrintWriter out = res.getResponse().getWriter();
    Database db = req.getDatabase();

    try
    {

      AuthStatus authStatus = MolgenisServiceAuthenticationHelper.handleAuthentication(req, out);

      if (!authStatus.isShowApi())
      {
        out.println("<html><body>");
        out.println(authStatus.getPrintMe());
        out.println("</body></html>");
      }
      else
      {
        // if no type selected: show data type choice
        if (req.getString(INPUT_DATATYPE) == null)
        {
          out.println("<html><body>");
          authStatus.getPrintMe();
          if (req.getDatabase().getLogin().isAuthenticated())
          {
            out.println(MolgenisServiceAuthenticationHelper.displayLogoutForm());
          }
          showDataTypeChoice(out, req);
          out.println("</body></html>");
        }
        // if no data provided, show csv input form
        else if (req.get(INPUT_DATA) == null && req.get(INPUT_FILE) == null)
        {
          out.println("<html><body>");
          authStatus.getPrintMe();
          if (req.getDatabase().getLogin().isAuthenticated())
          {
            out.println(MolgenisServiceAuthenticationHelper.displayLogoutForm());
          }
          showCsvInputForm(out, req);
          out.println("</body></html>");
        }
        // process request
        else
        {
          processRequest(req, out);
        }
      }
    }
    catch (Exception e)
    {
      out.println(e.getMessage());
      e.printStackTrace();
      logger.error(e.getMessage());
    }
    finally
    {
      out.flush();
      out.close();
      db.close();
    }

    logger.info("servlet took: " + (System.currentTimeMillis() - start_time) + " ms");
    logger.info("------------");
View Full Code Here

    entityClass = req.getDatabase().getClassForName(simpleEntityName);

    String action = req.getString(INPUT_ACTION);

    // create a database
    Database db = req.getDatabase();

    CsvReader csvReader;
    if (req.get(INPUT_DATA) != null)
    {
      csvReader = new CsvReader(new StringReader(req.getString(INPUT_DATA)));
    }
    else if (req.get(INPUT_FILE) != null)
    {
      csvReader = new CsvReader(req.getFile(INPUT_FILE));
    }
    else
      csvReader = null;

    try
    {
      int nRowsChanged = 0;
      if (action.equals("ADD"))
      {
        File temp = File.createTempFile("molgenis", "tab");
        CsvWriter csvWriter = new CsvWriter(temp);
        try
        {
          if (req.get(INPUT_SILENT) != null && req.getBoolean(INPUT_SILENT) == true)
          {
            csvWriter.close();
            csvWriter = null;
          }

          if (req.get(INPUT_DATA) != null)
          {
            logger.info("processing textarea upload...");
            nRowsChanged = db.add(entityClass, csvReader, csvWriter);
          }
          else if (req.get(INPUT_FILE) != null)
          {
            logger.info("processing file upload...");
            nRowsChanged = db.add(entityClass, csvReader, csvWriter);
          }
          else
          {
            logger.error("no input data or input file provided.");
            out.print("ERROR: no input data or input file provided.");
          }
          out.print("Uploaded " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
        finally
        {
          if (csvWriter != null) csvWriter.close();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(temp),
            Charset.forName("UTF-8")));
        try
        {
          String line = null;
          while ((line = reader.readLine()) != null)
          {
            out.println(line);
          }
        }
        finally
        {
          IOUtils.closeQuietly(reader);
        }
        boolean ok = temp.delete();
        if (!ok) logger.warn("failed to delete file: " + temp);
      }
      else if (action.equals("UPDATE"))
      {
        if (req.get(INPUT_DATA) != null)
        {
          nRowsChanged = db.update(entityClass, csvReader);
          out.print("Updated " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
        else if (req.get(INPUT_FILE) != null)
        {
          nRowsChanged = db.update(entityClass, csvReader);
          out.print("Updated " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
      }
      else if (action.equals("REMOVE"))
      {
        if (req.get(INPUT_DATA) != null)
        {
          nRowsChanged = db.remove(entityClass, csvReader);
          out.print("Removed " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
        else if (req.get(INPUT_FILE) != null)
        {
          nRowsChanged = db.remove(entityClass, csvReader);
          out.print("Removed " + formatter.format(nRowsChanged) + " rows of "
              + entityClass.getCanonicalName() + "\n");
        }
      }
      else
View Full Code Here

   * @param response
   */
  public void handleRequest(MolgenisRequest request, MolgenisResponse response) throws DatabaseException, IOException
  {

    Database db = request.getDatabase();
    this.db = db;

    // logout
    final HttpSession session = request.getRequest().getSession();
    if (request.getRequest().getParameter("__action") != null
        && request.getRequest().getParameter("__action").equalsIgnoreCase("Logout"))
    {
      session.setAttribute("application", null);
    }

    // Get application from session
    ApplicationController appController = (ApplicationController) session.getAttribute("application");

    // Login credentials from FrontController
    Login userLogin = request.getDatabase().getLogin();

    // Create GUI if null
    if (appController == null)
    {
      // FIXME: never reached? isLoginRequired is FALSE in both
      // implementations?
      // also, what is request.getRequest().getParameter("logout") ?
      // and when session.isNew() ?
      if ((!userLogin.isAuthenticated() && userLogin.isLoginRequired())
          || (request.getRequest().getParameter("logout") != null && !session.isNew()))
      {
        response.getResponse().setHeader("WWW-Authenticate", "BASIC realm=\"MOLGENIS\"");
        response.getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED);
        session.invalidate();
        return;
      }
      appController = createUserInterface();
    }

    // Always pass login to GUI
    appController.setLogin(userLogin);

    // handle request
    try
    {

      if (Show.SHOW_JQGRID.equals(request.getString(FormModel.INPUT_SHOW)))
      {
        ScreenController<? extends ScreenModel> controller = appController.get(request
            .getString(ScreenModel.INPUT_TARGET));
        controller.handleRequest(db, request, response.getResponse().getOutputStream());
      }
      // action == download an attached file
      // FIXME move to form controllers handlerequest...
      else if (FileInput.ACTION_DOWNLOAD.equals(request.getString(ScreenModel.INPUT_ACTION)))
      {
        // logger.info(requestTuple);

        File file = new File(db.getFilesource() + "/" + request.getString(FileInput.INPUT_CURRENT_DOWNLOAD));

        FileInputStream filestream = new FileInputStream(file);

        response.getResponse().setContentType("application/x-download");
        response.getResponse().setContentLength((int) file.length());
View Full Code Here

      {
        iDisplayStart = req.getInt("iDisplayStart");
      }

      // create a query on entity
      Database db = req.getDatabase();
      Query<?> q = db.query(entityClass);

      // sorting
      // iSortCol_0 = sort column number
      // resolve using mDataProp_1='name'
      String sortField = req.getString("mDataProp_" + req.getString("iSortCol_0"));
View Full Code Here

      Class<? extends Entity> entityClass = req.getDatabase().getClassForName(req.getString("entity"));

      /****** RETRIEVE DATA ******/

      // create a query on entity
      Database db = req.getDatabase();
      Query<?> q = db.query(entityClass);

      // search?
      if (!req.isNull("filter") && !"".equals(req.getString("filter")))
      {
        q.search(req.getString("filter").trim());
View Full Code Here

    res.getResponse().setBufferSize(10000);
    res.getResponse().setContentType("text/html; charset=UTF-8");

    PrintWriter out = res.getResponse().getWriter();
    Database db = req.getDatabase();

    try
    {

      AuthStatus authStatus = MolgenisServiceAuthenticationHelper.handleAuthentication(req, out);

      if (!authStatus.isShowApi())
      {
        out.println("<html><body>");
        out.println(authStatus.getPrintMe());
        out.println("</body></html>");
      }
      else
      {

        String entityName = req.getRequest().getPathInfo().substring(req.getServicePath().length());

        if (entityName.startsWith("/"))
        {
          entityName = entityName.substring(1);
        }

        if (entityName.equals(""))
        {
          out.println("<html><body>");
          out.println(authStatus.getPrintMe());
          if (req.getDatabase().getLogin().isAuthenticated())
          {
            out.println(MolgenisServiceAuthenticationHelper.displayLogoutForm());
          }
          showAvailableDownloads(out, db, req);
          out.println("</body></html>");
        }
        else
        {
          // Check if this entity exists and is downloadable
          List<org.molgenis.model.elements.Entity> downloadableEntities = getDownloadableEntities(db);
          boolean found = false;
          for (int i = 0; i < downloadableEntities.size() && !found; i++)
          {
            String name = downloadableEntities.get(i).getName();
            if ((name != null) && name.equals(entityName))
            {
              found = true;
            }
          }

          if (!found)
          {
            res.getResponse().sendError(404);// NOT FOUND
            return;
          }

          if (req.getRequest().getQueryString() != null
              && req.getRequest().getQueryString().equals("__showQueryDialogue=true"))
          {
            out.println("<html><body>");
            out.println(authStatus.getPrintMe());
            if (req.getDatabase().getLogin().isAuthenticated())
            {
              out.println(MolgenisServiceAuthenticationHelper.displayLogoutForm());
            }
            showFilterableDownload(out, entityName, db);
            out.println("</body></html>");
          }
          else
          {
            executeQuery(out, req, db, entityName);
          }
        }
      }
    }
    catch (Exception e)
    {
      out.println(e.getMessage());
      e.printStackTrace();
      logger.error(e.getMessage());
    }
    finally
    {
      out.flush();
      out.close();
      db.close();
    }

    logger.info("servlet took: " + (System.currentTimeMillis() - start_time));
    logger.info("------------");
View Full Code Here

TOP

Related Classes of org.molgenis.framework.db.Database

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.