From ebe71a80091997a8d2ee8148f240bf8a74e5c170 Mon Sep 17 00:00:00 2001 From: Eirik Wang Date: Fri, 23 Sep 2011 00:00:00 +0200 Subject: [PATCH] =?UTF-8?q?Lagt=20til=20mulighet=20for=20=C3=A5=20legge=20?= =?UTF-8?q?til=20attendants=20p=C3=A5=20en=20course?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/no/bekkopen/dao/CourseDao.java | 15 ++- .../java/no/bekkopen/dao/CourseDaoImpl.java | 38 +++++++- .../java/no/bekkopen/domain/Attendant.java | 94 +++++++++++++++++++ .../main/java/no/bekkopen/domain/Course.java | 21 ++++- .../java/no/bekkopen/feature/Feature.java | 3 +- .../main/resources/META-INF/persistence.xml | 5 +- .../web/controller/AttendantController.java | 76 +++++++++++++++ .../java/no/bekkopen/web/tags/Feature.java | 2 - webapp/src/main/resources/messages.properties | 10 +- .../webapp/WEB-INF/jsp/attendant/form.jsp | 30 ++++++ .../webapp/WEB-INF/jsp/attendant/search.jsp | 39 ++++++++ .../main/webapp/WEB-INF/jsp/course/form.jsp | 34 ++++++- .../main/webapp/WEB-INF/jsp/course/search.jsp | 13 +++ 13 files changed, 368 insertions(+), 12 deletions(-) create mode 100644 core/src/main/java/no/bekkopen/domain/Attendant.java create mode 100644 webapp/src/main/java/no/bekkopen/web/controller/AttendantController.java create mode 100644 webapp/src/main/webapp/WEB-INF/jsp/attendant/form.jsp create mode 100644 webapp/src/main/webapp/WEB-INF/jsp/attendant/search.jsp diff --git a/core/src/main/java/no/bekkopen/dao/CourseDao.java b/core/src/main/java/no/bekkopen/dao/CourseDao.java index 66091ad..d79c237 100644 --- a/core/src/main/java/no/bekkopen/dao/CourseDao.java +++ b/core/src/main/java/no/bekkopen/dao/CourseDao.java @@ -1,5 +1,6 @@ package no.bekkopen.dao; +import no.bekkopen.domain.Attendant; import no.bekkopen.domain.Course; import java.util.List; @@ -8,11 +9,19 @@ * @author Eirik Wang - eirik.wang@bekk.no */ public interface CourseDao { - public List findCourses(); + List findCourses(); - public Course findCourse(Long id); + Course findCourse(Long id); - public Course save(Course course); + Course save(Course course); void delete(Course course); + + Attendant findAttendant(Long id); + + Attendant save(Attendant attendant); + + void delete(Attendant attendant); + + Attendant addAttendant(Attendant attendant); } diff --git a/core/src/main/java/no/bekkopen/dao/CourseDaoImpl.java b/core/src/main/java/no/bekkopen/dao/CourseDaoImpl.java index 403352a..f71dba0 100644 --- a/core/src/main/java/no/bekkopen/dao/CourseDaoImpl.java +++ b/core/src/main/java/no/bekkopen/dao/CourseDaoImpl.java @@ -1,5 +1,6 @@ package no.bekkopen.dao; +import no.bekkopen.domain.Attendant; import no.bekkopen.domain.Course; import no.bekkopen.feature.Feature; @@ -20,10 +21,12 @@ public class CourseDaoImpl implements CourseDao { @PersistenceContext private EntityManager em = null; - /** @noinspection unchecked*/ + /** + * @noinspection unchecked + */ @Override public List findCourses() { - return em.createQuery("from Course ").getResultList(); + return em.createQuery("from Course c left join fetch c.attendants").getResultList(); } @Override @@ -49,4 +52,35 @@ public void delete(Course course) { em.remove(em.getReference(Course.class, course.getId())); } } + + @Override + public Attendant addAttendant(Attendant attendant) { + if (Feature.Course.Attendants.isEnabled()) { + return em.merge(attendant); + } + return null; + } + + @Override + public Attendant findAttendant(Long id) { + if (Feature.Course.Attendants.isEnabled()) { + return em.find(Attendant.class, id); + } + return null; + } + + @Override + public Attendant save(Attendant attendant) { + if (Feature.Course.Attendants.isEnabled()) { + return em.merge(attendant); + } + return null; + } + + @Override + public void delete(Attendant attendant) { + if (Feature.Course.Attendants.isEnabled()) { + em.remove(attendant); + } + } } diff --git a/core/src/main/java/no/bekkopen/domain/Attendant.java b/core/src/main/java/no/bekkopen/domain/Attendant.java new file mode 100644 index 0000000..5279766 --- /dev/null +++ b/core/src/main/java/no/bekkopen/domain/Attendant.java @@ -0,0 +1,94 @@ +package no.bekkopen.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name = "Attendant") +public class Attendant { + + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + @Column(name = "id") + private Long id; + @Column(name = "name") + private String name; + @Column(name = "eMail") + private String email; + @ManyToOne() + @JoinColumn(name="course_id", nullable=false, updatable=false) + private Course course; + + public void setId(Long id) { + this.id = id; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public void setEMail(String location) { + this.email = location; + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + @Override + public int hashCode() { + final int prime = 33; + int result = 1; + + result = prime * result + ((id == null) ? 0 : id.hashCode()); + + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Attendant other = (Attendant) obj; + + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + + return true; + } + +} diff --git a/core/src/main/java/no/bekkopen/domain/Course.java b/core/src/main/java/no/bekkopen/domain/Course.java index fa98ecb..690500f 100644 --- a/core/src/main/java/no/bekkopen/domain/Course.java +++ b/core/src/main/java/no/bekkopen/domain/Course.java @@ -1,10 +1,14 @@ package no.bekkopen.domain; +import java.util.Set; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.Type; @@ -27,6 +31,8 @@ public class Course { @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) private LocalDate date; + @OneToMany(mappedBy = "course", cascade = CascadeType.ALL, fetch = FetchType.EAGER) + private Set attendants; public void setId(Long id) { this.id = id; @@ -60,9 +66,17 @@ public void setDate(LocalDate date) { this.date = date; } + public Set getAttendants() { + return attendants; + } + + public void setAttendants(Set attendants) { + this.attendants = attendants; + } + @Override public int hashCode() { - final int prime = 31; + final int prime = 35; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); @@ -94,4 +108,9 @@ public boolean equals(Object obj) { return true; } + public Attendant newAttendant() { + Attendant a = new Attendant(); + a.setCourse(this); + return a; + } } diff --git a/core/src/main/java/no/bekkopen/feature/Feature.java b/core/src/main/java/no/bekkopen/feature/Feature.java index 5332053..4004e34 100644 --- a/core/src/main/java/no/bekkopen/feature/Feature.java +++ b/core/src/main/java/no/bekkopen/feature/Feature.java @@ -9,7 +9,8 @@ private Feature() { public enum Course implements Enabled{ Save(true), - Delete(false); + Delete(false), + Attendants(true); private boolean courseEnabled = true; private boolean enabled; diff --git a/core/src/main/resources/META-INF/persistence.xml b/core/src/main/resources/META-INF/persistence.xml index 23e7a01..fc73549 100644 --- a/core/src/main/resources/META-INF/persistence.xml +++ b/core/src/main/resources/META-INF/persistence.xml @@ -7,6 +7,7 @@ no.bekkopen.domain.Artifact no.bekkopen.domain.Course + no.bekkopen.domain.Attendant @@ -14,7 +15,7 @@ - + @@ -23,6 +24,8 @@ no.bekkopen.domain.Artifact no.bekkopen.domain.Course + no.bekkopen.domain.Attendant + diff --git a/webapp/src/main/java/no/bekkopen/web/controller/AttendantController.java b/webapp/src/main/java/no/bekkopen/web/controller/AttendantController.java new file mode 100644 index 0000000..ca7f3e2 --- /dev/null +++ b/webapp/src/main/java/no/bekkopen/web/controller/AttendantController.java @@ -0,0 +1,76 @@ +package no.bekkopen.web.controller; + +import no.bekkopen.dao.CourseDao; +import no.bekkopen.domain.Attendant; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author Eirik Wang - eirik.wang@bekk.no + */ +@Controller +public class AttendantController { + private static final String SEARCH_VIEW_KEY = "redirect:search.html"; + private static final String SEARCH_MODEL_KEY = "courses"; + + @Autowired + protected CourseDao courseDao = null; + + /** + * For every request for this controller, this will + * create an course instance for the form. + */ + @ModelAttribute + public Attendant newRequest(@RequestParam(required = false) Long courseId, @RequestParam(required = false) Long id) { + if(courseId != null){ + return courseDao.findCourse(courseId).newAttendant(); + } + return (id != null ? courseDao.findAttendant(id) : new Attendant()); + } + + /** + *

Attendant form request.

+ *

+ *

Expected HTTP GET and request '/course/form'.

+ */ + @RequestMapping(value = "/attendant/form", method = RequestMethod.GET) + public void form() { + } + + /** + *

Saves an course.

+ *

+ *

Expected HTTP POST and request '/course/form'.

+ */ + @RequestMapping(value = "/attendant/form", method = RequestMethod.POST) + public String form(Attendant course, Model model) { + Attendant result = courseDao.save(course); + + // set id from create + if (course.getId() == null) { + course.setId(result.getId()); + } + model.addAttribute("statusMessageKey", "course.form.msg.success"); + return "redirect:/course/search.html"; + } + + /** + *

Deletes an course.

+ *

+ *

Expected HTTP POST and request '/course/delete'.

+ */ + @RequestMapping(value = "/attendant/delete", method = RequestMethod.POST) + public String delete(Attendant course) { + courseDao.delete(course); + return SEARCH_VIEW_KEY; + } + +} diff --git a/webapp/src/main/java/no/bekkopen/web/tags/Feature.java b/webapp/src/main/java/no/bekkopen/web/tags/Feature.java index d9ff811..e48c255 100644 --- a/webapp/src/main/java/no/bekkopen/web/tags/Feature.java +++ b/webapp/src/main/java/no/bekkopen/web/tags/Feature.java @@ -28,13 +28,11 @@ public void setName(String feature) throws ClassNotFoundException { String[] features = feature.split("\\."); Class clz = Class.forName("no.bekkopen.feature.Feature$" + features[0]); enabled = (Enabled) ReflectionUtils.invokeMethod(findMethod(clz, "valueOf", String.class), null, features[1]); - System.out.println(enabled.isEnabled()); } public static void main(String[] args) throws ClassNotFoundException, JspException { Feature f = new Feature(); f.setName("Course.Delete"); - System.out.println(f.doStartTag()); } public Enabled getEnabled() { diff --git a/webapp/src/main/resources/messages.properties b/webapp/src/main/resources/messages.properties index e1d5471..d192881 100644 --- a/webapp/src/main/resources/messages.properties +++ b/webapp/src/main/resources/messages.properties @@ -2,6 +2,7 @@ button.cancel=Cancel button.create=Create button.edit=Edit button.delete=Delete +button.add=Add button.reset=Reset button.save=Save button.search=Search @@ -29,10 +30,17 @@ artifact.form.msg.success=The artifact has been saved. course.search.title=Coarse Search -course.form.title=Artifact +course.form.title=Course course.form.name=Name course.form.date=Date course.form.location=Location +course.form.attendants=# Attendants course.form.msg.success=The coarse has been saved. + + + +attendant.form.title=Attendant +attendant.form.name=Name +attendant.form.email=E-Mail \ No newline at end of file diff --git a/webapp/src/main/webapp/WEB-INF/jsp/attendant/form.jsp b/webapp/src/main/webapp/WEB-INF/jsp/attendant/form.jsp new file mode 100644 index 0000000..b831930 --- /dev/null +++ b/webapp/src/main/webapp/WEB-INF/jsp/attendant/form.jsp @@ -0,0 +1,30 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + +

+ + +

+
+ + + + + + +
+
+ + +
+
+ + +
+
+
"/>
+
+
+ +
\ No newline at end of file diff --git a/webapp/src/main/webapp/WEB-INF/jsp/attendant/search.jsp b/webapp/src/main/webapp/WEB-INF/jsp/attendant/search.jsp new file mode 100644 index 0000000..d4dfeff --- /dev/null +++ b/webapp/src/main/webapp/WEB-INF/jsp/attendant/search.jsp @@ -0,0 +1,39 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="feature" uri="http://open.bekk.no/jsp/jstl/feature" %> + +

+ + + + + + + + + + + + + + + + +
+ + +
+ +
+ + + + + + \ No newline at end of file diff --git a/webapp/src/main/webapp/WEB-INF/jsp/course/form.jsp b/webapp/src/main/webapp/WEB-INF/jsp/course/form.jsp index ccf1318..e954363 100644 --- a/webapp/src/main/webapp/WEB-INF/jsp/course/form.jsp +++ b/webapp/src/main/webapp/WEB-INF/jsp/course/form.jsp @@ -1,6 +1,7 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ taglib prefix="feature" uri="http://open.bekk.no/jsp/jstl/feature" %>

@@ -28,6 +29,37 @@
"/>
+ + + +
+

+ + + + + + + + - \ No newline at end of file + + + + +
+ + + +
+ + + + + +
+
\ No newline at end of file diff --git a/webapp/src/main/webapp/WEB-INF/jsp/course/search.jsp b/webapp/src/main/webapp/WEB-INF/jsp/course/search.jsp index d4dfeff..3b83a72 100644 --- a/webapp/src/main/webapp/WEB-INF/jsp/course/search.jsp +++ b/webapp/src/main/webapp/WEB-INF/jsp/course/search.jsp @@ -2,6 +2,7 @@ <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="feature" uri="http://open.bekk.no/jsp/jstl/feature" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

@@ -10,6 +11,10 @@ + + + + @@ -34,6 +39,14 @@ + + + + + ${fn:length(course.attendants)} + + + \ No newline at end of file