Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
hellerchr committed Oct 8, 2015
1 parent b9b5510 commit cbb1f94
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 32 deletions.
35 changes: 4 additions & 31 deletions src/main/java/com/sap/todo/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import com.sap.security.um.service.UserManagementAccessor;
import com.sap.security.um.user.PersistenceException;
import com.sap.security.um.user.UserProvider;

import com.sap.todo.dao.TaskDAO;
import com.sap.todo.dao.TaskListDAO;

public class Application extends ResourceConfig {

Expand All @@ -19,7 +20,6 @@ public Application() {
this.applicationRootPackage = this.getClass().getPackage().getName();
registerEndpoints();
registerDependecyInjection();
myMain();
}

private void registerEndpoints() {
Expand All @@ -38,6 +38,8 @@ protected void configure() {
bindFactory(EMFactory.class).to(EntityManager.class).in(RequestScoped.class);

// DAOs
bind(TaskDAO.class).to(TaskDAO.class).in(RequestScoped.class);
bind(TaskListDAO.class).to(TaskListDAO.class).in(RequestScoped.class);
}

});
Expand All @@ -50,33 +52,4 @@ private UserProvider getUserProvider() {
throw new RuntimeException(e);
}
}

private void myMain() {
/*
EntityManager em = EMFactory.getEntityManagerFactory().createEntityManager();
TaskDAO taskDAO = new TaskDAO();
taskDAO.setEntityManager(em);
TaskListDAO taskListDAO = new TaskListDAO();
taskListDAO.setEntityManager(em);
Tasklist tasklist = new Tasklist();
tasklist.setName("Shopping");
tasklist.setId(99);
taskListDAO.persist(tasklist);
Task task = new Task();
task.setId(100);
task.setTaskList(tasklist);
task.setDescription("Buy Milk");
tasklist.getTasks().add(task);
taskDAO.persist(task);
Task searchedTask = taskDAO.findTaskByListIdAndTaskId(99, 100);
boolean done = searchedTask.isDone();
em.close();
*/
}
}
159 changes: 159 additions & 0 deletions src/main/java/com/sap/todo/api/TasklistAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.sap.todo.api;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
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 com.sap.todo.dao.TaskDAO;
import com.sap.todo.dao.TaskListDAO;
import com.sap.todo.entity.Task;
import com.sap.todo.entity.Tasklist;

@Path("/tasklist")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class TasklistAPI {
@Inject
TaskListDAO taskListDAO;

@Inject
TaskDAO taskDAO;

@GET
@Path("/")
public List<Tasklist> readAllTasklists() {
return taskListDAO.findAll();
}


@POST
@Path("/")
public Tasklist createTaskList(Tasklist tasklist) {
return taskListDAO.persist(tasklist);
}

@POST
@Path("/{id}/task/")
public Task createTask(@PathParam("id") long listId, Task task) {
Tasklist taskList = taskListDAO.find(listId);

if (taskList == null)
throw new NotFoundException();

taskList.addTask(task);
return taskDAO.persist(task);
}

@PUT
@Path("/{id}/task/{task_id}")
public Task updateTaskOfTasklist(@PathParam("id") long listId, @PathParam("task_id") long taskId, Task task) {
Task existingTask = taskDAO.findTaskByListIdAndTaskId(listId, taskId);
if (existingTask == null)
throw new NotFoundException();

task.setTaskList(existingTask.getTaskList());
task.setId(taskId);

return taskDAO.merge(task);
}

@DELETE
@Path("/{id}")
public Tasklist deleteTasklist(@PathParam("id") long listId) {
Tasklist tasklist = taskListDAO.find(listId);
if (tasklist == null)
throw new NotFoundException();

return taskListDAO.remove(tasklist);
}

@DELETE
@Path("/{id}/done_tasks")
public List<Task> deleteTasklistDoneTasks(@PathParam("id") long listId) {
Tasklist tasklist = taskListDAO.find(listId);
if (tasklist == null)
throw new NotFoundException();

ArrayList<Task> tasksToRemove = new ArrayList<>();
for (Task task : tasklist.getTasks()) {
if (task.isDone())
tasksToRemove.add(task);
}

tasklist.getTasks().removeAll(tasksToRemove);
taskDAO.remove(tasksToRemove);

return tasksToRemove;
}

//
// @GET
// @Path("/{id}/task/{task_id}")
// public Task readTask(@PathParam("id") long listId, @PathParam("task_id") long taskId) {
// Task task = taskDAO.findTaskByListIdAndTaskId(listId, taskId);
//
// if (task == null)
// throw new NotFoundException();
//
// return task;
// }


// @GET
// @Path("/{id}")
// public Tasklist readTasklist(@PathParam("id") long id) {
// Tasklist tasklist = taskListDAO.find(id);
// if (tasklist == null)
// throw new NotFoundException();
//
// return tasklist;
// }


// @GET
// @Path("/{id}/task/")
// public List<Task> readAllTasks(@PathParam("id") long id) {
// List<Task> tasklist = taskListDAO.findAllTasksByListId(id);
//
// if (tasklist == null)
// throw new NotFoundException();
//
// return tasklist;
// }


// @PUT
// @Path("/{id}")
// public Tasklist updateTasklist(@PathParam("id") long id, Tasklist tasklist) {
// if (!taskListDAO.exists(id))
// throw new NotFoundException();
//
// tasklist.setId(id);
// return taskListDAO.merge(tasklist);
// }


// @DELETE
// @Path("/{id}/task/{task_id}")
// public Task deleteTask(@PathParam("id") long listId, @PathParam("task_id") long taskId) {
// Task task = taskDAO.findTaskByListIdAndTaskId(listId, taskId);
// if (task == null)
// throw new NotFoundException();
//
// Tasklist taskList = task.getTaskList();
// taskList.getTasks().remove(task);
//
// return taskDAO.remove(task);
// }
}
24 changes: 24 additions & 0 deletions src/main/java/com/sap/todo/dao/TaskDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.sap.todo.dao;

import javax.persistence.NoResultException;

import com.sap.todo.entity.Task;

public class TaskDAO extends BaseDAO<Task, Long> {
public TaskDAO() {
super(Task.class);
}

public Task findTaskByListIdAndTaskId(long tasklistId, long taskId) {
try {
Task task = (Task) em.createQuery("select t from Task t where t.tasklist.id = :tasklistId and t.id = :taskId")
.setParameter("tasklistId", tasklistId)
.setParameter("taskId", taskId)
.getSingleResult();
return task;
}
catch (NoResultException e) {
return null;
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/sap/todo/dao/TaskListDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.sap.todo.dao;

import com.sap.todo.entity.Tasklist;

public class TaskListDAO extends BaseDAO<Tasklist, Long> {
public TaskListDAO() {
super(Tasklist.class);
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/sap/todo/entity/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.sap.todo.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@Entity
@XmlRootElement
public class Task {
@Id
@GeneratedValue
private long id;

private String description;
private boolean done;

@ManyToOne
private Tasklist tasklist;

public Task() {
}

public Task(Tasklist tasklist, String description) {
this.tasklist = tasklist;
this.description = description;
this.done = true;
}

public void setId(long id) {
this.id = id;
}

public void setDescription(String description) {
this.description = description;
}

public void setDone(boolean done) {
this.done = done;
}

public String getDescription() {
return description;
}

public boolean isDone() {
return done;
}

public Long getId() {
return id;
}

@XmlTransient
public Tasklist getTaskList() {
return tasklist;
}

public void setTaskList(Tasklist taskList) {
this.tasklist = taskList;
}
}
72 changes: 72 additions & 0 deletions src/main/java/com/sap/todo/entity/Tasklist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.sap.todo.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement
public class Tasklist {
@Id
@GeneratedValue
private long id;

private String name;
private String icon;

@OneToMany(cascade = CascadeType.REMOVE)
private List<Task> tasks;

public Tasklist() {
}

public Tasklist(String name, String icon) {
tasks = new ArrayList<>();
this.name = name;
this.icon = icon;
}

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 getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public List<Task> getTasks() {
return tasks;
}

public void setTasks(List<Task> tasks) {
this.tasks = tasks;
}

public void addTask(Task task) {
if (task.getTaskList() != this)
task.setTaskList(this);
tasks.add(task);
}
}
Loading

0 comments on commit cbb1f94

Please sign in to comment.