Пример #1
0
  /** updates the XMl with hashcode for the files */
  protected BudgetSubAwards updateXML(
      byte xmlContents[], Map fileMap, BudgetSubAwards budgetSubAwardBean, Budget budget)
      throws Exception {

    javax.xml.parsers.DocumentBuilderFactory domParserFactory =
        javax.xml.parsers.DocumentBuilderFactory.newInstance();
    javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder();
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents);

    org.w3c.dom.Document document = domParser.parse(byteArrayInputStream);
    byteArrayInputStream.close();
    String namespace = null;
    String formName = null;
    if (document != null) {
      Node node;
      Element element = document.getDocumentElement();
      NamedNodeMap map = element.getAttributes();
      String namespaceHolder =
          element.getNodeName().substring(0, element.getNodeName().indexOf(':'));
      node = map.getNamedItem("xmlns:" + namespaceHolder);
      namespace = node.getNodeValue();
      FormMappingInfo formMappingInfo = new FormMappingLoader().getFormInfo(namespace);
      formName = formMappingInfo.getFormName();
      budgetSubAwardBean.setNamespace(namespace);
      budgetSubAwardBean.setFormName(formName);
    }

    String xpathEmptyNodes =
        "//*[not(node()) and local-name(.) != 'FileLocation' and local-name(.) != 'HashValue']";
    String xpathOtherPers =
        "//*[local-name(.)='ProjectRole' and local-name(../../.)='OtherPersonnel' and count(../NumberOfPersonnel)=0]";
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    removeAllEmptyNodes(document, xpathOtherPers, 1);
    removeAllEmptyNodes(document, xpathEmptyNodes, 0);
    changeDataTypeForNumberOfOtherPersons(document);

    List<String> fedNonFedSubAwardForms = getFedNonFedSubawardForms();
    NodeList budgetYearList =
        XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']");
    for (int i = 0; i < budgetYearList.getLength(); i++) {
      Node bgtYearNode = budgetYearList.item(i);
      String period = getValue(XPathAPI.selectSingleNode(bgtYearNode, "BudgetPeriod"));
      if (fedNonFedSubAwardForms.contains(namespace)) {
        Element newBudgetYearElement =
            copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName());
        bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
      } else {
        Element newBudgetYearElement =
            copyElementToName((Element) bgtYearNode, bgtYearNode.getNodeName() + period);
        bgtYearNode.getParentNode().replaceChild(newBudgetYearElement, bgtYearNode);
      }
    }

    Node oldroot = document.removeChild(document.getDocumentElement());
    Node newroot = document.appendChild(document.createElement("Forms"));
    newroot.appendChild(oldroot);

    org.w3c.dom.NodeList lstFileName = document.getElementsByTagName("att:FileName");
    org.w3c.dom.NodeList lstFileLocation = document.getElementsByTagName("att:FileLocation");
    org.w3c.dom.NodeList lstMimeType = document.getElementsByTagName("att:MimeType");
    org.w3c.dom.NodeList lstHashValue = document.getElementsByTagName("glob:HashValue");

    if ((lstFileName.getLength() != lstFileLocation.getLength())
        || (lstFileLocation.getLength() != lstHashValue.getLength())) {
      //            throw new RuntimeException("Tag occurances mismatch in XML File");
    }

    org.w3c.dom.Node fileNode, hashNode, mimeTypeNode;
    org.w3c.dom.NamedNodeMap fileNodeMap, hashNodeMap;
    String fileName;
    byte fileBytes[];
    String contentId;
    List attachmentList = new ArrayList();

    for (int index = 0; index < lstFileName.getLength(); index++) {
      fileNode = lstFileName.item(index);

      Node fileNameNode = fileNode.getFirstChild();
      fileName = fileNameNode.getNodeValue();

      fileBytes = (byte[]) fileMap.get(fileName);

      if (fileBytes == null) {
        throw new RuntimeException("FileName mismatch in XML and PDF extracted file");
      }
      String hashVal = GrantApplicationHash.computeAttachmentHash(fileBytes);

      hashNode = lstHashValue.item(index);
      hashNodeMap = hashNode.getAttributes();

      Node temp = document.createTextNode(hashVal);
      hashNode.appendChild(temp);

      hashNode = hashNodeMap.getNamedItem("glob:hashAlgorithm");

      hashNode.setNodeValue(S2SConstants.HASH_ALGORITHM);

      fileNode = lstFileLocation.item(index);
      fileNodeMap = fileNode.getAttributes();
      fileNode = fileNodeMap.getNamedItem("att:href");

      contentId = fileNode.getNodeValue();
      String encodedContentId = cleanContentId(contentId);
      fileNode.setNodeValue(encodedContentId);

      mimeTypeNode = lstMimeType.item(0);
      String contentType = mimeTypeNode.getFirstChild().getNodeValue();

      BudgetSubAwardAttachment budgetSubAwardAttachmentBean = new BudgetSubAwardAttachment();
      budgetSubAwardAttachmentBean.setAttachment(fileBytes);
      budgetSubAwardAttachmentBean.setContentId(encodedContentId);

      budgetSubAwardAttachmentBean.setContentType(contentType);
      budgetSubAwardAttachmentBean.setBudgetId(budgetSubAwardBean.getBudgetId());
      budgetSubAwardAttachmentBean.setSubAwardNumber(budgetSubAwardBean.getSubAwardNumber());

      attachmentList.add(budgetSubAwardAttachmentBean);
    }

    budgetSubAwardBean.setBudgetSubAwardAttachments(attachmentList);

    javax.xml.transform.Transformer transformer =
        javax.xml.transform.TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    javax.xml.transform.stream.StreamResult result =
        new javax.xml.transform.stream.StreamResult(bos);
    javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(document);

    transformer.transform(source, result);

    budgetSubAwardBean.setSubAwardXmlFileData(new String(bos.toByteArray()));

    bos.close();

    return budgetSubAwardBean;
  }
Пример #2
0
 /**
  * @see
  *     org.kuali.kra.proposaldevelopment.budget.service.BudgetSubAwardService#updateSubAwardBudgetDetails(org.kuali.kra.budget.core.Budget,
  *     org.kuali.kra.proposaldevelopment.budget.bo.BudgetSubAwards, java.util.List)
  */
 public boolean updateSubAwardBudgetDetails(
     Budget budget, BudgetSubAwards budgetSubAward, List<String[]> errors) throws Exception {
   boolean result = true;
   // extarct xml from the pdf because the stored xml has been modified
   if (budgetSubAward.getSubAwardXfdFileData() == null
       || budgetSubAward.getSubAwardXfdFileData().length == 0) {
     errors.add(new String[] {Constants.SUBAWARD_FILE_NOT_EXTRACTED});
     return false;
   }
   PdfReader reader = new PdfReader(budgetSubAward.getSubAwardXfdFileData());
   byte[] xmlContents = getXMLFromPDF(reader);
   if (xmlContents == null) {
     return false;
   }
   javax.xml.parsers.DocumentBuilderFactory domParserFactory =
       javax.xml.parsers.DocumentBuilderFactory.newInstance();
   javax.xml.parsers.DocumentBuilder domParser = domParserFactory.newDocumentBuilder();
   ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlContents);
   org.w3c.dom.Document document = domParser.parse(byteArrayInputStream);
   NodeList budgetYearList =
       XPathAPI.selectNodeList(document, "//*[local-name(.) = 'BudgetYear']");
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   boolean fnfForm = StringUtils.contains(budgetSubAward.getFormName(), "RR_FedNonFedBudget");
   for (int i = 0; i < budgetYearList.getLength(); i++) {
     Node budgetYear = budgetYearList.item(i);
     Node startDateNode = XPathAPI.selectSingleNode(budgetYear, "BudgetPeriodStartDate");
     if (startDateNode == null) {
       startDateNode = XPathAPI.selectSingleNode(budgetYear, "PeriodStartDate");
     }
     Node endDateNode = XPathAPI.selectSingleNode(budgetYear, "BudgetPeriodEndDate");
     if (endDateNode == null) {
       endDateNode = XPathAPI.selectSingleNode(budgetYear, "PeriodEndDate");
     }
     Date startDate = dateFormat.parse(startDateNode.getTextContent());
     Date endDate = dateFormat.parse(endDateNode.getTextContent());
     // attempt to find a matching budget period
     BudgetSubAwardPeriodDetail periodDetail =
         findBudgetSubAwardPeriodDetail(budget, budgetSubAward, startDate, endDate);
     if (periodDetail != null) {
       Node directCostNode, indirectCostNode, costShareNode = null;
       if (fnfForm) {
         directCostNode = XPathAPI.selectSingleNode(budgetYear, "DirectCosts/FederalSummary");
         indirectCostNode =
             XPathAPI.selectSingleNode(
                 budgetYear, "IndirectCosts/TotalIndirectCosts/FederalSummary");
         costShareNode = XPathAPI.selectSingleNode(budgetYear, "TotalCosts/NonFederalSummary");
       } else {
         directCostNode = XPathAPI.selectSingleNode(budgetYear, "DirectCosts");
         indirectCostNode =
             XPathAPI.selectSingleNode(budgetYear, "IndirectCosts/TotalIndirectCosts");
       }
       if (directCostNode != null) {
         periodDetail.setDirectCost(
             new BudgetDecimal(Float.parseFloat(directCostNode.getTextContent())));
       }
       if (indirectCostNode != null) {
         periodDetail.setIndirectCost(
             new BudgetDecimal(Float.parseFloat(indirectCostNode.getTextContent())));
       }
       if (costShareNode != null) {
         periodDetail.setCostShare(
             new BudgetDecimal(Float.parseFloat(costShareNode.getTextContent())));
       } else {
         periodDetail.setCostShare(BudgetDecimal.ZERO);
       }
       periodDetail.computeTotal();
     } else {
       Node budgetPeriodNode = XPathAPI.selectSingleNode(budgetYear, "BudgetPeriod");
       String budgetPeriod = null;
       if (budgetPeriodNode != null) {
         budgetPeriod = budgetPeriodNode.getTextContent();
       }
       LOG.debug(
           "Unable to find matching period for uploaded period '"
               + budgetPeriod
               + "' -- "
               + startDateNode.getTextContent()
               + " - "
               + endDateNode.getTextContent());
       errors.add(
           new String[] {
             Constants.SUBAWARD_FILE_PERIOD_NOT_FOUND,
             budgetPeriod,
             startDateNode.getTextContent(),
             endDateNode.getTextContent()
           });
     }
   }
   return result;
 }