KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.util.StringUtil;
21 import com.blandware.atleap.model.core.ContentField;
22 import com.blandware.atleap.model.core.ContentFieldValue;
23 import com.blandware.atleap.model.core.ContentLocale;
24 import com.blandware.atleap.service.core.ContentFieldManager;
25 import com.blandware.atleap.service.core.ContentLocaleManager;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.core.ContentFieldValueForm;
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.ArrayList JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.SortedMap JavaDoc;
42 import java.util.TreeMap JavaDoc;
43
44 /**
45  * <p>Exposes requested ContentFieldValue bean to request to view its properties on JSP page
46  * </p>
47  * <p><a HREF="ViewContentFieldValueAction.java.htm"><i>View Source</i></a></p>
48  * <p/>
49  *
50  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
51  * @version $Revision: 1.27 $ $Date: 2006/03/10 17:10:19 $
52  * @struts.action path="/core/contentFieldValue/view"
53  * name="contentFieldValueForm"
54  * scope="request"
55  * validate="false"
56  * roles="core-contentFieldValue-view"
57  * @struts.action-forward name="viewContentFieldValue"
58  * path=".core.contentFieldValue.view"
59  * @struts.action-forward name="listContentFieldValues"
60  * path="/core/contentFieldValue/list.do"
61  * redirect="false"
62  */

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

73     public ActionForward execute(ActionMapping mapping, ActionForm form,
74                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
75
76         String JavaDoc redirectUrl = request.getParameter("redirectUrl");
77         if (!GenericValidator.isBlankOrNull(redirectUrl)) {
78             request.getSession().setAttribute(WebappConstants.REDIRECT_URL_KEY, redirectUrl);
79         }
80
81         ContentFieldValueForm contentFieldValueForm = (ContentFieldValueForm) form;
82         Long JavaDoc contentFieldId = null;
83         if ( !GenericValidator.isBlankOrNull(contentFieldValueForm.getContentFieldId()) ) {
84             contentFieldId = Long.valueOf(contentFieldValueForm.getContentFieldId());
85         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) {
86             contentFieldId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY);
87         } else {
88             if ( log.isWarnEnabled() ) {
89                 log.warn("Missing content field ID. Returning to index...");
90             }
91             return mapping.findForward("admin");
92         }
93
94         request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId);
95
96         List JavaDoc cfvLocales = null;
97
98         if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY) != null ) {
99             cfvLocales = (List JavaDoc) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY);
100         } else if ( !GenericValidator.isBlankOrNull(request.getParameter("locale")) ) {
101             String JavaDoc locale = request.getParameter("locale");
102             cfvLocales = new ArrayList JavaDoc();
103             cfvLocales.add(locale);
104         } else {
105             if ( log.isWarnEnabled() ) {
106                 log.warn("Missing content field value locale. Returning to list...");
107             }
108             return mapping.findForward("listContentFieldValues");
109         }
110
111         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
112         ContentLocaleManager contentLocaleManager = (ContentLocaleManager) getBean(Constants.CONTENT_LOCALE_MANAGER_BEAN);
113
114         // export content field to session
115
ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId);
116         request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_KEY, contentField);
117
118         SortedMap JavaDoc contentFieldValues = new TreeMap JavaDoc();
119
120         // we need string representations of our BLOB values, so create one more map
121
SortedMap JavaDoc values = new TreeMap JavaDoc();
122
123         for ( int i = 0; i < cfvLocales.size(); i++ ) {
124             String JavaDoc localeIdentifier = (String JavaDoc) cfvLocales.get(i);
125             ContentLocale locale = contentLocaleManager.retrieveContentLocale(localeIdentifier);
126             if ( locale != null ) {
127                 ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByContentFieldIdAndLocale(contentFieldId, localeIdentifier);
128                 if ( contentFieldValue != null ) {
129                     contentFieldValues.put(locale, contentFieldValue);
130                     if ( contentFieldValue.getContentField().getType() == ContentField.LINE_TYPE || contentFieldValue.getContentField().getType() == ContentField.MULTILINE_TYPE ) {
131                         String JavaDoc value = null;
132                         if ( contentFieldValue.getContentField().getType() == ContentField.LINE_TYPE ) {
133                             value = contentFieldValue.getSimpleValue();
134                         } else {
135                             byte[] valueBytes = contentFieldValue.getValue();
136                             value = ConvertUtil.convertToString(valueBytes);
137                         }
138                         value = StringUtil.elToBr(StringUtil.htmlEncode(value));
139                         values.put(contentFieldValue.getId(), value);
140                     } else {
141                         byte[] value = contentFieldValue.getValue();
142                         values.put(contentFieldValue.getId(), ConvertUtil.convertToString(value));
143                     }
144                 }
145             }
146         }
147
148         if ( contentFieldValues == null || contentFieldValues.isEmpty() ) {
149             // not content field value found. it might be deleted by someone else
150
ActionMessages errors = new ActionMessages();
151             String JavaDoc localeString = ConvertUtil.convertListToString(cfvLocales, ", ");
152             errors.add("contentFieldValueNotFound", new ActionMessage("core.contentFieldValue.errors.notFound", localeString));
153             saveErrors(request, errors);
154             return mapping.findForward("listContentFieldValues");
155         }
156
157         request.setAttribute("contentFieldValues", contentFieldValues);
158         request.setAttribute(WebappConstants.CONTENT_FIELD_VALUE_KEY, values);
159
160         // get information about owner
161
String JavaDoc ownerInfo = WebappUtil.getLocalizableInfo(contentField.getOwner(), request);
162         request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo);
163
164         return mapping.findForward("viewContentFieldValue");
165     }
166 }
Popular Tags