001: package org.manentia.kasai.ui.users;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Comparator;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011:
012: import javax.servlet.http.HttpServletRequest;
013:
014: import org.apache.commons.beanutils.BeanComparator;
015: import org.apache.commons.collections.comparators.ReverseComparator;
016: import org.apache.commons.lang.StringUtils;
017: import org.manentia.kasai.KasaiFacade;
018: import org.manentia.kasai.User;
019: import org.manentia.kasai.exceptions.AlreadyExistsException;
020: import org.manentia.kasai.exceptions.CannotAuditException;
021: import org.manentia.kasai.exceptions.DataAccessException;
022: import org.manentia.kasai.exceptions.DoesntExistsException;
023: import org.manentia.kasai.exceptions.InvalidAttributesException;
024: import org.manentia.kasai.exceptions.InvalidPasswordException;
025: import org.manentia.kasai.exceptions.NotEnoughPermissionException;
026: import org.manentia.kasai.exceptions.ServiceException;
027: import org.manentia.kasai.exceptions.ServiceNotAvailableException;
028: import org.manentia.kasai.ui.UserView;
029: import org.manentia.kasai.ui.actions.BaseAction;
030: import org.manentia.kasai.ui.exceptions.SessionExpiredException;
031:
032: import com.manentia.commons.CriticalException;
033: import com.manentia.commons.xml.XMLException;
034:
035: import uk.ltd.getahead.dwr.WebContext;
036: import uk.ltd.getahead.dwr.WebContextFactory;
037:
038: public class UserFacade {
039: private String lastSortOrder = null;
040:
041: public void createNewUser(String login, String firstName,
042: String lastName, String email, boolean blocked,
043: String description, boolean super User)
044: throws SessionExpiredException, DataAccessException,
045: AlreadyExistsException, InvalidAttributesException,
046: DoesntExistsException, NotEnoughPermissionException,
047: CannotAuditException, CriticalException,
048: InvalidPasswordException {
049: WebContext ctx = WebContextFactory.get();
050: HttpServletRequest request = ctx.getHttpServletRequest();
051:
052: UserView userView = new BaseAction().validateSession(request);
053:
054: KasaiFacade.getInstance().createUser(userView.getUser(), login,
055: firstName, lastName, email, blocked, description,
056: super User, request.getRemoteAddr());
057: }
058:
059: public void modifyUser(String login, String firstName,
060: String lastName, String email, boolean blocked,
061: String description, boolean super User)
062: throws SessionExpiredException, DataAccessException,
063: AlreadyExistsException, InvalidAttributesException,
064: DoesntExistsException, NotEnoughPermissionException,
065: CannotAuditException, CriticalException {
066: WebContext ctx = WebContextFactory.get();
067: HttpServletRequest request = ctx.getHttpServletRequest();
068:
069: UserView userView = new BaseAction().validateSession(request);
070:
071: KasaiFacade.getInstance().updateUser(userView.getUser(), login,
072: firstName, lastName, email, blocked, description,
073: super User, request.getRemoteAddr());
074: }
075:
076: public void block(String login) throws SessionExpiredException,
077: DataAccessException, InvalidAttributesException,
078: NotEnoughPermissionException, DoesntExistsException,
079: CannotAuditException, XMLException {
080: WebContext ctx = WebContextFactory.get();
081: HttpServletRequest request = ctx.getHttpServletRequest();
082:
083: UserView userView = new BaseAction().validateSession(request);
084:
085: KasaiFacade.getInstance().blockUser(userView.getUser(), login,
086: request.getRemoteAddr());
087: }
088:
089: public void newPassword(String login)
090: throws SessionExpiredException, DataAccessException,
091: InvalidAttributesException, NotEnoughPermissionException,
092: DoesntExistsException, CannotAuditException,
093: ServiceException, ServiceNotAvailableException,
094: XMLException {
095: WebContext ctx = WebContextFactory.get();
096: HttpServletRequest request = ctx.getHttpServletRequest();
097:
098: UserView userView = new BaseAction().validateSession(request);
099:
100: KasaiFacade.getInstance().resetPasswordUser(userView.getUser(),
101: login, request.getRemoteAddr());
102: }
103:
104: public void deleteUser(String login)
105: throws SessionExpiredException, DataAccessException,
106: InvalidAttributesException, NotEnoughPermissionException,
107: DoesntExistsException, CannotAuditException,
108: ServiceException, ServiceNotAvailableException {
109: WebContext ctx = WebContextFactory.get();
110: HttpServletRequest request = ctx.getHttpServletRequest();
111:
112: UserView userView = new BaseAction().validateSession(request);
113:
114: KasaiFacade.getInstance().deleteUser(userView.getUser(), login,
115: request.getRemoteAddr());
116: }
117:
118: public void unblock(String login) throws SessionExpiredException,
119: DataAccessException, InvalidAttributesException,
120: NotEnoughPermissionException, DoesntExistsException,
121: CannotAuditException, XMLException {
122: WebContext ctx = WebContextFactory.get();
123: HttpServletRequest request = ctx.getHttpServletRequest();
124:
125: UserView userView = new BaseAction().validateSession(request);
126:
127: KasaiFacade.getInstance().unblockUser(userView.getUser(),
128: login, request.getRemoteAddr());
129: }
130:
131: public Map refresh(int page, String login, String firstName,
132: String lastName, String email, String description,
133: String group) throws SessionExpiredException,
134: DataAccessException, XMLException {
135: WebContext ctx = WebContextFactory.get();
136: HttpServletRequest request = ctx.getHttpServletRequest();
137:
138: UserView userView = new BaseAction().validateSession(request);
139:
140: Collection users = KasaiFacade.getInstance().listUsers(
141: userView.getUser(), login, firstName, lastName, email,
142: -1, description, group);
143:
144: request.getSession().setAttribute("users", users);
145:
146: return goToPageUsers(page);
147: }
148:
149: public User readUser(String login)
150: throws NotEnoughPermissionException, CannotAuditException,
151: DataAccessException, DoesntExistsException,
152: SessionExpiredException, XMLException {
153: WebContext ctx = WebContextFactory.get();
154: HttpServletRequest request = ctx.getHttpServletRequest();
155:
156: UserView userView = new BaseAction().validateSession(request);
157:
158: User result = KasaiFacade.getInstance().readUser(
159: userView.getUser(), login, request.getRemoteAddr());
160:
161: return result;
162: }
163:
164: public Map goToPageUsers(int page) {
165: ArrayList result = null;
166: WebContext ctx = WebContextFactory.get();
167: HttpServletRequest request = ctx.getHttpServletRequest();
168:
169: Map map = new HashMap();
170:
171: Collection users = (List) request.getSession().getAttribute(
172: "users");
173: int rowsPerPage = ((Integer) request.getSession().getAttribute(
174: "rowsPerPage")).intValue();
175:
176: result = new ArrayList(rowsPerPage);
177: int index = 0;
178: for (Iterator iter = users.iterator(); iter.hasNext()
179: && index < rowsPerPage * page;) {
180: User user = (User) iter.next();
181: if (index >= rowsPerPage * (page - 1)
182: && index < rowsPerPage * page) {
183: result.add(user);
184: }
185: index++;
186: }
187:
188: map.put("list", result);
189: map.put("totalSize", new Integer(users.size()));
190:
191: return map;
192: }
193:
194: public Map sortUsers(String sortOrder) {
195: WebContext ctx = WebContextFactory.get();
196: HttpServletRequest request = ctx.getHttpServletRequest();
197:
198: if (StringUtils.isEmpty(sortOrder)) {
199: sortOrder = "login";
200: }
201:
202: boolean reverse = sortOrder.equalsIgnoreCase(lastSortOrder);
203: Comparator comparator = new BeanComparator(sortOrder);
204:
205: if (reverse) {
206: comparator = new ReverseComparator(comparator);
207: }
208:
209: List users = (List) request.getSession().getAttribute("users");
210: Collections.sort(users, comparator);
211:
212: lastSortOrder = reverse ? "__CHANGE__" : sortOrder;
213:
214: return goToPageUsers(1);
215: }
216:
217: }
|