KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > pim > business > UserManager


1 package org.enhydra.pim.business;
2
3 import java.math.BigDecimal JavaDoc;
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 /**
17  * @author P.Djojic Apr 19, 2005 11:48:55 PM
18  *
19  * TODO UserManager
20  */

21 public class UserManager implements UserManagerI{
22     
23     
24     
25     /**
26      * @param username
27      * @param password
28      */

29     public OwnerI newOwner(String JavaDoc username, String JavaDoc password) {
30         return new Owner(username, password);
31     }
32
33     /**
34      * @param handle
35      * @param username
36      * @param password
37      */

38     public OwnerI newOwner(BigDecimal JavaDoc handle, String JavaDoc username, String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc username, String JavaDoc 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 JavaDoc 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 JavaDoc username, String JavaDoc 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 JavaDoc 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 JavaDoc 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