KickJava   Java API By Example, From Geeks To Geeks.

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


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.ContentPage;
21 import com.blandware.atleap.model.core.Layout;
22 import com.blandware.atleap.model.core.Localizable;
23 import com.blandware.atleap.service.core.LookupManager;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ContentFieldForm;
26 import com.blandware.atleap.webapp.util.core.WebappConstants;
27 import com.blandware.atleap.webapp.util.core.WebappUtil;
28 import org.apache.commons.validator.GenericValidator;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.TreeSet JavaDoc;
39
40 /**
41  * <p>Returns page with form to create new content field
42  * </p>
43  * <p><a HREF="CallCreateContentFieldAction.java.htm"><i>View Source</i></a></p>
44  * <p/>
45  *
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.20 $ $Date: 2006/03/26 13:51:47 $
48  * @struts.action path="/core/contentField/callCreate"
49  * name="contentFieldForm"
50  * validate="false"
51  * roles="core-contentField-create, core-contentField-override, core-contentField-createIndexed"
52  * @struts.action-forward name="createContentField"
53  * path=".core.contentField.create"
54  * @struts.action-forward name="listContentFields"
55  * path="/core/contentField/list.do"
56  * redirect="false"
57  */

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

68     public ActionForward execute(ActionMapping mapping, ActionForm form,
69                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
70
71         ContentFieldForm contentFieldForm = (ContentFieldForm) form;
72
73         Long JavaDoc ownerId = null;
74         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) {
75             ownerId = Long.valueOf(contentFieldForm.getOwnerId());
76         } else {
77             if ( log.isWarnEnabled() ) {
78                 log.warn("Missing owner ID. Returning to index...");
79             }
80             return mapping.findForward("admin");
81         }
82
83         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
84
85         LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN);
86         Localizable owner = lookupManager.retrieveLocalizable(ownerId);
87
88         if ( owner == null ) {
89             ActionMessages errors = new ActionMessages();
90             errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound"));
91             saveErrors(request, errors);
92             return mapping.findForward("admin");
93         }
94
95         boolean canCreateOrUpdate = false;
96         Set JavaDoc identifiers = null;
97         Set JavaDoc ownerIdentifiers = WebappUtil.getLocalizableUnIndexedFieldsIdentifiers(owner);
98         if ( owner instanceof ContentPage || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-create")) ) {
99             // Overriding field
100
if ( !request.isUserInRole("core-contentField-override") ) {
101                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
102                 return null;
103             }
104             identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
105             identifiers.removeAll(ownerIdentifiers);
106             canCreateOrUpdate = identifiers != null && !identifiers.isEmpty();
107             request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY, identifiers);
108         } else if ( owner instanceof Layout && !request.isUserInRole("core-contentField-create") ) {
109             // Creating new index or overriding
110
boolean canOverride = request.isUserInRole("core-contentField-override");
111             boolean canCreateIndexed = request.isUserInRole("core-contentField-createIndexed");
112             if ( !canCreateIndexed && !canOverride ) {
113                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
114                 return null;
115             }
116             Layout ownerLayout = (Layout) owner;
117             if (canCreateIndexed) {
118                 identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request);
119                 if (identifiers == null) {
120                     identifiers = new TreeSet JavaDoc();
121                 }
122             }
123             if (canOverride) {
124                 identifiers.addAll(WebappUtil.getOverridableFieldIdentifiers(owner, request));
125             }
126             identifiers.removeAll(ownerIdentifiers);
127             canCreateOrUpdate = identifiers != null && !identifiers.isEmpty();
128             request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY, identifiers);
129         } else {
130             // (it's AP or Layout and user can create fields) OR (its not CP,
131
// AP or Layout)
132
request.getSession().removeAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY);
133             if ( !request.isUserInRole("core-contentField-create") ) {
134                 response.sendError(HttpServletResponse.SC_FORBIDDEN);
135                 return null;
136             } else {
137                 canCreateOrUpdate = true;
138             }
139         }
140
141         if ( !canCreateOrUpdate ) {
142             ActionMessages errors = new ActionMessages();
143             errors.add("cantCreateOrUpdate", new ActionMessage("core.contentField.errors.cantCreateOrUpdate"));
144             saveErrors(request, errors);
145             request.getSession().removeAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY);
146             return mapping.findForward("listContentFields");
147         }
148
149         String JavaDoc ownerInfo = WebappUtil.getLocalizableInfo(owner, request);
150         request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo);
151
152         // save transaction token in request
153
saveToken(request);
154         return mapping.findForward("createContentField");
155     }
156 }
Popular Tags