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.ContactTypeI; 7 import org.enhydra.pim.business.base.ContactType; 8 import org.enhydra.pim.data.production.ContactTypeDO; 9 import org.enhydra.pim.data.production.ContactTypeQuery; 10 import org.enhydra.pim.exception.EnhydraPimDatabaseException; 11 import org.enhydra.pim.exception.EnhydraPimException; 12 import org.enhydra.pim.exception.EnhydraPimLogicException; 13 14 import com.lutris.appserver.server.sql.DBTransaction; 15 import com.lutris.appserver.server.sql.ObjectId; 16 17 22 public class ContactTypeManager implements ContactTypeManagerI { 23 24 27 public ContactTypeI newContactType(String contact_type) { 28 return new ContactType(contact_type); 29 } 30 31 35 public ContactTypeI newContactType(BigDecimal handle, String contact_type) { 36 return new ContactType(handle, contact_type); 37 } 38 39 ContactTypeI contactTypeFromDO(ContactTypeDO contactTypeDO) throws EnhydraPimDatabaseException { 40 ContactTypeI contactType = null; 41 try { 42 contactType = new ContactType(contactTypeDO.getOId().toBigDecimal(), contactTypeDO.getContact_type()); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 PimBase.logError("[id=4791]Data access error"); 46 throw new EnhydraPimDatabaseException("[id=4791]Data access error"); 47 } 48 return contactType; 49 } 50 51 ContactTypeDO fillContactTypeDO(ContactTypeI contactType, ContactTypeDO contactTypeDO) 52 throws EnhydraPimDatabaseException { 53 try { 54 contactTypeDO.setContact_type(contactType.getContact_type()); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 PimBase.logError("[id=4781]Data access error"); 58 throw new EnhydraPimDatabaseException("[id=4781]Data access error"); 59 } 60 return contactTypeDO; 61 } 62 63 public ContactTypeI addContactType(ContactTypeI cType) throws EnhydraPimException { 64 65 if ((cType == null)) { 66 PimBase.logDebug("[id=4711]Invalid Contact Type (null)"); 67 throw new EnhydraPimLogicException("[id=4711]Invalid Contact Type (null)"); 68 } 69 70 DBTransaction dbTrans = null; 71 try { 72 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 73 ContactTypeDO cTypeDO = ContactTypeDO.createVirgin(dbTrans); 74 cTypeDO.setContact_type(cType.getContact_type()); 75 cTypeDO.save(dbTrans); 76 dbTrans.commit(); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 PimBase.logError("[id=4702]Database access error: ContactType can't be registred"); 80 throw new EnhydraPimDatabaseException("[id=4702]Database access error: ContactType can't be registred"); 81 } finally { 82 if (dbTrans != null) 83 dbTrans.release(); 84 } 85 return cType; 86 } 87 88 public ContactTypeI updateContactType(ContactTypeI cType) throws EnhydraPimException { 89 if ((cType == null)) { 90 PimBase.logDebug("[id=4732]Invalid Contact Type (null)"); 91 throw new EnhydraPimLogicException("[id=4732]Invalid Contact Type (null)"); 92 } 93 DBTransaction dbTrans = null; 94 try { 95 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 96 ContactTypeDO cTypeDO = ContactTypeDO.createExisting(cType.getHandle(), dbTrans); 97 cTypeDO.setContact_type(cType.getContact_type()); 98 cTypeDO.save(dbTrans); 99 dbTrans.commit(); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 PimBase.logError("[id=4709]Database access error: Note can't be registred"); 103 throw new EnhydraPimDatabaseException("[id=4709]Database access error: Note can't be registred"); 104 } finally { 105 if (dbTrans != null) 106 dbTrans.release(); 107 } 108 return cType; 109 } 110 111 public Vector getContactTypes() throws EnhydraPimException { 112 Vector contactTypes = new Vector (); 113 DBTransaction dbTrans = null; 114 try { 115 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 116 ContactTypeQuery contactTypeQuery = new ContactTypeQuery(dbTrans); 117 ContactTypeDO[] contactTypesDOs = contactTypeQuery.getDOArray(); 118 if (contactTypesDOs != null) { 119 for (int i = 0; i < contactTypesDOs.length; i++) { 120 ContactTypeDO contactTypeDO = contactTypesDOs[i]; 121 ContactType contactType = new ContactType(contactTypesDOs[i].getOId().toBigDecimal(), 122 contactTypesDOs[i].getContact_type()); 123 contactTypes.add(contactType); 124 } 125 } 126 } catch (Exception e) { 127 e.printStackTrace(); 128 PimBase.logError("[id=4736]Database access error"); 129 throw new EnhydraPimDatabaseException("[id=4736]Database access error"); 130 } finally { 131 if (dbTrans != null) 132 dbTrans.release(); 133 } 134 return contactTypes; 135 } 136 137 public void removeContactType(ContactTypeI contactType) throws EnhydraPimException { 138 DBTransaction dbTrans = null; 139 try { 140 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 141 ContactTypeDO contactTypeDO = ContactTypeDO.createExisting(contactType.getHandle(), dbTrans); 142 if (contactTypeDO != null) { 143 contactTypeDO.delete(dbTrans); 144 contactTypeDO.save(dbTrans); 145 } 146 dbTrans.commit(); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 PimBase.logError("[id=4733]Database access error"); 150 throw new EnhydraPimDatabaseException("[id=4733]Database access error"); 151 } finally { 152 if (dbTrans != null) 153 dbTrans.release(); 154 } 155 } 156 157 public ContactTypeI findContactType(ObjectId oid) throws EnhydraPimException { 158 return findContactType(oid.toBigDecimal()); 159 } 160 161 public ContactTypeI findContactType(BigDecimal handle) throws EnhydraPimException { 162 DBTransaction dbTrans = null; 163 ContactType contactType = null; 164 try { 165 166 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 167 168 ContactTypeDO contactTypeDO = ContactTypeDO.createExisting(handle, dbTrans); 169 if (contactTypeDO != null) { 170 contactType = new ContactType(handle, contactTypeDO.getContact_type()); 171 } 172 dbTrans.commit(); 173 } catch (Exception e) { 174 e.printStackTrace(); 175 PimBase.logError("[id=4711]Database access error"); 176 throw new EnhydraPimDatabaseException("[id=4711]Database access error"); 177 } finally { 178 if (dbTrans != null) 179 dbTrans.release(); 180 } 181 return contactType; 182 } 183 184 public ContactTypeI findContactType(String typeStr) throws EnhydraPimException { 185 DBTransaction dbTrans = null; 186 try { 187 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 188 ContactTypeQuery contactTypeQuery = new ContactTypeQuery(dbTrans); 189 contactTypeQuery.setQueryContact_type(typeStr); 190 ContactTypeDO[] contactTypesDOs = contactTypeQuery.getDOArray(); 191 if (contactTypesDOs != null && contactTypesDOs.length > 0) { 192 return contactTypeFromDO(contactTypesDOs[0]); 193 } 194 } catch (Exception e) { 195 e.printStackTrace(); 196 PimBase.logError("[id=4776]Database access error"); 197 throw new EnhydraPimDatabaseException("[id=4776]Database access error"); 198 } finally { 199 if (dbTrans != null) 200 dbTrans.release(); 201 } 202 return (ContactTypeI) null; 203 } 204 205 } | Popular Tags |