From 2ac8f56d933bde2702e5b0130af595d603c9f8a1 Mon Sep 17 00:00:00 2001 From: quasi Date: Mon, 1 Mar 2010 12:34:53 +0000 Subject: [PATCH] Survey MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * SurveyPersistenProcessListener wieder gelöscht. Funktioniert so nicht. * Speichert jetzt die Antworten in der Datenbank git-svn-id: https://svn.libreccm.org/ccm/trunk@359 8810af33-2d31-482b-a856-94f89814c4df --- .../com/arsdigita/content-types/Survey.pdl | 8 +- .../cms/contenttypes/FormCopier.java | 277 ++++++++++++++++++ .../arsdigita/cms/contenttypes/Survey.java | 189 ++++++++++-- .../cms/contenttypes/SurveyAnswer.java | 17 +- .../cms/contenttypes/SurveyResponse.java | 26 +- .../ui/SurveyPersistentProcessListener.java | 67 ----- .../ui/SurveyProcessListener.java | 23 +- 7 files changed, 488 insertions(+), 119 deletions(-) create mode 100644 ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/FormCopier.java delete mode 100644 ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyPersistentProcessListener.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 cf407fb11..f000ac3ee 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 @@ -21,7 +21,7 @@ object type SurveyResponse extends ContentItem { Date[1..1] entryDate = ct_surveys_responses.entry_date DATE; 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 + component SurveyAnswer[0..n] answers = join ct_surveys_responses.response_id to ct_surveys_answers.response_id; // XXX hack to allow us to filter a data query @@ -33,9 +33,9 @@ object type SurveyResponse extends ContentItem { object type SurveyAnswer extends ContentItem { - BigDecimal[1..1] questionNumber = ct_surveys_answers.question_number INTEGER; - String[1..1] key = ct_surveys_answers.key VARCHAR(4000); - String[0..1] value = ct_surveys_answers.value VARCHAR(4000); + Integer[1..1] questionNumber = ct_surveys_answers.question_number INTEGER; + String[1..1] key = ct_surveys_answers.key VARCHAR(4000); + String[0..1] value = ct_surveys_answers.value VARCHAR(4000); reference key (ct_surveys_answers.answer_id); } \ No newline at end of file diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/FormCopier.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/FormCopier.java new file mode 100644 index 000000000..f45a134ff --- /dev/null +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/FormCopier.java @@ -0,0 +1,277 @@ +/* + * 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; + +import com.arsdigita.formbuilder.PersistentForm; +import com.arsdigita.formbuilder.PersistentFormSection; +import com.arsdigita.formbuilder.PersistentOptionGroup; +import com.arsdigita.formbuilder.PersistentWidget; +import com.arsdigita.formbuilder.WidgetLabel; + +import com.arsdigita.cms.ACSObjectFactory; +import com.arsdigita.cms.ContentItem; +import com.arsdigita.cms.formbuilder.FormSectionWrapper; +import com.arsdigita.cms.util.GlobalizationUtil; +import com.arsdigita.domain.DataObjectNotFoundException; +import com.arsdigita.domain.DomainObjectFactory; +import com.arsdigita.domain.DomainService; +import com.arsdigita.kernel.ACSObject; +import com.arsdigita.persistence.DataAssociation; +import com.arsdigita.persistence.DataAssociationCursor; +import com.arsdigita.persistence.DataObject; +import com.arsdigita.persistence.metadata.ObjectType; +import com.arsdigita.persistence.metadata.Property; +import com.arsdigita.util.Assert; +import com.arsdigita.util.UncheckedWrapperException; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; + +/** + * A crude class to copy a form and all its widgets. + * + * This class should be replaced ASAP by a more + * generic ACSObject copier and a 'CopyableACSObject' + * interface + */ +class FormCopier extends DomainService { + private static org.apache.log4j.Logger s_log = + org.apache.log4j.Logger.getLogger(FormCopier.class); + + private HashMap m_copied = new HashMap(); + + public PersistentForm copyForm(PersistentForm form) { + PersistentForm tgt = (PersistentForm)copyFormSection(form); + + return tgt; + } + + public PersistentFormSection copyFormSection(PersistentFormSection src) { + PersistentFormSection tgt = (PersistentFormSection)copyObject(src); + + if( s_log.isDebugEnabled() ) { + s_log.debug("Copying form section " + src.getClass().getName()); + } + + copyDataObjectAssociation(getDataObject(src), + getDataObject(tgt), + "listeners"); + copyDataObjectAssociation(getDataObject(src), + getDataObject(tgt), + "component"); + tgt.save(); + + s_log.debug("Done copying, is now "); + + return tgt; + } + + protected FormSectionWrapper copyFormSectionWrapper(FormSectionWrapper src) { + FormSectionWrapper tgt = (FormSectionWrapper)copyObject(src); + + if( s_log.isDebugEnabled() ) { + s_log.debug("Copying form section wrapper " + src.getOID()); + } + + tgt.setFormSectionItem(src.getFormSectionItem()); + tgt.setVersion(ContentItem.LIVE); + + s_log.debug("Done copying form section wrapper "); + + return tgt; + } + + protected WidgetLabel copyWidgetLabel(WidgetLabel src) { + WidgetLabel tgt = (WidgetLabel)copyObject(src); + + if( s_log.isDebugEnabled() ) { + s_log.debug("Copying widget label " + src.getOID()); + } + + PersistentWidget srcWgt = null; + try { + srcWgt = src.getWidget(); + } catch (DataObjectNotFoundException ex) { + throw new UncheckedWrapperException( + "cannot find widget for label " + src.getOID(), ex); + } + + DataObject dstObj = copySingleObject( getDataObject( srcWgt ) ); + + PersistentWidget dstWgt = (PersistentWidget) + DomainObjectFactory.newInstance( dstObj ); + + tgt.setWidget(dstWgt); + + s_log.debug("Done copying widget label "); + + return tgt; + } + + + public PersistentOptionGroup copyOptionGroup(PersistentOptionGroup src) { + PersistentOptionGroup tgt = (PersistentOptionGroup)copyWidget(src); + + if( s_log.isDebugEnabled() ) { + s_log.debug("Copying option group " + src.getClass().getName()); + } + + copyDataObjectAssociation(getDataObject(src), + getDataObject(tgt), + "component"); + + s_log.debug("Done copying, is now "); + + return tgt; + } + + + private PersistentWidget copyWidget(PersistentWidget src) { + PersistentWidget tgt = (PersistentWidget)copyObject(src); + + if( s_log.isDebugEnabled() ) { + s_log.debug("Copying form widget " + src.getClass().getName()); + } + + copyDataObjectAssociation(getDataObject(src), + getDataObject(tgt), + "listeners"); + + s_log.debug("Done form widget"); + + return tgt; + } + + + private ACSObject copyObject(ACSObject src) { + String objectType = src.getSpecificObjectType(); + String javaClass = (String)get(src, ACSObject.DEFAULT_DOMAIN_CLASS); + + if( s_log.isDebugEnabled() ) { + s_log.debug("Copying object " + src.getClass().getName() + " " + src.getOID()); + } + + Assert.exists(objectType, String.class); + Assert.exists(javaClass, String.class); + + // Attempt to instantiate the copy + ACSObject tgt; + try { + tgt = + (ACSObject)ACSObjectFactory.createACSObject(javaClass, objectType); + } catch (Exception ex) { + throw new UncheckedWrapperException( + (String) GlobalizationUtil.globalize( + "cms.formbuilder.cannot_create_acsobject").localize(), ex); + } + + copyDataObjectAttributes(getDataObject(src), + getDataObject(tgt)); + + s_log.debug("Done copying object"); + + return tgt; + } + + private void copyDataObjectAssociation(DataObject src, + DataObject tgt, + String name) { + if( s_log.isDebugEnabled() ) { + s_log.debug("Copy association " + src.get("objectType") + " " + name); + } + DataAssociation tgtAssoc = (DataAssociation)tgt.get(name); + + DataAssociation srcAssoc = (DataAssociation)src.get(name); + DataAssociationCursor daCursor = ((DataAssociation)srcAssoc).cursor(); + + while (daCursor.next()) { + if( s_log.isDebugEnabled() ) { + s_log.debug("Copy association object " + name + + " " + daCursor.getDataObject()); + } + DataObject copy = copySingleObject(daCursor.getDataObject()); + + DataObject tgtLink = tgtAssoc.add(copy); + if (tgtLink != null) { + if( s_log.isDebugEnabled() ) { + s_log.debug("Copy link " + daCursor.getLink()); + } + copyDataObjectAttributes(daCursor.getLink(), + tgtLink); + } + } + + s_log.debug("Done association"); + } + + private DataObject copySingleObject(DataObject src) { + ACSObject srcObj = (ACSObject) DomainObjectFactory.newInstance(src); + + ACSObject tgtObj = (ACSObject) m_copied.get( srcObj.getOID().toString() ); + if( null != tgtObj ) { + if( s_log.isDebugEnabled() ) { + s_log.debug( "Using cached copy of " + srcObj.getOID() ); + } + } else { + if( s_log.isDebugEnabled() ) { + s_log.debug( "Copying " + srcObj.getClass().getName() + " " + + srcObj.getOID() ); + } + + if (srcObj instanceof PersistentFormSection) { + tgtObj = copyFormSection((PersistentFormSection)srcObj); + } else if (srcObj instanceof PersistentOptionGroup) { + tgtObj = copyOptionGroup((PersistentOptionGroup)srcObj); + } else if (srcObj instanceof FormSectionWrapper) { + tgtObj = copyFormSectionWrapper((FormSectionWrapper)srcObj); + } else if (srcObj instanceof PersistentWidget) { + tgtObj = copyWidget((PersistentWidget)srcObj); + } else if (srcObj instanceof WidgetLabel) { + tgtObj = copyWidgetLabel((WidgetLabel)srcObj); + } else { + tgtObj = copyObject(srcObj); + } + + m_copied.put( srcObj.getOID().toString(), tgtObj ); + } + + return getDataObject(tgtObj); + } + + + private void copyDataObjectAttributes(DataObject src, + DataObject tgt) { + ObjectType type = src.getOID().getObjectType(); + Collection keyAttrNames = ACSObjectFactory.getKeyAttributeNames(type); + + for (Iterator i = type.getProperties(); i.hasNext(); ) { + Property prop = (Property) i.next(); + if (!prop.isAttribute()) { + continue; + } + String attrName = prop.getName(); + + // Do not copy primary key attributes + if (!keyAttrNames.contains(attrName)) { + tgt.set(attrName, src.get(attrName)); + } + } + } +} 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 007b842a8..3abaebc65 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 @@ -1,22 +1,48 @@ package com.arsdigita.cms.contenttypes; +import com.arsdigita.bebop.Component; +import com.arsdigita.bebop.Form; +import com.arsdigita.bebop.Label; +import com.arsdigita.bebop.Page; +import com.arsdigita.bebop.PageState; +import com.arsdigita.bebop.form.Widget; import com.arsdigita.bebop.parameters.BigDecimalParameter; +import com.arsdigita.bebop.util.Traversal; +import com.arsdigita.cms.CMS; import com.arsdigita.cms.ContentPage; +import com.arsdigita.cms.CustomCopy; +import com.arsdigita.cms.ItemCopier; +import com.arsdigita.cms.contenttypes.ui.SurveyPersistentProcessListener; import com.arsdigita.cms.contenttypes.ui.SurveyPersistentProcessListener; import com.arsdigita.cms.contenttypes.ui.SurveyProcessListener; +import com.arsdigita.cms.contenttypes.ui.SurveyProcessListener; +import com.arsdigita.cms.dispatcher.SimpleXMLGenerator; +import com.arsdigita.cms.dispatcher.XMLGenerator; +import com.arsdigita.cms.formbuilder.FormUnavailableException; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; import com.arsdigita.persistence.SessionManager; import com.arsdigita.persistence.DataCollection; import java.math.BigDecimal; import com.arsdigita.domain.DataObjectNotFoundException; - import com.arsdigita.formbuilder.PersistentForm; import com.arsdigita.formbuilder.PersistentHidden; +import com.arsdigita.formbuilder.PersistentSubmit; +import com.arsdigita.formbuilder.ui.BaseAddObserver; +import com.arsdigita.formbuilder.ui.PlaceholdersInitListener; import com.arsdigita.kernel.User; +import com.arsdigita.persistence.metadata.Property; +import com.arsdigita.util.UncheckedWrapperException; +import com.arsdigita.web.URL; +import com.arsdigita.web.Web; +import com.arsdigita.xml.Element; import java.util.Date; +import com.arsdigita.formbuilder.actions.ConfirmEmailListener; +import com.arsdigita.formbuilder.ui.FormBuilderXMLRenderer; +import com.arsdigita.formbuilder.util.FormBuilderUtil; + /** * A survey content type that represents a survey. This is partially based on * the simplesurvey application and CT FormItem. @@ -24,7 +50,7 @@ import java.util.Date; * @author Sören Bernstein * */ -public class Survey extends ContentPage { +public class Survey extends ContentPage implements XMLGenerator { /** PDL property name for formSection */ public static final String SURVEY_ID = "survey_id"; @@ -123,38 +149,49 @@ public class Survey extends ContentPage { setAssociation(FORM, form); } - /* // Preset the responsesPublic if (getResponsesPublic() == null) { setResponsesPublic(false); } - */ - /* // Preset the responsesAnonym if (getResponsesAnonym() == null) { setResponsesAnonym(false); } - */ } super.beforeSave(); } - /* accessors *****************************************************/ - public void setForm(PersistentForm persistentForm) { -// persistentForm.addProcessListener(new SurveyPersistentProcessListener()); -// PersistentHidden survey_id = PersistentHidden.create(SURVEY_ID); -// survey_id.setDefaultValue(getSurveyID()); -// persistentForm.addComponent(survey_id); - set(FORM, persistentForm); + // This will be called during publish + @Override + public boolean copyProperty(CustomCopy src, + Property property, + ItemCopier copier) { + if (property.getName().equals(FORM)) { + PersistentForm pForm = ((Survey) src).getForm(); + + // Add hideden field with survey id + PersistentHidden survey_id = PersistentHidden.create(SURVEY_ID); + survey_id.setDefaultValue(((Survey) src).getSurveyID().toString()); + pForm.addComponent(survey_id); + + // Add a submit button + PersistentSubmit submit = PersistentSubmit.create("submit"); + pForm.addComponent(submit); + setAssociation(FORM, (new FormCopier()).copyForm(pForm)); + return true; + } + + return super.copyProperty(src, property, copier); } + /* accessors *****************************************************/ public PersistentForm getForm() { return new PersistentForm((DataObject) get(FORM)); } public BigDecimal getSurveyID() { - return (BigDecimal) get(SURVEY_ID); + return getID(); } public void setStartDate(Date startDate) { @@ -190,21 +227,21 @@ public class Survey extends ContentPage { } public SurveyResponse addResponse(User user) { - SurveyResponse surveyResponse = new SurveyResponse(user); + SurveyResponse surveyResponse = new SurveyResponse(getSurveyID(), user); addResponse(surveyResponse); return surveyResponse; } - + protected void addResponse(SurveyResponse surveyResponse) { add(RESPONSES, surveyResponse); } public SurveyResponseCollection getResponses() { - return new SurveyResponseCollection ((DataCollection) get(RESPONSES)); + return new SurveyResponseCollection((DataCollection) get(RESPONSES)); } public SurveyResponseCollection getResponses(User user) { - return new SurveyResponseCollection ((DataCollection) get(RESPONSES), user); + return new SurveyResponseCollection((DataCollection) get(RESPONSES), user); } /* methods ****************************************************************/ @@ -218,7 +255,121 @@ public class Survey extends ContentPage { public boolean isActive() { Date currentDate = new Date(); - return currentDate.compareTo(getStartDate()) > 0 && currentDate.compareTo(getEndDate()) > 0; + return currentDate.compareTo(getStartDate()) > 0 && currentDate.compareTo(getEndDate()) < 0; + } + + public void generateXML(PageState state, Element parent, String useContext) { + + PersistentForm form = getForm(); + Component c = instantiateForm(form, "itemAdminSummary".equals(useContext)); + + // Fake the page context for the item, since we + // have no access to the real page context. + Page p = new Page("dummy"); + p.add(c); + p.lock(); + + PageState fake; + try { +// if ("itemAdminSummary".equals(useContext)) { + // Chop off all the parameters to stop bebop stategetting confused +// fake = p.process(new NoParametersHttpServletRequest( +// state.getRequest()), state.getResponse()); +// } else { + // Really serving the user page, so need the params when + // processing the form + fake = p.process(state.getRequest(), state.getResponse()); +// } + } catch (Exception e) { + throw new UncheckedWrapperException(e); + } + +// Traversal t = new VisibleTraverse(fake); +// t.preorder(c); + + + // Simply embed the bebop xml as a child of the cms:item tag + Element element = parent.newChildElement("cms:item", CMS.CMS_XML_NS); +// generateXMLBody(fake, element, form); + String action = form.getAction(); + if (action == null) { + final URL requestURL = Web.getContext().getRequestURL(); + + if (requestURL == null) { + action = state.getRequest().getRequestURI(); + } else { + action = requestURL.getRequestURI(); + } + } + + element.addAttribute(FormBuilderUtil.FORM_ACTION, action); + + FormBuilderXMLRenderer renderer = + new FormBuilderXMLRenderer(element); + renderer.setWrapAttributes(true); + renderer.setWrapRoot(false); + renderer.setRevisitFullObject(true); + renderer.setWrapObjects(false); + + renderer.walk(this, SimpleXMLGenerator.ADAPTER_CONTEXT); + + // then, if the component is actually a form, we need + // to print out any possible errors + // Ideally we could do this as part of the "walk" but for now + // that does not work because we don't pass in the page state + // although that can always we updated. +// if (c instanceof Form) { +// Element infoElement = +// element.newChildElement(FormBuilderUtil.FORMBUILDER_FORM_INFO, +// FormBuilderUtil.FORMBUILDER_XML_NS); +// Form f = (Form) c; + +// Traversal infoTraversal = +// new ComponentTraverse(state, ((Form) c).getFormData(state), +// infoElement); +// infoTraversal.preorder(f); +// } + + // we need to generate the state so that it can be part of the form + // and correctly included when the form is submitted. We could + // do this by iterating through the form data but it does not + // seem like a good idea to just cut and paste the code out + // of the PageState class + fake.setControlEvent(c); + fake.generateXML(element.newChildElement(FormBuilderUtil.FORMBUILDER_PAGE_STATE, + FormBuilderUtil.FORMBUILDER_XML_NS)); + } + + protected Component instantiateForm(PersistentForm persistentForm, boolean readOnly) { + + try { + + persistentForm.setComponentAddObserver(new BaseAddObserver()); + Form form = (Form) persistentForm.createComponent(); + form.addInitListener(new PlaceholdersInitListener()); + form.addProcessListener(new SurveyProcessListener()); + form.setMethod(Form.GET); + + if (readOnly) { + Traversal t = new Traversal() { + + public void act(Component c) { + try { + Widget widget = (Widget) c; + widget.setDisabled(); + widget.setReadOnly(); + } catch (ClassCastException ex) { + // Nada + } + } + }; + t.preorder(form); + } + + return form; + } catch (FormUnavailableException ex) { + return new Label("This form is temporarily unavailable"); + } } /* diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswer.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswer.java index e8fea8e2c..b745813b4 100755 --- a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswer.java +++ b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/SurveyAnswer.java @@ -1,10 +1,7 @@ package com.arsdigita.cms.contenttypes; import com.arsdigita.cms.ContentItem; -import com.arsdigita.db.Sequences; import com.arsdigita.domain.DataObjectNotFoundException; -import com.arsdigita.formbuilder.PersistentLabel; -import com.arsdigita.formbuilder.PersistentWidget; import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; import java.math.BigDecimal; @@ -32,11 +29,14 @@ public class SurveyAnswer extends ContentItem { /** * Default constructor. This creates a new (empty) Survey. **/ - public SurveyAnswer(int order, String key, String value) { + public SurveyAnswer(BigDecimal surveyResponseID, int questNum, String key, String value) { this(BASE_DATA_OBJECT_TYPE); + // Set unneeded but mandatory fields from content item + setName("SurveyAnswer-for-SurveyRepsonse-" + surveyResponseID); + // Set the data - setOrder(order); + setQuestionNumber(questNum); setKey(key); setValue(value); } @@ -89,11 +89,11 @@ public class SurveyAnswer extends ContentItem { } /* accessors *****************************************************/ - private void setOrder(int order) { - set(QUESTION_NUMBER, order); + private void setQuestionNumber(int questNum) { + set(QUESTION_NUMBER, questNum); } - public int getOrder() { + public int getQuestionNumber() { return ((Integer) get(QUESTION_NUMBER)).intValue(); } @@ -112,5 +112,4 @@ public class SurveyAnswer extends ContentItem { public String getValue() { return (String) get(VALUE); } - } 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 5dc8e6156..97fc1f449 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 @@ -20,8 +20,6 @@ import java.util.Date; */ public class SurveyResponse extends ContentItem { - /** PDL property name for survey */ - public static final String SURVEY = "survey"; /** PDL property name for user */ public static final String USER = "user"; /** PDL property name for entryDate */ @@ -37,14 +35,22 @@ public class SurveyResponse extends ContentItem { * * @param survey The survey for this SurveyResponse. **/ - public SurveyResponse(User user) { + public SurveyResponse(BigDecimal surveyID, User user) { this(BASE_DATA_OBJECT_TYPE); + // Set unneeded but manadatory fields from ContentItem + setName("SurveyResponse-for-Survey-" + surveyID); + // Save the date setEntryDate(); // Save the user, if any setUser(user); + + // Save, so I can add answers afterwards. If I don't save here, the + // persistence can't save this and the SurveyAnswers because of missing + // arguments. + save(); } /** @@ -94,6 +100,10 @@ public class SurveyResponse extends ContentItem { } /* accessors *****************************************************/ + public BigDecimal getSurveyResponseID() { + return getID(); + } + private void setEntryDate() { set(ENTRY_DATE, new Date()); } @@ -110,16 +120,8 @@ public class SurveyResponse extends ContentItem { return (User) get(USER); } -// private void setSurvey(Survey survey) { -// set(SURVEY, survey); -// set(SURVEY + "ID", survey.getID()); -// } - public Survey getSurvey() { - return (Survey) get(SURVEY); - } - public void addAnswer(int order, String key, String value) { - SurveyAnswer answer = new SurveyAnswer(order, key, value); + SurveyAnswer answer = new SurveyAnswer(getSurveyResponseID(), order, key, value); add(ANSWERS, answer); } diff --git a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyPersistentProcessListener.java b/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyPersistentProcessListener.java deleted file mode 100644 index 52dffa4af..000000000 --- a/ccm-cms-types-survey/src/com/arsdigita/cms/contenttypes/ui/SurveyPersistentProcessListener.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.arsdigita.cms.contenttypes.ui; - -import com.arsdigita.bebop.event.FormProcessListener; -import com.arsdigita.persistence.OID; -import com.arsdigita.persistence.DataObject; -import com.arsdigita.persistence.metadata.ObjectType; - -import com.arsdigita.formbuilder.PersistentProcessListener; - -import java.math.BigDecimal; - -public class SurveyPersistentProcessListener extends PersistentProcessListener { - - public static final String BASE_DATA_OBJECT_TYPE = - "com.arsdigita.cms.contenttypes.ui.SurveyPersistentProcessListener"; - - public SurveyPersistentProcessListener() { - this(BASE_DATA_OBJECT_TYPE); - } - - public SurveyPersistentProcessListener(String typeName) { - super(typeName); - } - - public SurveyPersistentProcessListener(ObjectType type) { - super(type); - } - - public SurveyPersistentProcessListener(DataObject obj) { - super(obj); - } - - public SurveyPersistentProcessListener(BigDecimal id) { - this(new OID(BASE_DATA_OBJECT_TYPE, id)); - } - - public SurveyPersistentProcessListener(OID oid) { - super(oid); - } - - public static SurveyPersistentProcessListener create(String name, String description) { - - SurveyPersistentProcessListener l = new SurveyPersistentProcessListener(); - l.setup(name, description); - - return l; - } - - @Override - protected void setup(String name, String description) { - super.setup(name, description); - } - - // XXX hack to get around some wierd issues - // with mdsql associations where the object - // type in question is a subtype of the - // one named in the association definition - @Override - public boolean isContainerModified() { - return false; - } - - @Override - public FormProcessListener createProcessListener() { - return new SurveyProcessListener(); - } -} 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 55c67f4aa..6efd4ef24 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 @@ -55,7 +55,7 @@ public class SurveyProcessListener implements FormProcessListener { // 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)); + BigDecimal surveyID = new BigDecimal((String) formData.getParameter(SURVEY_ID).getValue()); try { @@ -72,7 +72,7 @@ public class SurveyProcessListener implements FormProcessListener { } // If this survey isn't anonymous - if(!survey.getResponsesAnonym()) { + if (!survey.getResponsesAnonym()) { // Get the current user user = (User) Kernel.getContext().getParty(); @@ -81,29 +81,36 @@ public class SurveyProcessListener implements FormProcessListener { // Create the new SurveyResponse object surveyResponse = survey.addResponse(user); - - // 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, ++numQuestions, parameterData.getName(), parameterData.getValue()); + String parameterName = parameterData.getName().toString(); + + // Skip some unneeded Parameters, p.ex. submit button, survey_id + if (parameterName.startsWith("submit") || + parameterName.startsWith(SURVEY_ID) || + parameterName.startsWith("form.")) { + continue; + } + + addAnswer(surveyResponse, ++numQuestions, parameterName, parameterData.getValue()); } } - private void addAnswer(SurveyResponse surveyResponse, int questionNumber, Object name, Object value) { + private void addAnswer(SurveyResponse surveyResponse, int questionNumber, String name, Object value) { // Test if value is a string array - if(value instanceof String[]) { + 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, questionNumber, name, ((String[]) value)[i]); } } else { // Create new SurveyAnswer object - surveyResponse.addAnswer(questionNumber, (String) name, (String) value); + surveyResponse.addAnswer(questionNumber, name, (String) value); } } }