Esempio n. 1
0
  @Action("NewMapping_input")
  @Override
  public String input() throws Exception {

    if (!user.hasRight(User.SUPER_USER) && !user.hasRight(User.MODIFY_DATA)) {
      addActionError("No mapping rights");
      return ERROR;
    }
    if (user.organization == null) {
      Organization o = DB.getDatasetDAO().findById(uploadId, false).getOrganization();
      this.orgId = o.getDbID();

    } else {
      Organization o = user.organization;
      this.orgId = user.getOrganization().getDbID();
    }
    Dataset du = DB.getDatasetDAO().findById(uploadId, false);
    if (!du.getItemizerStatus().equals(Dataset.ITEMS_OK)) {

      addActionError("You must first define the Item Level and Item Label by choosing step 1.");
      return ERROR;
    }
    findSchemas();

    return super.input();
  }
Esempio n. 2
0
  public List<DataUpload> getImports() {

    Organization org = DB.getOrganizationDAO().findById(this.organizationId, false);

    List<DataUpload> du = new ArrayList<DataUpload>();
    if (this.userId > -1) {
      User u = DB.getUserDAO().findById(userId, false);
      du = DB.getDataUploadDAO().findByOrganizationUser(org, u);
      // check transform status and set boolean here
      return du;
    }
    du = DB.getDataUploadDAO().findByOrganization(org);

    if (du == null) return Collections.emptyList();

    // log.debug("startImport:"+startImport+"  maxImports:"+maxImports);

    return du;
  }
Esempio n. 3
0
 /**
  * Select which uploads are involved in this item Panel call Use it to retrieve items or item
  * count !! 3 cases: - For a specific upload: the uploadID > 1 - For an organization all items
  * (the "normal" case) organizationId > -1 userId == -1 - For a specific user in an org:
  * organizationId and userId > -1
  *
  * @return
  */
 private List<DataUpload> findUploads() {
   List<DataUpload> result = Collections.emptyList();
   if (getUploadId() > -1) {
     log.debug("Items by Upload " + uploadId);
     result = new ArrayList<DataUpload>();
     DataUpload du = DB.getDataUploadDAO().getById(getUploadId(), false);
     result.add(du);
   } else {
     Organization org = DB.getOrganizationDAO().findById(organizationId, false);
     if (org != null) {
       if (getUserId() > -1) {
         log.debug("Items by User " + userId + " and Org " + organizationId);
         User user = DB.getUserDAO().findById(this.userId, false);
         result = DB.getDataUploadDAO().getByUserOrg(user, org);
       } else {
         log.debug("Items by Org " + organizationId);
         result = org.getDataUploads();
       }
     }
   }
   return result;
 }
Esempio n. 4
0
  public boolean checkName(String newname) {
    boolean exists = false;
    try {
      Organization org = user.getOrganization();
      for (Mapping m : DB.getMappingDAO().findByOrganization(org)) {
        if (m.getName().equalsIgnoreCase(newname)) {
          exists = true;
          break;
        }
      }

    } catch (Exception ex) {
      log.debug(" ERROR GETTING MAPPINGS:" + ex.getMessage());
    }
    return exists;
  }
Esempio n. 5
0
  /** allow for filtering the schemas, only do that for non super users */
  public void findSchemas() {
    List<XmlSchema> allSchemas = DB.getXmlSchemaDAO().findAll();

    Set<String> visibleSchemas = null;
    if (!getUser().hasRight(User.ALL_RIGHTS)) {
      String schemaFilter = Config.getWithDefault("schema.filter", "");
      if (!StringUtils.empty(schemaFilter)) {
        visibleSchemas = TextParseUtil.commaDelimitedStringToSet(schemaFilter);
      }
    }

    for (XmlSchema schema : allSchemas) {
      if (schema.getJsonTemplate() != null) {
        if ((visibleSchemas != null) && (!visibleSchemas.contains(schema.getName()))) continue;
        schemas.add(schema);
      }
    }
  }
Esempio n. 6
0
  @Action(value = "AjaxApproval")
  public String execute() throws Exception {

    PrintWriter writer = null;
    try {
      writer = response.getWriter();

    } catch (IOException ex) {
      System.out.println(
          AjaxFileReader.class.getName() + "has thrown an exception: " + ex.getMessage());
    }
    try {

      Long dbID = Long.parseLong(request.getParameter("id"));
      DataUpload du = DB.getDataUploadDAO().getById(dbID, false);
      tr = DB.getTransformationDAO().findOneByUpload(du);

      // new version of the transformation for this session
      if (tr == null) {

        return ERROR;
      } else {
        tr.setIsApproved(Integer.parseInt(request.getParameter("approved")));
        DB.commit();
        String urlParameters =
            request.getParameter("id")
                + "/"
                + request.getParameter("approved")
                + '/'
                + user.getDbID();
        log.debug("Parameters " + urlParameters);
        String request =
            "https://ams.americanarchive.org/mintimport/update_transformed_info/" + urlParameters;
        log.debug("Request URL " + request);
        URL url = new URL(request);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setUseCaches(false);
        connection.setAllowUserInteraction(false);
        connection.setDoOutput(true);
        connection.setReadTimeout(10000);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("charset", "utf-8");
        connection.setUseCaches(false);
        connection.connect();
        int responseCode = connection.getResponseCode();
        log.debug("Response Code " + responseCode);
        log.debug("Nouman Tayyab Bokhari");
        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder sb = new StringBuilder();

        String line = "";
        while ((line = rd.readLine()) != null) {
          sb.append(line + '\n');
        }

        System.out.println(sb.toString());
        connection.disconnect();
      }
    } catch (Exception e) {
      // already handled, but needed to skip readNodes or index if transform or readNodes fails
    } catch (Throwable t) {
      writer.print("uhh " + t);
    }

    response.setStatus(HttpServletResponse.SC_OK);

    writer.flush();
    writer.close();
    return "success";
  }
Esempio n. 7
0
  /**
   * All items for given list of uploads. Respect startItem and maxItem
   *
   * @param uploads
   * @return
   */
  private List<Item> itemsByUploads(List<DataUpload> uploads) {

    if (resultItemList != null) return resultItemList;
    resultItemList = new ArrayList<Item>();
    long currentStart = 0l;
    int itemsRead = 0;
    boolean transform = false;
    for (DataUpload du : uploads) {
      transform = false;
      if (du.getItemXpath() != null) {
        if (DB.getTransformationDAO().findByUpload(du).size() > 0) {
          if (DB.getTransformationDAO().findByUpload(du).get(0).getStatusCode() == 0)
            transform = true;
        }
        if (currentStart + du.getItemXpath().getCount() < startItem) {
          currentStart += du.getItemXpath().getCount();
          continue;
        }
        List<XMLNode> l =
            du.getItemXpath().getNodes(startItem + itemsRead - currentStart, maxItems);
        for (XMLNode x : l) {
          log.debug("get tree .. ");
          XMLNode tree = DB.getXMLNodeDAO().getDOMTree(x);
          boolean trunc = false;
          if (tree.getSize() > 20000) {
            trunc = true;
          }

          log.debug(" . done");
          if (itemsRead < maxItems) {
            List<? extends XMLNode> labelList = tree.getChildrenByXpath(du.getItemLabelXpath());
            if (labelList.size() != 1) log.warn("Label not unique");

            XMLNode label;
            Item i = new Item();
            if (labelList.size() == 1) {
              label = labelList.get(0);
              i.name = label.getContent();
            } else if (labelList.size() == 0) {
              i.name = "<no label>";
            } else {
              i.name = "<" + labelList.size() + " labels>";
            }
            i.truncated = trunc;
            i.uploadId = du.getDbID();
            i.direct = du.isDirect();
            i.nodeId = x.getNodeId();
            i.transformed = transform;
            if (du.getUploadDate() == null) i.itemdate = "";
            else i.itemdate = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(du.getUploadDate());
            i.importname = du.getOriginalFilename();
            /*if( du.getOriginalFilename().length()<15 )
            	i.importname=du.getOriginalFilename();
            else
            	i.importname=".."+du.getOriginalFilename()
            	.substring(du.getOriginalFilename().length()-12);
            	 */
            resultItemList.add(i);
            itemsRead += 1;
            log.debug("adding item:" + itemsRead + " with date:" + i.getDate());
          }
        }
        if (itemsRead == maxItems) break;
        currentStart += du.getItemXpath().getCount();
      }
    }
    endItem = startItem + resultItemList.size();
    return resultItemList;
  }
Esempio n. 8
0
 public void setUserId(long userId) {
   this.userId = userId;
   this.u = DB.getUserDAO().findById(userId, false);
 }
Esempio n. 9
0
 public void setOrganizationId(long organizationId) {
   this.organizationId = organizationId;
   this.o = DB.getOrganizationDAO().findById(organizationId, false);
 }
Esempio n. 10
0
  @Action(value = "NewMapping")
  public String execute() throws Exception {
    if (selaction == null) {
      selaction = "";
    }
    Organization o;
    if (user.organization == null) {
      o = DB.getDatasetDAO().findById(uploadId, false).getOrganization();
      this.orgId = o.getDbID();

    } else {
      o = user.organization;
      this.orgId = user.getOrganization().getDbID();
    }
    findSchemas();
    if ("createschemanew".equals(selaction)) {
      if (mapName == null || mapName.length() == 0) {

        addActionError("Specify a mapping name!");
        return ERROR;
      }

      if (getSchemaSel() <= 0) {

        addActionError("No schema specified!");
        return ERROR;
      }

      Mapping mp = new Mapping();
      mp.setCreationDate(new java.util.Date());
      if (checkName(mapName) == true) {

        addActionError("Mapping name already exists!");
        return ERROR;
      }
      mp.setName(mapName);
      mp.setOrganization(o);
      // mp.setOrganization(user.getOrganization());
      if (getSchemaSel() > 0) {
        long schemaId = getSchemaSel();
        XmlSchema schema = DB.getXmlSchemaDAO().getById(schemaId, false);
        mp.setTargetSchema(schema);
        mp.setJsonString(schema.getJsonTemplate());
      }

      Dataset ds = DB.getDatasetDAO().findById(uploadId, false);

      // apply automatic mappings from schema configuration
      try {
        mp.applyConfigurationAutomaticMappings(ds);
      } catch (Exception e) {
        e.printStackTrace();
      }

      // if automatic mappings are enabled, try to apply them
      if (this.getAutomatic()) {
        try {
          mp.applyAutomaticMappings(ds);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      // if id mappings are required check if schema supports and map them
      if (this.getIdMappings()) {
        try {
          log.debug("Apply id mappings");
          mp.applyConfigurationAutomaticMappings(ds);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      // save mapping name to db and commit

      DB.getMappingDAO().makePersistent(mp);
      DB.commit();
      this.setSelectedMapping(mp.getDbID());
      this.url +=
          "?selectedMapping="
              + this.selectedMapping
              + "&uploadId="
              + this.uploadId
              + "&orgId="
              + this.orgId
              + "&userId="
              + this.user.getDbID()
              + "&selaction="
              + this.getSelaction();

      return "success";
    } else if ("uploadmapping".equals(selaction)) {
      if (this.upfile == null || upfile.length() == 0) {

        addActionError("Please upload a file first!");
        return ERROR;
      }
      if (mapName == null || mapName.length() == 0) {

        addActionError("Specify a mapping name!");
        return ERROR;
      }

      if (getSchemaSel() <= 0) {

        addActionError("No schema specified!");
        return ERROR;
      }

      Mapping mp = new Mapping();
      mp.setCreationDate(new java.util.Date());
      if (checkName(mapName) == true) {

        addActionError("Mapping name already exists!");
        return ERROR;
      }
      mp.setName(mapName);

      mp.setOrganization(o);

      String convertedMapping = null;
      if (upfile != null) {
        try {
          String dir = System.getProperty("java.io.tmpdir") + File.separator;
          File newmapping = new File(dir + upfile);
          StringBuffer contents = StringUtils.fileContents(newmapping);
          Mappings mappings = new Mappings(contents.toString());
          MappingConverter.upgradeToLatest(mappings);
        } catch (Exception e) { // Catch exception if any
          e.printStackTrace();

          System.err.println("Error importing file: " + e.getMessage());
          addActionError("Mappings import failed: " + e.getMessage());
          return ERROR;
        }
      }
      if (getSchemaSel() > 0) {
        long schemaId = getSchemaSel();
        XmlSchema schema = DB.getXmlSchemaDAO().getById(schemaId, false);
        mp.setTargetSchema(schema);

        if (convertedMapping != null) {
          mp.setJsonString(convertedMapping);
        } else {
          mp.setJsonString(schema.getJsonTemplate());
        }
      }

      // save mapping name to db and commit?

      DB.getMappingDAO().makePersistent(mp);
      DB.commit();
      this.setSelectedMapping(mp.getDbID());
      this.url +=
          "?selectedMapping="
              + this.selectedMapping
              + "&uploadId="
              + this.uploadId
              + "&orgId="
              + this.orgId
              + "&userId="
              + this.user.getDbID()
              + "&selaction="
              + this.getSelaction();

      return "success";
    } else if ("uploadxsl".equals(selaction)) {
      if (this.upfile == null || upfile.length() == 0) {

        addActionError("Please upload a file first!");
        return ERROR;
      }
      if (mapName == null || mapName.length() == 0) {

        addActionError("Specify a mapping name!");
        return ERROR;
      }

      Mapping mp = new Mapping();
      mp.setCreationDate(new java.util.Date());
      if (checkName(mapName) == true) {

        addActionError("Mapping name already exists!");
        return ERROR;
      }
      mp.setName(mapName);

      mp.setOrganization(o);

      String xsl = null;

      if (upfile != null) {
        try {
          String dir = System.getProperty("java.io.tmpdir") + File.separator;
          File newmapping = new File(dir + upfile);
          StringBuffer contents = StringUtils.fileContents(newmapping, true);
          xsl = contents.toString();
        } catch (Exception e) { // Catch exception if any
          e.printStackTrace();

          System.err.println("Error importing file: " + e.getMessage());
          addActionError("Mappings import failed: " + e.getMessage());
          return ERROR;
        }
      }

      if (getSchemaSel() > 0) {
        long schemaId = getSchemaSel();
        XmlSchema schema = DB.getXmlSchemaDAO().getById(schemaId, false);
        mp.setTargetSchema(schema);
      }

      if (xsl != null) {
        mp.setXsl(xsl);
      } else {
        System.err.println("Error importing xsl: xsl is null");
        addActionError("Mappings import failed: xsl is null");
        return ERROR;
      }

      // save mapping name to db and commit?

      DB.getMappingDAO().makePersistent(mp);
      DB.commit();
      this.setSelectedMapping(mp.getDbID());
      this.url =
          "successxsl?selectedMapping="
              + this.selectedMapping
              + "&uploadId="
              + this.uploadId
              + "&orgId="
              + this.orgId
              + "&userId="
              + this.user.getDbID()
              + "&selaction="
              + this.getSelaction();

      return "successxsl";
    } else {
      log.error("Unknown action");
      addActionError("Specify a mapping action!");

      return ERROR;
    }
  }
Esempio n. 11
0
 public void setSelectedMapping(long selectedMapping) {
   this.selectedMapping = selectedMapping;
   this.selmapping = DB.getMappingDAO().findById(this.selectedMapping, false);
 }