Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions core/src/main/java/no/bekkopen/dao/CourseDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.bekkopen.dao;

import no.bekkopen.domain.Attendant;
import no.bekkopen.domain.Course;

import java.util.List;
Expand All @@ -8,11 +9,19 @@
* @author Eirik Wang - eirik.wang@bekk.no
*/
public interface CourseDao {
public List<Course> findCourses();
List<Course> 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);
}
38 changes: 36 additions & 2 deletions core/src/main/java/no/bekkopen/dao/CourseDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.bekkopen.dao;

import no.bekkopen.domain.Attendant;
import no.bekkopen.domain.Course;
import no.bekkopen.feature.Feature;

Expand All @@ -20,10 +21,12 @@ public class CourseDaoImpl implements CourseDao {
@PersistenceContext
private EntityManager em = null;

/** @noinspection unchecked*/
/**
* @noinspection unchecked
*/
@Override
public List<Course> findCourses() {
return em.createQuery("from Course ").getResultList();
return em.createQuery("from Course c left join fetch c.attendants").getResultList();
}

@Override
Expand All @@ -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);
}
}
}
94 changes: 94 additions & 0 deletions core/src/main/java/no/bekkopen/domain/Attendant.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
21 changes: 20 additions & 1 deletion core/src/main/java/no/bekkopen/domain/Course.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Attendant> attendants;

public void setId(Long id) {
this.id = id;
Expand Down Expand Up @@ -60,9 +66,17 @@ public void setDate(LocalDate date) {
this.date = date;
}

public Set<Attendant> getAttendants() {
return attendants;
}

public void setAttendants(Set<Attendant> 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());
Expand Down Expand Up @@ -94,4 +108,9 @@ public boolean equals(Object obj) {
return true;
}

public Attendant newAttendant() {
Attendant a = new Attendant();
a.setCourse(this);
return a;
}
}
3 changes: 2 additions & 1 deletion core/src/main/java/no/bekkopen/feature/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion core/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

<class>no.bekkopen.domain.Artifact</class>
<class>no.bekkopen.domain.Course</class>
<class>no.bekkopen.domain.Attendant</class>

<exclude-unlisted-classes />

<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>

Expand All @@ -23,6 +24,8 @@

<class>no.bekkopen.domain.Artifact</class>
<class>no.bekkopen.domain.Course</class>
<class>no.bekkopen.domain.Attendant</class>


<exclude-unlisted-classes />
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

/**
* <p>Attendant form request.</p>
* <p/>
* <p>Expected HTTP GET and request '/course/form'.</p>
*/
@RequestMapping(value = "/attendant/form", method = RequestMethod.GET)
public void form() {
}

/**
* <p>Saves an course.</p>
* <p/>
* <p>Expected HTTP POST and request '/course/form'.</p>
*/
@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";
}

/**
* <p>Deletes an course.</p>
* <p/>
* <p>Expected HTTP POST and request '/course/delete'.</p>
*/
@RequestMapping(value = "/attendant/delete", method = RequestMethod.POST)
public String delete(Attendant course) {
courseDao.delete(course);
return SEARCH_VIEW_KEY;
}

}
2 changes: 0 additions & 2 deletions webapp/src/main/java/no/bekkopen/web/tags/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 9 additions & 1 deletion webapp/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Loading