/** * This method is to write the submitted application data to a pdfStream * * @param pdDoc Proposal Development Document. * @return ByteArrayOutputStream[] of the submitted application data. * @throws S2SException */ protected List<Printable> getSubmittedPDFStream(ProposalDevelopmentDocument pdDoc) throws S2SException { GrantApplicationDocument submittedDocument; String frmXpath = null; String frmAttXpath = null; grantsGovXmlDirectoryFile = null; try { S2sAppSubmission s2sAppSubmission = getLatestS2SAppSubmission(pdDoc); String submittedApplicationXml = findSubmittedXml(s2sAppSubmission); String submittedApplication = getS2SUtilService().removeTimezoneFactor(submittedApplicationXml); submittedDocument = GrantApplicationDocument.Factory.parse(submittedApplication); } catch (XmlException e) { LOG.error(e.getMessage(), e); throw new S2SException(e); } FormMappingInfo info = null; S2SFormGenerator s2sFormGenerator = null; List<AuditError> errors = new ArrayList<AuditError>(); List<String> sortedNameSpaces = getSortedNameSpaces(pdDoc.getDevelopmentProposal().getS2sOppForms()); boolean formEntryFlag = true; List<Printable> formPrintables = new ArrayList<Printable>(); for (String namespace : sortedNameSpaces) { XmlObject formFragment = null; try { info = new FormMappingLoader().getFormInfo(namespace); formFragment = getFormObject(submittedDocument, info); frmXpath = "//*[namespace-uri(.) = '" + namespace + "']"; frmAttXpath = "//*[namespace-uri(.) = '" + namespace + "']//*[local-name(.) = 'FileLocation' and namespace-uri(.) = 'http://apply.grants.gov/system/Attachments-V1.0']"; } catch (S2SGeneratorNotFoundException e) { // Form generator not available for the namespace continue; } // XmlObject formObject = s2sFormGenerator.getFormObject(formFragment); // if (s2SValidatorService.validate(formObject, errors)) { byte[] formXmlBytes = formFragment.xmlText().getBytes(); S2SFormPrint formPrintable = new S2SFormPrint(); ArrayList<Source> templates = new ArrayList<Source>(); Source xsltSource = new StreamSource(getClass().getResourceAsStream("/" + info.getStyleSheet())); templates.add(xsltSource); formPrintable.setXSLT(templates); // Linkedhashmap is used to preserve the order of entry. Map<String, byte[]> formXmlDataMap = new LinkedHashMap<String, byte[]>(); formXmlDataMap.put(info.getFormName(), formXmlBytes); formPrintable.setXmlDataMap(formXmlDataMap); // S2sAppSubmission submittedS2SAppSubmission = getLatestS2SAppSubmission(pdDoc); S2sApplication s2sApplciation = getBusinessObjectService() .findBySinglePrimaryKey( S2sApplication.class, pdDoc .getDevelopmentProposal() .getProposalNumber()); // submittedS2SAppSubmission.getS2sApplication(); List<S2sAppAttachments> attachmentList = s2sApplciation.getS2sAppAttachmentList(); Map<String, byte[]> formAttachments = new LinkedHashMap<String, byte[]>(); try { XPathExecutor executer = new XPathExecutor(formFragment.toString()); org.w3c.dom.Node d = executer.getNode(frmXpath); org.w3c.dom.NodeList attList = XPathAPI.selectNodeList(d, frmAttXpath); int attLen = attList.getLength(); for (int i = 0; i < attLen; i++) { org.w3c.dom.Node attNode = attList.item(i); String contentId = ((Element) attNode) .getAttributeNS("http://apply.grants.gov/system/Attachments-V1.0", "href"); if (attachmentList != null && !attachmentList.isEmpty()) { for (S2sAppAttachments attAppAttachments : attachmentList) { if (attAppAttachments.getContentId().equals(contentId)) { ByteArrayOutputStream attStream = new ByteArrayOutputStream(); try { attStream.write(getAttContent(pdDoc, attAppAttachments.getContentId())); } catch (IOException e) { LOG.error(e.getMessage(), e); throw new S2SException(e); } StringBuilder attachment = new StringBuilder(); attachment.append(" ATT : "); attachment.append(attAppAttachments.getContentId()); formAttachments.put( attachment.toString(), getAttContent(pdDoc, attAppAttachments.getContentId())); } } } } } catch (Exception e) { LOG.error(e.getMessage(), e); } try { if (pdDoc.getDevelopmentProposal().getGrantsGovSelectFlag()) { List<AttachmentData> attachmentLists = new ArrayList<AttachmentData>(); saveGrantsGovXml(pdDoc, formEntryFlag, formFragment, attachmentLists, attachmentList); formEntryFlag = false; } } catch (Exception e) { LOG.error(e.getMessage(), e); } formPrintable.setAttachments(formAttachments); formPrintables.add(formPrintable); // } } return formPrintables; }
protected void saveGrantsGovXml( ProposalDevelopmentDocument pdDoc, boolean formEntryFlag, XmlObject formObject, List<AttachmentData> attachmentList, List<S2sAppAttachments> attachmentLists) throws Exception { String loggingDirectory = KraServiceLocator.getService(ConfigurationService.class) .getPropertyValueAsString(Constants.PRINT_XML_DIRECTORY); String opportunityId = pdDoc.getDevelopmentProposal().getS2sOpportunity().getOpportunityId(); String proposalnumber = pdDoc.getDevelopmentProposal().getProposalNumber(); String exportDate = StringUtils.replaceChars( (pdDoc.getDevelopmentProposal().getUpdateTimestamp().toString()), ":", "_"); exportDate = StringUtils.replaceChars(exportDate, " ", "."); if (grantsGovXmlDirectoryFile == null) { grantsGovXmlDirectoryFile = new File(loggingDirectory + proposalnumber); } File newgrantsGovXmlDirectoryFile = null; int suffix = 1; while (grantsGovXmlDirectoryFile.exists() && formEntryFlag) { grantsGovXmlDirectoryFile = new File(loggingDirectory + proposalnumber); String filename = String.valueOf(suffix); newgrantsGovXmlDirectoryFile = new File(loggingDirectory + proposalnumber + "-" + filename); if (!newgrantsGovXmlDirectoryFile.exists()) { grantsGovXmlDirectoryFile = newgrantsGovXmlDirectoryFile; break; } suffix++; } if (formEntryFlag) { grantsGovXmlDirectoryFile.mkdir(); } pdDoc.setSaveXmlFolderName(grantsGovXmlDirectoryFile.getName()); for (AttachmentData attachmentData : attachmentList) { File attachmentFile = new File(grantsGovXmlDirectoryFile, "Attachments"); attachmentFile.mkdir(); File attachedFile = new File(attachmentFile, attachmentData.getFileName()); FileOutputStream output = new FileOutputStream(attachedFile); output.write(attachmentData.getContent()); output.close(); } for (S2sAppAttachments attAppAttachments : attachmentLists) { File attachmentFile = new File(grantsGovXmlDirectoryFile, "Attachments"); attachmentFile.mkdir(); AttachmentDataSource ads = getAttributeContent(pdDoc, attAppAttachments.getContentId()); File attachedFile = new File(attachmentFile, ads.getFileName()); FileOutputStream output = new FileOutputStream(attachedFile); output.write(getAttContent(pdDoc, attAppAttachments.getContentId())); output.close(); } File xmlFile = new File( grantsGovXmlDirectoryFile, opportunityId + "." + proposalnumber + "." + exportDate + ".xml"); BufferedWriter out = new BufferedWriter(new FileWriter(xmlFile)); out.write(formObject.xmlText()); out.close(); ZipOutputStream zipOutputStream = null; FileOutputStream fileOutputStream = new FileOutputStream(grantsGovXmlDirectoryFile + ".zip"); zipOutputStream = new ZipOutputStream(fileOutputStream); addFolderToZip("", grantsGovXmlDirectoryFile.getPath(), zipOutputStream); zipOutputStream.flush(); zipOutputStream.close(); }