1 package org.enhydra.pim.business; 2 3 import java.math.BigDecimal ; 4 import java.util.Vector ; 5 6 import org.enhydra.pim.business.api.OwnerI; 7 import org.enhydra.pim.business.api.TaskI; 8 import org.enhydra.pim.business.base.Owner; 9 import org.enhydra.pim.business.base.Task; 10 import org.enhydra.pim.data.production.OwnerDO; 11 import org.enhydra.pim.data.production.TaskDO; 12 import org.enhydra.pim.data.production.TaskQuery; 13 import org.enhydra.pim.exception.EnhydraPimDatabaseException; 14 import org.enhydra.pim.exception.EnhydraPimException; 15 import org.enhydra.pim.exception.EnhydraPimLogicException; 16 17 import com.lutris.appserver.server.sql.DBTransaction; 18 import com.lutris.appserver.server.sql.ObjectId; 19 import com.lutris.dods.builder.generator.query.NonUniqueQueryException; 20 21 26 public class TaskManager implements TaskManagerI{ 27 28 public TaskI addTask(TaskI task, OwnerI owner) throws EnhydraPimException { 29 30 if (task == null || task.getNote() == null) { 31 PimBase.logDebug("[id=5100]Invalid task (null)"); 32 throw new EnhydraPimLogicException("[id=5100]Invalid note (null)"); 33 } 34 if ((owner == null) || !owner.isValid()) { 35 PimBase.logDebug("[id=5101]Invalid User"); 36 throw new EnhydraPimLogicException("[id=5101]Invalid User"); 37 } 38 DBTransaction dbTrans = null; 39 try { 40 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 41 TaskDO taskDO = TaskDO.createVirgin(dbTrans); 42 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 43 taskDO.setAlarm_note(task.getAlarm_note()); 44 taskDO.setNote(task.getNote()); 45 taskDO.setDate_start(task.getDate_start()); 46 taskDO.setDate_stop(task.getDate_stop()); 47 taskDO.setTime_start(task.getTime_start()); 48 taskDO.setTime_stop(task.getTime_stop()); 49 taskDO.setAlarm(task.getAlarm()); 50 taskDO.setPriority(task.getPriority()); 51 taskDO.setCompletion(task.getCompletion()); 52 taskDO.setRepaeat_type(task.getRepaeat_type()); 53 taskDO.setRepaeat_count(task.getRepaeat_type()); 54 taskDO.setOwner(ownerDO); 55 taskDO.save(dbTrans); 56 dbTrans.commit(); 57 task.setHandle(taskDO.getOId().toBigDecimal()); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 PimBase.logError("[id=5102]Database access error: Task can't be registred"); 61 throw new EnhydraPimDatabaseException("[id=5102]Database access error: Task can't be registred"); 62 } finally { 63 if (dbTrans != null) 64 dbTrans.release(); 65 } 66 return task; 67 } 68 69 public TaskI updateTask(TaskI task, OwnerI owner) throws EnhydraPimException { 70 71 if (task == null || task.getNote() == null) { 72 PimBase.logDebug("[id=5107]Invalid task (null)"); 73 throw new EnhydraPimLogicException("[id=5107]Invalid task (null)"); 74 } 75 if ((owner == null) || !owner.isValid()) { 76 PimBase.logDebug("[id=5108]Invalid User"); 77 throw new EnhydraPimLogicException("[id=5108]Invalid User"); 78 } 79 DBTransaction dbTrans = null; 80 try { 81 TaskDO taskDO = TaskDO.createExisting(task.getHandle(), dbTrans); 82 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 83 taskDO.setAlarm_note(task.getAlarm_note()); 84 taskDO.setNote(task.getNote()); 85 taskDO.setDate_start(task.getDate_start()); 86 taskDO.setDate_stop(task.getDate_stop()); 87 taskDO.setTime_start(task.getTime_start()); 88 taskDO.setTime_stop(task.getTime_stop()); 89 taskDO.setAlarm(task.getAlarm()); 90 taskDO.setPriority(task.getPriority()); 91 taskDO.setCompletion(task.getCompletion()); 92 taskDO.setRepaeat_type(task.getRepaeat_type()); 93 taskDO.setRepaeat_count(task.getRepaeat_type()); 94 taskDO.setOwner(ownerDO); 95 taskDO.save(dbTrans); 96 dbTrans.commit(); 97 } catch (Exception e) { 98 e.printStackTrace(); 99 PimBase.logError("[id=5109]Database access error: Note can't be registred"); 100 throw new EnhydraPimDatabaseException("[id=5109]Database access error: Note can't be registred"); 101 } finally { 102 if (dbTrans != null) 103 dbTrans.release(); 104 } 105 return task; 106 } 107 108 public Vector getOwnerTasks(OwnerI owner) throws EnhydraPimException { 109 Vector ownerTasks = new Vector (); 110 if ((owner == null) || !owner.isValid()) { 111 PimBase.logDebug("[id=5104]Invalid User"); 112 throw new EnhydraPimLogicException("[id=5104]Invalid User"); 113 } 114 DBTransaction dbTrans = null; 115 try { 116 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 117 118 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 119 TaskQuery taskQuery = new TaskQuery(dbTrans); 120 taskQuery.setQueryOwner(ownerDO); 121 TaskDO[] taskDOs = taskQuery.getDOArray(); 122 123 if (taskDOs != null) { 124 for (int i = 0; i < taskDOs.length; i++) { 125 Task newTask = new Task(taskDOs[i].get_OId().toBigDecimal(), taskDOs[i].getAlarm_note(), owner, 126 taskDOs[i].getNote(), taskDOs[i].getDate_start(), taskDOs[i].getDate_stop(), 127 taskDOs[i].getTime_start(), taskDOs[i].getTime_stop(), taskDOs[i].getAlarm(), 128 taskDOs[i].getPriority(), taskDOs[i].getCompletion(), taskDOs[i].getRepaeat_type(), 129 taskDOs[i].getRepaeat_count()); 130 ownerTasks.add(newTask); 131 } 132 } 133 } catch (NonUniqueQueryException e) { 134 PimBase.logDebug("[id=5105]More then one user have same username and password"); 135 throw new EnhydraPimLogicException("[id=5105]More then one user have same username and password"); 136 } catch (Exception e) { 137 e.printStackTrace(); 138 PimBase.logError("[id=5106]Database access error"); 139 throw new EnhydraPimDatabaseException("[id=5106]Database access error"); 140 } finally { 141 if (dbTrans != null) 142 dbTrans.release(); 143 } 144 return ownerTasks; 145 } 146 147 public void removeTask(TaskI task) throws EnhydraPimException { 148 DBTransaction dbTrans = null; 149 try { 150 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 151 TaskDO taskDO = TaskDO.createExisting(task.getHandle(), dbTrans); 152 if (taskDO != null) { 153 taskDO.delete(dbTrans); 154 taskDO.save(dbTrans); 155 } 156 dbTrans.commit(); 157 } catch (Exception e) { 158 e.printStackTrace(); 159 PimBase.logError("[id=5103]Database access error"); 160 throw new EnhydraPimDatabaseException("[id=5103]Database access error"); 161 } finally { 162 if (dbTrans != null) 163 dbTrans.release(); 164 } 165 } 166 167 public TaskI findTask(ObjectId oid) throws EnhydraPimException { 168 return findTask(oid.toBigDecimal()); 169 } 170 171 public TaskI findTask(BigDecimal handle) throws EnhydraPimException { 172 DBTransaction dbTrans = null; 173 Task task = null; 174 try { 175 UserManager uMan = new UserManager(); 176 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 177 TaskDO taskDO = TaskDO.createExisting(handle, dbTrans); 178 if (taskDO != null && taskDO.getOwner() != null) { 179 Owner owner = (Owner) uMan.checkOwner(taskDO.getOwner().getUsername(), taskDO.getOwner().getPassword()); 180 if (owner == null) { 181 PimBase.logError("[id=5106]Unknown User"); 182 throw new EnhydraPimLogicException("[id=5106]Unknown User"); 183 } 184 task = new Task(taskDO.get_OId().toBigDecimal(), taskDO.getAlarm_note(), owner, taskDO.getNote(), 185 taskDO.getDate_start(), taskDO.getDate_stop(), taskDO.getTime_start(), taskDO 186 .getTime_stop(), taskDO.getAlarm(), taskDO.getPriority(), taskDO 187 .getCompletion(), taskDO.getRepaeat_type(), taskDO.getRepaeat_count()); 188 } 189 dbTrans.commit(); 190 } catch (EnhydraPimLogicException ex) { 191 throw ex; 192 } catch (Exception e) { 193 e.printStackTrace(); 194 PimBase.logError("[id=5104]Database access error"); 195 throw new EnhydraPimDatabaseException("[id=5104]Database access error"); 196 } finally { 197 if (dbTrans != null) 198 dbTrans.release(); 199 } 200 return task; 201 } 202 203 } | Popular Tags |