1 /*
2 * Demosys.
3 *
4 * Terms of license - http://opensource.org/licenses/apachepl.php
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 * Implementation of a UserService in memory.
16 */
17 public class MemoryUserService implements UserService {
18 private Map memory = new HashMap();
19
20 public synchronized void newUser(UserView user)
21 throws InvalidUserException, DuplicateUserException {
22 user.control();
23
24 if (memory.containsKey(user.getId())) {
25 throw new DuplicateUserException();
26 }
27
28 memory.put(user.getId(), user.clone());
29 }
30
31
32 public synchronized void updateUser(UserView user)
33 throws UnknownUserException, InvalidUserException {
34 user.control();
35
36 UserView storedView = retrieveUser(user.getId());
37
38 updateUserBean(storedView, user);
39 }
40
41
42 public synchronized void deleteUser(String id)
43 throws UnknownUserException {
44 if (!memory.containsKey(id)) {
45 throw new UnknownUserException();
46 }
47 memory.remove(id);
48 }
49
50
51 public synchronized UserView getUser(String id)
52 throws UnknownUserException {
53 UserView userView = retrieveUser(id);
54
55 return (UserView)userView.clone();
56 }
57
58
59 public synchronized UserViewList getAllUsers() {
60 UserViewList list = new UserViewList();
61 list.setUsers(new ArrayList(memory.values()));
62 return list;
63 }
64
65
66 private UserView retrieveUser(String id) throws UnknownUserException {
67 UserView userView = (UserView)memory.get(id);
68
69 if (userView == null) {
70 throw new UnknownUserException(id);
71 }
72 return userView;
73 }
74
75
76 private void updateUserBean(UserView storedView, UserView user) {
77 try {
78 BeanUtils.copyProperties(storedView, user);
79 }
80 catch (IllegalAccessException e) {
81 e.printStackTrace();
82 // Impossible
83 }
84 catch (InvocationTargetException e) {
85 e.printStackTrace();
86 // Impossible
87 }
88 }
89 }
This page was automatically generated by Maven