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.NoteI; 7 import org.enhydra.pim.business.api.OwnerI; 8 import org.enhydra.pim.business.base.Note; 9 import org.enhydra.pim.business.base.Owner; 10 import org.enhydra.pim.data.production.NoteDO; 11 import org.enhydra.pim.data.production.NoteQuery; 12 import org.enhydra.pim.data.production.OwnerDO; 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 NoteManager implements NoteManagerI { 27 28 public NoteI addNote(NoteI note, OwnerI owner) throws EnhydraPimException { 29 30 if (note == null || note.getNote() == null) { 31 PimBase.logDebug("[id=2100]Invalid note (null)"); 32 throw new EnhydraPimLogicException("[id=2100]Invalid note (null)"); 33 } 34 if ((owner == null) || !owner.isValid()) { 35 PimBase.logDebug("[id=2101]Invalid User"); 36 throw new EnhydraPimLogicException("[id=2101]Invalid User"); 37 } 38 DBTransaction dbTrans = null; 39 try { 40 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 41 NoteDO noteDO = NoteDO.createVirgin(dbTrans); 42 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 43 noteDO.setNote(note.getNote()); 44 noteDO.setDate(note.getDate()); 45 noteDO.setTime(note.getTime()); 46 noteDO.setOwner(ownerDO); 47 noteDO.save(dbTrans); 48 dbTrans.commit(); 49 note.setHandle(noteDO.getOId().toBigDecimal()); 50 } catch (Exception e) { 51 e.printStackTrace(); 52 PimBase.logError("[id=2102]Database access error: Note can't be registred"); 53 throw new EnhydraPimDatabaseException("[id=2102]Database access error: Note can't be registred"); 54 } finally { 55 if (dbTrans != null) 56 dbTrans.release(); 57 } 58 return note; 59 } 60 61 public NoteI updateNote(NoteI note, OwnerI owner) throws EnhydraPimException { 62 63 if (note == null || note.getNote() == null) { 64 PimBase.logDebug("[id=2107]Invalid note (null)"); 65 throw new EnhydraPimLogicException("[id=2107]Invalid note (null)"); 66 } 67 if ((owner == null) || !owner.isValid()) { 68 PimBase.logDebug("[id=2108]Invalid User"); 69 throw new EnhydraPimLogicException("[id=2108]Invalid User"); 70 } 71 DBTransaction dbTrans = null; 72 try { 73 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 74 NoteDO noteDO = NoteDO.createExisting(note.getHandle(), dbTrans); 75 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 76 noteDO.setNote(note.getNote()); 77 noteDO.setDate(note.getDate()); 78 noteDO.setTime(note.getTime()); 79 noteDO.setOwner(ownerDO); 80 noteDO.save(dbTrans); 81 dbTrans.commit(); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 PimBase.logError("[id=2109]Database access error: Note can't be registred"); 85 throw new EnhydraPimDatabaseException("[id=2109]Database access error: Note can't be registred"); 86 } finally { 87 if (dbTrans != null) 88 dbTrans.release(); 89 } 90 return note; 91 } 92 93 public Vector getOwnerNotes(OwnerI owner) throws EnhydraPimException { 94 Vector ownerNotes = new Vector (); 95 if ((owner == null) || !owner.isValid()) { 96 PimBase.logDebug("[id=2104]Invalid User"); 97 throw new EnhydraPimLogicException("[id=2104]Invalid User"); 98 } 99 DBTransaction dbTrans = null; 100 try { 101 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 102 103 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 104 NoteQuery noteQuery = new NoteQuery(dbTrans); 105 noteQuery.setQueryOwner(ownerDO); 106 NoteDO[] noteDOs = noteQuery.getDOArray(); 107 if (noteDOs != null) { 108 for (int i = 0; i < noteDOs.length; i++) { 109 Note newNote = new Note(noteDOs[i].get_OId().toBigDecimal(), noteDOs[i].getNote(), noteDOs[i] 110 .getDate(), noteDOs[i].getTime(), owner); 111 ownerNotes.add(newNote); 112 } 113 } 114 } catch (NonUniqueQueryException e) { 115 PimBase.logDebug("[id=2105]More then one user have same username and password"); 116 throw new EnhydraPimLogicException("[id=2105]More then one user have same username and password"); 117 } catch (Exception e) { 118 e.printStackTrace(); 119 PimBase.logError("[id=2106]Database access error"); 120 throw new EnhydraPimDatabaseException("[id=2106]Database access error"); 121 } finally { 122 if (dbTrans != null) 123 dbTrans.release(); 124 } 125 return ownerNotes; 126 } 127 128 public void removeNote(NoteI note) throws EnhydraPimException { 129 DBTransaction dbTrans = null; 130 try { 131 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 132 NoteDO noteDO = NoteDO.createExisting(note.getHandle(), dbTrans); 133 if (noteDO != null) { 134 noteDO.delete(dbTrans); 135 noteDO.save(dbTrans); 136 } 137 dbTrans.commit(); 138 } catch (Exception e) { 139 e.printStackTrace(); 140 PimBase.logError("[id=2103]Database access error"); 141 throw new EnhydraPimDatabaseException("[id=2103]Database access error"); 142 } finally { 143 if (dbTrans != null) 144 dbTrans.release(); 145 } 146 } 147 148 public NoteI findNote(ObjectId oid) throws EnhydraPimException { 149 return findNote(oid.toBigDecimal()); 150 } 151 152 public NoteI findNote(BigDecimal handle) throws EnhydraPimException { 153 DBTransaction dbTrans = null; 154 Note note = null; 155 try { 156 UserManager uMan = new UserManager(); 157 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 158 NoteDO noteDO = NoteDO.createExisting(handle, dbTrans); 159 if (noteDO != null && noteDO.getOwner() != null) { 160 Owner owner = (Owner) uMan.checkOwner(noteDO.getOwner().getUsername(), noteDO.getOwner().getPassword()); 161 if (owner == null) { 162 PimBase.logError("[id=2110]Unknown User"); 163 throw new EnhydraPimLogicException("[id=2110]Unknown User"); 164 } 165 note = new Note(noteDO.get_OId().toBigDecimal(), noteDO.getNote(), noteDO.getDate(), noteDO.getTime(), 166 owner); 167 } 168 dbTrans.commit(); 169 } catch (EnhydraPimLogicException ex) { 170 throw ex; 171 } catch (Exception e) { 172 e.printStackTrace(); 173 PimBase.logError("[id=2111]Database access error"); 174 throw new EnhydraPimDatabaseException("[id=2111]Database access error"); 175 } finally { 176 if (dbTrans != null) 177 dbTrans.release(); 178 } 179 return note; 180 } 181 182 } | Popular Tags |