Set<String> selectFields = UtilMisc.toSetArray(fields);
List<String> orderByFields = UtilMisc.toList("sequenceNum");
List<GenericValue> compDocParts = delegator.findList("ContentAssocRevisionItemView", conditionList, selectFields, orderByFields, null, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
document.setPageSize(PageSize.LETTER);
//Rectangle rect = document.getPageSize();
//PdfWriter writer = PdfWriter.getInstance(document, baos);
PdfCopy writer = new PdfCopy(document, baos);
document.open();
int pgCnt =0;
for(GenericValue contentAssocRevisionItemView : compDocParts) {
//String thisContentId = contentAssocRevisionItemView.getString("contentId");
//String thisContentRevisionSeqId = contentAssocRevisionItemView.getString("maxRevisionSeqId");
String thisDataResourceId = contentAssocRevisionItemView.getString("dataResourceId");
GenericValue dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", thisDataResourceId));
String inputMimeType = null;
if (dataResource != null) {
inputMimeType = dataResource.getString("mimeTypeId");
}
byte [] inputByteArray = null;
PdfReader reader = null;
if (inputMimeType != null && inputMimeType.equals("application/pdf")) {
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, thisDataResourceId, https, webSiteId, locale, rootDir);
inputByteArray = byteBuffer.array();
reader = new PdfReader(inputByteArray);
} else if (inputMimeType != null && inputMimeType.equals("text/html")) {
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, thisDataResourceId, https, webSiteId, locale, rootDir);
inputByteArray = byteBuffer.array();
String s = new String(inputByteArray);
Debug.logInfo("text/html string:" + s, module);
continue;
} else if (inputMimeType != null && inputMimeType.equals("application/vnd.ofbiz.survey.response")) {
String surveyResponseId = dataResource.getString("relatedDetailId");
String surveyId = null;
String acroFormContentId = null;
GenericValue surveyResponse = null;
if (UtilValidate.isNotEmpty(surveyResponseId)) {
surveyResponse = delegator.findByPrimaryKey("SurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId));
if (surveyResponse != null) {
surveyId = surveyResponse.getString("surveyId");
}
}
if (UtilValidate.isNotEmpty(surveyId)) {
GenericValue survey = delegator.findByPrimaryKey("Survey", UtilMisc.toMap("surveyId", surveyId));
if (survey != null) {
acroFormContentId = survey.getString("acroFormContentId");
if (UtilValidate.isNotEmpty(acroFormContentId)) {
// TODO: is something supposed to be done here?
}
}
}
if (surveyResponse != null) {
if (UtilValidate.isEmpty(acroFormContentId)) {
// Create AcroForm PDF
Map<String, Object> survey2PdfResults = dispatcher.runSync("buildPdfFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyId));
if (ServiceUtil.isError(survey2PdfResults)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentSurveyErrorBuildingPDF", locale), null, null, survey2PdfResults);
}
ByteBuffer outByteBuffer = (ByteBuffer) survey2PdfResults.get("outByteBuffer");
inputByteArray = outByteBuffer.array();
reader = new PdfReader(inputByteArray);
} else {
// Fill in acroForm
Map<String, Object> survey2AcroFieldResults = dispatcher.runSync("setAcroFieldsFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId));
if (ServiceUtil.isError(survey2AcroFieldResults)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentSurveyErrorSettingAcroFields", locale), null, null, survey2AcroFieldResults);
}
ByteBuffer outByteBuffer = (ByteBuffer) survey2AcroFieldResults.get("outByteBuffer");
inputByteArray = outByteBuffer.array();
reader = new PdfReader(inputByteArray);
}
}
} else {
ByteBuffer inByteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, thisDataResourceId, https, webSiteId, locale, rootDir);
Map<String, Object> convertInMap = UtilMisc.<String, Object>toMap("userLogin", userLogin, "inByteBuffer", inByteBuffer, "inputMimeType", inputMimeType, "outputMimeType", "application/pdf");
if (UtilValidate.isNotEmpty(oooHost)) convertInMap.put("oooHost", oooHost);
if (UtilValidate.isNotEmpty(oooPort)) convertInMap.put("oooPort", oooPort);
Map<String, Object> convertResult = dispatcher.runSync("convertDocumentByteBuffer", convertInMap);
if (ServiceUtil.isError(convertResult)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentConvertingDocumentByteBuffer", locale), null, null, convertResult);
}
ByteBuffer outByteBuffer = (ByteBuffer) convertResult.get("outByteBuffer");
inputByteArray = outByteBuffer.array();
reader = new PdfReader(inputByteArray);
}
if (reader != null) {
int n = reader.getNumberOfPages();
for (int i=0; i < n; i++) {
PdfImportedPage pg = writer.getImportedPage(reader, i + 1);
//cb.addTemplate(pg, left, height * pgCnt);
writer.addPage(pg);
pgCnt++;
}
}
}
document.close();
ByteBuffer outByteBuffer = ByteBuffer.wrap(baos.toByteArray());
Map<String, Object> results = ServiceUtil.returnSuccess();
results.put("outByteBuffer", outByteBuffer);
return results;