From b7f5b05a1eaaee29e79e525ba6dcedcfd69808e6 Mon Sep 17 00:00:00 2001 From: quasi Date: Tue, 23 Feb 2010 09:31:57 +0000 Subject: [PATCH] Survey * SurveyProcessListener angefangen * SurveyAnswerCollection erzeugt * SurveyResponseCollection erzeugt Diverse Anpassungen in survey.pdl git-svn-id: https://svn.libreccm.org/ccm/trunk@357 8810af33-2d31-482b-a856-94f89814c4df --- .../com/arsdigita/content-types/Survey.pdl | 20 +- .../arsdigita/cms/contenttypes/Survey.java | 73 ++--- .../contenttypes/SurveyAnswerCollection.java | 41 +++ .../cms/contenttypes/SurveyResponse.java | 56 ++-- .../SurveyResponseCollection.java | 44 +++ .../ui/SurveyProcessListener.java | 257 ++++++++---------- 6 files changed, 273 insertions(+), 218 deletions(-) create mode 100644 ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswerCollection.java create mode 100644 ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponseCollection.java diff --git a/ccm-cms-types-survey/pdl/com/arsdigita/content-types/Survey.pdl b/ccm-cms-types-survey/pdl/com/arsdigita/content-types/Survey.pdl index 6e8b7f05c..de8ed5a6b 100755 --- a/ccm-cms-types-survey/pdl/com/arsdigita/content-types/Survey.pdl +++ b/ccm-cms-types-survey/pdl/com/arsdigita/content-types/Survey.pdl @@ -5,27 +5,27 @@ import com.arsdigita.formbuilder.*; import com.arsdigita.kernel.User; object type Survey extends ContentPage { - Date[0..1] startDate = ct_surveys.start_date DATE; - Date[0..1] endDate = ct_surveys.end_date DATE; - Boolean[0..1] responsesPublic = ct_surveys.responses_public; - component FormSection[1..1] form = join ct_surveys.form_id - to bebop_form_sections.form_section_id; + Date[0..1] startDate = ct_surveys.start_date DATE; + Date[0..1] endDate = ct_surveys.end_date DATE; + Boolean[0..1] responsesPublic = ct_surveys.responses_public; + component FormSection[1..1] form = join ct_surveys.form_id + to bebop_form_sections.form_section_id; + component SurveyResponse[0..n] responses = join ct_surveys.survey_id + to ct_surveys_responses.survey_id; reference key ( ct_surveys.survey_id ); } object type SurveyResponse extends ContentItem { Date[1..1] entryDate = ct_surveys_responses.entry_date DATE; - Survey[1..1] survey = join ct_surveys_responses.survey_id - to ct_surveys.survey_id; - User[1..1] user = join ct_surveys_responses.user_id + User[0..1] user = join ct_surveys_responses.user_id to users.user_id; component SurveyAnswer[1..n] answers = join ct_surveys_responses.response_id to ct_surveys_answers.response_id; // XXX hack to allow us to filter a data query - BigDecimal[1..1] surveyID = ct_surveys_responses.survey_id INTEGER; - BigDecimal[1..1] userID = ct_surveys_responses.user_id INTEGER; +// BigDecimal[1..1] surveyID = ct_surveys_responses.survey_id INTEGER; +// BigDecimal[1..1] userID = ct_surveys_responses.user_id INTEGER; reference key (ct_surveys_responses.response_id); } diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/Survey.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/Survey.java index 89a03e35a..bbac5e66e 100755 --- a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/Survey.java +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/Survey.java @@ -10,11 +10,12 @@ import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.formbuilder.PersistentForm; +import com.arsdigita.kernel.User; import java.util.Date; /** - * A survey content type that represents a survey. This is - * based on the simplesurvey application. + * A survey content type that represents a survey. This is partially based on + * the simplesurvey application and CT FormItem. * * @author Sören Bernstein * @@ -23,6 +24,8 @@ public class Survey extends ContentPage { /** PDL property name for formSection */ public static final String FORM = "form"; + /** PDL property name for surveyResponses */ + public static final String RESPONSES = "responses"; /** PDL property name for startDate */ public static final String START_DATE = "startDate"; /** PDL property name for endDate */ @@ -32,18 +35,17 @@ public class Survey extends ContentPage { /** Data object type for this domain object */ public static final String BASE_DATA_OBJECT_TYPE = "com.arsdigita.cms.contenttypes.Survey"; -/* + /* private static final SurveyConfig s_config = new SurveyConfig(); static { - s_config.load(); + s_config.load(); } public static final SurveyConfig getConfig() { - return s_config; + return s_config; } -*/ - + */ /** * Default constructor. This creates a new (empty) Survey. **/ @@ -98,17 +100,6 @@ public class Survey extends ContentPage { super(type); } - /** - * For new content items, sets the associated content type if it - * has not been already set. - */ - /* @Override - public void beforeSave() { - super.beforeSave(); - - Assert.exists(getContentType(), ContentType.class); - } - */ /** * This will handle the mandatory FormSection. If there is no * FormSection set it will create an empty new form and assign it @@ -123,11 +114,13 @@ public class Survey extends ContentPage { form.setAdminName(getName()); setAssociation(FORM, form); } + /* + // Preset the responsesPublic if (getResponsesPublic() == null) { - setResponsesPublic(false); + setResponsesPublic(false); } - */ + */ } super.beforeSave(); @@ -166,39 +159,31 @@ public class Survey extends ContentPage { set(RESPONSES_PUBLIC, responsesPublic); } - /* Class methods *********************************************************/ - public static Survey retrieve(BigDecimal id) - throws DataObjectNotFoundException { - - Survey survey = new Survey(id); - - return survey; + public SurveyResponse addResponse() { + SurveyResponse surveyResponse = new SurveyResponse(); + addResponse(surveyResponse); + return surveyResponse; + } + + protected void addResponse(SurveyResponse surveyResponse) { + add(RESPONSES, surveyResponse); } - public static Survey retrieve(DataObject obj) { - Survey survey = new Survey(obj); - - return survey; - } - /* public SurveyResponseCollection getResponses() { - return SurveyResponse.retrieveBySurvey(this); + return new SurveyResponseCollection ((DataCollection) get(RESPONSES)); } - public SurveyResponseCollection getUserResponses(User user) { - return SurveyResponse.retrieveBySurvey(this, user); + public SurveyResponseCollection getResponses(User user) { + return new SurveyResponseCollection ((DataCollection) get(RESPONSES), user); } - public boolean hasUserResponded(User user) { - SurveyResponseCollection responses = getUserResponses(user); + public boolean hasResponses() { + return !this.getResponses().isEmpty(); + } - if (responses.next()) { - responses.close(); - return true; + public boolean hasResponses(User user) { + return !this.getResponses(user).isEmpty(); } - return false; - } - */ /* public DataQuery getLabelDataQuery() { diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswerCollection.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswerCollection.java new file mode 100644 index 000000000..ba7f8c7a5 --- /dev/null +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswerCollection.java @@ -0,0 +1,41 @@ + +package com.arsdigita.cms.contenttypes; + +import com.arsdigita.domain.DomainCollection; +import com.arsdigita.persistence.DataCollection; + +/** + * + * @author Sören Bernstein + */ +public class SurveyAnswerCollection extends DomainCollection { + + /** + * Creates a new instance of SurveyAnswerCollection + */ + public SurveyAnswerCollection(DataCollection dataCollection) { + super(dataCollection); + +// m_dataCollection.addOrder(ORDER); + } + + // Get the label + public String getLabel() { + return (String) m_dataCollection.get(SurveyAnswer.LABEL); + } + + // Get the widget + public String getWidget() { + return (String) m_dataCollection.get(SurveyAnswer.WIDGET); + } + + // Get the value + public String getValue() { + return (String) m_dataCollection.get(SurveyAnswer.VALUE); + } + + public SurveyAnswer getSurveyAnswer() { + return new SurveyAnswer(m_dataCollection.getDataObject()); + } + +} diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponse.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponse.java index f1b25b7ef..708cf1081 100755 --- a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponse.java +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponse.java @@ -5,6 +5,7 @@ import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.formbuilder.PersistentLabel; import com.arsdigita.formbuilder.PersistentWidget; import com.arsdigita.kernel.User; +import com.arsdigita.persistence.DataCollection; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; import java.math.BigDecimal; @@ -33,17 +34,28 @@ public class SurveyResponse extends ContentItem { public static final String BASE_DATA_OBJECT_TYPE = "com.arsdigita.cms.contenttypes.SurveyResponse"; /** - * Default constructor. This creates a new (empty) Survey. + * Default constructor. This creates a new SurveyResponse. There can't be a + * SurveyResponse without a proper Survey object + * + * @param survey The survey for this SurveyResponse. **/ public SurveyResponse() { this(BASE_DATA_OBJECT_TYPE); + + // Save the date + setEntryDate(); + + // Save the corresponding survey +// setSurvey(survey); + + // XXX hack - see pdl file +// set(USER + "ID", user.getID()); } /** * Constructor. The contained DataObject is retrieved * from the persistent storage mechanism with an OID - * specified by id and - * Address.BASE_DATA_OBJECT_TYPE. + * specified by id. * * @param id The id for the retrieved * DataObject. @@ -87,35 +99,37 @@ public class SurveyResponse extends ContentItem { } /* accessors *****************************************************/ - public void addAnswer(PersistentLabel label, PersistentWidget widget, String value) { - - SurveyAnswer answer = SurveyAnswer.create(label, widget, value); - add(ANSWERS, answer); + private void setEntryDate() { + set(ENTRY_DATE, new Date()); } public Date getEntryDate() { return (Date) get(ENTRY_DATE); } - /* Class methods **********************************************************/ - public static SurveyResponse create(Survey survey, User user) { - SurveyResponse response = new SurveyResponse(); - response.setup(survey, user); - return response; - } - - protected void setup(Survey survey, User user) { - set(ENTRY_DATE, new Date()); - - // XXX hack - see pdl file - set(USER + "ID", user.getID()); + private void setSurvey(Survey survey) { +// set(SURVEY, survey); set(SURVEY + "ID", survey.getID()); } - public static SurveyResponse retrieve(DataObject obj) { - return new SurveyResponse(obj); + public Survey getSurvey() { + return (Survey) get(SURVEY); } + public void addAnswer(PersistentLabel label, PersistentWidget widget, String value) { + SurveyAnswer answer = SurveyAnswer.create(label, widget, value); + add(ANSWERS, answer); + } + + public SurveyAnswerCollection getAnswers() { + return new SurveyAnswerCollection ((DataCollection) get(ANSWERS)); + } + + public boolean hasAnswers() { + return !this.getAnswers().isEmpty(); + } + + /* Class methods **********************************************************/ /* public static SurveyResponseCollection retrieveBySurvey(Survey survey) { DataCollection responses = diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponseCollection.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponseCollection.java new file mode 100644 index 000000000..611c28b1e --- /dev/null +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyResponseCollection.java @@ -0,0 +1,44 @@ + +package com.arsdigita.cms.contenttypes; + +import com.arsdigita.domain.DomainCollection; +import com.arsdigita.kernel.User; +import com.arsdigita.persistence.DataCollection; +import java.util.Date; + +/** + * + * @author Sören Bernstein + */ +public class SurveyResponseCollection extends DomainCollection { + + /** + * Creates a new instance of SurveyResponseCollection + */ + public SurveyResponseCollection(DataCollection dataCollection) { + super(dataCollection); + + m_dataCollection.addOrder(SurveyResponse.ENTRY_DATE); + } + + public SurveyResponseCollection(DataCollection dataCollection, User user) { + this(dataCollection); + +// m_dataCollection.addFilter(SurveyResponse.USER); + } + + // Get the entry date + public Date getEntryDate() { + return (Date) m_dataCollection.get(SurveyResponse.ENTRY_DATE); + } + + // Get the user + public User getUser() { + return (User) m_dataCollection.get(SurveyResponse.USER); + } + + public SurveyResponse getSurveyResponse() { + return new SurveyResponse(m_dataCollection.getDataObject()); + } + +} diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyProcessListener.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyProcessListener.java index 208b77402..ed7316e70 100755 --- a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyProcessListener.java +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyProcessListener.java @@ -1,21 +1,3 @@ -/* - * Copyright (C) 2002-2004 Red Hat Inc. All Rights Reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ package com.arsdigita.cms.contenttypes.ui; import com.arsdigita.bebop.event.FormProcessListener; @@ -46,6 +28,7 @@ import java.util.Iterator; import com.arsdigita.formbuilder.util.FormBuilderUtil; +import com.arsdigita.kernel.Kernel; import com.arsdigita.kernel.User; import java.math.BigDecimal; @@ -53,18 +36,18 @@ import java.math.BigDecimal; import org.apache.log4j.Logger; /** - * The process lister that processes a survey response entered by a user. + * A process listener to save the responses from a user to the database. * + * @author Sören Bernstein */ -public class SurveyProcessListener - implements FormProcessListener { +public class SurveyProcessListener implements FormProcessListener { - public static final String SURVEY_ID_NAME = "__ss_survey_id__"; - public static final String RESPONSE_ID = "__ss_response_id__"; - public static final BigDecimal THE_PUBLIC_USER = new BigDecimal(-200); - private static final String KNOWLEDGE_TEST = "knowledge_test"; - protected RequestLocal m_persistentForm = new RequestLocal(); - private RequestLocal m_nameQuestionMap = new RequestLocal(); + public static final String SURVEY_ID = "SurveyID"; +// public static final String RESPONSE_ID = "__ss_response_id__"; +// public static final BigDecimal THE_PUBLIC_USER = new BigDecimal(-200); +// private static final String KNOWLEDGE_TEST = "knowledge_test"; +// protected RequestLocal m_persistentForm = new RequestLocal(); +// private RequestLocal m_nameQuestionMap = new RequestLocal(); private RequestLocal m_response; private static org.apache.log4j.Logger s_log = Logger.getLogger(SurveyProcessListener.class.getName()); @@ -79,159 +62,147 @@ public class SurveyProcessListener public void process(FormSectionEvent event) { + Survey survey = null; + SurveyResponse surveyResponse = null; + + // Get the form data FormData formData = event.getFormData(); - PageState ps = event.getPageState(); - BigDecimal surveyID = (BigDecimal) formData.get(SURVEY_ID_NAME); - BigDecimal responseID = (BigDecimal) formData.get(RESPONSE_ID); - m_response.set(ps, responseID); + // Read the needed information to create a new response and create + // a new instance of SurveyResponse to store this information + BigDecimal surveyID = (BigDecimal) formData.get(formData.getParameter(SURVEY_ID)); - Survey survey = (Survey) FormBuilderUtil.instantiateObjectOneArg(Survey.class.getName(), surveyID); - SurveyResponse response = null; try { - response = (SurveyResponse) DomainObjectFactory.newInstance(new OID(SurveyResponse.class.getName(), responseID)); - } catch (DataObjectNotFoundException ex) { - // s_log.warn("Can't create this object" + responseID); - } - //Let's not save the data twice in the case of a double-click - if (response.questionsAnswered()) { + // Try to get the corresponding Survey object + survey = new Survey(surveyID); + + } catch (DataObjectNotFoundException ex) { + + // Strange, there is no survey with this id. Someone is messing aroound + s_log.warn("Can't find survey object with ID " + surveyID + ". Someone is messing around."); + + // Abort processing return; } - m_persistentForm.set(ps, survey.getForm()); + // Get the user + User user = (User) Kernel.getContext().getParty(); - // Get the responding user - User user = KernelHelper.getCurrentUser(ps.getRequest()); + // Create the new SurveyResponse object + surveyResponse = survey.addResponse(); + + + + // Process the answers by iteration over the form widget parameters + Iterator parameterIterator = formData.getParameters().iterator(); + while (parameterIterator.hasNext()) { + + ParameterData parameterData = (ParameterData) parameterIterator.next(); + addAnswer(surveyResponse, parameterData.getName(), parameterData.getValue()); - // Use the generic user "The Public" if the user is not registered - if (user == null) { - try { - user = User.retrieve(THE_PUBLIC_USER); - } catch (DataObjectNotFoundException e) { - s_log.error("Public User does not exist."); - } } - - // Iterate over the widget parameters and insert the answers to the survey response - Iterator parameterIter = formData.getParameters().iterator(); - while (parameterIter.hasNext()) { - s_log.warn("Found some formData"); - ParameterData parameterData = (ParameterData) parameterIter.next(); - - String parameterName = (String) parameterData.getName(); - - Object parameterValue = parameterData.getValue(); - if (parameterValue instanceof java.lang.String[]) { - // This is a multi-answer question - iterate over the - // answer values and add them one by one - String[] valueArray = (String[]) parameterValue; - for (int i = 0; i < valueArray.length; ++i) { -// addAnswer(response, ps, valueArray[i], parameterName); - - } - } else { - // Single answer question - -// addAnswer(response, ps, parameterValue, parameterName); - } - } - // Save the survey response to the database - response.save(); -// saveScore(survey, response); } -/* - private void saveScore(Survey survey, SurveyResponse response) { + private void addAnswer(SurveyResponse surveyResponse, Object name, Object value) { - String query; - if (survey.getQuizType().equals(KNOWLEDGE_TEST)) { - query = "com.arsdigita.simplesurvey.saveScore"; + // Test if value is a string array + if(value instanceof String[]) { + // This is a multi-answer question, so iterate over the answers + for (int i = 0; i < ((String[]) value).length; i++) { + addAnswer(surveyResponse, name, ((String[]) value)[i]); + } } else { - query = "com.arsdigita.simplesurvey.saveAssessmentScore"; + // Create new SurveyAnswer object +// surveyResponse.addAnswer(,, (String) value); } - DataOperation dao = SessionManager.getSession().retrieveDataOperation(query); - dao.setParameter("responseID", response.getID()); - dao.execute(); } -*/ -/* + + /* + PageState ps = event.getPageState(); + + BigDecimal responseID = (BigDecimal) formData.get(RESPONSE_ID); + m_response.set(ps, responseID); + + m_persistentForm.set(ps, survey.getForm()); + + */ + /* private void addAnswer(SurveyResponse surveyResponse, - PageState ps, - Object parameterValue, - String parameterName) { + PageState ps, + Object parameterValue, + String parameterName) { - s_log.debug("formData name " + parameterName + " value " + parameterValue); + s_log.debug("formData name " + parameterName + " value " + parameterValue); - Question question = getQuestion(ps, parameterName); + Question question = getQuestion(ps, parameterName); - if (question != null) { + if (question != null) { - PersistentLabel persistentLabel = question.getLabel(); - PersistentWidget persistentWidget = question.getWidget(); + PersistentLabel persistentLabel = question.getLabel(); + PersistentWidget persistentWidget = question.getWidget(); - surveyResponse.addAnswer(persistentLabel, persistentWidget, getStringValue(parameterValue)); - } + surveyResponse.addAnswer(persistentLabel, persistentWidget, getStringValue(parameterValue)); } -*/ -/* + } + */ + /* private String getStringValue(Object parameterValue) { - return parameterValue == null ? "" : parameterValue.toString(); + return parameterValue == null ? "" : parameterValue.toString(); } -*/ -/* + */ + /* protected Question getQuestion(PageState ps, String parameterName) { - if (m_nameQuestionMap.get(ps) == null) { + if (m_nameQuestionMap.get(ps) == null) { - // Populate the parameter name label id map - synchronized (this) { + // Populate the parameter name label id map + synchronized (this) { - Map nameQuestionMap = new HashMap(); + Map nameQuestionMap = new HashMap(); - s_log.debug("initializing the parameter name persistent label map"); + s_log.debug("initializing the parameter name persistent label map"); - PersistentForm persistentForm = (PersistentForm) m_persistentForm.get(ps); - DataAssociationCursor componentCursor = persistentForm.getComponents(); - PersistentLabel lastPersistentLabel = null; - while (componentCursor.next()) { + PersistentForm persistentForm = (PersistentForm) m_persistentForm.get(ps); + DataAssociationCursor componentCursor = persistentForm.getComponents(); + PersistentLabel lastPersistentLabel = null; + while (componentCursor.next()) { - PersistentComponent factory = (PersistentComponent) DomainObjectFactory.newInstance(componentCursor.getDataObject()); + PersistentComponent factory = (PersistentComponent) DomainObjectFactory.newInstance(componentCursor.getDataObject()); - s_log.debug("iterating, component " + factory.toString()); + s_log.debug("iterating, component " + factory.toString()); - // If this is a PersistentLabel save its id - if (factory instanceof com.arsdigita.formbuilder.PersistentLabel) { + // If this is a PersistentLabel save its id + if (factory instanceof com.arsdigita.formbuilder.PersistentLabel) { - lastPersistentLabel = (PersistentLabel) factory; - } - - // Add the previous label id if this is a PersistentWidget - if (factory instanceof com.arsdigita.formbuilder.PersistentWidget) { - - s_log.debug("adding to map " + ((PersistentWidget) factory).getParameterName() + - " mapped to " + lastPersistentLabel); - - Question question = new Question(lastPersistentLabel, - (PersistentWidget) factory); - - nameQuestionMap.put(((PersistentWidget) factory).getParameterName(), question); - } - } - - m_nameQuestionMap.set(ps, nameQuestionMap); - } - } - - s_log.debug("fetching label for parameter name " + parameterName); - - Question question = (Question) ((Map) m_nameQuestionMap.get(ps)).get(parameterName); - - s_log.debug("returning " + question); - - return question; + lastPersistentLabel = (PersistentLabel) factory; } - */ + // Add the previous label id if this is a PersistentWidget + if (factory instanceof com.arsdigita.formbuilder.PersistentWidget) { + + s_log.debug("adding to map " + ((PersistentWidget) factory).getParameterName() + + " mapped to " + lastPersistentLabel); + + Question question = new Question(lastPersistentLabel, + (PersistentWidget) factory); + + nameQuestionMap.put(((PersistentWidget) factory).getParameterName(), question); + } + } + + m_nameQuestionMap.set(ps, nameQuestionMap); + } + } + + s_log.debug("fetching label for parameter name " + parameterName); + + Question question = (Question) ((Map) m_nameQuestionMap.get(ps)).get(parameterName); + + s_log.debug("returning " + question); + + return question; + } + */ }