Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. public void createMultipleChoiceSingleAnswerQuestion() {
  2. try {
  3. currentQuestion = new Question();
  4. currentQuestion.setId(0);
  5. currentQuestion.setQuestionnaireid(currentQuestionnaire);
  6. getFacade().create(currentQuestion);
  7. //System.out.println("QUESTIONNAIRE IIIIIIIDDDDDDDDDDD: "+currentQuestionnaire.getId());
  8. //getFacade().create(q);
  9. } catch (Exception e) {
  10. System.out.println("ERRORRRR: "+e.getMessage());
  11.  
  12. }
  13. }
  14.  
  15. /*
  16. * To change this template, choose Tools | Templates
  17. * and open the template in the editor.
  18. */
  19. package model;
  20.  
  21. import java.io.Serializable;
  22. import java.util.Collection;
  23. import javax.persistence.*;
  24. import javax.validation.constraints.NotNull;
  25. import javax.validation.constraints.Size;
  26. import javax.xml.bind.annotation.XmlRootElement;
  27. import javax.xml.bind.annotation.XmlTransient;
  28.  
  29. /**
  30. *
  31. * @author Pedram
  32. */
  33. @Entity
  34. @Table(name = "question")
  35. @XmlRootElement
  36. @NamedQueries({
  37. @NamedQuery(name = "Question.findAll", query = "SELECT q FROM Question q"),
  38. @NamedQuery(name = "Question.findById", query = "SELECT q FROM Question q WHERE q.id = :id"),
  39. @NamedQuery(name = "Question.findByType", query = "SELECT q FROM Question q WHERE q.type = :type"),
  40. @NamedQuery(name = "Question.findByMandatory", query = "SELECT q FROM Question q WHERE q.mandatory = :mandatory"),
  41. @NamedQuery(name = "Question.findByOrdernumber", query = "SELECT q FROM Question q WHERE q.ordernumber = :ordernumber"),
  42. @NamedQuery(name = "Question.findByWeight", query = "SELECT q FROM Question q WHERE q.weight = :weight"),
  43. @NamedQuery(name = "Question.findByQuestionnaire", query = "SELECT q FROM Question q WHERE q.questionnaireid = :questionnaireid"),
  44. @NamedQuery(name = "Question.findByTag", query = "SELECT q FROM Question q WHERE q.tag = :tag")})
  45. public class Question implements Serializable {
  46. private static final long serialVersionUID = 1L;
  47. @Id
  48. @GeneratedValue(strategy = GenerationType.IDENTITY)
  49. @Basic(optional = false)
  50. @NotNull
  51. @Column(name = "id")
  52. private Integer id;
  53. @Size(max = 40)
  54. @Column(name = "type")
  55. private String type;
  56. @Lob
  57. @Size(max = 65535)
  58. @Column(name = "questiontext")
  59. private String questiontext;
  60. @Lob
  61. @Size(max = 65535)
  62. @Column(name = "answertext")
  63. private String answertext;
  64. @Column(name = "mandatory")
  65. private Boolean mandatory;
  66. @Lob
  67. @Size(max = 65535)
  68. @Column(name = "correctanswer")
  69. private String correctanswer;
  70. @Column(name = "ordernumber")
  71. private Integer ordernumber;
  72. @Column(name = "weight")
  73. private Integer weight;
  74. @Size(max = 40)
  75. @Column(name = "tag")
  76. private String tag;
  77. @OneToMany(mappedBy = "questionid")
  78. private Collection<Textanswer> textanswerCollection;
  79. @JoinColumn(name = "questionnaireid", referencedColumnName = "id")
  80. @ManyToOne
  81. private Questionnaire questionnaireid;
  82. @OneToMany(mappedBy = "questionid")
  83. private Collection<Choiceanswer> choiceanswerCollection;
  84.  
  85. public Question() {
  86. }
  87.  
  88. public Question(Integer id) {
  89. this.id = id;
  90. }
  91.  
  92. public Integer getId() {
  93. return id;
  94. }
  95.  
  96. public void setId(Integer id) {
  97. this.id = id;
  98. }
  99.  
  100. public String getType() {
  101. return type;
  102. }
  103.  
  104. public void setType(String type) {
  105. this.type = type;
  106. }
  107.  
  108. public String getQuestiontext() {
  109. return questiontext;
  110. }
  111.  
  112. public void setQuestiontext(String questiontext) {
  113. this.questiontext = questiontext;
  114. }
  115.  
  116. public String getAnswertext() {
  117. return answertext;
  118. }
  119.  
  120. public void setAnswertext(String answertext) {
  121. this.answertext = answertext;
  122. }
  123.  
  124. public Boolean getMandatory() {
  125. return mandatory;
  126. }
  127.  
  128. public void setMandatory(Boolean mandatory) {
  129. this.mandatory = mandatory;
  130. }
  131.  
  132. public String getCorrectanswer() {
  133. return correctanswer;
  134. }
  135.  
  136. public void setCorrectanswer(String correctanswer) {
  137. this.correctanswer = correctanswer;
  138. }
  139.  
  140. public Integer getOrdernumber() {
  141. return ordernumber;
  142. }
  143.  
  144. public void setOrdernumber(Integer ordernumber) {
  145. this.ordernumber = ordernumber;
  146. }
  147.  
  148. public Integer getWeight() {
  149. return weight;
  150. }
  151.  
  152. public void setWeight(Integer weight) {
  153. this.weight = weight;
  154. }
  155.  
  156. public String getTag() {
  157. return tag;
  158. }
  159.  
  160. public void setTag(String tag) {
  161. this.tag = tag;
  162. }
  163.  
  164. @XmlTransient
  165. public Collection<Textanswer> getTextanswerCollection() {
  166. return textanswerCollection;
  167. }
  168.  
  169. public void setTextanswerCollection(Collection<Textanswer> textanswerCollection) {
  170. this.textanswerCollection = textanswerCollection;
  171. }
  172.  
  173. public Questionnaire getQuestionnaireid() {
  174. return questionnaireid;
  175. }
  176.  
  177. public void setQuestionnaireid(Questionnaire questionnaireid) {
  178. this.questionnaireid = questionnaireid;
  179. }
  180.  
  181. @XmlTransient
  182. public Collection<Choiceanswer> getChoiceanswerCollection() {
  183. return choiceanswerCollection;
  184. }
  185.  
  186. public void setChoiceanswerCollection(Collection<Choiceanswer> choiceanswerCollection) {
  187. this.choiceanswerCollection = choiceanswerCollection;
  188. }
  189.  
  190. @Override
  191. public int hashCode() {
  192. int hash = 0;
  193. hash += (id != null ? id.hashCode() : 0);
  194. return hash;
  195. }
  196.  
  197. @Override
  198. public boolean equals(Object object) {
  199. // TODO: Warning - this method won't work in the case the id fields are not set
  200. if (!(object instanceof Question)) {
  201. return false;
  202. }
  203. Question other = (Question) object;
  204. if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
  205. return false;
  206. }
  207. return true;
  208. }
  209.  
  210. @Override
  211. public String toString() {
  212. return "model.Question[ id=" + id + " ]";
  213. }
  214.  
  215. }
  216.  
  217. /*
  218. * To change this template, choose Tools | Templates
  219. * and open the template in the editor.
  220. */
  221. package facade;
  222.  
  223. import java.util.List;
  224. import javax.ejb.Stateless;
  225. import javax.persistence.EntityManager;
  226. import javax.persistence.Persistence;
  227. import javax.persistence.PersistenceContext;
  228. import javax.persistence.Query;
  229. import model.Question;
  230. import model.Questionnaire;
  231.  
  232. /**
  233. *
  234. * @author Pedram
  235. */
  236. @Stateless
  237. public class QuestionFacade extends AbstractFacade<Question> {
  238. @PersistenceContext(unitName = "MobileSamplingToolkit0.4PU")
  239. private EntityManager em;
  240. List<Question> results = null;
  241.  
  242. @Override
  243. protected EntityManager getEntityManager() {
  244. return em;
  245. }
  246.  
  247. public QuestionFacade() {
  248. super(Question.class);
  249. }
  250.  
  251. public List<Question> getQuestionsByQuestionnaire(Questionnaire questionnaire){
  252. try {
  253. em = Persistence.createEntityManagerFactory("MobileSamplingToolkit0.4PU").createEntityManager();
  254. Query query = em.createNamedQuery("Question.findByQuestionnaire");
  255. query.setParameter("questionnaireid", questionnaire);
  256. results = query.getResultList();
  257. } catch (Exception e) {
  258. System.out.println("ERROR IN Question FACADE:" + e.getMessage());
  259. }
  260. return results;
  261. }
  262.  
  263. }
  264.  
  265. /*
  266. * To change this template, choose Tools | Templates
  267. * and open the template in the editor.
  268. */
  269. package facade;
  270.  
  271. import java.util.List;
  272. import javax.persistence.EntityManager;
  273.  
  274. /**
  275. *
  276. * @author Pedram
  277. */
  278. public abstract class AbstractFacade<T> {
  279. private Class<T> entityClass;
  280.  
  281. public AbstractFacade(Class<T> entityClass) {
  282. this.entityClass = entityClass;
  283. }
  284.  
  285. protected abstract EntityManager getEntityManager();
  286.  
  287. public void create(T entity) {
  288. getEntityManager().persist(entity);
  289. }
  290.  
  291. public void edit(T entity) {
  292. getEntityManager().merge(entity);
  293. }
  294.  
  295. public void remove(T entity) {
  296. getEntityManager().remove(getEntityManager().merge(entity));
  297. }
  298.  
  299. public T find(Object id) {
  300. return getEntityManager().find(entityClass, id);
  301. }
  302.  
  303. public List<T> findAll() {
  304. javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
  305. cq.select(cq.from(entityClass));
  306. return getEntityManager().createQuery(cq).getResultList();
  307. }
  308.  
  309. public List<T> findRange(int[] range) {
  310. javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
  311. cq.select(cq.from(entityClass));
  312. javax.persistence.Query q = getEntityManager().createQuery(cq);
  313. q.setMaxResults(range[1] - range[0]);
  314. q.setFirstResult(range[0]);
  315. return q.getResultList();
  316. }
  317.  
  318. public int count() {
  319. javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
  320. javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
  321. cq.select(getEntityManager().getCriteriaBuilder().count(rt));
  322. javax.persistence.Query q = getEntityManager().createQuery(cq);
  323. return ((Long) q.getSingleResult()).intValue();
  324. }
  325.  
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement