001: package org.claros.intouch.tasks.controllers;
002:
003: import java.util.List;
004:
005: import org.claros.commons.exception.NoPermissionException;
006: import org.claros.intouch.common.utility.Constants;
007: import org.claros.intouch.common.utility.Utility;
008: import org.claros.intouch.tasks.models.Task;
009:
010: import com.jenkov.mrpersister.impl.mapping.AutoGeneratedColumnsMapper;
011: import com.jenkov.mrpersister.itf.IGenericDao;
012: import com.jenkov.mrpersister.itf.mapping.IObjectMappingKey;
013: import com.jenkov.mrpersister.util.JdbcUtil;
014:
015: public class TaskController {
016:
017: /**
018: *
019: * @param username
020: * @return
021: * @throws Exception
022: */
023: public static List<Task> getTasks(String username) throws Exception {
024: IGenericDao dao = null;
025: List res = null;
026: try {
027: dao = Utility.getDbConnection();
028:
029: String sql = "SELECT * FROM TASKS WHERE USERNAME=? ORDER BY PRIORITY DESC";
030:
031: res = dao.readList(Task.class, sql,
032: new Object[] { username });
033: } finally {
034: JdbcUtil.close(dao);
035: dao = null;
036: }
037: return res;
038: }
039:
040: /**
041: *
042: * @param username
043: * @param id
044: * @return
045: * @throws Exception
046: */
047: public static Task getTaskById(String username, Long id)
048: throws Exception {
049: IGenericDao dao = null;
050: Task res = null;
051: try {
052: dao = Utility.getDbConnection();
053: String sql = "SELECT * FROM TASKS WHERE USERNAME=? AND ID = ?";
054: res = (Task) dao.read(Task.class, sql, new Object[] {
055: username, id });
056: } finally {
057: JdbcUtil.close(dao);
058: dao = null;
059: }
060: return res;
061: }
062:
063: /**
064: *
065: * @param username
066: * @param task
067: * @throws Exception
068: */
069: @SuppressWarnings("deprecation")
070: public static void saveTask(String username, Task task)
071: throws Exception {
072: IGenericDao dao = null;
073: try {
074: dao = Utility.getDbConnection();
075:
076: Long id = task.getId();
077: if (id == null) {
078: // it is an insert
079: IObjectMappingKey myObj = Constants.persistMan
080: .getObjectMappingFactory().createInstance(
081: Task.class,
082: new AutoGeneratedColumnsMapper(true));
083: dao.insert(myObj, task);
084: } else {
085: // it is an update
086: Task tmp = getTaskById(username, task.getId());
087: if (tmp == null) {
088: // there is no such task for this user.
089: throw new NoPermissionException();
090: }
091: dao.update(task);
092: }
093: } finally {
094: JdbcUtil.close(dao);
095: dao = null;
096: }
097: }
098:
099: /**
100: *
101: * @param username
102: * @param id
103: * @throws Exception
104: */
105: public static void deleteTask(String username, Long id)
106: throws Exception {
107: Task tmp = getTaskById(username, id);
108: if (tmp == null || !username.equals(tmp.getUsername())) {
109: // there is no such task for this user
110: throw new NoPermissionException();
111: }
112:
113: IGenericDao dao = null;
114: try {
115: dao = Utility.getDbConnection();
116: dao.deleteByPrimaryKey(Task.class, id);
117: } catch (Exception e) {
118: // do nothing sier
119: } finally {
120: JdbcUtil.close(dao);
121: dao = null;
122: }
123: }
124: }
|