User and Course Entities

PlaygroundDevelopment
nils 7 years ago
parent 9edbf0231a
commit f6b2adce48

@ -15,9 +15,7 @@
<configuration>
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="" />
<datasource-mapping>
<factory-entry name="Persistence-unit" />
</datasource-mapping>
<datasource-mapping />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/resources/META-INF/persistence.xml" />
</configuration>
</facet>

@ -49,10 +49,7 @@
<version>2.6.4</version>
</dependency>
</dependencies>
</project>
</project>

@ -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<Course> getCourses(){
// return courseService.getCourses();
// }
// public List<PersonDomain> 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;
// }
}
}

@ -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;

@ -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.

@ -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();
}
}
}

@ -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();
}
}

@ -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;
}
}

@ -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<User> getUser();
public List<User> getUserContaining(String filter);
}

@ -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<User> getUser() {
List<UserEntity> 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<User> getUserContaining(String filter) {
List<UserEntity> 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());
}
}
Loading…
Cancel
Save