KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
20 import com.blandware.atleap.service.core.ContentFieldManager;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.ContentFieldForm;
23 import com.blandware.atleap.webapp.util.core.LocaleUtil;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import com.blandware.atleap.webapp.util.core.WebappUtil;
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * <p>Exposes bean to request to view its properties on JSP page (content field
41  * is to be viewed)
42  * </p>
43  * <p><a HREF="ViewContentFieldAction.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.28 $ $Date: 2006/03/17 15:39:52 $
48  * @struts.action path="/core/contentField/view"
49  * name="contentFieldForm"
50  * scope="request"
51  * validate="false"
52  * roles="core-contentField-view"
53  * @struts.action-forward name="viewContentField"
54  * path=".core.contentField.view"
55  * @struts.action-forward name="listContentFields"
56  * path="/core/contentField/list.do"
57  * redirect="false"
58  */

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

69     public ActionForward execute(ActionMapping mapping, ActionForm form,
70                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71
72         ContentFieldForm contentFieldForm = (ContentFieldForm) form;
73
74         // Check whether it's view of field or choice of locales to edit field
75
String JavaDoc redirectUrl = request.getParameter("redirectUrl");
76         if ( !GenericValidator.isBlankOrNull(redirectUrl) ) {
77             // It's just a choice
78
request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl);
79         } else if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) {
80             // It's a view
81
request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY);
82         } else {
83             // That can be any of two
84
}
85
86         Long JavaDoc contentFieldId = null;
87         if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) {
88             contentFieldId = Long.valueOf(contentFieldForm.getId());
89         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) {
90             contentFieldId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY);
91         } else {
92
93             Long JavaDoc ownerId = null;
94             if ( contentFieldForm.getOwnerId() != null && contentFieldForm.getOwnerId().length() > 0 ) {
95                 ownerId = Long.valueOf(contentFieldForm.getOwnerId());
96             } else {
97                 if ( log.isWarnEnabled() ) {
98                     log.warn("Missing owner ID. Returning to index...");
99                 }
100                 return mapping.findForward("admin");
101             }
102
103             request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
104
105             if ( log.isWarnEnabled() ) {
106                 log.warn("Missing content field ID. Returning to list...");
107             }
108
109             return mapping.findForward("listContentFields");
110         }
111
112         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
113         ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId);
114         if ( contentField == null ) {
115             // content field not found. it might be deleted by someone else
116
ActionMessages errors = new ActionMessages();
117             errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound"));
118             saveErrors(request, errors);
119             return mapping.findForward("listContentFields");
120         }
121
122         String JavaDoc fieldIdentifier = contentField.getIdentifier();
123         int k = fieldIdentifier.indexOf('[');
124         String JavaDoc index = null;
125         if ( k != -1 ) {
126             index = fieldIdentifier.substring(k + 1, fieldIdentifier.indexOf(']', k));
127             fieldIdentifier = fieldIdentifier.substring(0, k);
128             contentFieldForm.setIdentifier(fieldIdentifier);
129             contentFieldForm.setIndex(index);
130         }
131
132         // create a marker
133
List JavaDoc contentFieldValues = contentField.getContentFieldValues();
134         int cfvSize = contentFieldValues == null ? 0 : contentFieldValues.size();
135
136         Collection JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
137         int clSize = contentLocales == null ? 0 : contentLocales.size();
138
139         if ( cfvSize == clSize ) {
140             request.setAttribute("isCFVCreate", Boolean.FALSE);
141         } else {
142             request.setAttribute("isCFVCreate", Boolean.TRUE);
143         }
144
145         request.setAttribute("contentField", contentField);
146
147         // get information about owner
148
Localizable owner = contentField.getOwner();
149         String JavaDoc ownerInfo = WebappUtil.getLocalizableInfo(owner, request);
150         request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo);
151
152         boolean canUpdateCF = request.isUserInRole("core-contentField-update");
153         boolean canUpdateOverridenCF = request.isUserInRole("core-contentField-updateOverriden");
154         boolean canUpdateIndexedCF = request.isUserInRole("core-contentField-updateIndexed");
155         boolean allowedIndexedField = index != null && !index.equalsIgnoreCase("0");
156
157         Set JavaDoc identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request);
158         boolean overridenField = identifiers.contains(fieldIdentifier);
159
160         if ( canUpdateCF || (owner instanceof ContentPage && canUpdateOverridenCF) || (owner instanceof ActionPage && canUpdateOverridenCF && overridenField) || (owner instanceof Layout && ((canUpdateIndexedCF && allowedIndexedField) || (canUpdateOverridenCF && overridenField))) ) {
161             request.setAttribute("updateAllowed", "true");
162         }
163
164         saveToken(request);
165         return mapping.findForward("viewContentField");
166     }
167 }
Popular Tags