Improved: General refactoring and code improvements, package

org.apache.ofbiz.content.survey.
(OFBIZ-10079)

Thanks Julian Leichert for reporting and providing the patches.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1818337 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-15 21:02:07 +00:00
parent 89c28c3762
commit c8053bb3ca
2 changed files with 38 additions and 56 deletions
@@ -281,7 +281,7 @@ public class PdfSurveyServices {
/**
*/
public static Map<String, Object> getAcroFieldsFromPdf(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> acroFieldMap = new HashMap<String, Object>();
Map<String, Object> acroFieldMap = new HashMap<>();
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Delegator delegator = dctx.getDelegator();
@@ -297,15 +297,9 @@ public class PdfSurveyServices {
acroFieldMap.put(fieldName, parmValue);
}
} catch (DocumentException e) {
} catch (DocumentException | GeneralException | IOException e) {
System.err.println(e.getMessage());
return ServiceUtil.returnError(e.getMessage());
} catch (GeneralException e) {
System.err.println(e.getMessage());
return ServiceUtil.returnError(e.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
return ServiceUtil.returnError(ioe.getMessage());
}
Map<String, Object> results = ServiceUtil.returnSuccess();
@@ -353,21 +347,9 @@ public class PdfSurveyServices {
baos.close();
ByteBuffer outByteBuffer = ByteBuffer.wrap(baos.toByteArray());
results.put("outByteBuffer", outByteBuffer);
} catch (DocumentException e) {
} catch (DocumentException | IOException | GeneralException e) {
System.err.println(e.getMessage());
results = ServiceUtil.returnError(e.getMessage());
} catch (GeneralException e) {
System.err.println(e.getMessage());
results = ServiceUtil.returnError(e.getMessage());
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
results = ServiceUtil.returnError(e.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
results = ServiceUtil.returnError(ioe.getMessage());
} catch (Exception ioe) {
System.err.println(ioe.getMessage());
results = ServiceUtil.returnError(ioe.getMessage());
}
return results;
}
@@ -431,10 +413,7 @@ public class PdfSurveyServices {
}
ByteBuffer outByteBuffer = ByteBuffer.wrap(baos.toByteArray());
results.put("outByteBuffer", outByteBuffer);
} catch (GenericEntityException e) {
System.err.println(e.getMessage());
results = ServiceUtil.returnError(e.getMessage());
} catch (DocumentException e) {
} catch (GenericEntityException | DocumentException e) {
System.err.println(e.getMessage());
results = ServiceUtil.returnError(e.getMessage());
}
@@ -449,7 +428,7 @@ public class PdfSurveyServices {
Delegator delegator = dctx.getDelegator();
Map<String, Object> results = ServiceUtil.returnSuccess();
String surveyResponseId = (String)context.get("surveyResponseId");
List<Object> qAndA = new LinkedList<Object>();
List<Object> qAndA = new LinkedList<>();
try {
List<GenericValue> responses = EntityQuery.use(delegator).from("SurveyResponseAnswer").where("surveyResponseId", surveyResponseId).queryList();
@@ -474,7 +453,7 @@ public class PdfSurveyServices {
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
Map<String, Object> results = ServiceUtil.returnSuccess();
Map<String, Object> acroFieldMap = new HashMap<String, Object>();
Map<String, Object> acroFieldMap = new HashMap<>();
String surveyResponseId = (String)context.get("surveyResponseId");
String acroFormContentId = null;
@@ -502,7 +481,7 @@ public class PdfSurveyServices {
GenericValue surveyQuestion = EntityQuery.use(delegator).from("SurveyQuestion").where("surveyQuestionId", surveyQuestionId).cache().queryOne();
GenericValue surveyQuestionAppl = EntityQuery.use(delegator).from("SurveyQuestionAppl")
.where("surveyId", surveyId,
.where("surveyId", surveyId,
"surveyQuestionId", surveyQuestionId)
.orderBy("-fromDate")
.filterByDate().cache().queryFirst();
@@ -565,7 +544,9 @@ public class PdfSurveyServices {
try (FileInputStream fis = new FileInputStream(pdfFileNameIn)) {
int c;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((c = fis.read()) != -1) baos.write(c);
while ((c = fis.read()) != -1) {
baos.write(c);
}
inputByteBuffer = ByteBuffer.wrap(baos.toByteArray());
} catch (IOException e) {
throw(new GeneralException(e.getMessage()));
@@ -103,7 +103,7 @@ public class SurveyWrapper {
*/
public void setPassThru(Map<String, Object> passThru) {
if (passThru != null) {
this.passThru = new HashMap<String, Object>();
this.passThru = new HashMap<>();
this.passThru.putAll(passThru);
}
}
@@ -114,7 +114,7 @@ public class SurveyWrapper {
*/
public void setDefaultValues(Map<String, Object> defaultValues) {
if (defaultValues != null) {
this.defaultValues = new HashMap<String, Object>();
this.defaultValues = new HashMap<>();
this.defaultValues.putAll(defaultValues);
}
}
@@ -126,7 +126,7 @@ public class SurveyWrapper {
*/
public void addToTemplateContext(String name, Object value) {
if (templateContext == null) {
templateContext = new HashMap<String, Object>();
templateContext = new HashMap<>();
}
templateContext.put(name, value);
}
@@ -136,8 +136,9 @@ public class SurveyWrapper {
* @param name
*/
public void removeFromTemplateContext(String name) {
if (templateContext != null)
if (templateContext != null) {
templateContext.remove(name);
}
}
/**
@@ -181,7 +182,7 @@ public class SurveyWrapper {
currentAnswers = this.getResponseAnswers(null);
}
Map<String, Object> sqaaWithColIdListByMultiRespId = new HashMap<String, Object>();
Map<String, Object> sqaaWithColIdListByMultiRespId = new HashMap<>();
for (GenericValue surveyQuestionAndAppl : surveyQuestionAndAppls) {
String surveyMultiRespColId = surveyQuestionAndAppl.getString("surveyMultiRespColId");
if (UtilValidate.isNotEmpty(surveyMultiRespColId)) {
@@ -191,7 +192,7 @@ public class SurveyWrapper {
}
if (templateContext == null) {
templateContext = new HashMap<String, Object>();
templateContext = new HashMap<>();
}
templateContext.put("partyId", partyId);
@@ -211,9 +212,7 @@ public class SurveyWrapper {
Template template = this.getTemplate(templateUrl);
try {
FreeMarkerWorker.renderTemplate(template, templateContext, writer);
} catch (TemplateException e) {
Debug.logError(e, "Error rendering Survey with template at [" + templateUrl.toExternalForm() + "]", module);
} catch (IOException e) {
} catch (TemplateException | IOException e) {
Debug.logError(e, "Error rendering Survey with template at [" + templateUrl.toExternalForm() + "]", module);
}
}
@@ -275,18 +274,17 @@ public class SurveyWrapper {
String responseId = this.getThisResponseId();
if (responseId == null) {
return true;
} else {
GenericValue survey = this.getSurvey();
if ("Y".equals(survey.getString("allowMultiple"))) {
return true;
}
}
GenericValue survey = this.getSurvey();
if ("Y".equals(survey.getString("allowMultiple"))) {
return true;
}
return false;
}
// returns a list of SurveyQuestions (in order by sequence number) for the current Survey
public List<GenericValue> getSurveyQuestionAndAppls() {
List<GenericValue> questions = new LinkedList<GenericValue>();
List<GenericValue> questions = new LinkedList<>();
try {
questions = EntityQuery.use(delegator).from("SurveyQuestionAndAppl")
@@ -358,7 +356,7 @@ public class SurveyWrapper {
// returns a Map of answers keyed on SurveyQuestion ID from the most current SurveyResponse ID
public Map<String, Object> getResponseAnswers(String responseId) throws SurveyWrapperException {
Map<String, Object> answerMap = new HashMap<String, Object>();
Map<String, Object> answerMap = new HashMap<>();
if (responseId != null) {
List<GenericValue> answers = null;
@@ -381,7 +379,7 @@ public class SurveyWrapper {
if (key.toUpperCase(Locale.getDefault()).startsWith("ANSWERS_")) {
int splitIndex = key.indexOf('_');
String questionId = key.substring(splitIndex+1);
Map<String, Object> thisAnswer = new HashMap<String, Object>();
Map<String, Object> thisAnswer = new HashMap<>();
String answer = (String) passThru.remove(key);
thisAnswer.put("booleanResponse", answer);
thisAnswer.put("currencyResponse", answer);
@@ -434,7 +432,7 @@ public class SurveyWrapper {
}
public Map<String, Object> getResults(List<GenericValue> questions) throws SurveyWrapperException {
Map<String, Object> questionResults = new HashMap<String, Object>();
Map<String, Object> questionResults = new HashMap<>();
if (questions != null) {
for (GenericValue question : questions) {
Map<String, Object> results = getResultInfo(question);
@@ -448,7 +446,7 @@ public class SurveyWrapper {
// returns a map of question reqsults
public Map<String, Object> getResultInfo(GenericValue question) throws SurveyWrapperException {
Map<String, Object> resultMap = new HashMap<String, Object>();
Map<String, Object> resultMap = new HashMap<>();
// special keys in the result:
// "_q_type" - question type (SurveyQuestionTypeId)
@@ -478,7 +476,7 @@ public class SurveyWrapper {
// create the map of option info ("_total", "_percent")
for (Entry<String, Object> entry : thisResult.entrySet()) {
Map<String, Object> optMap = new HashMap<String, Object>();
Map<String, Object> optMap = new HashMap<>();
Long optTotal = (Long) entry.getValue();
String optId = entry.getKey();
if (optTotal == null) {
@@ -586,7 +584,7 @@ public class SurveyWrapper {
} catch (GenericTransactionException gte) {
Debug.logError(gte, "Unable to begin transaction", module);
}
try (EntityListIterator eli = this.getEli(question, -1)) {
if (eli != null) {
GenericValue value;
@@ -635,16 +633,19 @@ public class SurveyWrapper {
// average
switch (type) {
case 1:
if (result[0] > 0)
result[2] = result[1] / ((long) result[0]);
if (result[0] > 0) {
result[2] = result[1] / ((long) result[0]);
}
break;
case 2:
if (result[0] > 0)
if (result[0] > 0) {
result[2] = (((double) Math.round((result[1] / result[0]) * 100)) / 100);
}
break;
case 3:
if (result[0] > 0)
result[2] = result[1] / (long) result[0];
if (result[0] > 0) {
result[2] = result[1] / (long) result[0];
}
break;
}
@@ -667,7 +668,7 @@ public class SurveyWrapper {
}
private Map<String, Object> getOptionResult(GenericValue question) throws SurveyWrapperException {
Map<String, Object> result = new HashMap<String, Object>();
Map<String, Object> result = new HashMap<>();
long total = 0;
boolean beganTransaction = false;