1 16 package com.blandware.atleap.service.core.impl; 17 18 import com.blandware.atleap.common.util.PartialCollection; 19 import com.blandware.atleap.common.util.QueryInfo; 20 import com.blandware.atleap.common.util.RandomGUID; 21 import com.blandware.atleap.common.util.StringUtil; 22 import com.blandware.atleap.model.core.User; 23 import com.blandware.atleap.model.core.UserCookie; 24 import com.blandware.atleap.persistence.core.UserDAO; 25 import com.blandware.atleap.service.core.UserManager; 26 import com.blandware.atleap.service.exception.BeanAlreadyExistsException; 27 import com.blandware.atleap.service.exception.BeanNotFoundException; 28 import org.apache.commons.lang.StringUtils; 29 30 31 41 public class UserManagerImpl extends BaseManagerImpl implements UserManager { 42 43 46 protected UserDAO userDAO; 47 48 51 public void setUserDAO(UserDAO userDAO) { 52 this.userDAO = userDAO; 53 } 54 55 57 60 public void createUser(User user) throws BeanAlreadyExistsException { 61 62 if ( log.isDebugEnabled() ) { 63 log.debug("Creating new user (name=" + user.getName() + ")..."); 64 } 65 66 User tmp = userDAO.retrieveUser(user.getName()); 67 if ( tmp != null ) { 68 String errorMessage = "User with name '" + user.getName() + "' already exists"; 70 if ( log.isErrorEnabled() ) { 71 log.error(errorMessage); 72 } 73 throw new BeanAlreadyExistsException(errorMessage); 74 } 75 76 userDAO.createUser(user); 78 79 if ( log.isDebugEnabled() ) { 80 log.debug("New user has been created succefully."); 81 } 82 } 83 84 87 public User retrieveUser(String userName) { 88 User user = null; 89 user = userDAO.retrieveUser(userName); 90 return user; 91 } 92 93 96 public void updateUser(User user) { 97 98 userDAO.removeFromCache(user); 100 if ( log.isDebugEnabled() ) { 101 log.debug("Updating user (name=" + user.getName() + ")..."); 102 } 103 104 userDAO.updateUser(user); 105 106 if ( log.isDebugEnabled() ) { 107 log.debug("User was updated succefully."); 108 } 109 } 110 111 114 public void deleteUser(String userName) throws BeanNotFoundException { 115 User user = userDAO.retrieveUser(userName); 116 if ( user == null ) { 117 String errorMessage = "No user with name=" + userName + " could be found"; 118 throw new BeanNotFoundException(errorMessage); 119 } 120 121 userDAO.deleteUser(user); 122 123 if ( log.isDebugEnabled() ) { 124 log.debug("Deleted user with name=" + userName); 125 } 126 } 127 128 130 133 public PartialCollection listUsers(QueryInfo queryInfo) { 134 return userDAO.listUsers(queryInfo); 135 } 136 137 140 public String checkUserCookie(String value) throws BeanNotFoundException { 141 value = StringUtil.decodeString(value); 142 143 String [] values = StringUtils.split(value, "|"); 144 String userName = values[0]; 145 String cookieId = values[1]; 146 147 148 if ( log.isDebugEnabled() ) { 149 log.debug("looking up cookieId: " + cookieId); 150 } 151 152 UserCookie userCookie = null; 153 userCookie = userDAO.retrieveUserCookie(cookieId); 154 155 if ( userCookie != null ) { 156 if ( log.isDebugEnabled() ) { 157 log.debug("cookieId lookup succeeded, generating new cookieId"); 158 } 159 return createUserCookie(userCookie, userName); 160 } else { 161 if ( log.isDebugEnabled() ) { 162 log.debug("cookieId lookup failed, returning null"); 163 } 164 165 return null; 166 } 167 } 168 169 172 public String createUserCookie(UserCookie userCookie, String userName) throws BeanNotFoundException { 173 User user = null; 174 user = userDAO.retrieveUser(userName); 175 176 if ( user == null ) { 177 String errorMessage = "User with name '" + userName + " could not be found"; 178 if ( log.isDebugEnabled() ) { 179 log.debug(errorMessage); 180 } 181 throw new BeanNotFoundException(errorMessage); 182 } 183 184 userCookie.setCookieId(new RandomGUID().toString()); 185 userDAO.createUserCookie(userCookie, user); 186 return StringUtil.encodeString(user.getName() + "|" + 187 userCookie.getCookieId()); 188 } 189 190 193 public void deleteUserCookies(String username) { 194 userDAO.deleteUserCookies(username); 195 } 196 197 } 198 | Popular Tags |