📄 surveywrapper.java
字号:
/* * $Id: SurveyWrapper.java 7321 2006-04-18 04:54:14Z jonesde $ * * Copyright (c) 2003-2006 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.content.survey;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.StringWriter;import java.io.Writer;import java.net.URL;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import javolution.util.FastList;import javolution.util.FastMap;import javolution.util.FastSet;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.GeneralException;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilURL;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.template.FreeMarkerWorker;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.condition.EntityConditionList;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.transaction.TransactionUtil;import org.ofbiz.entity.util.EntityFindOptions;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.entity.util.EntityUtil;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;/** * Survey Wrapper - Class to render survey forms * * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 7321 $ * @since 3.0 */public class SurveyWrapper { public static final String module = SurveyWrapper.class.getName(); protected GenericDelegator delegator = null; protected String responseId = null; protected String partyId = null; protected String surveyId = null; protected Map passThru = null; protected boolean edit = false; protected SurveyWrapper() {} public SurveyWrapper(GenericDelegator delegator, String responseId, String partyId, String surveyId, Map passThru) { this.delegator = delegator; this.responseId = responseId; this.partyId = partyId; this.surveyId = surveyId; if (passThru != null) { this.passThru = new HashMap(passThru); } this.checkParameters(); } public SurveyWrapper(GenericDelegator delegator, String surveyId) { this(delegator, null, null, surveyId, null); } protected void checkParameters() { if (delegator == null || surveyId == null) { throw new IllegalArgumentException("Missing one or more required parameters (delegator, surveyId)"); } } /** * Renders the Survey * @return Writer object from the parsed Freemarker Template * @throws SurveyWrapperException */ public Writer render(String templatePath) throws SurveyWrapperException { URL templateUrl = UtilURL.fromResource(templatePath); if (templateUrl == null) { String errMsg = "Problem getting the template for Survey from URL: " + templatePath; Debug.logError(errMsg, module); throw new IllegalArgumentException(errMsg); } Writer writer = new StringWriter(); this.render(templateUrl, writer); return writer; } /** * Renders the Survey * @return Writer object from the parsed Freemarker Template * @throws SurveyWrapperException */ public void render(URL templateUrl, Writer writer) throws SurveyWrapperException { String responseId = this.getThisResponseId(); GenericValue survey = this.getSurvey(); List surveyQuestionAndAppls = this.getSurveyQuestionAndAppls(); Map results = this.getResults(surveyQuestionAndAppls); Map currentAnswers = null; if (responseId != null && canUpdate()) { currentAnswers = this.getResponseAnswers(responseId); } Map sqaaWithColIdListByMultiRespId = FastMap.newInstance(); Iterator surveyQuestionAndApplIter = surveyQuestionAndAppls.iterator(); while (surveyQuestionAndApplIter.hasNext()) { GenericValue surveyQuestionAndAppl = (GenericValue) surveyQuestionAndApplIter.next(); String surveyMultiRespColId = surveyQuestionAndAppl.getString("surveyMultiRespColId"); if (UtilValidate.isNotEmpty(surveyMultiRespColId)) { String surveyMultiRespId = surveyQuestionAndAppl.getString("surveyMultiRespId"); List surveyQuestionAndApplList = (List) sqaaWithColIdListByMultiRespId.get(surveyMultiRespId); if (surveyQuestionAndApplList == null) { surveyQuestionAndApplList = FastList.newInstance(); sqaaWithColIdListByMultiRespId.put(surveyMultiRespId, surveyQuestionAndApplList); } surveyQuestionAndApplList.add(surveyQuestionAndAppl); } } Map templateContext = FastMap.newInstance(); FreeMarkerWorker.addAllOfbizTransforms(templateContext); templateContext.put("partyId", partyId); templateContext.put("survey", survey); templateContext.put("surveyResults", results); templateContext.put("surveyQuestionAndAppls", surveyQuestionAndAppls); templateContext.put("sqaaWithColIdListByMultiRespId", sqaaWithColIdListByMultiRespId); templateContext.put("alreadyShownSqaaPkWithColId", FastSet.newInstance()); templateContext.put("surveyAnswers", currentAnswers); templateContext.put("surveyResponseId", responseId); templateContext.put("sequenceSort", UtilMisc.toList("sequenceNum")); templateContext.put("additionalFields", passThru); Template template = this.getTemplate(templateUrl); try { template.process(templateContext, writer); } catch (TemplateException e) { Debug.logError(e, "Error rendering Survey with template at [" + templateUrl.toExternalForm() + "]", module); } catch (IOException e) { Debug.logError(e, "Error rendering Survey with template at [" + templateUrl.toExternalForm() + "]", module); } } // returns the FTL Template object protected Template getTemplate(URL templateUrl) { Configuration config = null; try { config = FreeMarkerWorker.makeDefaultOfbizConfig(); } catch (IOException e) { Debug.logError(e, "Error creating default OFBiz FreeMarker Configuration", module); } catch (TemplateException e) { Debug.logError(e, "Error creating default OFBiz FreeMarker Configuration", module); } Template template = null; try { InputStream templateStream = templateUrl.openStream(); InputStreamReader templateReader = new InputStreamReader(templateStream); template = new Template(templateUrl.toExternalForm(), templateReader, config); } catch (IOException e) { Debug.logError(e, "Unable to get template from URL :" + templateUrl.toExternalForm(), module); } return template; } public void setEdit(boolean edit) { this.edit = edit; } // returns the GenericValue object for the current Survey public GenericValue getSurvey() { GenericValue survey = null; try { survey = delegator.findByPrimaryKey("Survey", UtilMisc.toMap("surveyId", surveyId)); } catch (GenericEntityException e) { Debug.logError(e, "Unable to get Survey : " + surveyId, module); } return survey; } public String getSurveyName() { GenericValue survey = this.getSurvey(); if (survey != null) { return survey.getString("surveyName"); } return ""; } // true if we can update this survey public boolean canUpdate() { if (this.edit) { return true; } GenericValue survey = this.getSurvey(); if (!"Y".equals(survey.getString("allowMultiple")) || !"Y".equals(survey.getString("allowUpdate"))) { return false; } return true; } public boolean canRespond() { String responseId = this.getThisResponseId(); if (responseId == null) { return true; } else { 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 getSurveyQuestionAndAppls() { List questions = new LinkedList(); try { Map fields = UtilMisc.toMap("surveyId", surveyId); List order = UtilMisc.toList("sequenceNum", "surveyMultiRespColId"); questions = delegator.findByAnd("SurveyQuestionAndAppl", fields, order); if (questions != null) { questions = EntityUtil.filterByDate(questions); } } catch (GenericEntityException e) { Debug.logError(e, "Unable to get questions for survey : " + surveyId, module); } return questions; } // returns the most current SurveyResponse ID for a survey; null if no party is found protected String getThisResponseId() { if (responseId != null) { return responseId; } if (partyId == null) { return null; } String responseId = null; List responses = null; try { responses = delegator.findByAnd("SurveyResponse", UtilMisc.toMap("surveyId", surveyId, "partyId", partyId), UtilMisc.toList("-lastModifiedDate")); } catch (GenericEntityException e) { Debug.logError(e, module); } if (responses != null && responses.size() > 0) { GenericValue response = EntityUtil.getFirst(responses); responseId = response.getString("surveyResponseId"); if (responses.size() > 1) { Debug.logWarning("More then one response found for survey : " + surveyId + " by party : " + partyId + " using most current", module); } } return responseId; } protected void setThisResponseId(String responseId) { this.responseId = responseId; } public long getNumberResponses() throws SurveyWrapperException { long responses = 0; try { responses = delegator.findCountByAnd("SurveyResponse", UtilMisc.toMap("surveyId", surveyId)); } catch (GenericEntityException e) { throw new SurveyWrapperException(e); } return responses; } public List getSurveyResponses(GenericValue question) throws SurveyWrapperException { List responses = null; try { responses = delegator.findByAnd("SurveyResponse", UtilMisc.toMap("surveyQuestionId", question.getString("surveyQuestionId"))); } catch (GenericEntityException e) { throw new SurveyWrapperException(e); } return responses; } // returns a Map of answers keyed on SurveyQuestion ID from the most current SurveyResponse ID public Map getResponseAnswers(String responseId) throws SurveyWrapperException { if (responseId == null) { throw new SurveyWrapperException("Null response ID is not supported at this time"); } Map answerMap = new HashMap(); if (responseId != null) { List answers = null; try { answers = delegator.findByAnd("SurveyResponseAnswer", UtilMisc.toMap("surveyResponseId", responseId)); } catch (GenericEntityException e) { Debug.logError(e, module); } if (answers != null && answers.size() > 0) { Iterator i = answers.iterator(); while (i.hasNext()) { GenericValue answer = (GenericValue) i.next(); answerMap.put(answer.get("surveyQuestionId"), answer); } } } // get the pass-thru (posted form data) if (passThru != null && passThru.size() > 0) { Iterator i = passThru.keySet().iterator(); while (i.hasNext()) { String key = (String) i.next(); if (key.toUpperCase().startsWith("ANSWERS_")) { int splitIndex = key.indexOf('_'); String questionId = key.substring(splitIndex+1); Map thisAnswer = new HashMap(); String answer = (String) passThru.remove(key); thisAnswer.put("booleanResponse", answer); thisAnswer.put("currencyResponse", answer); thisAnswer.put("floatResponse", answer); thisAnswer.put("numericResponse", answer); thisAnswer.put("textResponse", answer); thisAnswer.put("surveyOptionSeqId", answer); // this is okay since only one will be looked at answerMap.put(questionId, thisAnswer);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -