Source Code Cross Referenced for SurveyWrapper.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » content » survey » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » SourceTap CRM » org.ofbiz.content.survey 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: SurveyWrapper.java,v 1.7 2003/12/06 00:50:20 ajzeneski Exp $
003:         *
004:         *  Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:        package org.ofbiz.content.survey;
025:
026:        import org.ofbiz.entity.GenericDelegator;
027:        import org.ofbiz.entity.GenericValue;
028:        import org.ofbiz.entity.GenericEntityException;
029:        import org.ofbiz.entity.util.EntityUtil;
030:        import org.ofbiz.base.util.UtilMisc;
031:        import org.ofbiz.base.util.Debug;
032:        import org.ofbiz.base.util.GeneralException;
033:        import org.ofbiz.base.util.UtilURL;
034:
035:        import java.util.Map;
036:        import java.util.List;
037:        import java.util.HashMap;
038:        import java.util.Iterator;
039:        import java.net.URL;
040:        import java.io.InputStreamReader;
041:        import java.io.IOException;
042:        import java.io.Writer;
043:        import java.io.StringWriter;
044:
045:        import freemarker.template.Template;
046:        import freemarker.template.Configuration;
047:        import freemarker.template.TemplateException;
048:        import freemarker.ext.beans.BeansWrapper;
049:
050:        /**
051:         * Survey Wrapper - Class to render survey forms
052:         *
053:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
054:         * @version    $Revision: 1.7 $
055:         * @since      3.0
056:         */
057:        public class SurveyWrapper {
058:
059:            public static final String module = SurveyWrapper.class.getName();
060:
061:            protected GenericDelegator delegator = null;
062:            protected String partyId = null;
063:            protected String surveyId = null;
064:            protected String templatePath = null;
065:            protected Map passThru = null;
066:
067:            protected SurveyWrapper() {
068:            }
069:
070:            public SurveyWrapper(GenericDelegator delegator, String partyId,
071:                    String surveyId, String templatePath, Map passThru) {
072:                this .delegator = delegator;
073:                this .partyId = partyId;
074:                this .surveyId = surveyId;
075:                this .templatePath = templatePath;
076:                if (passThru != null) {
077:                    this .passThru = new HashMap(passThru);
078:                }
079:                this .checkParameters();
080:            }
081:
082:            protected void checkParameters() {
083:                if (delegator == null || surveyId == null
084:                        || templatePath == null) {
085:                    throw new IllegalArgumentException(
086:                            "Missing one or more required parameters (delegator, surveyId, templatePath");
087:                }
088:            }
089:
090:            /**
091:             * Renders the Survey
092:             * @return Writer object from the parsed Freemarker Tempalte
093:             * @throws SurveyWrapperException
094:             */
095:            public Writer renderSurvey() throws SurveyWrapperException {
096:                GenericValue survey = this .getSurvey();
097:                List questions = this .getQuestions();
098:                Map templateContext = new HashMap();
099:                templateContext.put("partyId", partyId);
100:                templateContext.put("survey", survey);
101:                templateContext.put("surveyQuestions", questions);
102:                templateContext.put("surveyAnswers", this .getAnswers());
103:                templateContext.put("surveyResponseId", this .getResponseId());
104:                templateContext.put("sequenceSort", UtilMisc
105:                        .toList("sequenceNum"));
106:                templateContext.put("additionalFields", passThru);
107:
108:                Template template = this .getTemplate();
109:                Writer writer = new StringWriter();
110:                try {
111:                    template.process(templateContext, writer);
112:                } catch (TemplateException e) {
113:                    Debug.logError(e, module);
114:                } catch (IOException e) {
115:                    Debug.logError(e, module);
116:                }
117:                return writer;
118:            }
119:
120:            // returns the GenericValue object for the current Survey
121:            protected GenericValue getSurvey() {
122:                GenericValue survey = null;
123:                try {
124:                    survey = delegator.findByPrimaryKey("Survey", UtilMisc
125:                            .toMap("surveyId", surveyId));
126:                } catch (GenericEntityException e) {
127:                    Debug.logError(e, "Unable to get Survey : " + surveyId,
128:                            module);
129:                }
130:                return survey;
131:            }
132:
133:            // returns a list of SurveyQuestions (in order by sequence number) for the current Survey
134:            protected List getQuestions() {
135:                List surveyQuestions = null;
136:                try {
137:                    Map fields = UtilMisc.toMap("surveyId", surveyId);
138:                    List order = UtilMisc.toList("sequenceNum");
139:                    surveyQuestions = delegator.findByAnd(
140:                            "SurveyQuestionAndAppl", fields, order);
141:                    if (surveyQuestions != null) {
142:                        surveyQuestions = EntityUtil
143:                                .filterByDate(surveyQuestions);
144:                    }
145:                } catch (GenericEntityException e) {
146:                    Debug.logError(e, "Unable to get questions for survey : "
147:                            + surveyId, module);
148:                }
149:                return surveyQuestions;
150:            }
151:
152:            // returns the FTL Template object
153:            protected Template getTemplate() {
154:                URL templateUrl = UtilURL.fromResource(templatePath);
155:
156:                if (templateUrl == null) {
157:                    Debug.logError("Problem getting the template URL: "
158:                            + templatePath + " not found", module);
159:                }
160:
161:                Configuration config = Configuration.getDefaultConfiguration();
162:                config.setObjectWrapper(BeansWrapper.getDefaultInstance());
163:                try {
164:                    config.setSetting("datetime_format",
165:                            "yyyy-MM-dd HH:mm:ss.SSS");
166:                } catch (TemplateException e) {
167:                    Debug.logError(e, module);
168:                }
169:
170:                Template template = null;
171:                try {
172:                    InputStreamReader templateReader = new InputStreamReader(
173:                            templateUrl.openStream());
174:                    template = new Template(templateUrl.toExternalForm(),
175:                            templateReader, config);
176:                } catch (IOException e) {
177:                    Debug.logError(e, "Unable to get template from URL :"
178:                            + templatePath, module);
179:                }
180:                return template;
181:            }
182:
183:            // returns the most current SurveyResponse ID for an updateable survey; null if no party or survey cannot be updated
184:            protected String getResponseId() {
185:                if (partyId == null) {
186:                    return null;
187:                }
188:
189:                GenericValue survey = this .getSurvey();
190:                if (!"Y".equals(survey.getString("allowMultiple"))
191:                        || !"Y".equals(survey.getString("allowUpdate"))) {
192:                    // can only update if multiple responses is true and the updateable flag is set
193:                    return null;
194:                }
195:
196:                String responseId = null;
197:                List responses = null;
198:                try {
199:                    responses = delegator.findByAnd("SurveyResponse", UtilMisc
200:                            .toMap("surveyId", surveyId, "partyId", partyId),
201:                            UtilMisc.toList("-lastModifiedDate"));
202:                } catch (GenericEntityException e) {
203:                    Debug.logError(e, module);
204:                }
205:
206:                if (responses != null && responses.size() > 0) {
207:                    GenericValue response = EntityUtil.getFirst(responses);
208:                    responseId = response.getString("surveyResponseId");
209:                    if (responses.size() > 1) {
210:                        Debug.logWarning(
211:                                "More then one response found for survey : "
212:                                        + surveyId + " by party : " + partyId
213:                                        + " using most current", module);
214:                    }
215:                }
216:
217:                return responseId;
218:            }
219:
220:            // returns a Map of answers keyed on SurveyQuestion ID from the most current SurveyResponse ID
221:            protected Map getAnswers() {
222:                Map answerMap = new HashMap();
223:                String responseId = this .getResponseId();
224:
225:                if (responseId != null) {
226:                    List answers = null;
227:                    try {
228:                        answers = delegator.findByAnd("SurveyResponseAnswer",
229:                                UtilMisc.toMap("surveyResponseId", responseId));
230:                    } catch (GenericEntityException e) {
231:                        Debug.logError(e, module);
232:                    }
233:
234:                    if (answers != null && answers.size() > 0) {
235:                        Iterator i = answers.iterator();
236:                        while (i.hasNext()) {
237:                            GenericValue answer = (GenericValue) i.next();
238:                            answerMap.put(answer.get("surveyQuestionId"),
239:                                    answer);
240:                        }
241:                    }
242:                }
243:
244:                // get the pass-thru (posted form data)
245:                if (passThru != null && passThru.size() > 0) {
246:                    Iterator i = passThru.keySet().iterator();
247:                    while (i.hasNext()) {
248:                        String key = (String) i.next();
249:                        if (key.toUpperCase().startsWith("ANSWERS_")) {
250:                            int splitIndex = key.indexOf('_');
251:                            String questionId = key.substring(splitIndex + 1);
252:                            Map this Answer = new HashMap();
253:                            String answer = (String) passThru.remove(key);
254:                            this Answer.put("booleanResponse", answer);
255:                            this Answer.put("currencyResponse", answer);
256:                            this Answer.put("floatResponse", answer);
257:                            this Answer.put("numericResponse", answer);
258:                            this Answer.put("textResponse", answer);
259:                            this Answer.put("surveyOptionSeqId", answer);
260:                            // this is okay since only one will be looked at
261:                            answerMap.put(questionId, this Answer);
262:                        }
263:                    }
264:                }
265:
266:                return answerMap;
267:            }
268:
269:            class SurveyWrapperException extends GeneralException {
270:
271:                public SurveyWrapperException() {
272:                    super ();
273:                }
274:
275:                public SurveyWrapperException(String str) {
276:                    super (str);
277:                }
278:
279:                public SurveyWrapperException(String str, Throwable nested) {
280:                    super (str, nested);
281:                }
282:
283:                public SurveyWrapperException(Throwable nested) {
284:                    super(nested);
285:                }
286:            }
287:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.