KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentPage > AssignContentPageRolesAction


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.contentPage;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentPage;
20 import com.blandware.atleap.model.core.Role;
21 import com.blandware.atleap.search.SearchManager;
22 import com.blandware.atleap.service.core.PageManager;
23 import com.blandware.atleap.service.core.RoleManager;
24 import com.blandware.atleap.service.exception.OwnerNotFoundException;
25 import com.blandware.atleap.webapp.action.core.BaseAction;
26 import com.blandware.atleap.webapp.form.core.SelectRolesForm;
27 import com.blandware.atleap.webapp.util.core.CacheUtil;
28 import com.blandware.atleap.webapp.util.core.WebappConstants;
29 import com.blandware.atleap.webapp.util.core.WebappUtil;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35 import org.springframework.orm.ObjectOptimisticLockingFailureException;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 /**
44  * <p>Assigns Roles to Content Page
45  * </p>
46  * <p><a HREF="AssignContentPageRolesAction.java.htm"><i>View Source</i></a></p>
47  * <p/>
48  *
49  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
50  * @version $Revision: 1.16 $ $Date: 2006/03/10 17:10:20 $
51  * @struts.action path="/core/contentPage/assignRoles"
52  * name="selectRolesForm"
53  * scope="request"
54  * input="inputForward"
55  * validate="true"
56  * roles="core-contentPage-assignRoles"
57  * @struts.action-forward name="inputForward"
58  * path=".core.contentPage.assignRoles"
59  * @struts.action-forward name="listContentPages"
60  * path="/core/contentPage/list.do"
61  * redirect="true"
62  * @struts.action-forward name="callAssignRoles"
63  * path="/core/contentPage/callAssignRoles.do"
64  * redirect="false"
65  * @struts.action-forward name="unsatisfiable"
66  * path="/core/contentPage/list.do"
67  */

68 public final class AssignContentPageRolesAction extends BaseAction {
69     /**
70      * @param mapping The ActionMapping used to select this instance
71      * @param form The optional ActionForm bean for this request (if any)
72      * @param request The HTTP request we are proceeding
73      * @param response The HTTP response we are creating
74      * @return an ActionForward instance describing where and how
75      * control should be forwarded, or null if response
76      * has already been completed
77      */

78     public ActionForward execute(ActionMapping mapping, ActionForm form,
79                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
80
81         if ( !isCancelled(request) ) {
82             // acquire POJOs for all selected roles
83
RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
84             SelectRolesForm selectRolesForm = (SelectRolesForm) form;
85             String JavaDoc[] selectedRoleNames = selectRolesForm.getSelectedRoles();
86
87             List JavaDoc roles = new ArrayList JavaDoc();
88             for ( int i = 0; i < selectedRoleNames.length; i++ ) {
89                 String JavaDoc roleName = selectedRoleNames[i];
90                 Role role = roleManager.retrieveRole(roleName);
91                 if ( role != null ) {
92                     roles.add(role);
93                 }
94             }
95
96             Long JavaDoc contentPageId = null;
97             if ( request.getSession().getAttribute(WebappConstants.CONTENT_PAGE_ID_KEY) != null ) {
98                 contentPageId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_PAGE_ID_KEY);
99             } else {
100                 if ( log.isWarnEnabled() ) {
101                     log.warn("Missing content page ID. Returning to list...");
102                 }
103                 return mapping.findForward("listContentPages");
104             }
105
106             PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
107             ContentPage contentPage = pageManager.retrieveContentPage(contentPageId);
108
109             if ( contentPage == null ) {
110                 // content page not found. it might be deleted by someone else
111
ActionMessages errors = new ActionMessages();
112                 errors.add("contentPageNotFound", new ActionMessage("core.contentPage.errors.notFound"));
113                 saveErrors(request, errors);
114                 return mapping.findForward("listContentPages");
115             }
116
117             contentPage.setVersion(Long.valueOf(selectRolesForm.getVersion()));
118
119             // remove roles which were unselected
120
// list of roles is copied in order to prevent ConcurrentModifictionException to be thrown
121
List JavaDoc contentPageRoles = new ArrayList JavaDoc(contentPage.getRoles());
122             for ( int i = 0; i < contentPageRoles.size(); i++ ) {
123                 Role role = (Role) contentPageRoles.get(i);
124                 if ( !roles.contains(role) ) {
125                     contentPage.removeRole(role);
126                 }
127             }
128
129             // add new roles
130
for ( Iterator JavaDoc i = roles.iterator(); i.hasNext(); ) {
131                 Role role = (Role) i.next();
132                 contentPage.addRole(role);
133             }
134
135
136             try {
137                 pageManager.updateContentPage(contentPage, contentPage.getLayout().getId());
138             } catch ( OwnerNotFoundException e ) {
139                 // layout not found. it might has already been deleted by another transaction
140
ActionMessages errors = new ActionMessages();
141                 errors.add("ownerNotFound", new ActionMessage("core.contentPage.errors.ownerNotFound"));
142                 saveErrors(request, errors);
143                 return mapping.findForward("listContentPages");
144             } catch ( ObjectOptimisticLockingFailureException e ) {
145                 // content page was updated or deleted by another transaction
146
ActionMessages errors = new ActionMessages();
147                 errors.add("updateFailed", new ActionMessage("core.contentPage.errors.updateFailed"));
148                 saveErrors(request, errors);
149                 request.setAttribute(WebappConstants.CONTENT_PAGE_ID_KEY, contentPage.getId());
150                 return mapping.findForward("callAssignRoles");
151             }
152
153             //put into cache
154
if ( contentPage.getActive().booleanValue() ) {
155                 CacheUtil cacheUtil = CacheUtil.getInstance(request);
156                 CacheUtil.ContentPageData cpd = new CacheUtil.ContentPageData(contentPage.getLayout().getCpDefinition(), WebappUtil.rolesToString(contentPage.getRoles()), contentPage.getCacheMaxAge());
157                 cacheUtil.putContentPageInCache(cpd, contentPage.getUri());
158             }
159
160             //index
161
if ( contentPage.getActive().booleanValue() ) {
162                 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
163                 searchManager.reIndexPage(contentPage, request);
164             }
165
166         }
167         return mapping.findForward("listContentPages");
168     }
169 }
Popular Tags