This is an automated email from the ASF dual-hosted git repository.

mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new c1f4c2b39d Fixed: PDF survey services fail to store answers and throw 
a NullPointerException when no PDF is given (OFBIZ-13688)
c1f4c2b39d is described below

commit c1f4c2b39d0b27f4424032ef101bd53dbc2f0562
Author: Mridul Pathak <[email protected]>
AuthorDate: Fri Oct 16 13:25:45 2026 +0530

    Fixed: PDF survey services fail to store answers and throw a 
NullPointerException when no PDF is given (OFBIZ-13688)
    
    - buildSurveyResponseFromPdf created SurveyResponseAnswer without 
surveyMultiRespColId, which is part of the primary key, so building a survey 
response from a PDF failed on the first answer and rolled back. It now sets it 
to _NA_ like the regular survey service, and updates the existing answers when 
it runs again for the same survey response.
    - When no PDF was given, buildSurveyFromPdf, buildSurveyResponseFromPdf, 
getAcroFieldsFromPdf and setAcroFields threw a NullPointerException. They now 
return an error with the new ContentPDFInputMissing label.
---
 applications/content/config/ContentUiLabels.xml    |  3 ++
 .../ofbiz/content/survey/PdfSurveyServices.java    | 37 +++++++++++++++++-----
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/applications/content/config/ContentUiLabels.xml 
b/applications/content/config/ContentUiLabels.xml
index c3e02181e6..3aceaf1e91 100644
--- a/applications/content/config/ContentUiLabels.xml
+++ b/applications/content/config/ContentUiLabels.xml
@@ -1889,6 +1889,9 @@
         <value xml:lang="zh">生成PDF时出错:${errorString}</value>
         <value xml:lang="zh-TW">產生PDF時出錯:${errorString}</value>
     </property>
+    <property key="ContentPDFInputMissing">
+        <value xml:lang="en">No PDF was given.</value>
+    </property>
     <property key="ContentPDFSurveyName">
         <value xml:lang="da">Rundspørge navn</value>
         <value xml:lang="en">PDF/Survey Name</value>
diff --git 
a/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java
 
b/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java
index 3aff92eb7d..70b05688aa 100644
--- 
a/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java
+++ 
b/applications/content/src/main/java/org/apache/ofbiz/content/survey/PdfSurveyServices.java
@@ -83,7 +83,10 @@ public class PdfSurveyServices {
         Locale locale = (Locale) context.get("locale");
         Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
         String surveyId = null;
-        try (PDDocument pdDocument = 
Loader.loadPDF(getInputByteBuffer(context, delegator).array())) {
+        try (PDDocument pdDocument = loadInputPdf(context, delegator)) {
+            if (pdDocument == null) {
+                return 
ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, 
"ContentPDFInputMissing", locale));
+            }
             String surveyName = (String) context.get("surveyName");
 
             String contentId = (String) context.get("contentId");
@@ -203,7 +206,10 @@ public class PdfSurveyServices {
     public static Map<String, Object> 
buildSurveyResponseFromPdf(DispatchContext dctx, Map<String, ? extends Object> 
context) {
         String surveyResponseId = null;
         Locale locale = (Locale) context.get("locale");
-        try (PDDocument pdDocument = 
Loader.loadPDF(getInputByteBuffer(context, dctx.getDelegator()).array())) {
+        try (PDDocument pdDocument = loadInputPdf(context, 
dctx.getDelegator())) {
+            if (pdDocument == null) {
+                return 
ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, 
"ContentPDFInputMissing", locale));
+            }
             Delegator delegator = dctx.getDelegator();
             String partyId = (String) context.get("partyId");
             String surveyId = (String) context.get("surveyId");
@@ -238,12 +244,13 @@ public class PdfSurveyServices {
                 String surveyQuestionId = (String) 
surveyQuestionAndAppl.get("surveyQuestionId");
                 String surveyQuestionTypeId = (String) 
surveyQuestionAndAppl.get("surveyQuestionTypeId");
                 GenericValue surveyResponseAnswer = 
delegator.makeValue("SurveyResponseAnswer",
-                        UtilMisc.toMap("surveyResponseId", surveyResponseId, 
"surveyQuestionId", surveyQuestionId));
+                        UtilMisc.toMap("surveyResponseId", surveyResponseId, 
"surveyQuestionId", surveyQuestionId,
+                                "surveyMultiRespColId", "_NA_"));
                 if (surveyQuestionTypeId == null || 
"TEXT_SHORT".equals(surveyQuestionTypeId)) {
                     surveyResponseAnswer.set("textResponse", value);
                 }
 
-                delegator.create(surveyResponseAnswer);
+                delegator.createOrStore(surveyResponseAnswer);
             }
         } catch (GeneralException | IOException e) {
             Debug.logError(e, "Error generating PDF: " + e.toString(), MODULE);
@@ -262,8 +269,11 @@ public class PdfSurveyServices {
     public static Map<String, Object> getAcroFieldsFromPdf(DispatchContext 
dctx, Map<String, ? extends Object> context) throws GeneralException {
         Map<String, Object> acroFieldMap = new HashMap<>();
         Delegator delegator = dctx.getDelegator();
-        ByteBuffer byteBuffer = getInputByteBuffer(context, delegator);
-        try (PDDocument pdDocument = Loader.loadPDF(byteBuffer.array())) {
+        Locale locale = (Locale) context.get("locale");
+        try (PDDocument pdDocument = loadInputPdf(context, delegator)) {
+            if (pdDocument == null) {
+                return 
ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, 
"ContentPDFInputMissing", locale));
+            }
             PdfFormHelper pdfForm = new PdfFormHelper(pdDocument);
             for (PDTerminalField field : pdfForm.getFormFields()) {
                 acroFieldMap.put(field.getFullyQualifiedName(), 
pdfForm.getValue(field));
@@ -283,11 +293,14 @@ public class PdfSurveyServices {
     public static Map<String, Object> setAcroFields(DispatchContext dctx, 
Map<String, ? extends Object> context) {
         Map<String, Object> results = ServiceUtil.returnSuccess();
         Delegator delegator = dctx.getDelegator();
+        Locale locale = (Locale) context.get("locale");
         try {
             Map<String, Object> acroFieldMap = 
UtilGenerics.cast(context.get("acroFieldMap"));
-            ByteBuffer byteBuffer = getInputByteBuffer(context, delegator);
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            try (PDDocument pdDocument = Loader.loadPDF(byteBuffer.array())) {
+            try (PDDocument pdDocument = loadInputPdf(context, delegator)) {
+                if (pdDocument == null) {
+                    return 
ServiceUtil.returnError(UtilProperties.getMessage(RESOURCE, 
"ContentPDFInputMissing", locale));
+                }
                 PdfFormHelper pdfForm = new PdfFormHelper(pdDocument);
                 for (PDTerminalField field : pdfForm.getFormFields()) {
                     String fieldValue;
@@ -542,6 +555,14 @@ public class PdfSurveyServices {
         return inputByteBuffer;
     }
 
+    /**
+     * @return the PDF given by the inputByteBuffer, pdfFileNameIn or 
contentId parameter, null if none of them is given
+     */
+    private static PDDocument loadInputPdf(Map<String, ? extends Object> 
context, Delegator delegator) throws GeneralException, IOException {
+        ByteBuffer inputByteBuffer = getInputByteBuffer(context, delegator);
+        return inputByteBuffer != null ? 
Loader.loadPDF(inputByteBuffer.array()) : null;
+    }
+
     /**
      * Writes the lines in a new A4 PDF, in 12 points Helvetica, wrapping long 
lines and adding pages as needed. Characters
      * that Helvetica can't show are replaced by "?".

Reply via email to