| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
package org.demosys.web.user.impl; |
| 7 |
|
import java.lang.reflect.InvocationTargetException; |
| 8 |
|
import java.util.ArrayList; |
| 9 |
|
import java.util.HashMap; |
| 10 |
|
import java.util.Map; |
| 11 |
|
import org.apache.commons.beanutils.BeanUtils; |
| 12 |
|
import org.apache.struts.action.ActionErrors; |
| 13 |
|
import org.demosys.web.user.*; |
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
68 |
public class MemoryUserService implements UserService { |
| 18 |
68 |
private Map memory = new HashMap(); |
| 19 |
|
|
| 20 |
|
public synchronized void newUser(UserView user) |
| 21 |
|
throws InvalidUserException, DuplicateUserException { |
| 22 |
80 |
user.control(); |
| 23 |
|
|
| 24 |
60 |
if (memory.containsKey(user.getId())) { |
| 25 |
8 |
throw new DuplicateUserException(); |
| 26 |
|
} |
| 27 |
|
|
| 28 |
52 |
memory.put(user.getId(), user.clone()); |
| 29 |
52 |
} |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public synchronized void updateUser(UserView user) |
| 33 |
|
throws UnknownUserException, InvalidUserException { |
| 34 |
12 |
user.control(); |
| 35 |
|
|
| 36 |
8 |
UserView storedView = retrieveUser(user.getId()); |
| 37 |
|
|
| 38 |
4 |
updateUserBean(storedView, user); |
| 39 |
4 |
} |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
public synchronized void deleteUser(String id) |
| 43 |
|
throws UnknownUserException { |
| 44 |
16 |
if (!memory.containsKey(id)) { |
| 45 |
8 |
throw new UnknownUserException(); |
| 46 |
|
} |
| 47 |
8 |
memory.remove(id); |
| 48 |
8 |
} |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
public synchronized UserView getUser(String id) |
| 52 |
|
throws UnknownUserException { |
| 53 |
56 |
UserView userView = retrieveUser(id); |
| 54 |
|
|
| 55 |
36 |
return (UserView)userView.clone(); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
public synchronized UserViewList getAllUsers() { |
| 60 |
12 |
UserViewList list = new UserViewList(); |
| 61 |
12 |
list.setUsers(new ArrayList(memory.values())); |
| 62 |
12 |
return list; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
private UserView retrieveUser(String id) throws UnknownUserException { |
| 67 |
64 |
UserView userView = (UserView)memory.get(id); |
| 68 |
|
|
| 69 |
64 |
if (userView == null) { |
| 70 |
24 |
throw new UnknownUserException(id); |
| 71 |
|
} |
| 72 |
40 |
return userView; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
|
private void updateUserBean(UserView storedView, UserView user) { |
| 77 |
|
try { |
| 78 |
4 |
BeanUtils.copyProperties(storedView, user); |
| 79 |
4 |
} |
| 80 |
|
catch (IllegalAccessException e) { |
| 81 |
0 |
e.printStackTrace(); |
| 82 |
|
|
| 83 |
0 |
} |
| 84 |
|
catch (InvocationTargetException e) { |
| 85 |
0 |
e.printStackTrace(); |
| 86 |
|
|
| 87 |
0 |
} |
| 88 |
4 |
} |
| 89 |
|
} |