1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.component.survey.model;
19   
20   import javax.persistence.Basic;
21   import javax.persistence.Column;
22   import javax.persistence.Entity;
23   import javax.persistence.Inheritance;
24   import javax.persistence.InheritanceType;
25   import javax.persistence.Lob;
26   import javax.persistence.Transient;
27   import javax.xml.bind.annotation.XmlAccessOrder;
28   import javax.xml.bind.annotation.XmlAccessType;
29   import javax.xml.bind.annotation.XmlAccessorOrder;
30   import javax.xml.bind.annotation.XmlAccessorType;
31   import javax.xml.bind.annotation.XmlAttribute;
32   import javax.xml.bind.annotation.XmlElement;
33   import javax.xml.bind.annotation.XmlRootElement;
34   import javax.xml.bind.annotation.XmlTransient;
35   
36   import org.torweg.pulse.service.request.ServiceRequest;
37   import org.torweg.pulse.util.entity.AbstractBasicEntity;
38   
39   /**
40    * The base-class to derive a question from.
41    * 
42    * @author Daniel Dietz
43    * @version $Revision: 1586 $
44    */
45   @Entity
46   @Inheritance(strategy = InheritanceType.JOINED)
47   @XmlRootElement(name = "abstract-question")
48   @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
49   @XmlAccessorType(XmlAccessType.FIELD)
50   public abstract class AbstractQuestion extends AbstractBasicEntity {
51   
52       /**
53        * The serialVersionUID.
54        */
55       private static final long serialVersionUID = -2520177698849847270L;
56   
57       /**
58        * The question text of the {@code AbstractQuestion}. Is used to be
59        * able to identify the question for export.
60        */
61       @Lob
62       @Column(length = 1048576, unique = false, nullable = true, updatable = true)
63       @XmlTransient
64       private String questionText = null;
65   
66       /**
67        * Indicates whether the {@code AbstractQuestion} requires to be
68        * answered.
69        * <p>
70        * default: {@code false}
71        * </p>
72        */
73       @Basic
74       @XmlElement(name = "is-allow-blank")
75       private boolean isAllowBlank = false;
76   
77       /**
78        * The f.q. class-name.
79        */
80       @Transient
81       @XmlAttribute(name = "class")
82       // required in XSL (for template-matching)
83       @SuppressWarnings("unused")
84       private final String clazz = getClass().getCanonicalName();
85   
86       /**
87        * Sets the value for <tt>isAllowBlank</tt> of the
88        * {@code AbstractQuestion}.
89        * 
90        * @param allow
91        *            the value to set
92        */
93       public final void setIsAllowBlank(final boolean allow) {
94           this.isAllowBlank = allow;
95       }
96   
97       /**
98        * Returns the value for allow-blank.
99        * 
100       * @return the value for allow-blank
101       */
102      public final boolean isAllowBlank() {
103          return this.isAllowBlank;
104      };
105  
106      /**
107       * Sets the question text for the {@code AbstractQuestion}.
108       * 
109       * @param text
110       *            the question text to set
111       */
112      @XmlElement(name = "question-text")
113      public final void setQuestionText(final String text) {
114          String normalized = text.replaceAll("\\s+", " ");
115          this.questionText = normalized.trim();
116      }
117  
118      /**
119       * Returns the question text of the {@code AbstractQuestion}.
120       * 
121       * @return the question text
122       */
123      public final String getQuestionText() {
124          return this.questionText;
125      }
126  
127      /**
128       * Checks the given {@code AbstractTypedAnswer&lt;?&gt;} for validity.
129       * 
130       * @param answer
131       *            the {@code AbstractTypedAnswer&lt;?&gt;} to validate
132       * @return {@code true} if the
133       *         {@code AbstractTypedAnswer&lt;?&gt;} is valid,
134       *         {@code false} otherwise
135       */
136      public abstract boolean isValid(AbstractTypedAnswer<?> answer);
137  
138      /**
139       * Returns the {@code AbstractTypedAnswer&lt;? extends Object&gt;} for
140       * the given {@code ServiceRequest}.
141       * 
142       * @param request
143       *            the {@code ServiceRequest}
144       * 
145       * @return the {@code AbstractTypedAnswer&lt;? extends Object&gt;}
146       */
147      public abstract AbstractTypedAnswer<?> getAnswer(ServiceRequest request);
148  
149  }
150