KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.exception.BeanAlreadyExistsException;
27 import com.blandware.atleap.service.exception.OwnerNotFoundException;
28 import com.blandware.atleap.webapp.action.core.BaseAction;
29 import com.blandware.atleap.webapp.form.ContentFieldForm;
30 import com.blandware.atleap.webapp.util.core.WebappConstants;
31 import com.blandware.atleap.webapp.util.core.WebappUtil;
32 import org.apache.commons.validator.GenericValidator;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37 import org.apache.struts.action.ActionMessages;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.TreeSet JavaDoc;
44
45 /**
46  * <p>Creates new content field
47  * </p>
48  * <p><a HREF="CreateContentFieldAction.java.htm"><i>View Source</i></a></p>
49  * <p/>
50  *
51  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
52  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
53  * @version $Revision: 1.31 $ $Date: 2006/03/26 14:46:23 $
54  * @struts.action path="/core/contentField/create"
55  * name="contentFieldForm"
56  * scope="request"
57  * input="inputForward"
58  * validate="true"
59  * roles="core-contentField-create, core-contentField-override, core-contentField-createIndexed"
60  * @struts.action-forward name="inputForward"
61  * path=".core.contentField.create"
62  * @struts.action-forward name="viewContentField"
63  * path="/core/contentField/view.do"
64  * redirect="true"
65  * @struts.action-forward name="listContentFields"
66  * path="/core/contentField/list.do"
67  * redirect="false"
68  * @struts.action-forward name="unsatisfiable"
69  * path="/core/contentField/list.do"
70  */

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

81     public ActionForward execute(ActionMapping mapping, ActionForm form,
82                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
83
84         ContentFieldForm contentFieldForm = (ContentFieldForm) form;
85         Long JavaDoc ownerId = null;
86         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) {
87             ownerId = Long.valueOf(contentFieldForm.getOwnerId());
88         } else {
89             if ( log.isWarnEnabled() ) {
90                 log.warn("Missing owner ID. Returning to index...");
91             }
92             return mapping.findForward("admin");
93         }
94
95         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
96
97         if ( !isCancelled(request) ) {
98             LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN);
99             Localizable owner = lookupManager.retrieveLocalizable(ownerId);
100
101             if ( owner == null ) {
102                 ActionMessages errors = new ActionMessages();
103                 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound"));
104                 saveErrors(request, errors);
105                 return mapping.findForward("admin");
106             }
107
108             String JavaDoc fieldIdentifier = contentFieldForm.getIdentifier();
109
110             // check whether trying to change indexability
111
if (owner instanceof ContentPage || owner instanceof ActionPage) {
112                 Set JavaDoc indexedFieldIdentifiers = WebappUtil.getIndexedOverridableFieldIdentifiers(owner, request);
113                 Set JavaDoc unIndexedFieldIdentifiers = WebappUtil.getUnIndexedOverridableFieldIdentifiers(owner, request);
114                 Set JavaDoc intersection = new HashSet JavaDoc(indexedFieldIdentifiers);
115                 intersection.retainAll(unIndexedFieldIdentifiers);
116                 if ( !intersection.contains(fieldIdentifier) ) {
117                     if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
118                         if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) {
119                             ActionMessages errors = new ActionMessages();
120                             errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex"));
121                             saveErrors(request, errors);
122                             saveToken(request);
123                             return mapping.getInputForward();
124                         }
125                     } else {
126                         if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) {
127                             ActionMessages errors = new ActionMessages();
128                             errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected"));
129                             saveErrors(request, errors);
130                             saveToken(request);
131                             return mapping.getInputForward();
132                         }
133                     }
134                 }
135             } else if (owner instanceof Layout) {
136                 Layout ownerLayout = (Layout) owner;
137                 Set JavaDoc indexedFieldIdentifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
138                 Set JavaDoc unIndexedFieldIdentifiers = WebappUtil.getUnIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
139                 Set JavaDoc intersection = new HashSet JavaDoc(indexedFieldIdentifiers);
140                 intersection.retainAll(unIndexedFieldIdentifiers);
141                 if ( !intersection.contains(fieldIdentifier) ) {
142                     if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
143                         if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) {
144                             ActionMessages errors = new ActionMessages();
145                             errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex"));
146                             saveErrors(request, errors);
147                             saveToken(request);
148                             return mapping.getInputForward();
149                         }
150                     } else {
151                         if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) {
152                             ActionMessages errors = new ActionMessages();
153                             errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected"));
154                             saveErrors(request, errors);
155                             saveToken(request);
156                             return mapping.getInputForward();
157                         }
158                     }
159                 }
160             }
161
162             if ( owner instanceof ContentPage || (owner instanceof Layout && !request.isUserInRole("core-contentField-create") || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-create"))) ) {
163                 Set JavaDoc identifiers = null;
164                 if ( owner instanceof ContentPage || owner instanceof ActionPage ) {
165                     // Overriding field
166
if ( !request.isUserInRole("core-contentField-override") ) {
167                         response.sendError(HttpServletResponse.SC_FORBIDDEN);
168                         return null;
169                     }
170                     identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
171
172                     if ( !identifiers.contains(fieldIdentifier) ) {
173                         ActionMessages errors = new ActionMessages();
174                         errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier"));
175                         saveErrors(request, errors);
176                         saveToken(request);
177                         return mapping.getInputForward();
178                     }
179                 } else {
180                     // Creating new index or overriding field
181
boolean canCreateIndexed = request.isUserInRole("core-contentField-createIndexed");
182                     boolean canOverride = request.isUserInRole("core-contentField-override");
183                     if ( !canCreateIndexed && !canOverride ) {
184                         response.sendError(HttpServletResponse.SC_FORBIDDEN);
185                         return null;
186                     }
187                     Layout ownerLayout = (Layout) owner;
188                     if (canCreateIndexed) {
189                         identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
190                         if (identifiers == null) {
191                             identifiers = new TreeSet JavaDoc();
192                         }
193                     }
194                     if (canOverride) {
195                         identifiers.addAll(WebappUtil.getOverridableFieldIdentifiers(ownerLayout, request));
196                     }
197                     if ( !identifiers.contains(fieldIdentifier) ) {
198                         ActionMessages errors = new ActionMessages();
199                         errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier"));
200                         saveErrors(request, errors);
201                         saveToken(request);
202                         return mapping.getInputForward();
203                     }
204 /*
205                     if ( GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
206                         ActionMessages errors = new ActionMessages();
207                         errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected"));
208                         saveErrors(request, errors);
209                         saveToken(request);
210                         return mapping.getInputForward();
211                     }
212 */

213                 }
214             } else {
215                 // (it's AP or Layout and user can create fields) OR (its not CP,
216
// AP or Layout)
217
if ( !request.isUserInRole("core-contentField-create") ) {
218                     response.sendError(HttpServletResponse.SC_FORBIDDEN);
219                     return null;
220                 }
221             }
222
223             ContentField contentField = new ContentField();
224             WebappUtil.copyProperties(contentField, contentFieldForm, request);
225             if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) {
226                 fieldIdentifier += "[" + contentFieldForm.getIndex() + "]";
227                 contentField.setIdentifier(fieldIdentifier);
228             }
229             contentField.setInternal(Boolean.FALSE);
230
231             ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
232
233             Long JavaDoc contentFieldId = null;
234             try {
235                 contentFieldId = contentFieldManager.createContentField(contentField, ownerId);
236             } catch ( OwnerNotFoundException e ) {
237                 ActionMessages errors = new ActionMessages();
238                 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound"));
239                 saveErrors(request, errors);
240                 return mapping.findForward("admin");
241             } catch ( BeanAlreadyExistsException e ) {
242                 // contentField already exists
243
ActionMessages errors = new ActionMessages();
244                 errors.add("contentFieldAlreadyExists", new ActionMessage("core.contentField.errors.alreadyExists"));
245                 saveErrors(request, errors);
246                 saveToken(request);
247                 return mapping.getInputForward();
248             }
249             request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId);
250             return mapping.findForward("viewContentField");
251         } else {
252             // action was cancelled - return to page with list of content fields
253
return mapping.findForward("listContentFields");
254         }
255     }
256 }
Popular Tags