001: package org.manentia.kasai.ui.roles;
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.Role;
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: import org.manentia.kasai.ui.groups.GroupFacade;
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 RoleFacade {
041: private String lastSortOrder = null;
042:
043: public void createNewRole(String name, String description,
044: String[] operatives) throws SessionExpiredException,
045: DataAccessException, AlreadyExistsException,
046: InvalidAttributesException, DoesntExistsException,
047: NotEnoughPermissionException, CannotAuditException,
048: CriticalException, InvalidPasswordException {
049: WebContext ctx = WebContextFactory.get();
050: HttpServletRequest request = ctx.getHttpServletRequest();
051:
052: UserView userView = new BaseAction().validateSession(request);
053:
054: KasaiFacade.getInstance().createRole(userView.getUser(), name,
055: description, operatives, request.getRemoteAddr());
056: }
057:
058: public void modifyRole(int idRole, String name, String description,
059: String[] operatives) throws SessionExpiredException,
060: DataAccessException, AlreadyExistsException,
061: InvalidAttributesException, DoesntExistsException,
062: NotEnoughPermissionException, CannotAuditException,
063: CriticalException {
064: WebContext ctx = WebContextFactory.get();
065: HttpServletRequest request = ctx.getHttpServletRequest();
066:
067: UserView userView = new BaseAction().validateSession(request);
068:
069: KasaiFacade.getInstance().modifyRole(userView.getUser(),
070: idRole, name, description, operatives,
071: request.getRemoteAddr());
072: }
073:
074: public void deleteRole(int idRole) throws SessionExpiredException,
075: DataAccessException, InvalidAttributesException,
076: NotEnoughPermissionException, DoesntExistsException,
077: CannotAuditException, ServiceException,
078: ServiceNotAvailableException {
079: WebContext ctx = WebContextFactory.get();
080: HttpServletRequest request = ctx.getHttpServletRequest();
081:
082: UserView userView = new BaseAction().validateSession(request);
083:
084: KasaiFacade.getInstance().deleteRole(userView.getUser(),
085: idRole, request.getRemoteAddr());
086: }
087:
088: public Map refresh(int page, String name)
089: throws SessionExpiredException, DataAccessException,
090: XMLException {
091: WebContext ctx = WebContextFactory.get();
092: HttpServletRequest request = ctx.getHttpServletRequest();
093:
094: UserView userView = new BaseAction().validateSession(request);
095:
096: Collection roles = KasaiFacade.getInstance().listRoles(
097: userView.getUser(), name);
098:
099: request.getSession().setAttribute("roles", roles);
100:
101: return goToPageRoles(page);
102: }
103:
104: public Role readRole(int idRole)
105: throws NotEnoughPermissionException, CannotAuditException,
106: DataAccessException, DoesntExistsException,
107: SessionExpiredException, XMLException {
108: WebContext ctx = WebContextFactory.get();
109: HttpServletRequest request = ctx.getHttpServletRequest();
110:
111: UserView userView = new BaseAction().validateSession(request);
112:
113: Role result = KasaiFacade.getInstance().readRole(
114: userView.getUser(), idRole);
115: result.setOperatives(KasaiFacade.getInstance()
116: .listOperativesFromRole(userView.getUser(), idRole));
117:
118: return result;
119: }
120:
121: public Map goToPageRoles(int page) {
122: ArrayList<Role> result = null;
123: WebContext ctx = WebContextFactory.get();
124: HttpServletRequest request = ctx.getHttpServletRequest();
125:
126: Map<String, Object> map = new HashMap<String, Object>();
127:
128: Collection roles = (List) request.getSession().getAttribute(
129: "roles");
130: int rowsPerPage = ((Integer) request.getSession().getAttribute(
131: "rowsPerPage")).intValue();
132:
133: result = new ArrayList<Role>(rowsPerPage);
134: int index = 0;
135: for (Iterator iter = roles.iterator(); iter.hasNext()
136: && index < rowsPerPage * page;) {
137: Role role = (Role) iter.next();
138: if (index >= rowsPerPage * (page - 1)
139: && index < rowsPerPage * page) {
140: result.add(role);
141: }
142: index++;
143: }
144:
145: map.put("list", result);
146: map.put("totalSize", new Integer(roles.size()));
147:
148: return map;
149: }
150:
151: public Map sortRoles(String sortOrder) {
152: WebContext ctx = WebContextFactory.get();
153: HttpServletRequest request = ctx.getHttpServletRequest();
154:
155: if (StringUtils.isEmpty(sortOrder)) {
156: sortOrder = "id";
157: }
158:
159: boolean reverse = sortOrder.equalsIgnoreCase(lastSortOrder);
160: Comparator comparator = new BeanComparator(sortOrder);
161:
162: if (reverse) {
163: comparator = new ReverseComparator(comparator);
164: }
165:
166: List roles = (List) request.getSession().getAttribute("roles");
167: Collections.sort(roles, comparator);
168:
169: lastSortOrder = reverse ? "__CHANGE__" : sortOrder;
170:
171: return goToPageRoles(1);
172: }
173:
174: }
|