001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.user;
018:
019: import java.io.InputStream;
020: import java.sql.Timestamp;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.commons.lang.StringUtils;
027:
028: import edu.iu.uis.eden.clientapp.vo.EmplIdVO;
029: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
030: import edu.iu.uis.eden.clientapp.vo.UserIdVO;
031: import edu.iu.uis.eden.clientapp.vo.UuIdVO;
032: import edu.iu.uis.eden.clientapp.vo.WorkflowIdVO;
033: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
034: import edu.iu.uis.eden.user.dao.BaseUserDAO;
035: import edu.iu.uis.eden.xml.UserXmlHandler;
036:
037: /**
038: * A UserService implementation which is backed by a database and fronted by an in-memory cache.
039: * Because of the caching strategy being used, this service is not safe for use in a clustered environment.
040: * The user objects maintained within this service are instances of SimpleWorkflowUser.
041: *
042: * @author Eric Westfal
043: */
044: public class BaseUserService implements UserService {
045:
046: public static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(BaseUserService.class);
048: public static final String ERR_KEY_INVALID_USER = "user.userservice.id.invalid";
049:
050: protected BaseUserDAO userDao;
051:
052: protected UserCache cache = new UserCache();
053: protected UserCapabilities capabilities = UserCapabilities.getAll();
054:
055: public void setUserDAO(BaseUserDAO userDao) {
056: this .userDao = userDao;
057: }
058:
059: /**
060: * Supports all user capabilities.
061: */
062: public UserCapabilities getCapabilities() {
063: return capabilities;
064: }
065:
066: public WorkflowUser getWorkflowUser(UserIdVO userId)
067: throws EdenUserNotFoundException {
068: UserId userIdInterface = null;
069: if (userId instanceof EmplIdVO) {
070: userIdInterface = new EmplId(((EmplIdVO) userId)
071: .getEmplId());
072: } else if (userId instanceof NetworkIdVO) {
073: userIdInterface = new AuthenticationUserId(
074: ((NetworkIdVO) userId).getNetworkId());
075: } else if (userId instanceof UuIdVO) {
076: userIdInterface = new UuId(((UuIdVO) userId).getUuId());
077: } else if (userId instanceof WorkflowIdVO) {
078: userIdInterface = new WorkflowUserId(
079: ((WorkflowIdVO) userId).getWorkflowId());
080: } else {
081: throw new EdenUserNotFoundException(
082: "Attempting to fetch user with unknown id type");
083: }
084: return getWorkflowUser(userIdInterface);
085: }
086:
087: public WorkflowUser getWorkflowUser(UserId userId)
088: throws EdenUserNotFoundException {
089: WorkflowUser user = getFromCache(userId);
090: if (user == null) {
091: user = (WorkflowUser) userDao.getWorkflowUser(userId);
092: if (user == null) {
093: throw new EdenUserNotFoundException(
094: "User is invalid. userId " + userId.toString());
095: } else {
096: addToCache(user);
097: }
098: }
099: return user;
100: }
101:
102: public WorkflowUser getBlankUser() {
103: return new BaseWorkflowUser();
104: }
105:
106: public List<WorkflowUser> search(WorkflowUser user,
107: boolean usesWildCard) {
108: return userDao.getSearchResults(user.getLastName(), user
109: .getGivenName(), user.getAuthenticationUserId()
110: .getAuthenticationId(), user.getWorkflowUserId()
111: .getWorkflowId(), user.getEmplId().getEmplId(), user
112: .getUuId().getUuId());
113: }
114:
115: public void save(WorkflowUser user) {
116: BaseWorkflowUser simpleUser = (BaseWorkflowUser) user;
117: Timestamp currentTimestamp = new Timestamp(System
118: .currentTimeMillis());
119: if (user.getWorkflowId() == null
120: || simpleUser.getCreateDate() == null) {
121: simpleUser.setCreateDate(currentTimestamp);
122: } else {
123: removeFromCache(user.getWorkflowUserId());
124: }
125: simpleUser.setLastUpdateDate(currentTimestamp);
126: userDao.save(simpleUser);
127: }
128:
129: public WorkflowUser copy(WorkflowUser user, boolean preserveKeys) {
130: BaseWorkflowUser original = (BaseWorkflowUser) user;
131: BaseWorkflowUser userCopy = (BaseWorkflowUser) getBlankUser();
132: if (preserveKeys) {
133: userCopy.setWorkflowUserId(user.getWorkflowUserId());
134: userCopy.setLockVerNbr(original.getLockVerNbr());
135: }
136: userCopy.setAuthenticationUserId(original
137: .getAuthenticationUserId());
138: userCopy.setDisplayName(original.getDisplayName());
139: userCopy.setEmailAddress(original.getEmailAddress());
140: userCopy.setEmplId(original.getEmplId());
141: userCopy.setGivenName(original.getGivenName());
142: userCopy.setLastName(original.getLastName());
143: userCopy.setUuId(original.getUuId());
144: userCopy.setCreateDate(original.getCreateDate());
145: userCopy.setLastUpdateDate(original.getLastUpdateDate());
146: return userCopy;
147: }
148:
149: public void loadXml(InputStream stream, WorkflowUser user) {
150: try {
151: List parsedUsers = new UserXmlHandler().parseUserEntries(
152: this , stream);
153: for (Iterator iter = parsedUsers.iterator(); iter.hasNext();) {
154: save((BaseWorkflowUser) iter.next());
155: }
156: } catch (Exception e) {
157: if (e instanceof RuntimeException) {
158: throw (RuntimeException) e;
159: }
160: throw new RuntimeException(
161: "Caught Exception parsing user xml.", e);
162: }
163: }
164:
165: /**
166: * Add the given user to the cache.
167: */
168: protected void addToCache(WorkflowUser user) {
169: getCache().addToCache(user);
170: }
171:
172: /**
173: * Remove the user with the given id from the cache.
174: */
175: protected void removeFromCache(UserId userId) {
176: getCache().removeFromCache(userId);
177: }
178:
179: /**
180: * Retrieve the user with the given id from the cache. Returns null if there is no
181: * user in the cache with the given id.
182: */
183: protected WorkflowUser getFromCache(UserId userId) {
184: return getCache().getFromCache(userId);
185: }
186:
187: /**
188: * Return the UserCache in which the users are being cached.
189: */
190: protected UserCache getCache() {
191: return cache;
192: }
193:
194: protected BaseUserDAO getUserDAO() {
195: return userDao;
196: }
197:
198: /**
199: * A helper class which manages caching of users by various id types and retrieval of users by those id types.
200: *
201: * @author Eric Westfall
202: */
203: protected class UserCache {
204:
205: private Map authenticationIdMap = new HashMap();
206: private Map uuIdMap = new HashMap();
207: private Map emplIdMap = new HashMap();
208: private Map workflowIdMap = new HashMap();
209:
210: public void addToCache(WorkflowUser user) {
211: workflowIdMap.put(user.getWorkflowUserId(), user);
212: authenticationIdMap.put(user.getAuthenticationUserId(),
213: user);
214: if (user.getUuId() != null
215: && !StringUtils.isEmpty(user.getUuId().getUuId())) {
216: uuIdMap.put(user.getUuId(), user);
217: }
218: if (user.getEmplId() != null
219: && !StringUtils.isEmpty(user.getEmplId()
220: .getEmplId())) {
221: emplIdMap.put(user.getEmplId(), user);
222: }
223: }
224:
225: public void removeFromCache(UserId userId) {
226: WorkflowUser user = getFromCache(userId);
227: if (user != null) {
228: workflowIdMap.remove(user.getWorkflowUserId());
229: authenticationIdMap.remove(user
230: .getAuthenticationUserId());
231: if (user.getUuId() != null
232: && !StringUtils.isEmpty(user.getUuId()
233: .getUuId())) {
234: uuIdMap.remove(user.getUuId());
235: }
236: if (user.getEmplId() != null
237: && !StringUtils.isEmpty(user.getEmplId()
238: .getEmplId())) {
239: emplIdMap.remove(user.getEmplId());
240: }
241: }
242: }
243:
244: public WorkflowUser getFromCache(UserId userId) {
245: WorkflowUser user = null;
246: if (userId instanceof WorkflowUserId) {
247: user = (WorkflowUser) workflowIdMap.get(userId);
248: }
249: if (user == null && userId instanceof AuthenticationUserId) {
250: user = (WorkflowUser) authenticationIdMap.get(userId);
251: }
252: if (user == null && userId instanceof UuId) {
253: user = (WorkflowUser) uuIdMap.get(userId);
254: }
255: if (user == null && userId instanceof EmplId) {
256: user = (WorkflowUser) emplIdMap.get(userId);
257: }
258: return user;
259: }
260:
261: }
262: }
|