001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Admin/Sources/es/udc/mypersonalizer/admin/http/controller/actions/serviceperms/UpdateServicePermissionsAction.java,v 1.1.1.1 2004/03/25 12:08:38 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:38 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.admin.http.controller.actions.serviceperms;
042:
043: import java.util.Collection;
044: import java.util.Iterator;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050:
051: import org.apache.struts.action.ActionMapping;
052: import org.apache.struts.action.ActionForm;
053: import org.apache.struts.action.ActionForward;
054:
055: import es.udc.mypersonalizer.kernel.model.actions.ActionProcessorSingleton;
056: import es.udc.mypersonalizer.kernel.controller.actions.DefaultAction;
057: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserGroup;
058: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePermissions;
059: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceButtonPermission;
060:
061: import es.udc.mypersonalizer.admin.model.types.serviceperms.FindServicePermissionsResult;
062: import es.udc.mypersonalizer.admin.http.view.viewobjects.serviceperms.ViewServicePermissions;
063: import es.udc.mypersonalizer.admin.http.view.viewobjects.serviceperms.ViewResourcePermissions;
064: import es.udc.mypersonalizer.admin.http.view.viewobjects.serviceperms.ViewGroupPermission;
065: import es.udc.mypersonalizer.admin.http.view.actionforms.serviceperms.UpdateServicePermissionsForm;
066:
067: import es.udc.mypersonalizer.portal.config.PortalConfigManager;
068:
069: /**
070: * This action retrieves the current permissions for a given service, then,
071: * forwards to <code>UpdateServicePermissionsJSP</code>. For this task it
072: * retrieves its parameters from {@link UpdateServicePermissionsForm}.
073: *
074: * @author Abel Iago Toral Quiroga
075: * @since 1.0
076: */
077: public class UpdateServicePermissionsAction extends DefaultAction {
078:
079: protected ActionForward doExecute(ActionMapping mapping,
080: ActionForm form, HttpServletRequest request,
081: HttpServletResponse response) throws Exception {
082:
083: /* Get parameters */
084: UpdateServicePermissionsForm frm = (UpdateServicePermissionsForm) form;
085: String serviceIdentifier = frm.getServiceIdentifier();
086:
087: /* Get permissions for the given service */
088: FindServicePermissionsResult servicePermissionsResult = (FindServicePermissionsResult) ActionProcessorSingleton
089: .getInstance().execute("FindServicePermissionsAction",
090: serviceIdentifier);
091:
092: /* Create view-objects for the view */
093: ServicePermissions servicePermissions = servicePermissionsResult
094: .getServicePermissions();
095: Collection allUserGroups = servicePermissionsResult
096: .getAllGroups();
097:
098: /* 1.- Create a view-object for the service availability */
099: ViewResourcePermissions viewServiceAvailability = createResourcePermissionsViewObject(
100: serviceIdentifier, servicePermissions
101: .getAvailableToGroupIdentifiers(),
102: allUserGroups);
103:
104: /* 2.- Create a view-object for each service button availability */
105: Iterator iterator = PortalConfigManager.getConfig()
106: .getPortalModelConfig().getServiceButtonsConfig()
107: .getServiceButtonNames().iterator();
108:
109: Collection viewServiceButtonsAvailability = new ArrayList();
110: while (iterator.hasNext()) {
111:
112: String buttonType = (String) iterator.next();
113: ServiceButtonPermission buttonPermissions = (ServiceButtonPermission) servicePermissions
114: .getButtonPermissions().get(buttonType);
115: if (buttonPermissions == null) {
116: buttonPermissions = new ServiceButtonPermission(
117: buttonType, Arrays.asList(new Long[0]));
118: }
119:
120: ViewResourcePermissions viewButtonPermissions = createResourcePermissionsViewObject(
121: buttonPermissions.getServiceButtonType(),
122: buttonPermissions.getAllowedUserGroupIdentifiers(),
123: allUserGroups);
124:
125: viewServiceButtonsAvailability.add(viewButtonPermissions);
126: }
127:
128: /* 3.- Create a unique view-object with all the needed information
129: * for the view */
130: ViewServicePermissions viewServicePermissions = new ViewServicePermissions(
131: serviceIdentifier, viewServiceAvailability,
132: viewServiceButtonsAvailability);
133:
134: /* Do forward */
135: request.setAttribute("servicePermissions",
136: viewServicePermissions);
137:
138: return mapping.findForward("UpdateServicePermissionsJSP");
139: }
140:
141: /**
142: * Creates a <code>ViewResourcePermissions</code> object with the
143: * resource availability information needed for the JSP page.
144: * @param resource is the name of the resource (the service identifier,
145: * the button type, ...)
146: * @param availableToGroupIdentifiers the user groups identifiers to
147: * which the resource is available.
148: * @param allUserGroups all the user groups.
149: * @return the permissions for the resource in the format needed by the
150: * JSP view.
151: */
152: private ViewResourcePermissions createResourcePermissionsViewObject(
153: String resource, Collection availableToGroupIdentifiers,
154: Collection allUserGroups) {
155:
156: Collection availableTo = new ArrayList();
157:
158: Iterator iterator = allUserGroups.iterator();
159: while (iterator.hasNext()) {
160:
161: UserGroup userGroup = (UserGroup) iterator.next();
162: Long userGroupIdentifier = userGroup
163: .getUserGroupIdentifier();
164: String userGroupName = userGroup.getName();
165: boolean hasPermission;
166: if (availableToGroupIdentifiers
167: .contains(userGroupIdentifier)) {
168: hasPermission = true;
169: } else {
170: hasPermission = false;
171: }
172:
173: ViewGroupPermission groupPermission = new ViewGroupPermission(
174: userGroupIdentifier, userGroupName, hasPermission);
175: availableTo.add(groupPermission);
176: }
177:
178: return (new ViewResourcePermissions(resource, availableTo));
179: }
180: }
|