001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/actions/content/RemoveWorkspaceAction.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.DesktopLayoutEditor;
046: import es.udc.mypersonalizer.kernel.model.editors.EditorFactory;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.DesktopLayout;
048: import es.udc.mypersonalizer.kernel.util.exceptions.ActionNotAllowedException;
049: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
050: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
051: import es.udc.mypersonalizer.portal.controller.session.KeyChecker;
052: import es.udc.mypersonalizer.portal.controller.session.SessionIdentifierCookieManagerSingleton;
053: import es.udc.mypersonalizer.portal.controller.session.SessionManager;
054: import es.udc.mypersonalizer.portal.conventions.RequestParameterConventions;
055: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
056: import es.udc.mypersonalizer.portal.model.types.content.RemoveWorkspaceResult;
057: import es.udc.mypersonalizer.portal.model.types.content.WorkspaceEvent;
058: import java.util.*;
059: import javax.servlet.http.HttpServletRequest;
060: import javax.servlet.http.HttpServletResponse;
061: import org.apache.commons.beanutils.PropertyUtils;
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 workspace from the user's desktop. If the
068: * user is <tt>mypersonalizer</tt>, then this removal implies also the removal
069: * of all the workspaces of the same type for all the users. It calls the model
070: * action called <tt>RemoveWorkspaceAction</tt>, which must be registered
071: * in configuration. It also updates the session adequately, including changes
072: * to the default and current layouts (if the current layout is removed,
073: * the user is forwarded to the default one).<p>
074: *
075: * It receives as a parameter a struts action form which must have been
076: * populated with the following data:
077: * <ul>
078: * <li><tt>workspaceIdentifier</tt>: The property identifier of the workspace
079: * being removed.</li>
080: * </ul>
081: *
082: * For error notifications, it asumes the following keys to be declared in the
083: * resources file:
084: * <ul>
085: * <li><tt>ErrorMessages.ChangeDesktopContent.errorRemovingWorkspace</tt>:
086: * which will be displayed if the user tries to remove a workspace for
087: * which he / she has no removal permissions.</li>
088: * </ul>
089: *
090: * This action requires a <tt>Success</tt> forward, which will usually point
091: * to the action which shows the desktop contents to the user to let him/her
092: * change them, which is usully called <tt>ChangeDesktopContent.do</tt>. It
093: * also requires a <tt>Fail</tt> forward, in case there is any error related to
094: * permissions, usually pointing to the action which displays the desktop
095: * contents. This is the same as <tt>Success</tt>.
096: *
097: *
098: * @author Daniel Fernandez
099: * @since 1.0
100: */
101: public class RemoveWorkspaceAction extends DefaultAction {
102:
103: /**
104: * Name of the model action for removing a workspace layout.
105: * This is <tt>RemoveWorkspaceAction</tt>.
106: */
107: private static final String REMOVE_WORKSPACE_ACTION = "RemoveWorkspaceAction";
108:
109: /*
110: * Key for the error message when a workspace cannot be removed. Set to
111: * <tt>ErrorMessages.ChangeDesktopContent.errorRemovingWorkspace</tt>.
112: */
113: private static final String ERROR_REMOVING_WORKSPACE = DefaultAction.ERROR_MESSAGES_PREFIX
114: + "ChangeDesktopContent.errorRemovingWorkspace";
115:
116: protected ActionForward doExecute(ActionMapping mapping,
117: ActionForm form, HttpServletRequest request,
118: HttpServletResponse response) throws Exception {
119:
120: Map errors = new HashMap();
121:
122: try {
123:
124: ActionProcessorSingleton actionProcessorSingleton = ActionProcessorSingleton
125: .getInstance();
126:
127: UserCredentials userCredentials = SessionManager
128: .getUserCredentials(request);
129:
130: /* We get the action parameters from the action form */
131: String workspaceLayoutIdentifierStr = (String) PropertyUtils
132: .getSimpleProperty(
133: form,
134: RequestParameterConventions.WORKSPACE_IDENTIFIER);
135: Long workspaceLayoutIdentifier = null;
136: try {
137: workspaceLayoutIdentifier = new Long(
138: workspaceLayoutIdentifierStr);
139: } catch (Exception e) {
140: throw new InternalErrorException(
141: "Badly formed parameters");
142: }
143:
144: /*
145: * If there are no workspaces, this action cannot be executed.
146: */
147: if (!SessionManager.getUserHasWorkspaces(request)) {
148: throw new NoActiveWorkspacesException("Cannot remove "
149: + "workspace,there are no active workspaces.");
150: }
151:
152: /*
153: * We have to check whether the workspace is one of the
154: * valid ones for the user's desktop.
155: */
156: if (!KeyChecker.isValidWorkspaceLayoutIdentifier(request,
157: workspaceLayoutIdentifier)) {
158: throw new InstanceNotFoundException(
159: workspaceLayoutIdentifier, "WorkspaceLayout");
160: }
161:
162: /*
163: * We get the desktopLayout from the Session, as we need its
164: * timeStamp and identifier.
165: */
166: DesktopLayout desktopLayout = SessionManager
167: .getDesktopLayout(request);
168: EditorFactory editorFactory = EditorFactory.getInstance();
169: DesktopLayoutEditor desktopLayoutEditor = editorFactory
170: .createDesktopLayoutEditor(desktopLayout
171: .getProperty());
172:
173: Long desktopLayoutIdentifier = desktopLayout
174: .getIdentifier();
175: Long timeStamp = desktopLayoutEditor.getTimeStamp();
176:
177: /*
178: * And we check if we're removing the current workspace.
179: */
180: Long currentWorkspaceLayoutIdentifier = SessionManager
181: .getCurrentWorkspaceLayout(request).getIdentifier();
182: boolean changingCurrent = (currentWorkspaceLayoutIdentifier
183: .equals(workspaceLayoutIdentifier));
184:
185: WorkspaceEvent workspaceEvent = new WorkspaceEvent(
186: userCredentials, desktopLayoutIdentifier,
187: workspaceLayoutIdentifier, timeStamp);
188:
189: RemoveWorkspaceResult removeWorkspaceResult = (RemoveWorkspaceResult) actionProcessorSingleton
190: .execute(REMOVE_WORKSPACE_ACTION, workspaceEvent);
191:
192: /*
193: * And we put the data into the session.
194: */
195: SessionManager.setDesktopLayout(request,
196: removeWorkspaceResult.getDesktopLayout());
197: SessionManager.removeAvailableWorkspace(request,
198: workspaceLayoutIdentifier);
199:
200: /*
201: * If the current workspace has been removed, we will have been
202: * returned the default workspace. And it has to be put into the
203: * session.
204: */
205: if ((SessionManager.getUserHasWorkspaces(request))
206: && (changingCurrent)) {
207: SessionManager.setCurrentWorkspace(request,
208: removeWorkspaceResult
209: .getDefaultWorkspaceLayout(),
210: removeWorkspaceResult
211: .getDefaultServiceButtonsStates());
212:
213: /*
214: * The session cookie, which contains the current workspace
215: * identifier, is updated.
216: */
217: SessionIdentifierCookieManagerSingleton sessionIdentifierCookieManagerSingleton = SessionIdentifierCookieManagerSingleton
218: .getInstance();
219:
220: if (SessionManager.getUserHasWorkspaces(request)) {
221:
222: sessionIdentifierCookieManagerSingleton
223: .updateCookieWorkspace(
224: request,
225: response,
226: userCredentials.getLoginName(),
227: removeWorkspaceResult
228: .getDefaultWorkspaceLayout()
229: .getIdentifier());
230:
231: } else {
232:
233: sessionIdentifierCookieManagerSingleton
234: .updateCookieWorkspace(request, response,
235: userCredentials.getLoginName(),
236: new Long(-1)); // No valid Workspace to be set into the cookie
237:
238: }
239:
240: }
241:
242: } catch (ActionNotAllowedException e) {
243:
244: if (e.getAction().equals(
245: ActionNotAllowedException.REMOVE_WORKSPACE_ACTION)) {
246:
247: /*
248: * The user didn't have enough permissions to remove the
249: * workspace layout.
250: */
251: errors.put(e.getReason(), ERROR_REMOVING_WORKSPACE);
252:
253: } else {
254:
255: throw new InternalErrorException(e);
256:
257: }
258:
259: }
260:
261: if (errors.isEmpty()) {
262: return mapping
263: .findForward(DefaultAction.ACTION_SUCCESS_FORWARD);
264: } else {
265: request.setAttribute("errors", errors);
266: return mapping
267: .findForward(DefaultAction.ACTION_FAIL_FORWARD);
268: }
269: }
270:
271: }
|