001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/actions/content/RemoveServiceFromWorkspaceAction.java,v 1.1.1.1 2004/03/25 12:08:40 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:40 $
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.portal.controller.actions.content;
042:
043: import es.udc.mypersonalizer.kernel.controller.actions.DefaultAction;
044: import es.udc.mypersonalizer.kernel.model.actions.ActionProcessorSingleton;
045: import es.udc.mypersonalizer.kernel.model.editors.EditorFactory;
046: import es.udc.mypersonalizer.kernel.model.editors.ServiceKeys;
047: import es.udc.mypersonalizer.kernel.model.editors.WorkspaceLayoutEditor;
048: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyKey;
049: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayout;
050: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
051: import es.udc.mypersonalizer.portal.controller.session.KeyChecker;
052: import es.udc.mypersonalizer.portal.controller.session.SessionManager;
053: import es.udc.mypersonalizer.portal.conventions.RequestParameterConventions;
054: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
055: import es.udc.mypersonalizer.portal.model.types.content.ContentEvent;
056: import es.udc.mypersonalizer.portal.model.types.content.ContentResult;
057: import java.util.*;
058: import javax.servlet.http.HttpServletRequest;
059: import javax.servlet.http.HttpServletResponse;
060: import org.apache.commons.beanutils.PropertyUtils;
061: import org.apache.struts.action.ActionErrors;
062: import org.apache.struts.action.ActionForm;
063: import org.apache.struts.action.ActionForward;
064: import org.apache.struts.action.ActionMapping;
065:
066: /**
067: * This action removes a service from the user's current workspace. It calls
068: * the model action called <tt>ChangeWorkspaceContentAction</tt>, which must be
069: * registered in configuration. It also sets the new workspace layout
070: * into the session.<p>
071: *
072: * It receives as a parameter a struts action form which must have been
073: * populated with the following data:
074: * <ul>
075: * <li><tt>serviceIdentifier</tt>: The identifier of the service to be
076: * removed.</li>
077: * <li><tt>propertyIdentifier</tt>: The service property identifier being
078: * removed.</li>
079: * </ul>
080: *
081: * This action requires a <tt>Success</tt> forward, which will usually point
082: * to the portal main page.<p>
083: *
084: * @author Daniel Fernandez
085: * @since 1.0
086: */
087: public class RemoveServiceFromWorkspaceAction extends DefaultAction {
088:
089: /**
090: * Name of the model action for changing the workspace contents
091: * This is "ChangeWorkspaceContentAction".
092: */
093: private static final String CHANGE_WORKSPACE_CONTENT_ACTION = "ChangeWorkspaceContentAction";
094:
095: protected ActionForward doExecute(ActionMapping mapping,
096: ActionForm form, HttpServletRequest request,
097: HttpServletResponse response) throws Exception {
098:
099: ActionErrors errors = new ActionErrors();
100:
101: /*
102: * If there are no workspaces, this action cannot be executed.
103: */
104: if (!SessionManager.getUserHasWorkspaces(request)) {
105: throw new NoActiveWorkspacesException(
106: "Cannot remove service");
107: }
108:
109: ActionProcessorSingleton actionProcessorSingleton = ActionProcessorSingleton
110: .getInstance();
111:
112: UserCredentials userCredentials = SessionManager
113: .getUserCredentials(request);
114:
115: /* We get the action parameters from the action form */
116: String serviceIdentifier = (String) PropertyUtils
117: .getSimpleProperty(form,
118: RequestParameterConventions.SERVICE_IDENTIFIER);
119: String propertyIdentifierStr = (String) PropertyUtils
120: .getSimpleProperty(form,
121: RequestParameterConventions.PROPERTY_IDENTIFIER);
122:
123: Long propertyIdentifier = new Long(propertyIdentifierStr);
124:
125: /* We build a ServicePropertyKey object. */
126: ServicePropertyKey servicePropertyKey = new ServicePropertyKey(
127: serviceIdentifier, propertyIdentifier);
128:
129: /*
130: * We get the current workspace layout.
131: */
132: WorkspaceLayout workspaceLayout = SessionManager
133: .getCurrentWorkspaceLayout(request);
134:
135: /*
136: * We have to check the correctness of the service property key.
137: */
138: if (KeyChecker.isValidServicePropertyKey(request,
139: servicePropertyKey)) {
140:
141: /*
142: * We need an editor for the workspace layout.
143: */
144: EditorFactory editorFactory = EditorFactory.getInstance();
145: WorkspaceLayoutEditor workspaceLayoutEditor = editorFactory
146: .createWorkspaceLayoutEditor(workspaceLayout
147: .getProperty());
148:
149: /*
150: * We get the list of the services which are currently
151: * active in the workspace.
152: */
153: Map serviceKeysMap = workspaceLayoutEditor.getServiceKeys();
154: Collection serviceIdentifiers = new ArrayList();
155: Iterator serviceKeysIter = serviceKeysMap.values()
156: .iterator();
157: while (serviceKeysIter.hasNext()) {
158: List areaServices = (List) serviceKeysIter.next();
159: Iterator servicesIter = areaServices.iterator();
160: while (servicesIter.hasNext()) {
161: ServiceKeys serviceKeys = (ServiceKeys) servicesIter
162: .next();
163: serviceIdentifiers.add(serviceKeys
164: .getServicePropertyKey()
165: .getServiceIdentifier());
166: }
167: }
168:
169: /*
170: * We remove the adequate service from the list
171: */
172: serviceIdentifiers.remove(serviceIdentifier);
173:
174: /*
175: * We build the contentEvent to execute the model action
176: */
177: ContentEvent contentEvent = new ContentEvent(
178: userCredentials, workspaceLayout.getIdentifier(),
179: serviceIdentifiers, workspaceLayoutEditor
180: .getWorkspaceName(), workspaceLayoutEditor
181: .getTimeStamp());
182:
183: /*
184: * We call the model action
185: */
186: ContentResult contentResult = (ContentResult) actionProcessorSingleton
187: .execute(CHANGE_WORKSPACE_CONTENT_ACTION,
188: contentEvent);
189:
190: workspaceLayout = contentResult.getCurrentWorkspaceLayout();
191:
192: Collection serviceButtonsStates = contentResult
193: .getServiceButtonsStates();
194:
195: /* We push the new info into the session */
196: SessionManager.setCurrentWorkspace(request,
197: workspaceLayout, serviceButtonsStates);
198:
199: } else {
200: throw new InstanceNotFoundException(servicePropertyKey,
201: "ServicePropertyKey");
202: }
203:
204: return mapping
205: .findForward(DefaultAction.ACTION_SUCCESS_FORWARD);
206: }
207:
208: }
|