Exemplo n.º 1
0
  /**
   * This method is used to generate byte stream of forms
   *
   * @param pdDoc ProposalDevelopmentDocument
   * @param bookmarksList
   * @return ByteArrayOutputStream[] PDF byte Array
   * @throws S2SException
   */
  protected List<Printable> getPDFStream(ProposalDevelopmentDocument pdDoc) throws S2SException {
    FormMappingInfo info = null;
    S2SBaseFormGenerator s2sFormGenerator = null;
    grantsGovXmlDirectoryFile = null;
    List<AuditError> errors = new ArrayList<AuditError>();
    List<String> sortedNameSpaces =
        getSortedNameSpaces(pdDoc.getDevelopmentProposal().getS2sOppForms());

    List<Printable> formPrintables = new ArrayList<Printable>();
    boolean formEntryFlag = true;
    getS2SUtilService().deleteSystemGeneratedAttachments(pdDoc);
    Forms forms = Forms.Factory.newInstance();
    for (String namespace : sortedNameSpaces) {
      try {
        info = new FormMappingLoader().getFormInfo(namespace);
        s2sFormGenerator =
            (S2SBaseFormGenerator) s2SFormGeneratorService.getS2SGenerator(info.getNameSpace());
      } catch (S2SGeneratorNotFoundException e) {
        LOG.info("Form not found for namespace: " + namespace);
        continue;
      }
      s2sFormGenerator.setAuditErrors(errors);
      s2sFormGenerator.setAttachments(new ArrayList<AttachmentData>());
      XmlObject formObject = s2sFormGenerator.getFormObject(pdDoc);

      if (s2SValidatorService.validate(formObject, errors) && errors.isEmpty()) {
        String applicationXml = formObject.xmlText(s2SFormGeneratorService.getXmlOptionsPrefixes());
        String filteredApplicationXml = getS2SUtilService().removeTimezoneFactor(applicationXml);
        byte[] formXmlBytes = filteredApplicationXml.getBytes();
        S2SFormPrint formPrintable = new S2SFormPrint();
        // 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);

        ArrayList<Source> templates = new ArrayList<Source>();
        Source xsltSource =
            new StreamSource(getClass().getResourceAsStream("/" + info.getStylesheet()));
        templates.add(xsltSource);
        formPrintable.setXSLT(templates);

        List<AttachmentData> attachmentList = s2sFormGenerator.getAttachments();
        try {
          if (pdDoc.getDevelopmentProposal().getGrantsGovSelectFlag()) {
            List<S2sAppAttachments> attachmentLists = new ArrayList<S2sAppAttachments>();
            setFormObject(forms, formObject);
            saveGrantsGovXml(pdDoc, formEntryFlag, forms, attachmentList, attachmentLists);
            formEntryFlag = false;
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        }
        Map<String, byte[]> formAttachments = new LinkedHashMap<String, byte[]>();
        if (attachmentList != null && !attachmentList.isEmpty()) {
          for (AttachmentData attachmentData : attachmentList) {
            if (!isPdfType(attachmentData.getContent())) continue;
            StringBuilder attachment = new StringBuilder();
            attachment.append("   ATT : ");
            attachment.append(attachmentData.getContentId());
            formAttachments.put(attachment.toString(), attachmentData.getContent());
          }
        }
        if (formAttachments.size() > 0) {
          formPrintable.setAttachments(formAttachments);
        }
        formPrintables.add(formPrintable);
      }
    }
    if (!errors.isEmpty()) {
      setValidationErrorMessage(errors);
    }
    return formPrintables;
  }
Exemplo n.º 2
0
 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();
 }