📄 surveywrapper.java
字号:
} } } return answerMap; } public List getQuestionResponses(GenericValue question, int startIndex, int number) throws SurveyWrapperException { List resp = null; boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); EntityListIterator eli = this.getEli(question); if (startIndex > 0 && number > 0) { resp = eli.getPartialList(startIndex, number); } else { resp = eli.getCompleteList(); } eli.close(); } catch (GenericEntityException e) { try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, "Error getting survey question responses", e); } catch (GenericEntityException e2) { Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); } throw new SurveyWrapperException(e); } finally { try { // only commit the transaction if we started one... TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { throw new SurveyWrapperException(e); //Debug.logError(e, "Could not commit transaction: " + e.toString(), module); } } return resp; } public Map getResults(List questions) throws SurveyWrapperException { Map questionResults = new HashMap(); if (questions != null) { Iterator i = questions.iterator(); while (i.hasNext()) { GenericValue question = (GenericValue) i.next(); Map results = getResultInfo(question); if (results != null) { questionResults.put(question.getString("surveyQuestionId"), results); } } } return questionResults; } // returns a map of question reqsults public Map getResultInfo(GenericValue question) throws SurveyWrapperException { Map resultMap = new HashMap(); // special keys in the result: // "_q_type" - question type (SurveyQuestionTypeId) // "_a_type" - answer type ("boolean", "option", "long", "double", "text") // "_total" - number of total responses (all types) // "_tally" - tally of all response values (number types) // "_average" - average of all response values (number types) // "_yes_total" - number of 'Y' (true) reponses (boolean type) // "_no_total" - number of 'N' (false) responses (boolean type) // "_yes_percent" - number of 'Y' (true) reponses (boolean type) // "_no_percent" - number of 'N' (false) responses (boolean type) // [optionId] - Map containing '_total, _percent' keys (option type) String questionType = question.getString("surveyQuestionTypeId"); resultMap.put("_q_type", questionType); // call the proper method based on the question type // note this will need to be updated as new types are added if ("OPTION".equals(questionType)) { Map thisResult = getOptionResult(question); if (thisResult != null) { Long questionTotal = (Long) thisResult.remove("_total"); if (questionTotal == null) questionTotal = new Long(0); // set the total responses resultMap.put("_total", questionTotal); // create the map of option info ("_total", "_percent") Iterator i = thisResult.keySet().iterator(); while (i.hasNext()) { Map optMap = new HashMap(); String optId = (String) i.next(); Long optTotal = (Long) thisResult.get(optId); if (optTotal == null) optTotal = new Long(0); Long percent = new Long((long)(((double)optTotal.longValue() / (double)questionTotal.longValue()) * 100)); optMap.put("_total", optTotal); optMap.put("_percent", percent); resultMap.put(optId, optMap); } resultMap.put("_a_type", "option"); } } else if ("BOOLEAN".equals(questionType)) { long[] thisResult = getBooleanResult(question); long yesPercent = thisResult[1] > 0 ? (long)(((double)thisResult[1] / (double)thisResult[0]) * 100) : 0; long noPercent = thisResult[2] > 0 ? (long)(((double)thisResult[2] / (double)thisResult[0]) * 100) : 0; resultMap.put("_total", new Long(thisResult[0])); resultMap.put("_yes_total", new Long(thisResult[1])); resultMap.put("_no_total", new Long(thisResult[2])); resultMap.put("_yes_percent", new Long(yesPercent)); resultMap.put("_no_percent", new Long(noPercent)); resultMap.put("_a_type", "boolean"); } else if ("NUMBER_LONG".equals(questionType)) { double[] thisResult = getNumberResult(question, 1); resultMap.put("_total", new Long((long)thisResult[0])); resultMap.put("_tally", new Long((long)thisResult[1])); resultMap.put("_average", new Long((long)thisResult[2])); resultMap.put("_a_type", "long"); } else if ("NUMBER_CURRENCY".equals(questionType)) { double[] thisResult = getNumberResult(question, 2); resultMap.put("_total", new Long((long)thisResult[0])); resultMap.put("_tally", new Double(thisResult[1])); resultMap.put("_average", new Double(thisResult[2])); resultMap.put("_a_type", "double"); } else if ("NUMBER_FLOAT".equals(questionType)) { double[] thisResult = getNumberResult(question, 3); resultMap.put("_total", new Long((long)thisResult[0])); resultMap.put("_tally", new Double(thisResult[1])); resultMap.put("_average", new Double(thisResult[2])); resultMap.put("_a_type", "double"); } else if ("SEPERATOR_LINE".equals(questionType) || "SEPERATOR_TEXT".equals(questionType)) { // not really a question; ingore completely return null; } else { // default is text resultMap.put("_total", new Long(getTextResult(question))); resultMap.put("_a_type", "text"); } return resultMap; } private long[] getBooleanResult(GenericValue question) throws SurveyWrapperException { boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); long[] result = { 0, 0, 0 }; // index 0 = total responses // index 1 = total yes // index 2 = total no EntityListIterator eli = this.getEli(question); if (eli != null) { GenericValue value; while (((value = (GenericValue) eli.next()) != null)) { if ("Y".equalsIgnoreCase(value.getString("booleanResponse"))) { result[1]++; } else { result[2]++; } result[0]++; // increment the count } eli.close(); } return result; } catch (GenericEntityException e) { try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, "Error getting survey question responses Boolean result", e); } catch (GenericEntityException e2) { Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); } throw new SurveyWrapperException(e); } finally { try { // only commit the transaction if we started one... TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { throw new SurveyWrapperException(e); //Debug.logError(e, "Could not commit transaction: " + e.toString(), module); } } } private double[] getNumberResult(GenericValue question, int type) throws SurveyWrapperException { double[] result = { 0, 0, 0 }; // index 0 = total responses // index 1 = tally // index 2 = average boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); EntityListIterator eli = this.getEli(question); if (eli != null) { GenericValue value; while (((value = (GenericValue) eli.next()) != null)) { switch (type) { case 1: Long n = value.getLong("numericResponse"); result[1] += n.longValue(); break; case 2: Double c = value.getDouble("currencyResponse"); result[1] += (((double) Math.round((c.doubleValue() - c.doubleValue()) * 100)) / 100); break; case 3: Double f = value.getDouble("floatResponse"); result[1] += f.doubleValue(); break; } result[0]++; // increment the count } eli.close(); } } catch (GenericEntityException e) { try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, "Error getting survey question responses Number result", e); } catch (GenericEntityException e2) { Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); } throw new SurveyWrapperException(e); } finally { try { // only commit the transaction if we started one... TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { throw new SurveyWrapperException(e); //Debug.logError(e, "Could not commit transaction: " + e.toString(), module); } } // average switch (type) { case 1: if (result[0] > 0) result[2] = ((long) result[1]) / ((long) result[0]); break; case 2: 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] / result[0]; break; } return result; } private long getTextResult(GenericValue question) throws SurveyWrapperException { long result = 0; try { result = delegator.findCountByCondition("SurveyResponseAndAnswer", makeEliCondition(question), null); } catch (GenericEntityException e) { Debug.logError(e, module); throw new SurveyWrapperException("Unable to get responses", e); } return result; } private Map getOptionResult(GenericValue question) throws SurveyWrapperException { Map result = new HashMap(); long total = 0; boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(); EntityListIterator eli = this.getEli(question); if (eli != null) { GenericValue value; while (((value = (GenericValue) eli.next()) != null)) { String optionId = value.getString("surveyOptionSeqId"); Long optCount = (Long) result.remove(optionId); if (optCount == null) { optCount = new Long(1); } else { optCount = new Long(1 + optCount.longValue()); } result.put(optionId, optCount); total++; // increment the count } eli.close(); } } catch (GenericEntityException e) { try { // only rollback the transaction if we started one... TransactionUtil.rollback(beganTransaction, "Error getting survey question responses Option result", e); } catch (GenericEntityException e2) { Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module); } throw new SurveyWrapperException(e); } finally { try { // only commit the transaction if we started one... TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { throw new SurveyWrapperException(e); //Debug.logError(e, "Could not commit transaction: " + e.toString(), module); } } result.put("_total", new Long(total)); return result; } private EntityCondition makeEliCondition(GenericValue question) { return new EntityConditionList(UtilMisc.toList(new EntityExpr("surveyQuestionId", EntityOperator.EQUALS, question.getString("surveyQuestionId")), new EntityExpr("surveyId", EntityOperator.EQUALS, surveyId)), EntityOperator.AND); } private EntityListIterator getEli(GenericValue question) throws GenericEntityException { EntityFindOptions efo = new EntityFindOptions(); efo.setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE); efo.setResultSetConcurrency(EntityFindOptions.CONCUR_READ_ONLY); efo.setSpecifyTypeAndConcur(true); efo.setDistinct(false); EntityListIterator eli = null; eli = delegator.findListIteratorByCondition("SurveyResponseAndAnswer", makeEliCondition(question), null, null, null, efo); return eli; } protected class SurveyWrapperException extends GeneralException { public SurveyWrapperException() { super(); } public SurveyWrapperException(String str) { super(str); } public SurveyWrapperException(String str, Throwable nested) { super(str, nested); } public SurveyWrapperException(Throwable nested) { super(nested); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -