package se.chalmers.dat255.grupp12;
import java.sql.SQLException;
/**
* Created with IntelliJ IDEA.
* User: Haggan
* Date: 2013-10-16
* Time: 15:22
* To change this template use File | Settings | File Templates.
*/
public class TaskCommitHandler extends CommitHandler {
public TaskCommitHandler(ClientChangeList changes, User user) {
super(changes,user);
}
/**
* Insert a new Task to the database
* @param m
*/
@Override
public void insert(Modification m) {
// For structure
Task task = m.getTask();
int listId = m.getListId();
if(listId < 0) {
listId = changes.getNewId(listId);
}
try{
db.insertTask(task, listId);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Update an existing task on the database
* The modification handled contains a Change which contains a String type
* which tells what type of update is to be made
* The task is updated accordingly
*
* @param m - The modification containing the needed information
*/
@Override
public void update(Modification m) {
Modification.Change change = m.getChange();
String newValue = change.getTo();
try {
Task task = db.getTask(m.getTaskId());
String type = change.getType();
switch (type) {
case Constants.TASK_TITLE:
task.setTitle(newValue);
break;
case Constants.TASK_CONTENT:
task.setContent(newValue);
break;
case Constants.TASK_PRIO:
task.setPriority(Task.Priority.valueOf(newValue));
break;
case Constants.TASK_ISDONE:
task.isDone(Boolean.parseBoolean(newValue));
break;
case Constants.TASK_DUEDATE:
task.setDate(Utils.parseDate(newValue));
break;
case Constants.TASK_ASSIGNUSER:
System.out.println(newValue + user + task);
if (Boolean.parseBoolean(newValue)) {
db.assignUserToTask(task.getId(), user);
task.assignUser(user);
} else {
db.removeUserFromTask(task.getId(), user.getId());
task.removeUser(user);
}
return; //No update in tasks-table in database
default:
throw new UnsupportedOperationException("List change property not implemented:" + type);
}
db.updateTask(task.getId(),task);
} catch (SQLException | DataNotFoundException e) {
e.printStackTrace();
}
}
/**
* Removes a task from the database
*
* @param m - A modification containing the needed information
*/
@Override
public void delete(Modification m) {
try {
db.deleteTask(m.getTaskId());
} catch (SQLException e) {
e.printStackTrace();
}
}
}