KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentResource > CallAssignContentResourceRolesAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.core.contentResource;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentResource;
20 import com.blandware.atleap.model.core.Role;
21 import com.blandware.atleap.service.core.ContentResourceManager;
22 import com.blandware.atleap.service.core.RoleManager;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.form.core.SelectRolesForm;
25 import com.blandware.atleap.webapp.util.core.WebappConstants;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * <p>Returns page with two lists to reassign roles to content resource
40  * </p>
41  * <p><a HREF="CallAssignContentPageRolesAction.java.htm"><i>View Source</i></a></p>
42  * <p/>
43  *
44  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
45  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
46  * @version $Revision: 1.9 $ $Date: 2006/03/11 10:08:52 $
47  * @struts.action path="/core/contentResource/callAssignRoles"
48  * name="selectRolesForm"
49  * validate="false"
50  * roles="core-contentResource-assignRoles"
51  * @struts.action-forward name="assignRoles"
52  * path=".core.contentResource.assignRoles"
53  * @struts.action-forward name="listContentResources"
54  * path="/core/contentResource/list.do"
55  * redirect="true"
56  */

57 public final class CallAssignContentResourceRolesAction extends BaseAction {
58     /**
59      * @param mapping The ActionMapping used to select this instance
60      * @param form The optional ActionForm bean for this request (if any)
61      * @param request The HTTP request we are proceeding
62      * @param response The HTTP response we are creating
63      * @return an ActionForward instance describing where and how
64      * control should be forwarded, or null if response
65      * has already been completed
66      */

67     public ActionForward execute(ActionMapping mapping, ActionForm form,
68                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69
70         Long JavaDoc contentResourceId = null;
71         if ( request.getParameter("contentResourceId") != null ) {
72             contentResourceId = Long.valueOf(request.getParameter("contentResourceId"));
73         } else if ( request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) {
74             contentResourceId = (Long JavaDoc) request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
75         } else {
76             if ( log.isWarnEnabled() ) {
77                 log.warn("Missing content resource ID. Returning to list...");
78             }
79             return mapping.findForward("listContentResources");
80         }
81
82         request.getSession().setAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY, contentResourceId);
83
84         SelectRolesForm selectRolesForm = (SelectRolesForm) form;
85         ContentResourceManager resourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
86         ContentResource contentResource = resourceManager.retrieveContentResource(contentResourceId);
87
88         if ( contentResource == null ) {
89             // content resource not found. it might be deleted by someone else
90
ActionMessages errors = new ActionMessages();
91             errors.add("contentResourceNotFound", new ActionMessage("core.contentResource.errors.notFound"));
92             saveErrors(request, errors);
93             return mapping.findForward("listContentResources");
94         }
95
96         selectRolesForm.setVersion(contentResource.getVersion().toString());
97
98         List JavaDoc contentResourceRoles = new ArrayList JavaDoc();
99         for ( Iterator JavaDoc i = contentResource.getRoles().iterator(); i.hasNext(); ) {
100             Role role = (Role) i.next();
101             contentResourceRoles.add(role);
102         }
103
104         RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
105         List JavaDoc availableRoles = new ArrayList JavaDoc(roleManager.listRoles(null));
106         List JavaDoc allRoles = new ArrayList JavaDoc(availableRoles);
107         availableRoles.removeAll(contentResourceRoles);
108
109         selectRolesForm.setAvailableRolesList(availableRoles);
110         selectRolesForm.setSelectedRolesList(contentResourceRoles);
111
112         request.setAttribute("allRoles", allRoles);
113
114         // save transaction token in request
115
saveToken(request);
116         return mapping.findForward("assignRoles");
117     }
118 }
Popular Tags