diff --git a/src/main/java/com/yojana/access/LeaveRequestManager.java b/src/main/java/com/yojana/access/LeaveRequestManager.java new file mode 100644 index 0000000000000000000000000000000000000000..7405afd910cf5def373e9be3b8688dfec17864e8 --- /dev/null +++ b/src/main/java/com/yojana/access/LeaveRequestManager.java @@ -0,0 +1,76 @@ +package com.yojana.access; + +import java.io.Serializable; +import java.util.List; + +import javax.ejb.Stateless; +import javax.enterprise.context.Dependent; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.transaction.Transactional; + +import com.yojana.model.employee.Employee; +import com.yojana.model.employee.LeaveRequest; +import com.yojana.model.project.Project; + +@Dependent +@Stateless +public class LeaveRequestManager implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -8709713671106036600L; + + @PersistenceContext(unitName="comp4911-pms-rest-jpa") EntityManager em; + + /** find a Leave Request with id. */ + public LeaveRequest find(String id) { + return em.find(LeaveRequest.class, id); + } + + /** find a Leave Request with id. */ + public List<LeaveRequest> findByEmp(String id) { + int i = Integer.parseInt(id); + TypedQuery<LeaveRequest> query = em.createQuery("select p from LeaveRequest p where p.empId = "+i, + LeaveRequest.class); + List<LeaveRequest> leaves = query.getResultList(); + return leaves; + } + + /** find a Leave Request with id. */ + public List<LeaveRequest> findByType(String id) { + TypedQuery<LeaveRequest> query = em.createQuery("select p from LeaveRequest p where p.type = '"+id+"'", + LeaveRequest.class); + List<LeaveRequest> leaves = query.getResultList(); + return leaves; + } + + /** add a Leave Request. */ + @Transactional + public void persist(LeaveRequest leave) { + em.persist(leave); + } + + /** update a Leave Request. */ + @Transactional + public void merge(LeaveRequest leave) { + em.merge(leave); + } + + /** remove an Leave Request. */ + @Transactional + public void remove(LeaveRequest leave, String id) { + leave = find(id); + em.remove(leave); + } + + public List<LeaveRequest> getAll() { + TypedQuery<LeaveRequest> query = em.createQuery("select p from LeaveRequest p", + LeaveRequest.class); + List<LeaveRequest> leaves = query.getResultList(); + return leaves; + } + +} diff --git a/src/main/java/com/yojana/model/employee/LeaveRequest.java b/src/main/java/com/yojana/model/employee/LeaveRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..1209123927c2803a5a269db48dd3d003bbb921cb --- /dev/null +++ b/src/main/java/com/yojana/model/employee/LeaveRequest.java @@ -0,0 +1,120 @@ +package com.yojana.model.employee; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.yojana.access.LeaveRequestManager; +import com.yojana.model.auditable.AuditListener; + +@Entity +@Table(name = "LeaveRequest") +public class LeaveRequest implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 7569341008564405379L; + + + @Id + @Column(name = "LeaveRequestID", unique = true) + private String id; + + @Temporal(TemporalType.DATE) + @Column(name = "StartDate") + private Date startDate; + + @Temporal(TemporalType.DATE) + @Column(name = "EndDate") + private Date endDate; + + @Enumerated(EnumType.STRING) + @Column(name = "TypeRequest", columnDefinition = "enum") + private LeaveRequestType type; + + @Column(name = "Descrip", columnDefinition="TEXT") + private String description; + + /** + * Represents the ID of the employee + */ + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "EmpID") + private Employee employee; + + @Column(name = "EmpID", updatable = false, insertable = false) + private Integer empId; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Date getStartDate() { + return startDate; + } + + public void setStartDate(Date startDate) { + this.startDate = startDate; + } + + public Date getEndDate() { + return endDate; + } + + public void setEndDate(Date endDate) { + this.endDate = endDate; + } + + public LeaveRequestType getType() { + return type; + } + + public void setType(LeaveRequestType type) { + this.type = type; + } + + public Employee getEmployee() { + return employee; + } + + public void setEmployee(Employee employee) { + this.employee = employee; + } + + public Integer getEmpId() { + return empId; + } + + public void setEmpId(Integer empId) { + this.empId = empId; + } + +} diff --git a/src/main/java/com/yojana/model/employee/LeaveRequestType.java b/src/main/java/com/yojana/model/employee/LeaveRequestType.java new file mode 100644 index 0000000000000000000000000000000000000000..ddf0343a9b063676bc11bd0c52fcdc8c97ef2c92 --- /dev/null +++ b/src/main/java/com/yojana/model/employee/LeaveRequestType.java @@ -0,0 +1,7 @@ +package com.yojana.model.employee; + +public enum LeaveRequestType { + Vacation, + Medical, + other +} diff --git a/src/main/java/com/yojana/services/employee/LeaveRequestService.java b/src/main/java/com/yojana/services/employee/LeaveRequestService.java new file mode 100644 index 0000000000000000000000000000000000000000..60e54d271e4fcfaec3a834f1f9cad536059b3c35 --- /dev/null +++ b/src/main/java/com/yojana/services/employee/LeaveRequestService.java @@ -0,0 +1,163 @@ +package com.yojana.services.employee; + + +import java.net.URI; +import java.util.List; +import java.util.UUID; + +import javax.inject.Inject; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PATCH; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.yojana.access.EmployeeManager; +import com.yojana.access.LeaveRequestManager; +import com.yojana.access.ProjectManager; +import com.yojana.access.WorkPackageManager; +import com.yojana.model.employee.Employee; +import com.yojana.model.employee.LeaveRequest; +import com.yojana.model.employee.LeaveRequestType; +import com.yojana.model.project.Project; +import com.yojana.model.project.WorkPackage; +import com.yojana.model.project.WorkPackagePK; +import com.yojana.response.APIResponse; +import com.yojana.response.errors.ErrorMessageBuilder; +import com.yojana.security.annotations.AuthenticatedEmployee; +import com.yojana.security.annotations.Secured; + +@Path("/leaverequest") +@Secured +public class LeaveRequestService { + + @Inject + // Provides access to the employee table + private EmployeeManager employeeManager; + + @Inject + // Provides access to the project table + private LeaveRequestManager leaveManager; + + @POST + @Consumes("application/json") + @Produces("application/json") + // Inserts a timesheet ain't the database + public Response persist(LeaveRequest leave) { + APIResponse res = new APIResponse(); + if (leave.getEmpId() > 0) { + final Employee emp = employeeManager.find(leave.getEmpId()); + if (emp == null) { + ErrorMessageBuilder.notFound("Could not find employee for Leave Request ", null); + return Response.status(Response.Status.NOT_FOUND).entity(res).build(); + } + leave.setEmployee(emp); + } + leave.getStartDate().setDate(leave.getStartDate().getDate()+1); + leave.getEndDate().setDate(leave.getEndDate().getDate()+1); + UUID uuid = UUID.randomUUID(); + leave.setId(uuid.toString()); + leaveManager.persist(leave); + return Response.created(URI.create("/leave/" + leave.getId())).entity(res).build(); + } + + @GET + @Produces("application/json") + // Gets a list of all requests + public Response getAll() { + final APIResponse res = new APIResponse(); + List<LeaveRequest> projects = leaveManager.getAll(); + if (projects == null) { + res.getErrors().add(ErrorMessageBuilder.notFoundMultiple("Leave Reuest", null)); + return Response.status(Response.Status.NOT_FOUND).entity(res).build(); + } + res.getData().put("leaveRequest", projects); + return Response.ok().entity(res).build(); + } + + @GET + @Path("/{id}") + @Produces(MediaType.APPLICATION_JSON) + // Finds Request using id + public Response find(@PathParam("id") String id) { + LeaveRequest leave = leaveManager.find(id); + APIResponse res = new APIResponse(); + if (leave == null) { + res.getErrors().add(ErrorMessageBuilder.notFoundSingle("Leave Request", + id + "", + null + )); + return Response.status(Response.Status.NOT_FOUND).entity(res).build(); + } + res.getData().put("leaveRequest", leave); + return Response.ok().entity(res).build(); + } + + @DELETE + @Path("/{id}") + @Produces("application/json") + public Response del(@PathParam("id") String id) { + APIResponse res = new APIResponse(); + LeaveRequest leave = leaveManager.find(id); + leaveManager.remove(leave, id); + res.getData().put("leaveRequest", leave); + return Response.ok().entity(new APIResponse()).build(); + } + + @PATCH + @Path("/") + @Consumes("application/json") + @Produces("application/json") + public Response change(LeaveRequest req) { + APIResponse res = new APIResponse(); + req.getStartDate().setDate(req.getStartDate().getDate()+1); + req.getEndDate().setDate(req.getEndDate().getDate()+1); + if (req.getEmpId() > 0) { + final Employee emp = employeeManager.find(req.getEmpId()); + if (emp == null) { + ErrorMessageBuilder.notFound("Could not find employee for Leave Request", null); + return Response.status(Response.Status.NOT_FOUND).entity(res).build(); + } + req.setEmployee(emp); + } + leaveManager.merge(req); + res.getData().put("leaveRequest", req); + return Response.ok().entity(res).build(); + } + + @GET + @Consumes("application/json") + @Produces("application/json") + // Gets a list of all requests + public Response getByType(LeaveRequest req) { + final APIResponse res = new APIResponse(); + System.out.print(req.getType().toString()); + List<LeaveRequest> projects = leaveManager.findByType(req.getType().toString()); + if (projects == null) { + res.getErrors().add(ErrorMessageBuilder.notFoundMultiple("Leave Reuest", null)); + return Response.status(Response.Status.NOT_FOUND).entity(res).build(); + } + res.getData().put("leaveRequest", projects); + return Response.ok().entity(res).build(); + } + + @GET + @Path("/emp/{id}") + @Produces("application/json") + public Response getWorkPackages(@PathParam("id") String projectId) { + final APIResponse res = new APIResponse(); + List<LeaveRequest> projects = leaveManager.findByEmp(projectId); + if (projects == null) { + res.getErrors().add(ErrorMessageBuilder.notFoundMultiple("Leave Reuest", null)); + return Response.status(Response.Status.NOT_FOUND).entity(res).build(); + } + res.getData().put("leaveRequest", projects); + return Response.ok().entity(res).build(); + } +} diff --git a/src/main/sql/schema.sql b/src/main/sql/schema.sql index e48c5d1fd077e3f788b9ade7632360d7bd4a707c..b203b865b14718a445f75f431bd1ef1fe260ef98 100644 --- a/src/main/sql/schema.sql +++ b/src/main/sql/schema.sql @@ -92,7 +92,7 @@ CREATE TABLE LeaveRequest( EmpID INT NOT NULL, StartDate DATE, EndDate DATE, - TypeRequest VARCHAR(125), + TypeRequest ENUM('Vacation', 'Medical', 'Other') NOT NULL DEFAULT 'Other', Descrip VARCHAR(255), CONSTRAINT PKLeaveRequest PRIMARY KEY (LeaveRequestID), CONSTRAINT FKRequestLeaveEmpID