KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentFieldValue > CallCreateOrUpdateContentFieldValueAction


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.contentFieldValue;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.ConvertUtil;
20 import com.blandware.atleap.model.core.ContentField;
21 import com.blandware.atleap.model.core.ContentFieldValue;
22 import com.blandware.atleap.model.core.ContentLocale;
23 import com.blandware.atleap.service.core.ContentFieldManager;
24 import com.blandware.atleap.service.core.ContentLocaleManager;
25 import com.blandware.atleap.webapp.action.core.BaseAction;
26 import com.blandware.atleap.webapp.form.core.ContentFieldValueForm;
27 import com.blandware.atleap.webapp.util.core.LocaleUtil;
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.*;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.*;
36
37 /**
38  * <p>Prepares form bean to create or update content field value's data
39  * </p>
40  * <p><a HREF="CallUpdateContentFieldValueAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
44  * @version $Revision: 1.14 $ $Date: 2006/03/16 11:09:41 $
45  * @struts.action path="/core/contentFieldValue/callCreateOrUpdate"
46  * name="contentFieldValueForm"
47  * scope="request"
48  * validate="false"
49  * roles="core-contentFieldValue-createOrUpdate, core-contentFieldValue-view"
50  * @struts.action-forward name="createOrUpdateContentFieldValue"
51  * path=".core.contentFieldValue.createOrUpdate"
52  * @struts.action-forward name="listContentFieldValues"
53  * path="/core/contentFieldValue/list.do"
54  * redirect="false"
55  * @struts.action-forward name="listContentFields"
56  * path="/core/contentField/list.do"
57  * redirect="false"
58  */

59 public final class CallCreateOrUpdateContentFieldValueAction 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         // Check whether it's normal edit (from list of field values) or
73
// it was invoked from context menu (or from 'view linked objects' page)
74

75         String JavaDoc redirectUrl = request.getParameter("redirectUrl");
76         if ( !GenericValidator.isBlankOrNull(redirectUrl) ) {
77             // It was invoked from context menu
78
request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl);
79         }
80
81         request.getSession().removeAttribute(WebappConstants.CONTENT_FIELD_VALUE_SUBMITTED_ACTION_KEY);
82
83         ContentFieldValueForm contentFieldValueForm = (ContentFieldValueForm) form;
84         Long JavaDoc contentFieldId = null;
85         if ( !GenericValidator.isBlankOrNull(contentFieldValueForm.getContentFieldId()) ) {
86             contentFieldId = Long.valueOf(contentFieldValueForm.getContentFieldId());
87         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) {
88             contentFieldId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY);
89         } else {
90             if ( log.isWarnEnabled() ) {
91                 log.warn("Missing content field ID. Returning to index...");
92             }
93             return mapping.findForward("admin");
94         }
95
96         request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId);
97
98         if ( isCancelled(request) ) {
99             // if there's redirect URL in session, return to that URL
100
String JavaDoc url = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
101             if (!GenericValidator.isBlankOrNull(url)) {
102                 request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY);
103                 return new ActionForward(url, true);
104             }
105             return mapping.findForward("listContentFieldValues");
106         }
107
108         if (!request.isUserInRole("core-contentFieldValue-createOrUpdate")) {
109             response.sendError(HttpServletResponse.SC_FORBIDDEN);
110             return null;
111         }
112
113         // remove attribute 'inputId' from session, if it exists
114
request.getSession().removeAttribute(WebappConstants.HTML_INPUT_TAG_ID_KEY);
115
116         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
117         ContentLocaleManager contentLocaleManager = (ContentLocaleManager) getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN);
118
119         List cfvLocales = null;
120
121         if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY) != null ) {
122             cfvLocales = (List) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY);
123         } else if ( !GenericValidator.isBlankOrNull(request.getParameter("locale")) ) {
124             String JavaDoc locale = request.getParameter("locale");
125             cfvLocales = new ArrayList();
126             cfvLocales.add(locale);
127         }
128
129         // retrieve requested content field
130

131         ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId);
132
133         if ( contentField == null ) {
134             // content field not found. it might has already been deleted
135
ActionMessages errors = new ActionMessages();
136             errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound"));
137             saveErrors(request, errors);
138             return mapping.findForward("listContentFields");
139         }
140
141         // export field into session
142
request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_KEY, contentField);
143
144         SortedMap contentFieldValues = new TreeMap();
145         boolean CFVLocalesSpecified = cfvLocales != null && cfvLocales.size() > 0;
146
147         if ( !CFVLocalesSpecified ) {
148             List activeLocales = contentLocaleManager.getActiveLocales();
149             Map contentFieldValuesMap = contentField.getContentFieldValuesMap();
150             for (Iterator i = activeLocales.iterator(); i.hasNext();) {
151                 ContentLocale contentLocale = (ContentLocale) i.next();
152                 ContentFieldValue contentFieldValue = (ContentFieldValue) contentFieldValuesMap.get(contentLocale.getIdentifier());
153                 contentFieldValues.put(contentLocale, contentFieldValue);
154             }
155         } else {
156             for ( int i = 0; i < cfvLocales.size(); i++ ) {
157                 String JavaDoc localeIdentifier = (String JavaDoc) cfvLocales.get(i);
158                 ContentLocale locale = contentLocaleManager.retrieveContentLocale(localeIdentifier);
159                 if ( locale != null ) {
160                     ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByContentFieldIdAndLocale(contentFieldId, localeIdentifier);
161                     contentFieldValues.put(locale, contentFieldValue);
162                 }
163             }
164
165         }
166
167         if ( contentFieldValues == null || contentFieldValues.isEmpty() ) {
168             // content field value not found. it might be deleted by someone else
169
ActionMessages errors = new ActionMessages();
170             errors.add("contentFieldValueNotFound", new ActionMessage("core.contentFieldValue.errors.nothingFound"));
171             saveErrors(request, errors);
172             return mapping.findForward("listContentFieldValues");
173         }
174
175         Map mergedMap = null;
176         if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUES_MERGED_KEY) != null ) {
177             mergedMap = (Map) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUES_MERGED_KEY);
178         }
179
180         for ( Iterator i = contentFieldValues.entrySet().iterator(); i.hasNext(); ) {
181             Map.Entry entry = (Map.Entry) i.next();
182             ContentLocale locale = (ContentLocale) entry.getKey();
183             ContentFieldValue contentFieldValue = (ContentFieldValue) entry.getValue();
184             String JavaDoc key = locale.getIdentifier();
185
186             contentFieldValueForm.getLocaleMap().put(key, locale);
187             if ( contentFieldValue != null ) {
188                 contentFieldValueForm.setId(key, contentFieldValue.getId());
189                 if ( mergedMap == null ) {
190                     String JavaDoc newValue = null;
191                     if ( contentFieldValue.getContentField().getType() == ContentField.LINE_TYPE ) {
192                         newValue = contentFieldValue.getSimpleValue();
193                     } else {
194                         newValue = ConvertUtil.convertToString(contentFieldValue.getValue());
195                     }
196                     contentFieldValueForm.setValue(key, newValue);
197                 } else {
198                     contentFieldValueForm.setValue(key, mergedMap.get(contentFieldValue.getContentLocale().getIdentifier()));
199                 }
200                 contentFieldValueForm.setVersion(key, contentFieldValue.getVersion());
201             } else {
202                 contentFieldValueForm.setId(key, new Long JavaDoc(-1));
203                 contentFieldValueForm.setValue(key, new String JavaDoc());
204                 contentFieldValueForm.setVersion(key, new Integer JavaDoc(0));
205             }
206         }
207
208         // add empty values for locales, for which content has not been created yet
209
// this step is performed if and only if no CFV locale has been provided
210
if ( !CFVLocalesSpecified ) {
211             List contentLocales = new ArrayList(LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales());
212             for ( Iterator i = contentLocales.iterator(); i.hasNext(); ) {
213                 ContentLocale locale = (ContentLocale) i.next();
214                 String JavaDoc identifier = locale.getIdentifier();
215                 if ( !contentFieldValueForm.getIdMap().containsKey(identifier) ) {
216                     contentFieldValueForm.setId(identifier, new Long JavaDoc(-1));
217                     contentFieldValueForm.setValue(identifier, new String JavaDoc());
218                     contentFieldValueForm.setVersion(identifier, new Integer JavaDoc(0));
219                     // TODO: fix it
220
// methods getLocale and setLocale are not available in form, because there is 'locale' parameter
221
// in request in some actions, so use java.util.Map#put
222
contentFieldValueForm.getLocaleMap().put(identifier, locale);
223                 }
224             }
225         }
226
227         // get information about owner
228
String JavaDoc ownerInfo = WebappUtil.getLocalizableInfo(contentField.getOwner(), request);
229         request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo);
230
231         return mapping.findForward("createOrUpdateContentFieldValue");
232     }
233
234 }
Popular Tags