KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentDocument;
20 import com.blandware.atleap.model.core.ContentResource;
21 import com.blandware.atleap.model.core.Role;
22 import com.blandware.atleap.search.SearchManager;
23 import com.blandware.atleap.service.core.ContentResourceManager;
24 import com.blandware.atleap.service.core.RoleManager;
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 Resource
45  * </p>
46  * <p><a HREF="AssignContentResourceRolesAction.java.htm"><i>View Source</i></a></p>
47  * <p/>
48  *
49  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
50  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
51  * @version $Revision: 1.16 $ $Date: 2006/03/10 17:10:21 $
52  * @struts.action path="/core/contentResource/assignRoles"
53  * name="selectRolesForm"
54  * scope="request"
55  * input="inputForward"
56  * validate="true"
57  * roles="core-contentResource-assignRoles"
58  * @struts.action-forward name="inputForward"
59  * path=".core.contentResource.assignRoles"
60  * @struts.action-forward name="listContentResources"
61  * path="/core/contentResource/list.do"
62  * redirect="true"
63  * @struts.action-forward name="callAssignRoles"
64  * path="/core/contentResource/callAssignRoles.do"
65  * redirect="false"
66  * @struts.action-forward name="unsatisfiable"
67  * path="/core/contentResource/list.do"
68  */

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

79     public ActionForward execute(ActionMapping mapping, ActionForm form,
80                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
81
82         if ( !isCancelled(request) ) {
83             // acquire POJOs for all selected roles
84
RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
85             SelectRolesForm selectRolesForm = (SelectRolesForm) form;
86             String JavaDoc[] selectedRoleNames = selectRolesForm.getSelectedRoles();
87
88             List JavaDoc roles = new ArrayList JavaDoc();
89             for ( int i = 0; i < selectedRoleNames.length; i++ ) {
90                 String JavaDoc roleName = selectedRoleNames[i];
91                 Role role = roleManager.retrieveRole(roleName);
92                 if ( role != null ) {
93                     roles.add(role);
94                 }
95             }
96
97             Long JavaDoc contentResourceId = null;
98             if ( request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) {
99                 contentResourceId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
100             } else {
101                 if ( log.isWarnEnabled() ) {
102                     log.warn("Missing content resource ID. Returning to list...");
103                 }
104                 return mapping.findForward("listContentResources");
105             }
106
107             ContentResourceManager resourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
108             ContentResource contentResource = resourceManager.retrieveContentResource(contentResourceId);
109
110             if ( contentResource == null ) {
111                 // content resource not found. it might be deleted by someone else
112
ActionMessages errors = new ActionMessages();
113                 errors.add("contentResourceNotFound", new ActionMessage("core.contentResource.errors.notFound"));
114                 saveErrors(request, errors);
115                 return mapping.findForward("listContentResources");
116             }
117
118             contentResource.setVersion(Long.valueOf(selectRolesForm.getVersion()));
119
120             // remove roles which were unselected
121
// list of roles is copied in order to prevent ConcurrentModifictionException to be thrown
122
List JavaDoc contentResourceRoles = new ArrayList JavaDoc(contentResource.getRoles());
123             for ( int i = 0; i < contentResourceRoles.size(); i++ ) {
124                 Role role = (Role) contentResourceRoles.get(i);
125                 if ( !roles.contains(role) ) {
126                     contentResource.removeRole(role);
127                 }
128             }
129
130             // add new roles
131
for ( Iterator JavaDoc i = roles.iterator(); i.hasNext(); ) {
132                 Role role = (Role) i.next();
133                 contentResource.addRole(role);
134             }
135
136             try {
137                 resourceManager.updateContentResource(contentResource, contentResource.getResourceData());
138             } catch ( ObjectOptimisticLockingFailureException e ) {
139                 // content resource was updated or deleted by another transaction
140
ActionMessages errors = new ActionMessages();
141                 errors.add("updateFailed", new ActionMessage("core.contentResource.errors.updateFailed"));
142                 saveErrors(request, errors);
143                 request.setAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY, contentResource.getId());
144                 return mapping.findForward("callAssignRoles");
145             }
146
147             String JavaDoc charset = null;
148             if ( contentResource instanceof ContentDocument ) {
149                 ContentDocument contentDocument = (ContentDocument) contentResource;
150                 charset = contentDocument.getCharset();
151
152                 //index
153
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
154                 searchManager.reIndexDocument(contentDocument, request);
155             }
156
157             CacheUtil cacheUtil = CacheUtil.getInstance(request);
158             CacheUtil.ResourceData rd = new CacheUtil.ResourceData(contentResource.getResourceData().getData(),
159                     contentResource.getMimeType(),
160                     charset,
161                     WebappUtil.rolesToString(contentResource.getRoles()),
162                     contentResource.getLastUpdatedDatetime().getTime());
163             cacheUtil.putResourceInCache(rd, contentResource.getUri());
164
165         }
166         return mapping.findForward("listContentResources");
167     }
168 }
Popular Tags