예제 #1
0
  /**
   * Gets elements depending on the format specified.
   *
   * @param format The format specification of the elements to be returned.
   * @return a List of all elements in MetaDB with the specified format.
   */
  public List<Element> getElementList(String format) {
    List<Element> elementList = new ArrayList<Element>();

    Connection conn = Conn.initialize();
    if (conn != null) {
      try {
        PreparedStatement getElementListQuery = conn.prepareStatement(GET_ELEMENT_LIST_BY_FORMAT);
        getElementListQuery.setString(1, format);

        ResultSet getElementListQueryResult = getElementListQuery.executeQuery();

        while (getElementListQueryResult.next())
          elementList.add(
              new Element(
                  getElementListQueryResult.getString(Global.ELEMENT),
                  getElementListQueryResult.getString(Global.ELEMENT_FORMAT)));

        getElementListQueryResult.close();
        getElementListQuery.close();
        conn.close();
      } catch (Exception e) {
        MetaDbHelper.logEvent(e);
      }
    }
    return elementList;
  }
 /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   // TODO Auto-generated method stub
   PrintWriter out = response.getWriter();
   HttpSession session = request.getSession(false);
   if (session != null) {
     String projname = (String) session.getAttribute(Global.SESSION_PROJECT);
     int id = -1;
     try {
       id = Integer.parseInt(request.getParameter("id"));
     } catch (NumberFormatException e) {
       id = -1;
     }
     String notification = "Attr was deleted before save, or internal error occurred.";
     try {
       // MetaDbHelper.note("Retrieved attribute ID: "+id);
       if (id != -1)
         if (AdminDescAttributesDAO.deleteAttribute(id))
           notification = "Admin/Desc attr " + id + " deleted successfully, project: " + projname;
       out.print(notification);
     } catch (Exception e) {
       MetaDbHelper.logEvent(this.getClass().toString(), e);
     }
   } else out.print("Session Expired... Please Login");
   out.flush();
 }
예제 #3
0
 /**
  * Check if an element exists in the system.
  *
  * @param elementName The element to be checked for existence.
  * @return true if the element exists, false otherwise.
  */
 public static boolean elementExists(String elementName) {
   try {
     ArrayList<String> elementList = (ArrayList<String>) getElementList();
     for (String element : elementList) {
       if (element.equals(elementName)) return true;
     }
   } catch (Exception e) {
     MetaDbHelper.logEvent(e);
   }
   return false;
 }
예제 #4
0
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   // TODO Auto-generated method stub
   PrintWriter out = response.getWriter();
   HttpSession session = request.getSession(false);
   try {
     if (session != null) {
       String project = (String) session.getAttribute(Global.SESSION_PROJECT);
       if (project.equals("") && project == null)
         out.println("Please choose a project from the Home tab");
       else out.print(project);
     }
   } catch (Exception e) {
     MetaDbHelper.logEvent(this.getClass().toString(), e);
   }
   out.flush();
 }
예제 #5
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {

      String vocabName = request.getParameter("vocab-name");
      String filename_ext = ".txt";

      File tempDir = (File) getServletContext().getAttribute("javax.servlet.context.tempdir");
      // MetaDbHelper.note(tempDir.getCanonicalPath());
      // create a temporary file in that directory
      File tempFile = File.createTempFile(vocabName, filename_ext, tempDir);
      FileWriter fw = new FileWriter(tempFile);
      Set<String> vocabList = ControlledVocabDAO.getControlledVocab(vocabName);
      for (String v : vocabList) fw.write(v + "\r\n");
      fw.flush();
      fw.close();

      int length = 0;
      ServletOutputStream op = response.getOutputStream();
      ServletContext context = getServletConfig().getServletContext();
      String mimetype = context.getMimeType(tempFile.getName());

      response.setContentType((mimetype != null) ? mimetype : "application/x-download");
      response.setContentLength((int) tempFile.length());
      response.setHeader(
          "Content-Disposition", "attachment; filename=\"" + vocabName + filename_ext + "\"");
      // MetaDbHelper.note(vocabName + filename_ext);
      byte[] bbuf = new byte[1024];
      DataInputStream in = new DataInputStream(new FileInputStream(tempFile));

      while ((in != null) && ((length = in.read(bbuf)) != -1)) {
        op.write(bbuf, 0, length);
      }

      in.close();
      op.flush();
      op.close();
      if (tempFile.delete()) {
        // MetaDbHelper.note("Temp export file deleted");
      }
    } catch (Exception e) {
      MetaDbHelper.logEvent(e);
      // MetaDbHelper.logEvent("ExportVocab", "doPost",
      // getServletContext().getAttribute("javax.servlet.context.tempdir").toString());
    }
  }
예제 #6
0
  /**
   * Deletes an element from MetaDB.
   *
   * @param elementName The desired element name to be added
   * @return true if the element with name elementName was deleted, false otherwise
   */
  public static boolean deleteElement(String elementName) {
    Connection conn = Conn.initialize();
    if (conn != null) {
      try {
        PreparedStatement deleteElement = conn.prepareStatement(DELETE_ELEMENT);

        deleteElement.setString(1, elementName);
        deleteElement.executeUpdate();

        deleteElement.close();
        conn.close();

        return true;
      } catch (Exception e) {
        MetaDbHelper.logEvent(e);
      }
    }
    return false;
  }
예제 #7
0
  /**
   * Returns all the elements in the database.
   *
   * @return a List of all elements in MetaDB.
   */
  public static ArrayList<String> getElementList() {
    ArrayList<String> elementList = new ArrayList<String>();

    Connection conn = Conn.initialize();
    if (conn != null) {
      try {

        PreparedStatement getElementListQuery = conn.prepareStatement(GET_ELEMENT_LIST);
        ResultSet getElementListQueryResult = getElementListQuery.executeQuery();

        while (getElementListQueryResult.next())
          elementList.add(getElementListQueryResult.getString(Global.ELEMENT));

        getElementListQueryResult.close();
        getElementListQuery.close();
        conn.close();
      } catch (Exception e) {
        MetaDbHelper.logEvent(e);
      }
    }
    return elementList;
  }
예제 #8
0
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  @SuppressWarnings("unchecked")
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    JSONObject output = new JSONObject();
    String delimiter = "";
    String projname = "";
    String projnotes = "";
    String template = "";
    boolean useTemplate = false;
    boolean disableQualifier = false;
    try {
      if (ServletFileUpload.isMultipartContent(request)) {
        ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
        List fileItemsList = servletFileUpload.parseRequest(request);
        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
        diskFileItemFactory.setSizeThreshold(40960); /* the unit is bytes */

        Iterator it = fileItemsList.iterator();
        InputStream input = null;
        while (it.hasNext()) {
          FileItem fileItem = (FileItem) it.next();
          // MetaDbHelper.note("Field name: "+fileItem.getFieldName());
          // MetaDbHelper.note(fileItem.getString());
          if (fileItem.isFormField()) {
            /*
             * The file item contains a simple name-value pair of a
             * form field
             */
            if (fileItem.getFieldName().equals("delimiter") && delimiter.equals(""))
              delimiter = fileItem.getString();
            else if (fileItem.getFieldName().equals("projname") && projname.equals(""))
              projname = fileItem.getString().replace(" ", "_");
            else if (fileItem.getFieldName().equals("projnotes") && projnotes.equals(""))
              projnotes = fileItem.getString();
            else if (fileItem.getFieldName().equals("project-template-checkbox"))
              useTemplate = true;
            else if (fileItem.getFieldName().equals("project-template"))
              template = fileItem.getString().replace(" ", "_");
            // else if (fileItem.getFieldName().equals("disable-qualifier-checkbox"))
            // disableQualifier = true;
          } else input = fileItem.getInputStream();
        }
        String userName = Global.METADB_USER;

        HttpSession session = request.getSession(false);
        if (session != null) {
          userName = (String) session.getAttribute(Global.SESSION_USERNAME);
        }

        // MetaDbHelper.note("Delimiter: "+delimiter+", projname: "+projname+", projnotes:
        // "+projnotes+", use template or not? "+useTemplate+" template: "+template);
        if (useTemplate) {
          ProjectsDAO.createProject(template, projname, projnotes, "");
          output.put("projname", projname);
          output.put("success", true);
          output.put("message", "Project created successfully based on: " + template);
          SysLogDAO.log(
              userName,
              Global.SYSLOG_PROJECT,
              "User created project " + projname + " with template from " + template);
        } else {
          if (ProjectsDAO.createProject(projname, projnotes)) {
            output.put("projname", projname);
            String delimiterType = "csv";
            if (delimiter.equals("tab")) {
              delimiterType = "tsv";
              disableQualifier = true; // Disable text qualifiers for TSV files.
            }
            // MetaDbHelper.note("Delim: "+delimiterType+", projname: "+projname+", input: "+input);
            if (input != null) {
              Result res =
                  DataImporter.importFile(delimiterType, projname, input, disableQualifier);
              output.put("success", res.isSuccess());
              if (res.isSuccess()) {
                output.put("message", "Data import successfully");
                SysLogDAO.log(
                    userName,
                    Global.SYSLOG_PROJECT,
                    "User created project " + projname + " with file import");
              } else {

                output.put("message", "The following fields have been changed");
                SysLogDAO.log(
                    userName,
                    Global.SYSLOG_PROJECT,
                    "User created project " + projname + " with file import");
                String fields = "";
                ArrayList<String> data = (ArrayList<String>) res.getData();
                for (String field : data) fields += field + ',';
                output.put("fields", fields);
              }
            } else {
              output.put("success", true);
              output.put(
                  "message", "Project created successfully with default fields. No data imported");
              SysLogDAO.log(userName, Global.SYSLOG_PROJECT, "User created project " + projname);
            }
          } else {
            output.put("success", false);
            output.put("message", "Cannot create project");
            SysLogDAO.log(
                userName, Global.SYSLOG_PROJECT, "User failed to create project " + projname);
          }
        }
      } else {
        output.put("success", false);
        output.put("message", "Form is not multi-part");
      }
    } catch (NullPointerException e) {
      try {
        output.put("success", true);
        output.put("message", "Project created successfully");
      } catch (JSONException e1) {
        MetaDbHelper.logEvent(e1);
      }

    } catch (Exception e) {
      MetaDbHelper.logEvent(e);
      try {
        output.put("success", false);
        output.put("message", "Project was not created");
      } catch (JSONException e1) {
        MetaDbHelper.logEvent(e1);
      }
    }
    out.print(output);
    out.flush();
  }