1 package org.torweg.pulse.component.survey.model;
2
3 import javax.persistence.Basic;
4 import javax.persistence.Column;
5 import javax.persistence.Entity;
6 import javax.persistence.Inheritance;
7 import javax.persistence.InheritanceType;
8 import javax.persistence.Transient;
9 import javax.xml.bind.annotation.XmlAccessOrder;
10 import javax.xml.bind.annotation.XmlAccessType;
11 import javax.xml.bind.annotation.XmlAccessorOrder;
12 import javax.xml.bind.annotation.XmlAccessorType;
13 import javax.xml.bind.annotation.XmlAttribute;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlRootElement;
16
17 import org.hibernate.Session;
18 import org.hibernate.Transaction;
19 import org.torweg.pulse.invocation.lifecycle.Lifecycle;
20 import org.torweg.pulse.service.PulseException;
21 import org.torweg.pulse.util.entity.AbstractBasicEntity;
22
23
32 @Entity
33 @Inheritance(strategy = InheritanceType.JOINED)
34 @XmlRootElement(name = "abstract-answer")
35 @XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
36 @XmlAccessorType(XmlAccessType.FIELD)
37 public abstract class AbstractTypedAnswer<T> extends AbstractBasicEntity {
38
39
42 private static final long serialVersionUID = -3015370690973043330L;
43
44
48 @Basic
49 @Column(nullable = false, updatable = false)
50 @XmlAttribute(name = "question-id")
51 private Long questionId;
52
53
60 @Basic
61 @XmlElement(name = "is-valid")
62 private boolean isValid = true;
63
64
68 @Transient
69 @XmlElement(name = "validation-error")
70 private AnswerValidationError validationError = null;
71
72
79 protected final void setQuestionId(final Long questId) {
80 if (questId == null) {
81 throw new PulseException(
82 "The questionId for the answer cannot be null!");
83 }
84 this.questionId = questId;
85 }
86
87
93 public final Long getQuestionId() {
94 return this.questionId;
95 }
96
97
109 public final AbstractQuestion getQuestion(final Session s) {
110 AbstractQuestion question = null;
111
112 if (s != null) {
113 question = (AbstractQuestion) s.get(AbstractQuestion.class,
114 getQuestionId());
115 } else {
116 Session ss = Lifecycle.getHibernateDataSource().createNewSession();
117 Transaction tx = ss.beginTransaction();
118 try {
119 question = (AbstractQuestion) ss.get(AbstractQuestion.class,
120 getQuestionId());
121 tx.commit();
122 } catch (Exception e) {
123 tx.rollback();
124 throw new PulseException("Error: " + e.getLocalizedMessage(), e);
125 } finally {
126 ss.close();
127 }
128 }
129
130 return question;
131 }
132
133
139 protected final void setValid(final boolean valid) {
140 this.isValid = valid;
141 }
142
143
152 public final boolean isValid() {
153 return this.isValid;
154 }
155
156
162 protected final void setValidationError(final AnswerValidationError error) {
163 this.validationError = error;
164 }
165
166
171 public final AnswerValidationError getValidationError() {
172 return this.validationError;
173 }
174
175
180 public abstract T getValue();
181
182 }
183