1 package org.enhydra.pim.business; 2 3 import java.math.BigDecimal ; 4 5 import org.enhydra.pim.business.api.OwnerI; 6 import org.enhydra.pim.business.base.Owner; 7 import org.enhydra.pim.data.production.OwnerDO; 8 import org.enhydra.pim.data.production.OwnerQuery; 9 import org.enhydra.pim.exception.EnhydraPimDatabaseException; 10 import org.enhydra.pim.exception.EnhydraPimException; 11 import org.enhydra.pim.exception.EnhydraPimLogicException; 12 13 import com.lutris.appserver.server.sql.DBTransaction; 14 import com.lutris.dods.builder.generator.query.NonUniqueQueryException; 15 16 21 public class UserManager implements UserManagerI{ 22 23 24 25 29 public OwnerI newOwner(String username, String password) { 30 return new Owner(username, password); 31 } 32 33 38 public OwnerI newOwner(BigDecimal handle, String username, String password) { 39 return new Owner(handle, username, password); 40 } 41 42 43 OwnerI ownerFromDO(OwnerDO ownerDO) throws EnhydraPimDatabaseException { 44 try { 45 return new Owner(ownerDO.get_OId().toBigDecimal(), ownerDO.getUsername(), ownerDO.getPassword()); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 PimBase.logError("[id=1091]Data access error"); 49 throw new EnhydraPimDatabaseException("[id=1091]Data access error"); 50 } 51 } 52 53 OwnerDO fillOwnerDO(OwnerI owner, OwnerDO ownerDO) throws EnhydraPimDatabaseException { 54 try { 55 ownerDO.setUsername(owner.getUsername()); 56 ownerDO.setPassword(owner.getPassword()); 57 } catch (Exception e) { 58 e.printStackTrace(); 59 PimBase.logError("[id=1081]Data access error"); 60 throw new EnhydraPimDatabaseException("[id=1081]Data access error"); 61 } 62 return ownerDO; 63 } 64 65 public OwnerI registerOwner(String username, String password) throws EnhydraPimException { 66 Owner owner = null; 67 owner = (Owner) checkOwner(username, password); 68 if (owner == null) { 69 if (username != null && password != null) { 70 DBTransaction dbTrans = null; 71 try { 72 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 73 OwnerDO ownerDO = OwnerDO.createVirgin(dbTrans); 74 ownerDO.setUsername(username); 75 ownerDO.setPassword(password); 76 ownerDO.save(dbTrans); 77 dbTrans.commit(); 78 owner = new Owner(ownerDO.get_OId().toBigDecimal(), username, password); 79 } catch (Exception e) { 80 e.printStackTrace(); 81 PimBase.logError("[id=1020]Database access error: User can't be registred"); 82 throw new EnhydraPimDatabaseException("[id=1020]Database access error: User can't be registred"); 83 } finally { 84 if (dbTrans != null) 85 dbTrans.release(); 86 } 87 } else { 88 PimBase.logError("[id=1021]Invalid(empty) Username or Password."); 89 throw new EnhydraPimLogicException("[id=1021]Invalid(empty) Username or Password."); 90 } 91 return owner; 92 } else { 93 PimBase.logError("[id=1022]User already registred"); 94 throw new EnhydraPimLogicException("[id=1022]User already registred"); 95 } 96 } 97 98 public OwnerI checkOwner(String username, String password) throws EnhydraPimException { 99 Owner owner = null; 100 if (username != null && password != null) { 101 DBTransaction dbTrans = null; 102 try { 103 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 104 OwnerQuery ownerQuery = new OwnerQuery(dbTrans); 105 ownerQuery.setQueryUsername(username); 106 ownerQuery.setQueryPassword(password); 107 ownerQuery.setUnique(true); 108 OwnerDO[] ownerDOs = ownerQuery.getDOArray(); 109 if (ownerDOs != null && ownerDOs.length>0 && ownerDOs[0] != null) { 110 owner = new Owner(ownerDOs[0].get_OId().toBigDecimal(), ownerDOs[0].getUsername(), ownerDOs[0] 111 .getPassword()); 112 } 113 } catch (NonUniqueQueryException e) { 114 PimBase.logDebug("[id=1010]More then one user have same username and password"); 115 throw new EnhydraPimLogicException("[id=1010]More then one user have same username and password"); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 PimBase.logError("[id=1011]Database access error"); 119 throw new EnhydraPimDatabaseException("[id=1011]Database access error"); 120 } finally { 121 if (dbTrans != null) 122 dbTrans.release(); 123 } 124 } else { 125 PimBase.logDebug("[id=1012]Invalid(empty) Username or Password."); 126 throw new EnhydraPimLogicException("[id=1012]Invalid(empty) Username or Password."); 127 } 128 129 return owner; 130 } 131 132 public void removeOwner(OwnerI owner) throws EnhydraPimException { 133 DBTransaction dbTrans = null; 134 try { 135 dbTrans = PimBase.getPrimaryDatabase().createTransaction(); 136 OwnerDO ownerDO = OwnerDO.createExisting(owner.getHandle(), dbTrans); 137 if (ownerDO != null) { 138 ownerDO.delete(dbTrans); 139 ownerDO.save(dbTrans); 140 } 141 dbTrans.commit(); 142 } catch (Exception e) { 143 e.printStackTrace(); 144 PimBase.logError("[id=1030]Database access error"); 145 throw new EnhydraPimDatabaseException("[id=1030]Database access error"); 146 } finally { 147 if (dbTrans != null) 148 dbTrans.release(); 149 } 150 } 151 } | Popular Tags |