KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentField > CallUpdateContentFieldAction


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.contentField;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ActionPage;
20 import com.blandware.atleap.model.core.ContentField;
21 import com.blandware.atleap.model.core.ContentPage;
22 import com.blandware.atleap.model.core.Layout;
23 import com.blandware.atleap.model.core.Localizable;
24 import com.blandware.atleap.service.core.ContentFieldManager;
25 import com.blandware.atleap.service.core.LookupManager;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.ContentFieldForm;
28 import com.blandware.atleap.webapp.util.core.WebappConstants;
29 import com.blandware.atleap.webapp.util.core.WebappUtil;
30 import org.apache.commons.validator.GenericValidator;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.TreeSet JavaDoc;
41
42 /**
43  * <p>Prepares form bean to update content field's data
44  * </p>
45  * <p><a HREF="CallUpdateContentFieldAction.java.htm"><i>View Source</i></a></p>
46  * <p/>
47  *
48  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
49  * @version $Revision: 1.33 $ $Date: 2006/03/26 13:51:47 $
50  * @struts.action path="/core/contentField/callUpdate"
51  * name="contentFieldForm"
52  * scope="request"
53  * validate="false"
54  * roles="core-contentField-update, core-contentField-updateOverriden, core-contentField-updateIndexed, core-contentField-view"
55  * @struts.action-forward name="updateContentField"
56  * path=".core.contentField.update"
57  * @struts.action-forward name="listContentFields"
58  * path="/core/contentField/list.do"
59  * redirect="false"
60  */

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

71     public ActionForward execute(ActionMapping mapping, ActionForm form,
72                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
73
74         ContentFieldForm contentFieldForm = (ContentFieldForm) form;
75
76         Long JavaDoc ownerId = null;
77         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) {
78             ownerId = Long.valueOf(contentFieldForm.getOwnerId());
79         } else {
80             if ( log.isWarnEnabled() ) {
81                 log.warn("Missing owner ID. Returning to index...");
82             }
83             return mapping.findForward("admin");
84         }
85
86         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
87
88         if ( isCancelled(request) ) {
89             String JavaDoc requestUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
90             if ( !GenericValidator.isBlankOrNull(requestUrl) ) {
91                 request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY);
92                 return new ActionForward(requestUrl, true);
93             } else {
94                 return mapping.findForward("listContentFields");
95             }
96         }
97
98         if (!request.isUserInRole("core-contentField-update") && !request.isUserInRole("core-contentField-updateOverriden") && !request.isUserInRole("core-contentField-updateIndexed")) {
99             response.sendError(HttpServletResponse.SC_FORBIDDEN);
100             return null;
101         }
102
103         Long JavaDoc contentFieldId = null;
104         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) {
105             contentFieldId = Long.valueOf(contentFieldForm.getId());
106         } else {
107             if ( log.isWarnEnabled() ) {
108                 log.warn("Missing content field ID. Returning to list...");
109             }
110             return mapping.findForward("listContentFields");
111         }
112
113         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
114         ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId);
115         if ( contentField == null ) {
116             // content field not found. it might be deleted by someone else
117
ActionMessages errors = new ActionMessages();
118             errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound"));
119             saveErrors(request, errors);
120             return mapping.findForward("listContentFields");
121         }
122
123         WebappUtil.copyProperties(contentFieldForm, contentField, request);
124
125         String JavaDoc fieldIdentifier = contentField.getIdentifier();
126         int k = fieldIdentifier.indexOf('[');
127         String JavaDoc index = null;
128         if ( k != -1 ) {
129             index = fieldIdentifier.substring(k + 1, fieldIdentifier.indexOf(']', k));
130             fieldIdentifier = fieldIdentifier.substring(0, k);
131             contentFieldForm.setIdentifier(fieldIdentifier);
132             contentFieldForm.setIndex(index);
133         }
134
135         LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN);
136         Localizable owner = lookupManager.retrieveLocalizable(ownerId);
137
138         if ( owner == null ) {
139             ActionMessages errors = new ActionMessages();
140             errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound"));
141             saveErrors(request, errors);
142             return mapping.findForward("admin");
143         }
144
145 /*
146         //check for indexed field
147         if ( !request.isUserInRole("core-contentField-update") && owner instanceof Layout && (index == null || index.equalsIgnoreCase("0")) ) {
148             ActionMessages errors = new ActionMessages();
149             errors.add("cantCreateOrUpdate", new ActionMessage("core.contentField.errors.cantCreateOrUpdate"));
150             saveErrors(request, errors);
151             return mapping.findForward("listContentFields");
152         }
153 */

154
155         boolean canCreateOrUpdate = false;
156         Set JavaDoc identifiers = null;
157         Set JavaDoc ownerIdentifiers = WebappUtil.getLocalizableUnIndexedFieldsIdentifiers(owner);
158         if ( owner instanceof ContentPage || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-update")) ) {
159             // Updating overriden field
160
if ( !request.isUserInRole("core-contentField-updateOverriden") ) {
161                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
162                 return null;
163             }
164             identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
165             identifiers.removeAll(ownerIdentifiers);
166             identifiers.add(fieldIdentifier);
167             canCreateOrUpdate = identifiers != null && !identifiers.isEmpty() && identifiers.contains(fieldIdentifier);
168             request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY, identifiers);
169         } else if ( owner instanceof Layout && !request.isUserInRole("core-contentField-update") ) {
170             // Updating indexed or overriden field
171
boolean canUpdateOverriden = request.isUserInRole("core-contentField-updateOverriden");
172             boolean canUpdateIndexed = request.isUserInRole("core-contentField-updateIndexed");
173             if ( !canUpdateIndexed && !canUpdateOverriden ) {
174                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
175                 return null;
176             }
177             Layout ownerLayout = (Layout) owner;
178             if (canUpdateIndexed) {
179                 identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
180                 if (identifiers == null) {
181                     identifiers = new TreeSet JavaDoc();
182                 }
183             }
184             Set JavaDoc overradableIdentifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
185             //check for indexed field
186
if ( (index == null || index.equalsIgnoreCase("0")) && !overradableIdentifiers.contains(fieldIdentifier) ) {
187                 ActionMessages errors = new ActionMessages();
188                 errors.add("cantCreateOrUpdate", new ActionMessage("core.contentField.errors.cantCreateOrUpdate"));
189                 saveErrors(request, errors);
190                 return mapping.findForward("listContentFields");
191             }
192             if (canUpdateOverriden) {
193                 identifiers.addAll(overradableIdentifiers);
194             }
195             identifiers.removeAll(ownerIdentifiers);
196             identifiers.add(fieldIdentifier);
197             canCreateOrUpdate = identifiers != null && !identifiers.isEmpty();
198             request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY, identifiers);
199         } else {
200             // (it's AP or Layout and user can update fields) OR (its not CP,
201
// AP or Layout)
202
request.getSession().removeAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY);
203             if ( !request.isUserInRole("core-contentField-update") ) {
204                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
205                 return null;
206             } else {
207                 canCreateOrUpdate = true;
208             }
209         }
210
211         if ( !canCreateOrUpdate ) {
212             ActionMessages errors = new ActionMessages();
213             errors.add("cantCreateOrUpdate", new ActionMessage("core.contentField.errors.cantCreateOrUpdate"));
214             saveErrors(request, errors);
215             return mapping.findForward("listContentFields");
216         }
217
218         String JavaDoc ownerInfo = WebappUtil.getLocalizableInfo(owner, request);
219         request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo);
220
221         // save transaction token in request
222
saveToken(request);
223         return mapping.findForward("updateContentField");
224     }
225
226 }
Popular Tags