From 37387ee4a38192339ef204457479adaa74063523 Mon Sep 17 00:00:00 2001
From: joha-asc <aschan.johan@gmail.com>
Date: Wed, 27 Jul 2011 16:46:53 +0200
Subject: [PATCH] checklist data classes

---
 .../data/dao/interfaces/CheckListDao.java     |   4 +
 .../data/dao/jpa/CheckListDaoJPAImp.java      |  33 ++++-
 .../scipro/data/dataobjects/CheckList.java    | 131 +++++++++---------
 .../data/dataobjects/CheckListAnswer.java     |  68 +++++----
 .../data/dataobjects/CheckListQuestion.java   |  63 ++++++---
 .../data/dataobjects/CheckListTemplate.java   |  21 ++-
 6 files changed, 202 insertions(+), 118 deletions(-)

diff --git a/src/main/java/se/su/dsv/scipro/data/dao/interfaces/CheckListDao.java b/src/main/java/se/su/dsv/scipro/data/dao/interfaces/CheckListDao.java
index 0dab6f8068..4c5058033e 100644
--- a/src/main/java/se/su/dsv/scipro/data/dao/interfaces/CheckListDao.java
+++ b/src/main/java/se/su/dsv/scipro/data/dao/interfaces/CheckListDao.java
@@ -1,7 +1,11 @@
 package se.su.dsv.scipro.data.dao.interfaces;
 
+import java.util.List;
+
 import se.su.dsv.scipro.data.dataobjects.CheckList;
+import se.su.dsv.scipro.data.dataobjects.Project;
 
 public interface CheckListDao extends Dao<CheckList> {
 
+	public List<CheckList> findCheckLists(final Project project);
 }
diff --git a/src/main/java/se/su/dsv/scipro/data/dao/jpa/CheckListDaoJPAImp.java b/src/main/java/se/su/dsv/scipro/data/dao/jpa/CheckListDaoJPAImp.java
index 4f0786dd92..94994e3311 100644
--- a/src/main/java/se/su/dsv/scipro/data/dao/jpa/CheckListDaoJPAImp.java
+++ b/src/main/java/se/su/dsv/scipro/data/dao/jpa/CheckListDaoJPAImp.java
@@ -1,9 +1,20 @@
 package se.su.dsv.scipro.data.dao.jpa;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
+import javax.persistence.PersistenceException;
+import javax.persistence.TypedQuery;
+
+import org.hibernate.ejb.QueryHints;
+import org.springframework.orm.jpa.JpaCallback;
 import org.springframework.stereotype.Repository;
 
 import se.su.dsv.scipro.data.dao.interfaces.CheckListDao;
 import se.su.dsv.scipro.data.dataobjects.CheckList;
+import se.su.dsv.scipro.data.dataobjects.Project;
 
 /**
  * 
@@ -12,10 +23,28 @@ import se.su.dsv.scipro.data.dataobjects.CheckList;
  */
 
 @Repository("checkListDao")
-public class CheckListDaoJPAImp extends AbstractDaoJPAImp<CheckList>
-		implements CheckListDao {
+public class CheckListDaoJPAImp extends AbstractDaoJPAImp<CheckList> implements CheckListDao {
 
 	public CheckListDaoJPAImp() {
 		super(CheckList.class);
 	}
+
+	public List<CheckList> findCheckLists(final Project project) {
+		return getJpaTemplate().execute(new JpaCallback<List<CheckList>>() {
+			public List<CheckList> doInJpa(EntityManager em) throws PersistenceException {
+				String q = "select s " + "from CheckList s " + "where s.project = :project";
+
+				TypedQuery<CheckList> query = em.createQuery(q, CheckList.class);
+				query.setParameter("project", project);
+				query.setHint(QueryHints.HINT_CACHEABLE, "true");
+				try {
+					return query.getResultList();
+				} catch (NoResultException e) {
+					return new ArrayList<CheckList>();
+				}
+
+			}
+
+		});
+	}
 }
diff --git a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckList.java b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckList.java
index 9629983bbf..a44465e669 100644
--- a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckList.java
+++ b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckList.java
@@ -1,6 +1,7 @@
 package se.su.dsv.scipro.data.dataobjects;
 
-import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 import javax.persistence.Cacheable;
 import javax.persistence.Column;
@@ -8,49 +9,52 @@ import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import javax.persistence.ManyToOne;
+import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
 import org.hibernate.annotations.Cache;
 import org.hibernate.annotations.CacheConcurrencyStrategy;
+import org.hibernate.annotations.Sort;
+import org.hibernate.annotations.SortType;
 
 /**
-
- * @author Fredrik Norberg	fnorbe@dsv.su.se
- *
+ * 
+ * @author Fredrik Norberg fnorbe@dsv.su.se
+ * 
  */
 
 @Entity
-@Table(name="checklist")
+@Table(name = "checklist")
 @Cacheable(true)
-@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) //Hibernate
-public abstract class CheckList extends LazyDeletableDomainObject{
+@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
+// Hibernate
+public class CheckList extends DomainObject {
 
 	private static final long serialVersionUID = 2959377496669050427L;
-	
+
 	@Id
 	@GeneratedValue
 	private Long id;
-	
-	@Column(nullable=false)
+
+	@Column(nullable = false)
 	private String name;
+
+	@ManyToOne(optional = false)
+	private Project project;
+
 	
-	@ManyToOne(optional=false)
-	private User creator;
-	
-	@Column(nullable=false)
-	private List<CheckListQuestion> questions;
-	
-	@Column(nullable=false)
-	private ProjectClass level;
-	
-	public CheckList() {}
-	
-	public CheckList(String name, User creator, ProjectClass level) {
-	this.name = name;
-	this.creator = creator;
-	this.level = level;
+	@OneToMany
+	@Sort(type = SortType.NATURAL)
+	private SortedSet<CheckListQuestion> questions = new TreeSet<CheckListQuestion>();
+
+	public CheckList() {
 	}
-	
+
+	public CheckList(String name, Project project) {
+		this.name = name;
+		this.project = project;
+	}
+
 	public Long getId() {
 		return id;
 	}
@@ -59,66 +63,59 @@ public abstract class CheckList extends LazyDeletableDomainObject{
 		this.id = id;
 	}
 
-	public String getname() {
-		return name;
-	}
-	
-	public String getName(final int characters) {
-		String result = name;
-		if(name.length() > characters)
-			result = name.substring(0, characters)+"..";
-		return result;
-	}
-
 	public void setName(String name) {
 		this.name = name;
 	}
-	
-	
-	public void setCreator(User creator) {
-		this.creator = creator;
-	}
 
-	public User getCreator() {
-		return creator;
-	}
-	
-	public List<CheckListQuestion> getquestions(){
+	/**
+	 * @return the questions
+	 */
+	public SortedSet<CheckListQuestion> getQuestions() {
 		return questions;
 	}
-	
-	public void setquestions(List<CheckListQuestion> questions){
-		this.questions=questions;
+
+	/**
+	 * @param questions
+	 *            the questions to set
+	 */
+	public void setQuestions(SortedSet<CheckListQuestion> questions) {
+		this.questions = questions;
 	}
-	
-	public void setLevel(ProjectClass level){
-		this.level = level;
+
+	/**
+	 * @return the project
+	 */
+	public Project getProject() {
+		return project;
 	}
-	
-	public ProjectClass getLevel(){
-		return level;
+
+	/**
+	 * @param project the project to set
+	 */
+	public void setProject(Project project) {
+		this.project = project;
 	}
-	
+
 	@Override
 	public int hashCode() {
 		final int weight = 31;
 		int result = 17;
-		
+
 		result = weight * result + ((id == null) ? 0 : (int) (id ^ (id >>> 32)));
 
 		return result;
 	}
-	
+
 	@Override
 	public boolean equals(Object obj) {
 		if (this == obj)
 			return true;
 		if (obj == null)
 			return false;
-		if (obj instanceof CheckList){
-			
+		if (obj instanceof CheckList) {
+
 			CheckList other = (CheckList) obj;
-		
+
 			if (id == null) {
 				if (other.id != null)
 					return false;
@@ -130,11 +127,11 @@ public abstract class CheckList extends LazyDeletableDomainObject{
 		return false;
 	}
 
-	@Override
-	public String toString() {
-		return getname();
+	/**
+	 * @return the name
+	 */
+	public String getName() {
+		return name;
 	}
 
 }
-
-	
\ No newline at end of file
diff --git a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListAnswer.java b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListAnswer.java
index c979fecc97..3045baded3 100644
--- a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListAnswer.java
+++ b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListAnswer.java
@@ -1,53 +1,55 @@
 package se.su.dsv.scipro.data.dataobjects;
 
-import java.util.Date;
-
 import javax.persistence.Column;
 import javax.persistence.Entity;
-import javax.persistence.EntityManager;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
+import javax.persistence.Lob;
 import javax.persistence.ManyToOne;
 import javax.persistence.Table;
-import javax.persistence.TypedQuery;
-
-import org.apache.wicket.markup.html.panel.Panel;
 
 import se.su.dsv.scipro.data.enums.CheckListQuestionAnswer;
-import se.su.dsv.scipro.data.enums.EventStatus;
-import se.su.dsv.scipro.schedule.baseevent.panels.EventScheduleDetailsPanel;
-import se.su.dsv.scipro.util.IAjaxCallback;
 
 /**
- * @author Fredrik Norberg  fnorbe@dsv.su.se
- *
+ * @author Fredrik Norberg fnorbe@dsv.su.se
+ * 
  */
 @Entity
-@Table(name="checklist_answer")
-public class CheckListAnswer extends DomainObject{
+@Table(name = "checklist_answer")
+public class CheckListAnswer extends DomainObject {
 
 	private static final long serialVersionUID = 2959377496669050427L;
-	
+
 	@Id
 	@GeneratedValue
 	private Long id;
-	
+
 	@Enumerated(EnumType.STRING)
+	@Column(nullable=false)
 	private CheckListQuestionAnswer answer;
-	
-	@ManyToOne
+
+	@ManyToOne(optional=false)
 	private User user;
 	
-	
-	public CheckListAnswer() {}
-	
+	@Lob
+	private String comment;
+
+	public CheckListAnswer() {
+	}
+
 	public CheckListAnswer(User user, CheckListQuestionAnswer answer) {
 		this.user = user;
 		this.answer = answer;
 	}
 	
+	public CheckListAnswer(User user, CheckListQuestionAnswer answer, String comment) {
+		this.user = user;
+		this.answer = answer;
+		this.comment = comment;
+	}
+
 	public CheckListQuestionAnswer getAnswer() {
 		return answer;
 	}
@@ -71,27 +73,43 @@ public class CheckListAnswer extends DomainObject{
 	public void setId(Long id) {
 		this.id = id;
 	}
+	
+	
+
+	/**
+	 * @return the comment
+	 */
+	public String getComment() {
+		return comment;
+	}
+
+	/**
+	 * @param comment the comment to set
+	 */
+	public void setComment(String comment) {
+		this.comment = comment;
+	}
 
 	@Override
 	public int hashCode() {
 		final int weight = 31;
 		int result = 17;
-		
+
 		result = weight * result + ((id == null) ? 0 : (int) (id ^ (id >>> 32)));
 
 		return result;
 	}
-	
+
 	@Override
 	public boolean equals(Object obj) {
 		if (this == obj)
 			return true;
 		if (obj == null)
 			return false;
-		if (obj instanceof CheckListAnswer){
-			
+		if (obj instanceof CheckListAnswer) {
+
 			CheckListAnswer other = (CheckListAnswer) obj;
-		
+
 			if (id == null) {
 				if (other.id != null)
 					return false;
diff --git a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListQuestion.java b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListQuestion.java
index fb9d0d90a9..c81037bd91 100644
--- a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListQuestion.java
+++ b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListQuestion.java
@@ -1,5 +1,6 @@
 package se.su.dsv.scipro.data.dataobjects;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.persistence.Cacheable;
@@ -18,37 +19,37 @@ import org.hibernate.annotations.CacheConcurrencyStrategy;
  * @author Fredrik Norberg - fnorbe@dsv.su.se
  */
 
-
 @Entity
-@Table(name="checklist_question")
+@Table(name = "checklist_question")
 @Cacheable(true)
-@Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) //Hibernate
-public abstract class CheckListQuestion extends DomainObject 
-						implements Comparable<CheckListQuestion> {
+@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
+public class CheckListQuestion extends DomainObject implements
+		Comparable<CheckListQuestion> {
 
 	private static final long serialVersionUID = 2959377496669050427L;
-	
+
 	@Id
 	@GeneratedValue
 	private Long id;
-	
+
 	@Lob
-	@Column(nullable=false)
+	@Column(nullable = false)
 	private String question;
-	
-	@Column(nullable=false)
+
+	@Column(nullable = false)
 	private int questionNumber;
-	
+
 	@OneToMany
-	List<CheckListAnswer> answers;
-	
-	public CheckListQuestion() {}
-	
+	private List<CheckListAnswer> answers = new ArrayList<CheckListAnswer>(1);
+
+	public CheckListQuestion() {
+	}
+
 	public CheckListQuestion(String question) {
 		this.question = question;
 		questionNumber = answers.size() + 1;
 	}
-	
+
 	public String getQuestion() {
 		return question;
 	}
@@ -64,7 +65,7 @@ public abstract class CheckListQuestion extends DomainObject
 	public void setQuestionNumber(int questionNumber) {
 		this.questionNumber = questionNumber;
 	}
-	
+
 	public Long getId() {
 		return id;
 	}
@@ -72,27 +73,43 @@ public abstract class CheckListQuestion extends DomainObject
 	public void setId(Long id) {
 		this.id = id;
 	}
+	
+	
+
+	/**
+	 * @return the answers
+	 */
+	public List<CheckListAnswer> getAnswers() {
+		return answers;
+	}
+
+	/**
+	 * @param answers the answers to set
+	 */
+	public void setAnswers(List<CheckListAnswer> answers) {
+		this.answers = answers;
+	}
 
 	@Override
 	public int hashCode() {
 		final int weight = 31;
 		int result = 17;
-		
+
 		result = weight * result + ((id == null) ? 0 : (int) (id ^ (id >>> 32)));
 
 		return result;
 	}
-	
+
 	@Override
 	public boolean equals(Object obj) {
 		if (this == obj)
 			return true;
 		if (obj == null)
 			return false;
-		if (obj instanceof CheckListQuestion){
-			
+		if (obj instanceof CheckListQuestion) {
+
 			CheckListQuestion other = (CheckListQuestion) obj;
-		
+
 			if (id == null) {
 				if (other.id != null)
 					return false;
@@ -111,6 +128,6 @@ public abstract class CheckListQuestion extends DomainObject
 
 	@Override
 	public int compareTo(CheckListQuestion other) {
-		return questionNumber - other.questionNumber;
+		return other.questionNumber - questionNumber;
 	}
 }
\ No newline at end of file
diff --git a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListTemplate.java b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListTemplate.java
index 74a47ffacf..a4ba4cade7 100644
--- a/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListTemplate.java
+++ b/src/main/java/se/su/dsv/scipro/data/dataobjects/CheckListTemplate.java
@@ -1,5 +1,6 @@
 package se.su.dsv.scipro.data.dataobjects;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.persistence.Column;
@@ -30,11 +31,14 @@ public class CheckListTemplate extends DomainObject {
 	
 	@Lob
 	@ElementCollection
-	List<String> questions;
+	private List<String> questions = new ArrayList<String>(1);
 	
 	@ManyToOne(optional=false)
 	private User creator;
 	
+	@ManyToOne(optional=false)
+	private ProjectClass level;
+	
 	public CheckListTemplate() {}
 	
 	public CheckListTemplate(String name, User creator) {
@@ -74,6 +78,21 @@ public class CheckListTemplate extends DomainObject {
 		this.questions = questions;
 	}
 	
+	
+	/**
+	 * @return the level
+	 */
+	public ProjectClass getLevel() {
+		return level;
+	}
+
+	/**
+	 * @param level the level to set
+	 */
+	public void setLevel(ProjectClass level) {
+		this.level = level;
+	}
+
 	@Override
 	public int hashCode() {
 		final int weight = 31;