001: package org.enhydra.dm.business;
002:
003: import java.sql.SQLException;
004: import java.util.Properties;
005:
006: import org.enhydra.dm.api.AppUser;
007: import org.enhydra.dm.api.UserManager;
008: import org.enhydra.dm.api.exceptions.BaseException;
009: import org.enhydra.dm.api.loggers.Log;
010: import org.enhydra.dm.business.exceptions.DatabaseException;
011: import org.enhydra.dm.data.AppUserDO;
012: import org.enhydra.dm.data.AppUserQuery;
013:
014: import com.lutris.appserver.server.sql.DBRowUpdateException;
015: import com.lutris.appserver.server.sql.DatabaseManagerException;
016: import com.lutris.dods.builder.generator.query.DataObjectException;
017: import com.lutris.dods.builder.generator.query.NonUniqueQueryException;
018: import com.lutris.dods.builder.generator.query.QueryException;
019: import com.lutris.dods.builder.generator.query.RefAssertionException;
020:
021: /**
022: * Default implementation of UserManager interface. Used for FORM authentication.
023: *
024: * @author Svjetlana Milidrag
025: */
026:
027: public class UserManagerImpl implements UserManager {
028:
029: private Log logger = null;
030:
031: private final String DEFAULT_ROLE = "admin";
032:
033: public UserManagerImpl() {
034:
035: }
036:
037: /**
038: * Check if username already exists.
039: *
040: * @param username
041: * @return
042: * @throws BaseException
043: */
044: private boolean usernameAlreadyExists(String username)
045: throws BaseException {
046: if (null != logger) {
047: logger.log(Log.DEBUG, "Check for username " + username
048: + " existing! ");
049: }
050: try {
051: AppUserQuery query = new AppUserQuery();
052: query.setQueryUSERNAME(username);
053:
054: AppUserDO userDO = query.getNextDO();
055: if (null != userDO) {
056: return true;
057: }
058:
059: } catch (DataObjectException e) {
060: throw new DatabaseException(e);
061: } catch (QueryException e) {
062: throw new DatabaseException(e);
063: } catch (NonUniqueQueryException e) {
064: throw new DatabaseException(e);
065: }
066: return false;
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see org.enhydra.dm.api.UserManager#getUserByUsername(java.lang.String)
073: */
074: public AppUser getUserByUsername(String username)
075: throws BaseException {
076: if (null != logger) {
077: logger.log(Log.DEBUG, "Searching for user by username ! ",
078: username);
079: }
080: try {
081: AppUserQuery query = new AppUserQuery();
082: query.setQueryUSERNAME(username);
083:
084: AppUserDO userDO = query.getNextDO();
085: if (null != userDO) {
086:
087: return new AppUserImpl(userDO);
088: } else {
089: throw new DatabaseException("User " + username
090: + " doesn't exist in database!");
091: }
092:
093: } catch (DataObjectException e) {
094: throw new DatabaseException(e);
095: } catch (QueryException e) {
096: throw new DatabaseException(e);
097: } catch (NonUniqueQueryException e) {
098: throw new DatabaseException(e);
099: }
100:
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see org.enhydra.dm.api.UserManager#getUserById(java.lang.String)
107: */
108: public AppUser getUserById(String id) throws BaseException {
109: if (null != logger) {
110: logger.log(Log.DEBUG, "Searching for user by id ! ", id);
111: }
112: try {
113: AppUserDO userDO = AppUserDO.createExisting(id);
114:
115: if (null != userDO) {
116: AppUser user = new AppUserImpl(userDO);
117: return user;
118: } else {
119: throw new DatabaseException(
120: "User doesn't exist for id: " + id + ";");
121: }
122: } catch (Exception ex) {
123: throw new DatabaseException(ex.getMessage());
124: }
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.enhydra.dm.api.UserManager#createUser(java.lang.String, java.lang.String,
131: * java.lang.String, java.lang.String)
132: */
133: public boolean createUser(String firstname, String lastname,
134: String username, String password) throws BaseException {
135:
136: if (null != logger) {
137: logger.log(Log.DEBUG, "User name: " + firstname + " : "
138: + lastname + " with username: " + username
139: + " creation ! ");
140: }
141: if (usernameAlreadyExists(username)) {
142: return false;
143: }
144: AppUser user = new AppUserImpl();
145: user.setFirstname(firstname);
146: user.setLastname(lastname);
147: user.setUsername(username);
148: user.setPassword(password);
149: user.setRole(DEFAULT_ROLE);
150:
151: try {
152: user.createDO().save();
153: return true;
154: } catch (DatabaseManagerException e) {
155: throw new DatabaseException(e);
156: } catch (DataObjectException e) {
157: throw new DatabaseException(e);
158: } catch (RefAssertionException e) {
159: throw new DatabaseException(e);
160: } catch (DBRowUpdateException e) {
161: throw new DatabaseException(e);
162: } catch (QueryException e) {
163: throw new DatabaseException(e);
164: } catch (SQLException e) {
165: throw new DatabaseException(e);
166: }
167: }
168:
169: /*
170: * (non-Javadoc)
171: *
172: * @see org.enhydra.dm.api.UserManager#configure(java.util.Properties)
173: */
174: public void configure(Properties properties) throws BaseException {
175:
176: }
177:
178: /*
179: * (non-Javadoc)
180: *
181: * @see org.enhydra.dm.api.UserManager#getLogger()
182: */
183: public Log getLogger() {
184: return logger;
185: }
186:
187: /*
188: * (non-Javadoc)
189: *
190: * @see org.enhydra.dm.api.UserManager#setLogger(org.enhydra.dm.api.loggers.Log)
191: */
192: public void setLogger(Log logger) {
193: this.logger = logger;
194: }
195:
196: }
|