From f6b2adce48aa5b2d5f13ac9223bad1cd4d18c5d2 Mon Sep 17 00:00:00 2001 From: nils Date: Thu, 24 Aug 2017 14:31:22 +0200 Subject: [PATCH] User and Course Entities --- com.rotanareg.skolan.iml | 4 +- pom.xml | 5 +- .../com/rotanareg/skolan/CoursesBean.java | 65 +-------------- .../skolan/coursePersist/CourseEntity.java | 12 ++- .../skolan/coursePersist/CourseService.java | 5 +- .../com/rotanareg/skolan/domains/Course.java | 4 +- .../com/rotanareg/skolan/domains/User.java | 81 +++++++++++++++++++ .../skolan/userPersist/UserEntity.java | 75 +++++++++++++++++ .../skolan/userPersist/UserService.java | 20 +++++ .../skolan/userPersist/UserServiceImpl.java | 74 +++++++++++++++++ 10 files changed, 267 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/rotanareg/skolan/domains/User.java create mode 100644 src/main/java/com/rotanareg/skolan/userPersist/UserEntity.java create mode 100644 src/main/java/com/rotanareg/skolan/userPersist/UserService.java create mode 100644 src/main/java/com/rotanareg/skolan/userPersist/UserServiceImpl.java diff --git a/com.rotanareg.skolan.iml b/com.rotanareg.skolan.iml index 46af354..0ed5a4b 100644 --- a/com.rotanareg.skolan.iml +++ b/com.rotanareg.skolan.iml @@ -15,9 +15,7 @@ - - - + diff --git a/pom.xml b/pom.xml index 6c35d5f..cdfa35e 100644 --- a/pom.xml +++ b/pom.xml @@ -49,10 +49,7 @@ 2.6.4 - - - - \ No newline at end of file + diff --git a/src/main/java/com/rotanareg/skolan/CoursesBean.java b/src/main/java/com/rotanareg/skolan/CoursesBean.java index d36d400..3903b0e 100644 --- a/src/main/java/com/rotanareg/skolan/CoursesBean.java +++ b/src/main/java/com/rotanareg/skolan/CoursesBean.java @@ -7,11 +7,8 @@ import javax.ejb.EJB; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; -import java.util.List; + import java.util.List; -/** - * Created by Solidbeans on 2017-03-20. - */ @ManagedBean @RequestScoped @@ -76,62 +73,4 @@ public class CoursesBean { public void setCourseService(CourseService courseService) { this.courseService = courseService; } - // public String editPerson(Long id){ -// PersonDomain personDomain = personService.getPerson(id); -// setId(personDomain.getId()); -// setFirstname(personDomain.getFirstName()); -// setLastname(personDomain.getLastName()); -// return "person"; -// } -// public String removePerson(Long id){ -// personService.removePerson(id); -// return "person"; -// } -// public List getCourses(){ -// return courseService.getCourses(); -// } -// public List getPersonsFilter(){ -// if (myFilter==null || myFilter.equals("")) -// return personService.getPersons(); -// else -// return personService.getPersonsFirtsNameContain(myFilter); -// } -// -// public String getSubmitButtonLabel(){ -// if (id==null) -// return "Add"; -// else -// return "Update"; -// } -// public String getFirstname() { -// return firstname; -// } -// -// public void setFirstname(String firstname) { -// this.firstname = firstname; -// } -// -// public String getLastname() { -// return lastname; -// } -// -// public void setLastname(String lastname) { -// this.lastname = lastname; -// } -// -// public Long getId() { -// return id; -// } -// -// public void setId(Long id) { -// this.id = id; -// } -// -// public String getMyFilter() { -// return myFilter; -// } -// -// public void setMyFilter(String myFilter) { -// this.myFilter = myFilter; -// } -} \ No newline at end of file +} diff --git a/src/main/java/com/rotanareg/skolan/coursePersist/CourseEntity.java b/src/main/java/com/rotanareg/skolan/coursePersist/CourseEntity.java index 7310d37..2f0df3b 100644 --- a/src/main/java/com/rotanareg/skolan/coursePersist/CourseEntity.java +++ b/src/main/java/com/rotanareg/skolan/coursePersist/CourseEntity.java @@ -1,6 +1,14 @@ package com.rotanareg.skolan.coursePersist; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; + /** * Created by Solidbeans on 2017-03-20. @@ -12,7 +20,7 @@ import javax.persistence.*; @NamedQuery(name="selectSome",query="SELECT t FROM CourseEntity t WHERE LOCATE(:filt,t.name) >0 ") }) public class CourseEntity { - + @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; diff --git a/src/main/java/com/rotanareg/skolan/coursePersist/CourseService.java b/src/main/java/com/rotanareg/skolan/coursePersist/CourseService.java index 6769a80..279a498 100644 --- a/src/main/java/com/rotanareg/skolan/coursePersist/CourseService.java +++ b/src/main/java/com/rotanareg/skolan/coursePersist/CourseService.java @@ -1,9 +1,8 @@ package com.rotanareg.skolan.coursePersist; -import com.rotanareg.skolan.domains.Course; - -import javax.ejb.Local; import java.util.List; +import javax.ejb.Local; +import com.rotanareg.skolan.domains.Course; /** * Created by Solidbeans on 2017-03-20. diff --git a/src/main/java/com/rotanareg/skolan/domains/Course.java b/src/main/java/com/rotanareg/skolan/domains/Course.java index 39f993f..2c577d0 100644 --- a/src/main/java/com/rotanareg/skolan/domains/Course.java +++ b/src/main/java/com/rotanareg/skolan/domains/Course.java @@ -16,8 +16,6 @@ public class Course { this.courseNr = courseNr; } - - public Course(long id, String name, String description, String courseNr){ this.id = id; this.name = name; @@ -67,4 +65,4 @@ public class Course { sbc.append('}'); return sbc.toString(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/rotanareg/skolan/domains/User.java b/src/main/java/com/rotanareg/skolan/domains/User.java new file mode 100644 index 0000000..879c5ad --- /dev/null +++ b/src/main/java/com/rotanareg/skolan/domains/User.java @@ -0,0 +1,81 @@ +package com.rotanareg.skolan.domains; + +import com.rotanareg.skolan.Role; +/** + * Created by Nils Gerstner on 8/20/17. + */ + +public class User { + private long id; + private String name; + private String lastName; + private Role role; + private String passWord; + + public User(String name, String lastName, Role role){ + this.name = name; + this.lastName = lastName; + this.role = role; + this.passWord = null; + } + + public User(long id, String name, String lastName, Role role, String passWord){ + this.id = id; + this.name = name; + this.lastName = lastName; + this.role = role; + this.passWord = passWord; + } + + 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 getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Role getRole(){ + return role; + } + + public void setRole(Role role){ + this.role = role; + } + + public void setPassWord(String passWord) { + this.passWord = passWord; + } + + public String getPassWord() { + return passWord; + } + + @Override + public String toString(){ + StringBuilder sbc = new StringBuilder("User{"); + sbc.append("id=").append(id); + sbc.append(", name='").append(name).append('\''); + sbc.append(", lastName='").append(lastName).append('\''); + sbc.append(", role='").append(role.toString()).append('\''); + sbc.append('}'); + return sbc.toString(); + } + +} diff --git a/src/main/java/com/rotanareg/skolan/userPersist/UserEntity.java b/src/main/java/com/rotanareg/skolan/userPersist/UserEntity.java new file mode 100644 index 0000000..1e4d956 --- /dev/null +++ b/src/main/java/com/rotanareg/skolan/userPersist/UserEntity.java @@ -0,0 +1,75 @@ +package com.rotanareg.skolan.userPersist; + +import javax.persistence.*; +import com.rotanareg.skolan.Role; + +/** + * Created by Solidbeans on 2017-03-20. + */ +@Entity +@Table(name = "Person") +@NamedQueries({ + @NamedQuery(name="selectAll",query="SELECT u FROM UserEntity u"), + @NamedQuery(name="selectSome",query="SELECT t FROM UserEntity t WHERE LOCATE(:filt,t.name) >0 ") +}) +public class UserEntity { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + @Enumerated(EnumType.STRING) + private Role role; + private String name; + private String lastName; + private String passWord; + + public UserEntity() { + } + + public UserEntity(String name, String lastName, Role role, String passWord) { + this.name = name; + this.lastName = lastName; + this.role = role; + this.passWord = passWord; + } + + 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 getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Role getRole(){ + return role; + } + + public void setRole(Role role){ + this.role = role; + } + + public void setPassWord(String passWord) { + this.passWord = passWord; + } + + public String getPassWord() { + return passWord; + } +} diff --git a/src/main/java/com/rotanareg/skolan/userPersist/UserService.java b/src/main/java/com/rotanareg/skolan/userPersist/UserService.java new file mode 100644 index 0000000..413dc98 --- /dev/null +++ b/src/main/java/com/rotanareg/skolan/userPersist/UserService.java @@ -0,0 +1,20 @@ +package com.rotanareg.skolan.userPersist; + +import com.rotanareg.skolan.domains.User; + +import javax.ejb.Local; +import java.util.List; + +/** + * Created by Solidbeans on 2017-03-20. + */ +@Local +public interface UserService { + + void addUser(User user); + void updateUser(User user); + void removeUser(Long id); + User getUser(Long id); + List getUser(); + public List getUserContaining(String filter); +} diff --git a/src/main/java/com/rotanareg/skolan/userPersist/UserServiceImpl.java b/src/main/java/com/rotanareg/skolan/userPersist/UserServiceImpl.java new file mode 100644 index 0000000..c225e36 --- /dev/null +++ b/src/main/java/com/rotanareg/skolan/userPersist/UserServiceImpl.java @@ -0,0 +1,74 @@ +package com.rotanareg.skolan.userPersist; + +import com.rotanareg.skolan.Role; +import com.rotanareg.skolan.domains.User; + +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 UserServiceImpl implements UserService{ + @PersistenceContext + EntityManager em; + + @Override + public void addUser(User user) { + UserEntity u = new UserEntity(user.getName(),user.getLastName(),user.getRole(),user.getPassWord()); + em.persist(u); + + } + + @Override + public void updateUser(User user) { + UserEntity u = em.find(UserEntity.class,user.getId()); + u.setName(user.getName()); + u.setLastName(user.getLastName()); + u.setRole(user.getRole()); + em.merge(u); + } + + @Override + public void removeUser(Long id) { + UserEntity u = em.find(UserEntity.class, id); + em.remove(u); + } + + @Override + public User getUser(Long id) { + UserEntity u = em.find(UserEntity.class, id); + User ud = new User(u.getId(),u.getName(),u.getLastName(),u.getRole(),u.getPassWord()); + return ud; + } + @Override + public List getUser() { + List userEntityList = em.createNamedQuery("selectAll").getResultList(); + if (userEntityList.isEmpty()){ + UserEntity a = new UserEntity("Nils","Gerstner",Role.STUDENT,"1234"); + em.persist(a); + UserEntity b = new UserEntity("Jasna","Nilsson-Milkic",Role.STUDENT,"1234"); + em.persist(b); + UserEntity c = new UserEntity("Moustafa", "Almehyo",Role.TEACHER,"1234"); + em.persist(c); + UserEntity d = new UserEntity("Marko","Seppänen",Role.ADMIN,"1234"); + em.persist(d); + } + return userEntityList.stream(). + map(u->new User(u.getId(),u.getName(),u.getLastName(),u.getRole(),u.getPassWord())).collect(Collectors.toList()); + } + + @Override + public List getUserContaining(String filter) { + List userEntityList = em.createNamedQuery("selectSome").setParameter("filt",filter).getResultList(); + + return userEntityList.stream(). + map(c->new User(c.getId(),c.getName(),c.getLastName(),c.getRole(),c.getPassWord())). + collect(Collectors.toList()); + } +}