@Test public void generateNewTemplate() throws Exception { String targetFolderCmisId = "targetFolderCmisId"; Object[] queryArgs = { 500L }; Capture<Resource> captureResourceTemplate = new Capture<>(); Map<String, CorrespondenceQuery> correspondenceQueryBeansMap = new HashMap<>(); correspondenceQueryBeansMap.put("caseFileCorrespondenceQueryBean", correspondenceQuery); Capture<String> filename = new Capture<>(); expect(mockSpringContextHolder.getAllBeansOfType(CorrespondenceQuery.class)).andReturn(correspondenceQueryBeansMap); correspondenceQueryBeansMap.values().stream() .filter(cQuery -> cQuery.getType().toString().equals(correspondenceTemplate.getObjectType())).findFirst().get(); expect(mockEntityManager.createNativeQuery(correspondenceQuery.getSqlQuery())).andReturn(mockQuery); expect(mockQuery.setParameter(1, queryArgs[0])).andReturn(mockQuery); expect(mockQuery.getResultList()).andReturn(resultsData()); expect(mockCorrespondenceService.getActiveVersionMergeFieldsByType(correspondenceTemplate.getObjectType())) .andReturn(mergeFieldsData()); expect(mockLookupDao.getMergedLookups()).andReturn(lookupData()).anyTimes(); for (String key : substitutionsData().keySet()) { expect(mockTranslationService.translate(substitutionsData().get(key))).andReturn(substitutionsData().get(key)); } mockWordGenerator.generate(capture(captureResourceTemplate), eq(mockOutputStream), eq(substitutionsData())); expect(mockEcmFileDao.findSingleFileByParentObjectAndFolderCmisIdAndFileType(eq("CASE_FILE"), eq(500L), eq(targetFolderCmisId), eq(correspondenceTemplate.getDocumentType()))).andReturn(null); expect(mockEcmFileService.upload(eq(correspondenceTemplate.getDocumentType() + ".docx"), eq(correspondenceTemplate.getDocumentType()), eq(CorrespondenceGenerator.CORRESPONDENCE_CATEGORY), eq(mockInputStream), eq(CorrespondenceGenerator.WORD_MIME_TYPE), capture(filename), eq(mockAuthentication), eq(targetFolderCmisId), eq("CASE_FILE"), eq(500L))).andReturn(null); replayAll(); unit.generateCorrespondence(mockAuthentication, "CASE_FILE", 500L, targetFolderCmisId, correspondenceTemplate, queryArgs, mockOutputStream, mockInputStream); verifyAll(); Resource capturedResource = captureResourceTemplate.getValue(); assertEquals(correspondenceTemplate.getTemplateFilename(), capturedResource.getFilename()); }
public EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId, CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream, InputStream correspondenceInputStream) throws IOException, AcmCreateObjectFailedException, AcmUserActionFailedException, CorrespondenceMergeFieldVersionException { Map<String, Object> queryResult = query(template, queryArguments); if (queryResult == null || queryResult.isEmpty()) { throw new IllegalStateException("Database query returned no results"); } Map<String, String> substitutions = prepareSubstitutionMap(template, queryResult); Resource templateFile = new FileSystemResource(getCorrespondenceFolderName() + File.separator + template.getTemplateFilename()); log.debug("Generating correspondence from template '{}'", templateFile.getFile().getAbsolutePath()); getWordGenerator().generate(templateFile, correspondenceOutputStream, substitutions); EcmFile retval = null; EcmFile existing = ecmFileDao.findSingleFileByParentObjectAndFolderCmisIdAndFileType(parentObjectType, parentObjectId, targetFolderCmisId, template.getDocumentType()); if (existing == null) { String fileName = generateUniqueFilename(template); retval = ecmFileService.upload(template.getDocumentType() + ".docx", template.getDocumentType(), CORRESPONDENCE_CATEGORY, correspondenceInputStream, WORD_MIME_TYPE, fileName, user, targetFolderCmisId, parentObjectType, parentObjectId); } else { retval = ecmFileService.update(existing, correspondenceInputStream, SecurityContextHolder.getContext().getAuthentication()); } return retval; }
CorrespondenceGenerator { public EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId, CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream, InputStream correspondenceInputStream) throws IOException, AcmCreateObjectFailedException, AcmUserActionFailedException, CorrespondenceMergeFieldVersionException { Map<String, Object> queryResult = query(template, queryArguments); if (queryResult == null || queryResult.isEmpty()) { throw new IllegalStateException("Database query returned no results"); } Map<String, String> substitutions = prepareSubstitutionMap(template, queryResult); Resource templateFile = new FileSystemResource(getCorrespondenceFolderName() + File.separator + template.getTemplateFilename()); log.debug("Generating correspondence from template '{}'", templateFile.getFile().getAbsolutePath()); getWordGenerator().generate(templateFile, correspondenceOutputStream, substitutions); EcmFile retval = null; EcmFile existing = ecmFileDao.findSingleFileByParentObjectAndFolderCmisIdAndFileType(parentObjectType, parentObjectId, targetFolderCmisId, template.getDocumentType()); if (existing == null) { String fileName = generateUniqueFilename(template); retval = ecmFileService.upload(template.getDocumentType() + ".docx", template.getDocumentType(), CORRESPONDENCE_CATEGORY, correspondenceInputStream, WORD_MIME_TYPE, fileName, user, targetFolderCmisId, parentObjectType, parentObjectId); } else { retval = ecmFileService.update(existing, correspondenceInputStream, SecurityContextHolder.getContext().getAuthentication()); } return retval; } }
CorrespondenceGenerator { public EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId, CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream, InputStream correspondenceInputStream) throws IOException, AcmCreateObjectFailedException, AcmUserActionFailedException, CorrespondenceMergeFieldVersionException { Map<String, Object> queryResult = query(template, queryArguments); if (queryResult == null || queryResult.isEmpty()) { throw new IllegalStateException("Database query returned no results"); } Map<String, String> substitutions = prepareSubstitutionMap(template, queryResult); Resource templateFile = new FileSystemResource(getCorrespondenceFolderName() + File.separator + template.getTemplateFilename()); log.debug("Generating correspondence from template '{}'", templateFile.getFile().getAbsolutePath()); getWordGenerator().generate(templateFile, correspondenceOutputStream, substitutions); EcmFile retval = null; EcmFile existing = ecmFileDao.findSingleFileByParentObjectAndFolderCmisIdAndFileType(parentObjectType, parentObjectId, targetFolderCmisId, template.getDocumentType()); if (existing == null) { String fileName = generateUniqueFilename(template); retval = ecmFileService.upload(template.getDocumentType() + ".docx", template.getDocumentType(), CORRESPONDENCE_CATEGORY, correspondenceInputStream, WORD_MIME_TYPE, fileName, user, targetFolderCmisId, parentObjectType, parentObjectId); } else { retval = ecmFileService.update(existing, correspondenceInputStream, SecurityContextHolder.getContext().getAuthentication()); } return retval; }  }
CorrespondenceGenerator { public EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId, CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream, InputStream correspondenceInputStream) throws IOException, AcmCreateObjectFailedException, AcmUserActionFailedException, CorrespondenceMergeFieldVersionException { Map<String, Object> queryResult = query(template, queryArguments); if (queryResult == null || queryResult.isEmpty()) { throw new IllegalStateException("Database query returned no results"); } Map<String, String> substitutions = prepareSubstitutionMap(template, queryResult); Resource templateFile = new FileSystemResource(getCorrespondenceFolderName() + File.separator + template.getTemplateFilename()); log.debug("Generating correspondence from template '{}'", templateFile.getFile().getAbsolutePath()); getWordGenerator().generate(templateFile, correspondenceOutputStream, substitutions); EcmFile retval = null; EcmFile existing = ecmFileDao.findSingleFileByParentObjectAndFolderCmisIdAndFileType(parentObjectType, parentObjectId, targetFolderCmisId, template.getDocumentType()); if (existing == null) { String fileName = generateUniqueFilename(template); retval = ecmFileService.upload(template.getDocumentType() + ".docx", template.getDocumentType(), CORRESPONDENCE_CATEGORY, correspondenceInputStream, WORD_MIME_TYPE, fileName, user, targetFolderCmisId, parentObjectType, parentObjectId); } else { retval = ecmFileService.update(existing, correspondenceInputStream, SecurityContextHolder.getContext().getAuthentication()); } return retval; }  EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId,
            CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream,
            InputStream correspondenceInputStream); OutputStream generateCorrespondenceOutputStream(CorrespondenceTemplate template, Object[] queryArguments,
            OutputStream correspondenceOutputStream); EntityManager getEntityManager(); void setEntityManager(EntityManager entityManager); PoiWordGenerator getWordGenerator(); void setWordGenerator(PoiWordGenerator wordGenerator); EcmFileService getEcmFileService(); void setEcmFileService(EcmFileService ecmFileService); EcmFileDao getEcmFileDao(); void setEcmFileDao(EcmFileDao ecmFileDao); String getCorrespondenceFolderName(); void setCorrespondenceFolderName(String correspondenceFolderName); void setSpringContextHolder(SpringContextHolder springContextHolder); CorrespondenceService getCorrespondenceService(); void setCorrespondenceService(CorrespondenceService correspondenceService); LookupDao getLookupDao(); void setLookupDao(LookupDao lookupDao); TranslationService getTranslationService(); void setTranslationService(TranslationService translationService); }
CorrespondenceGenerator { public EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId, CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream, InputStream correspondenceInputStream) throws IOException, AcmCreateObjectFailedException, AcmUserActionFailedException, CorrespondenceMergeFieldVersionException { Map<String, Object> queryResult = query(template, queryArguments); if (queryResult == null || queryResult.isEmpty()) { throw new IllegalStateException("Database query returned no results"); } Map<String, String> substitutions = prepareSubstitutionMap(template, queryResult); Resource templateFile = new FileSystemResource(getCorrespondenceFolderName() + File.separator + template.getTemplateFilename()); log.debug("Generating correspondence from template '{}'", templateFile.getFile().getAbsolutePath()); getWordGenerator().generate(templateFile, correspondenceOutputStream, substitutions); EcmFile retval = null; EcmFile existing = ecmFileDao.findSingleFileByParentObjectAndFolderCmisIdAndFileType(parentObjectType, parentObjectId, targetFolderCmisId, template.getDocumentType()); if (existing == null) { String fileName = generateUniqueFilename(template); retval = ecmFileService.upload(template.getDocumentType() + ".docx", template.getDocumentType(), CORRESPONDENCE_CATEGORY, correspondenceInputStream, WORD_MIME_TYPE, fileName, user, targetFolderCmisId, parentObjectType, parentObjectId); } else { retval = ecmFileService.update(existing, correspondenceInputStream, SecurityContextHolder.getContext().getAuthentication()); } return retval; }  EcmFile generateCorrespondence(Authentication user, String parentObjectType, Long parentObjectId, String targetFolderCmisId,
            CorrespondenceTemplate template, Object[] queryArguments, OutputStream correspondenceOutputStream,
            InputStream correspondenceInputStream); OutputStream generateCorrespondenceOutputStream(CorrespondenceTemplate template, Object[] queryArguments,
            OutputStream correspondenceOutputStream); EntityManager getEntityManager(); void setEntityManager(EntityManager entityManager); PoiWordGenerator getWordGenerator(); void setWordGenerator(PoiWordGenerator wordGenerator); EcmFileService getEcmFileService(); void setEcmFileService(EcmFileService ecmFileService); EcmFileDao getEcmFileDao(); void setEcmFileDao(EcmFileDao ecmFileDao); String getCorrespondenceFolderName(); void setCorrespondenceFolderName(String correspondenceFolderName); void setSpringContextHolder(SpringContextHolder springContextHolder); CorrespondenceService getCorrespondenceService(); void setCorrespondenceService(CorrespondenceService correspondenceService); LookupDao getLookupDao(); void setLookupDao(LookupDao lookupDao); TranslationService getTranslationService(); void setTranslationService(TranslationService translationService);  }