parent
7a74507b9d
commit
e0cd6f12fc
@ -0,0 +1,56 @@
|
||||
package domain;
|
||||
|
||||
/**
|
||||
* Created by Nils Gerstner on 8/20/17.
|
||||
*/
|
||||
public class CourseDomain {
|
||||
private long id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String courseNr;
|
||||
|
||||
public CourseDomain(String name, String description, String courseNr){
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.courseNr = courseNr;
|
||||
}
|
||||
|
||||
public CourseDomain(long id, String name, String description, String courseNr){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.courseNr = courseNr;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCourseNr() {
|
||||
return courseNr;
|
||||
}
|
||||
|
||||
public void setCourseNr(String courseNr) {
|
||||
this.courseNr = courseNr;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package ejb;
|
||||
|
||||
import domain.CourseDomain;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Solidbeans on 2017-03-20.
|
||||
*/
|
||||
@Local
|
||||
public interface CourseService {
|
||||
|
||||
void addCourse(CourseDomain course);
|
||||
void updateCourse(CourseDomain course);
|
||||
void removeCourse(Long id);
|
||||
CourseDomain getCourse(Long id);
|
||||
List<CourseDomain> getCourses();
|
||||
public List<CourseDomain> getCoursesContaining(String filter);
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package ejb;
|
||||
|
||||
import domain.CourseDomain;
|
||||
import jpa.Course;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Created by Solidbeans on 2017-03-20.
|
||||
*/
|
||||
|
||||
@Stateless
|
||||
public class CourseServiceImpl implements CourseService{
|
||||
@PersistenceContext
|
||||
EntityManager em;
|
||||
|
||||
@Override
|
||||
public void addCourse(CourseDomain course) {
|
||||
Course c = new Course(course.getName(),course.getDescription(),course.getCourseNr());
|
||||
em.persist(c);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateCourse(CourseDomain course) {
|
||||
Course c = em.find(Course.class,course.getId());
|
||||
c.setName(course.getName());
|
||||
c.setDescription(course.getDescription());
|
||||
c.setCourseNr(course.getCourseNr());
|
||||
em.merge(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeCourse(Long id) {
|
||||
Course c = em.find(Course.class, id);
|
||||
em.remove(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CourseDomain getCourse(Long id) {
|
||||
Course c = em.find(Course.class,id);
|
||||
CourseDomain cd = new CourseDomain(c.getId(),c.getName(),c.getDescription(),c.getCourseNr());
|
||||
|
||||
return cd;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CourseDomain> getCourses() {
|
||||
List<Course> courseList = em.createNamedQuery("selectAll").getResultList();
|
||||
|
||||
return courseList.stream().
|
||||
map(c->new CourseDomain(c.getId(),c.getName(),c.getDescription(),c.getCourseNr())).
|
||||
collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CourseDomain> getCoursesContaining(String filter) {
|
||||
List<Course> courseList = em.createNamedQuery("selectSome").setParameter("filt",filter).getResultList();
|
||||
|
||||
return courseList.stream().
|
||||
map(c->new CourseDomain(c.getId(),c.getName(),c.getDescription(),c.getCourseNr())).
|
||||
collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package jpa;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* Created by Solidbeans on 2017-03-20.
|
||||
*/
|
||||
@Entity
|
||||
@NamedQueries({
|
||||
@NamedQuery(name="selectAll",query="SELECT c FROM Course c"),
|
||||
@NamedQuery(name="selectSome",query="SELECT t FROM Course t WHERE LOCATE(:filt,t.name) OR LOCATE(:filt,t.courseNr) >0 ")
|
||||
})
|
||||
public class Course {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String description;
|
||||
private String courseNr;
|
||||
|
||||
public Course() {
|
||||
}
|
||||
|
||||
public Course(String name, String description, String courseNr) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.courseNr = courseNr;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCourseNr() {
|
||||
return courseNr;
|
||||
}
|
||||
|
||||
public void setCourseNr(String courseNr) {
|
||||
this.courseNr = courseNr;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue