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