001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/actions/content/ChangeWorkspaceContentAction.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 java.util.ArrayList;
044: import java.util.Arrays;
045: import java.util.Collection;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053:
054: import org.apache.commons.beanutils.PropertyUtils;
055: import org.apache.struts.action.ActionForm;
056: import org.apache.struts.action.ActionForward;
057: import org.apache.struts.action.ActionMapping;
058:
059: import es.udc.mypersonalizer.kernel.controller.actions.DefaultAction;
060: import es.udc.mypersonalizer.kernel.model.actions.ActionProcessorSingleton;
061: import es.udc.mypersonalizer.kernel.model.editors.EditorFactory;
062: import es.udc.mypersonalizer.kernel.model.editors.WorkspaceLayoutEditor;
063: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayout;
064: import es.udc.mypersonalizer.kernel.util.exceptions.ActionNotAllowedException;
065: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
066: import es.udc.mypersonalizer.portal.controller.session.SessionManager;
067: import es.udc.mypersonalizer.portal.conventions.RequestParameterConventions;
068: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
069: import es.udc.mypersonalizer.portal.model.types.content.ContentEvent;
070: import es.udc.mypersonalizer.portal.model.types.content.ContentResult;
071:
072: /**
073: * This action changes the contents of the current workspace. It receives the
074: * list of service (identifiers) and passes it to the model action
075: * called <tt>ChangeWorkspaceContentAction</tt>, which must be registered in
076: * configuration. It also sets the new WorkspaceLayout object into the
077: * session.<p>
078: *
079: * It receives as a parameter a struts action form which must have been
080: * populated with the following data:
081: * <ul>
082: * <li><tt>workspaceName</tt>: The workspace name, so that it can be
083: * changed.</li>
084: * <li><tt>serviceIdentifiers</tt>: The list of service identifiers to be
085: * displayed in the workspace.</li>
086: * </ul>
087: *
088: * For error notifications, it asumes the following keys to be declared in the
089: * resources file:
090: * <ul>
091: * <li><tt>ErrorMessages.ChangeWorkspaceContent.errorAddingService</tt>: When
092: * there's an error adding a concrete service (usually a permissions problem).
093: * </li>
094: * <li><tt>ErrorMessages.ChangeWorkspaceContent.errorRemovingService</tt>:
095: * When there's an error removing a service (usually a permission
096: * problem).
097: * </li>
098: * </ul>
099: *
100: * This action requires a <tt>Success</tt> forward, which will usually point
101: * to the portal main page. It also requires a <tt>Fail</tt> forward, in case
102: * any error (permission related, possibly) happens during the operation,
103: * usually pointing to the jsp page which shows the possible services to add
104: * to the user.<p>
105: *
106: *
107: * @author Daniel Fernandez
108: * @since 1.0
109: */
110: public class ChangeWorkspaceContentAction extends DefaultAction {
111:
112: /**
113: * Name of the model action for changing the contents of a workspace.
114: * This is "ChangeWorkspaceContentAction".
115: */
116: private static final String CHANGE_WORKSPACE_CONTENT_ACTION = "ChangeWorkspaceContentAction";
117:
118: /*
119: * Key for the error message when a service cannot be added. Set to
120: * <tt>ErrorMessages.ChangeWorkspaceContent.errorAddingService</tt>.
121: */
122: private static final String ERROR_ADDING_SERVICE = DefaultAction.ERROR_MESSAGES_PREFIX
123: + "ChangeWorkspaceContent.errorAddingService";
124:
125: /*
126: * Key for the error message when a service cannot be removed. Set to
127: * <tt>ErrorMessages.ChangeWorkspaceContent.errorRemovingService</tt>.
128: */
129: private static final String ERROR_REMOVING_SERVICE = DefaultAction.ERROR_MESSAGES_PREFIX
130: + "ChangeWorkspaceContent.errorRemovingService";
131:
132: protected ActionForward doExecute(ActionMapping mapping,
133: ActionForm form, HttpServletRequest request,
134: HttpServletResponse response) throws Exception {
135:
136: Map errors = new HashMap();
137:
138: ActionProcessorSingleton actionProcessorSingleton = ActionProcessorSingleton
139: .getInstance();
140:
141: /* We get the service identifiers from the action form */
142: String[] serviceIdentifiersStr = new String[0];
143: if (request
144: .getParameter(RequestParameterConventions.SERVICE_IDENTIFIERS) != null) {
145: serviceIdentifiersStr = (String[]) PropertyUtils
146: .getSimpleProperty(
147: form,
148: RequestParameterConventions.SERVICE_IDENTIFIERS);
149: }
150:
151: Collection serviceIdentifiers = new ArrayList(Arrays
152: .asList(serviceIdentifiersStr));
153:
154: /*
155: * Now we get the workspace name that has been input.
156: */
157: String workspaceNameParam = (String) PropertyUtils
158: .getSimpleProperty(form,
159: RequestParameterConventions.WORKSPACE_NAME);
160:
161: try {
162:
163: /*
164: * If there are no workspaces, this action cannot be executed.
165: */
166: if (!SessionManager.getUserHasWorkspaces(request)) {
167: throw new NoActiveWorkspacesException(
168: "Cannot change contents");
169: }
170:
171: UserCredentials userCredentials = SessionManager
172: .getUserCredentials(request);
173:
174: WorkspaceLayout currentWorkspaceLayout = SessionManager
175: .getCurrentWorkspaceLayout(request);
176:
177: Long workspaceLayoutIdentifier = currentWorkspaceLayout
178: .getIdentifier();
179:
180: EditorFactory editorFactory = EditorFactory.getInstance();
181:
182: WorkspaceLayoutEditor currentWorkspaceLayoutEditor = editorFactory
183: .createWorkspaceLayoutEditor(currentWorkspaceLayout
184: .getProperty());
185:
186: String oldWorkspaceName = currentWorkspaceLayoutEditor
187: .getWorkspaceName();
188: String workspaceType = currentWorkspaceLayoutEditor
189: .getWorkspaceType();
190:
191: /*
192: * If the workspace name input is null, then the new workspace
193: * name will be the workspace type.
194: */
195: String newWorkspaceName = workspaceNameParam;
196: if ((newWorkspaceName == null)
197: || (newWorkspaceName.trim().equals(""))) {
198: newWorkspaceName = workspaceType;
199: } else {
200: newWorkspaceName = newWorkspaceName.trim();
201: }
202:
203: Long timeStamp = currentWorkspaceLayoutEditor
204: .getTimeStamp();
205:
206: ContentEvent contentEvent = new ContentEvent(
207: userCredentials, workspaceLayoutIdentifier,
208: serviceIdentifiers, newWorkspaceName, timeStamp);
209:
210: /* We execute the model action */
211: ContentResult contentResult = (ContentResult) actionProcessorSingleton
212: .execute(CHANGE_WORKSPACE_CONTENT_ACTION,
213: contentEvent);
214:
215: WorkspaceLayout newCurrentWorkspaceLayout = contentResult
216: .getCurrentWorkspaceLayout();
217:
218: Collection newServiceButtonsStates = contentResult
219: .getServiceButtonsStates();
220:
221: /*
222: * We push the new WorkspaceLayout (including buttons) into the
223: * session.
224: */
225: SessionManager.setCurrentWorkspace(request,
226: newCurrentWorkspaceLayout, newServiceButtonsStates);
227:
228: /*
229: * And finally we check if the workspace name has changed,
230: * and if it has, we update the list of available workspaces in
231: * the session.
232: */
233: checkWorkspaceNameInSession(workspaceLayoutIdentifier,
234: newWorkspaceName, request);
235:
236: } catch (ActionNotAllowedException e) {
237:
238: if (e.getAction().equals(
239: ActionNotAllowedException.ADD_SERVICE_ACTION)) {
240:
241: /*
242: * The user didn't have enough permissions to add a service.
243: */
244: errors.put(e.getReason(), ERROR_ADDING_SERVICE);
245:
246: } else if (e.getAction().equals(
247: ActionNotAllowedException.REMOVE_SERVICE_ACTION)) {
248:
249: /*
250: * The user didn't have enough permissions to remove a service.
251: */
252: errors.put(e.getReason(), ERROR_REMOVING_SERVICE);
253:
254: } else {
255:
256: throw new InternalErrorException(e);
257:
258: }
259:
260: }
261:
262: if (errors.isEmpty()) {
263: return mapping
264: .findForward(DefaultAction.ACTION_SUCCESS_FORWARD);
265: } else {
266: request.setAttribute("errors", errors);
267: return mapping
268: .findForward(DefaultAction.ACTION_FAIL_FORWARD);
269: }
270: }
271:
272: private void checkWorkspaceNameInSession(
273: Long workspaceLayoutIdentifier, String newWorkspaceName,
274: HttpServletRequest request) {
275:
276: List availableWorkspaces = SessionManager
277: .getAvailableWorkspaces(request);
278: Iterator availableWorkspacesIter = availableWorkspaces
279: .iterator();
280: while (availableWorkspacesIter.hasNext()) {
281:
282: Map workspaceData = (Map) availableWorkspacesIter.next();
283: Long currentWorkspaceLayoutIdentifier = (Long) workspaceData
284: .get("workspaceLayoutIdentifier");
285:
286: if (currentWorkspaceLayoutIdentifier
287: .equals(workspaceLayoutIdentifier)) {
288: workspaceData.put("workspaceName", newWorkspaceName);
289: }
290:
291: }
292:
293: }
294:
295: }
|