001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/actions/content/ChangeWorkspaceLayoutAction.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.WorkspaceLayoutEditor;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayout;
048: import es.udc.mypersonalizer.portal.controller.session.SessionManager;
049: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
050: import es.udc.mypersonalizer.portal.model.types.content.ChangeWorkspaceLayoutEvent;
051: import java.util.*;
052: import javax.servlet.http.HttpServletRequest;
053: import javax.servlet.http.HttpServletResponse;
054: import org.apache.struts.action.ActionForm;
055: import org.apache.struts.action.ActionForward;
056: import org.apache.struts.action.ActionMapping;
057:
058: /**
059: * This action performs a layout change. Using this action, the user can
060: * position his / her services in the area of his preference in the workspace.
061: * It calls the model action called <tt>ChangeWorkspaceLayoutAction</tt>, which
062: * must be registered in configuration. It also updates the workspace layout
063: * information in the session.<p>
064: *
065: * It needs to take from the request one parameter per area, called
066: * <tt>servicesList_AX</tt>, being <tt>X</tt> the number of area to which
067: * the parameter is related. The value of this parameter must be a
068: * comma-separated list of service identifiers, being in the order that
069: * these services must appear in the area.<p>
070: *
071: * This action requires a <tt>Success</tt> forward, which will usually point
072: * to the portal main page.<p>
073: *
074: * @author Daniel Fernandez
075: * @since 1.0
076: */
077: public class ChangeWorkspaceLayoutAction extends DefaultAction {
078:
079: /**
080: * Name of the model action for changing the layout of a workspace.
081: * This is "ChangeWorkspaceLayoutAction".
082: */
083: private static final String CHANGE_WORKSPACE_LAYOUT_ACTION = "ChangeWorkspaceLayoutAction";
084:
085: /**
086: * Prefix for the names of the form parameters which will carry
087: * the new layout for services. This is <tt>servicesList_A</tt> as
088: * the list for the first area would be <tt>servicesList_A0</tt>,
089: * the second <tt>servicesList_A1</tt>, etc.
090: */
091: private static final String SERVICE_LAYOUT_PARAMETERS_PREFIX = "servicesList_A";
092:
093: protected ActionForward doExecute(ActionMapping mapping,
094: ActionForm form, HttpServletRequest request,
095: HttpServletResponse response) throws Exception {
096:
097: /*
098: * We get the service identifiers from the action form building
099: * the "servicesLayout" map.
100: */
101: Map servicesLayout = new HashMap();
102: Enumeration parameterNames = request.getParameterNames();
103: while (parameterNames.hasMoreElements()) {
104: String parameterName = (String) parameterNames
105: .nextElement();
106:
107: if (parameterName
108: .startsWith(SERVICE_LAYOUT_PARAMETERS_PREFIX)) {
109:
110: Integer area = new Integer(parameterName.substring(14));
111: String parameterValue = request
112: .getParameter(parameterName);
113: StringTokenizer parameterTokens = new StringTokenizer(
114: parameterValue, ", ");
115: List serviceIdentifiers = new ArrayList();
116: while (parameterTokens.hasMoreTokens()) {
117: serviceIdentifiers.add(parameterTokens.nextToken());
118: }
119: servicesLayout.put(area, serviceIdentifiers);
120:
121: }
122:
123: }
124:
125: /*
126: * If there are no workspaces, this action cannot be executed.
127: */
128: if (!SessionManager.getUserHasWorkspaces(request)) {
129: throw new NoActiveWorkspacesException(
130: "Cannot change layout");
131: }
132:
133: ActionProcessorSingleton actionProcessorSingleton = ActionProcessorSingleton
134: .getInstance();
135:
136: UserCredentials userCredentials = SessionManager
137: .getUserCredentials(request);
138:
139: WorkspaceLayout currentWorkspaceLayout = SessionManager
140: .getCurrentWorkspaceLayout(request);
141:
142: Long workspaceLayoutIdentifier = currentWorkspaceLayout
143: .getIdentifier();
144:
145: EditorFactory editorFactory = EditorFactory.getInstance();
146:
147: WorkspaceLayoutEditor currentWorkspaceLayoutEditor = editorFactory
148: .createWorkspaceLayoutEditor(currentWorkspaceLayout
149: .getProperty());
150:
151: /*
152: * This time stamp will be necessary to avoid problems related
153: * to concurrent access, as this action modifies the workspace layout.
154: */
155: Long timeStamp = currentWorkspaceLayoutEditor.getTimeStamp();
156:
157: ChangeWorkspaceLayoutEvent changeWorkspaceLayoutEvent = new ChangeWorkspaceLayoutEvent(
158: userCredentials, workspaceLayoutIdentifier,
159: servicesLayout, timeStamp);
160:
161: /* We execute the model action */
162: WorkspaceLayout workspaceLayout = (WorkspaceLayout) actionProcessorSingleton
163: .execute(CHANGE_WORKSPACE_LAYOUT_ACTION,
164: changeWorkspaceLayoutEvent);
165:
166: /*
167: * We push the modified WorkspaceLayout into the
168: * session.
169: */
170: SessionManager.setCurrentWorkspace(request, workspaceLayout,
171: new ArrayList());
172:
173: return mapping
174: .findForward(DefaultAction.ACTION_SUCCESS_FORWARD);
175: }
176:
177: }
|