KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > linkedObjects > ViewLinkedObjectsAction


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.linkedObjects;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentField;
20 import com.blandware.atleap.model.core.ContentFieldValue;
21 import com.blandware.atleap.model.core.ContentResource;
22 import com.blandware.atleap.model.core.MenuItem;
23 import com.blandware.atleap.model.core.Page;
24 import com.blandware.atleap.service.core.ContentResourceManager;
25 import com.blandware.atleap.service.core.PageManager;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import org.apache.commons.validator.GenericValidator;
30 import org.apache.struts.Globals;
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.ListIterator JavaDoc;
42 import java.util.Locale JavaDoc;
43
44 /**
45  * <p>Exposes lists of content field values and menu items which refer to object of given type and
46  * with given identifier
47  * </p>
48  * <p><a HREF="ViewLinkedObjectsAction.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 Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
53  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
54  * @version $Revision: 1.12 $ $Date: 2006/03/22 11:22:06 $
55  * @struts.action path="/core/linkedObjects/view"
56  * scope="request"
57  * validate="false"
58  * roles="core-linkedObjects-view"
59  * @struts.action-forward name="viewLinkedObjects"
60  * path=".core.linkedObjects.view"
61  */

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

72     public ActionForward execute(ActionMapping mapping, ActionForm form,
73                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
74
75         Long JavaDoc id = null;
76         if ( !GenericValidator.isBlankOrNull(request.getParameter("id")) ) {
77             id = Long.valueOf(request.getParameter("id"));
78         } else if (request.getAttribute(WebappConstants.LINKED_OBJECT_ID_KEY) != null) {
79             id = (Long JavaDoc) request.getAttribute(WebappConstants.LINKED_OBJECT_ID_KEY);
80         } else if (request.getSession().getAttribute(WebappConstants.LINKED_OBJECT_ID_KEY) != null) {
81             id = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.LINKED_OBJECT_ID_KEY);
82         } else {
83             if ( log.isWarnEnabled() ) {
84                 log.warn("Object ID is not specified. Returning to index...");
85             }
86             return mapping.findForward("admin");
87         }
88
89         request.getSession().setAttribute(WebappConstants.LINKED_OBJECT_ID_KEY, id);
90
91         String JavaDoc type = request.getParameter("type");
92         if ( GenericValidator.isBlankOrNull(type) ) {
93             type = (String JavaDoc) request.getAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY);
94         }
95         if ( GenericValidator.isBlankOrNull(type) ) {
96             type = (String JavaDoc) request.getSession().getAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY);
97         }
98         if ( !"resource".equalsIgnoreCase("type") && !"page".equalsIgnoreCase(type) ) {
99             type = "resource";
100         }
101         request.getSession().setAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY, type);
102
103         String JavaDoc redirectUrl = null;
104         if (!GenericValidator.isBlankOrNull(request.getParameter("redirectUrl"))) {
105             redirectUrl = request.getParameter("redirectUrl");
106         } else if (!GenericValidator.isBlankOrNull((String JavaDoc) request.getSession().getAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY))) {
107             redirectUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY);
108         }
109         request.setAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY, redirectUrl);
110
111         if ("true".equalsIgnoreCase(request.getParameter("justView"))) {
112             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECT_NEW_URI_KEY);
113             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECT_CONTENT_LOCALE_ID_KEY);
114             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECT_CHARSET_KEY);
115             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECT_WIDTH_KEY);
116             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECT_HEIGHT_KEY);
117             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY);
118             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_DELETE_ACTION_KEY);
119         }
120
121         List JavaDoc linkedContentFieldValues = new ArrayList JavaDoc();
122         List JavaDoc linkedMenuItems = new ArrayList JavaDoc();
123         if ( "resource".equalsIgnoreCase(type) ) {
124             ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
125             ContentResource resource = contentResourceManager.retrieveContentResource(id);
126             if ( resource == null ) {
127                 ActionMessages errors = new ActionMessages();
128                 errors.add("resourceNotFound", new ActionMessage("core.contentResource.errors.notFound"));
129                 saveErrors(request, errors);
130                 return mapping.findForward("admin");
131             }
132             linkedContentFieldValues = new ArrayList JavaDoc(resource.getLinkedContentFieldValues());
133             linkedMenuItems = new ArrayList JavaDoc(resource.getLinkedMenuItems());
134         } else {
135             PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
136             Page page = pageManager.retrievePage(id);
137             if ( page == null ) {
138                 ActionMessages errors = new ActionMessages();
139                 errors.add("pageNotFound", new ActionMessage("core.page.errors.notFound"));
140                 saveErrors(request, errors);
141                 return mapping.findForward("admin");
142             }
143             linkedContentFieldValues = new ArrayList JavaDoc(page.getLinkedContentFieldValues());
144             linkedMenuItems = new ArrayList JavaDoc(page.getLinkedMenuItems());
145         }
146
147         for ( ListIterator JavaDoc i = linkedContentFieldValues.listIterator(); i.hasNext(); ) {
148             ContentFieldValue contentFieldValue = (ContentFieldValue) i.next();
149             i.set(new CFVInfo(contentFieldValue, request));
150         }
151
152         for ( ListIterator JavaDoc i = linkedMenuItems.listIterator(); i.hasNext(); ) {
153             MenuItem menuItem = (MenuItem) i.next();
154             i.set(new MenuItemInfo(menuItem, request));
155         }
156
157         request.setAttribute(WebappConstants.LINKED_CONTENT_FIELD_VALUES_COLLECTION_KEY, linkedContentFieldValues);
158         request.setAttribute(WebappConstants.LINKED_MENU_ITEMS_COLLECTION_KEY, linkedMenuItems);
159
160         if (request.getSession().getAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY) != null) {
161             saveToken(request);
162         }
163
164         return mapping.findForward("viewLinkedObjects");
165     }
166
167     /**
168      * Helper class to expose information about linked content field value
169      */

170     public static class CFVInfo {
171
172         /**
173          * Information about owner of corresponding content field
174          */

175         protected String JavaDoc fieldOwnerInfo;
176
177         /**
178          * Identifier of owning content field
179          */

180         protected String JavaDoc fieldIdentifier;
181
182         /**
183          * Language of content field value
184          */

185         protected String JavaDoc language;
186
187         /**
188          * ID of owning content field
189          */

190         protected Long JavaDoc fieldId;
191
192         /**
193          * ID of content field value
194          */

195         protected Long JavaDoc fieldValueId;
196
197         /**
198          * Creates new instance of CFVInfo
199          *
200          * @param contentFieldValue Content field value to get info about
201          * @param request HTTP servlet request we are currently proceeding
202          */

203         public CFVInfo(ContentFieldValue contentFieldValue, HttpServletRequest JavaDoc request) {
204             ContentField contentField = contentFieldValue.getContentField();
205             this.fieldId = contentField.getId();
206             this.fieldIdentifier = contentField.getIdentifier();
207             this.fieldOwnerInfo = WebappUtil.getLocalizableInfo(contentField.getOwner(), request);
208             this.language = contentFieldValue.getContentLocale().getIdentifier();
209             this.fieldValueId = contentFieldValue.getId();
210         }
211
212         /**
213          * Returns field owner info
214          *
215          * @return field owner info
216          */

217         public String JavaDoc getFieldOwnerInfo() {
218             return fieldOwnerInfo;
219         }
220
221         /**
222          * Sets field owner info
223          *
224          * @param fieldOwnerInfo field owner info to set
225          */

226         public void setFieldOwnerInfo(String JavaDoc fieldOwnerInfo) {
227             this.fieldOwnerInfo = fieldOwnerInfo;
228         }
229
230         /**
231          * Returns field identifier
232          *
233          * @return field identifier
234          */

235         public String JavaDoc getFieldIdentifier() {
236             return fieldIdentifier;
237         }
238
239         /**
240          * Sets field identifier
241          *
242          * @param fieldIdentifier to set
243          */

244         public void setFieldIdentifier(String JavaDoc fieldIdentifier) {
245             this.fieldIdentifier = fieldIdentifier;
246         }
247
248         /**
249          * Returns language
250          *
251          * @return language
252          */

253         public String JavaDoc getLanguage() {
254             return language;
255         }
256
257         /**
258          * Sets language
259          *
260          * @param language language to set
261          */

262         public void setLanguage(String JavaDoc language) {
263             this.language = language;
264         }
265
266         /**
267          * Returns field ID
268          *
269          * @return field ID
270          */

271         public Long JavaDoc getFieldId() {
272             return fieldId;
273         }
274
275         /**
276          * Sets field ID
277          *
278          * @param fieldId field ID to set
279          */

280         public void setFieldId(Long JavaDoc fieldId) {
281             this.fieldId = fieldId;
282         }
283
284         /**
285          * Returns field value ID
286          *
287          * @return field value ID
288          */

289         public Long JavaDoc getFieldValueId() {
290             return fieldValueId;
291         }
292
293         /**
294          * Sets field value ID
295          *
296          * @param fieldValueId field value ID to set
297          */

298         public void setFieldValueId(Long JavaDoc fieldValueId) {
299             this.fieldValueId = fieldValueId;
300         }
301
302     }
303
304     /**
305      * Helper class to expose information about linked menu item
306      */

307     public static class MenuItemInfo {
308
309         /**
310          * Title of menu item
311          */

312         protected String JavaDoc title;
313
314         /**
315          * Information about layer, on which this menu item has been created
316          */

317         protected String JavaDoc ownerInfo;
318
319         /**
320          * ID of menu item
321          */

322         protected Long JavaDoc menuItemId;
323
324         /**
325          * ID of menu item's owner
326          */

327         protected Long JavaDoc ownerId;
328
329         /**
330          * Whether or not associated menu item is dynamic
331          */

332         protected boolean dynamic;
333
334         /**
335          * Creates new instance of menu item info
336          *
337          * @param menuItem Menu item to get info about
338          * @param request HTTP servlet request we are currently proceeding
339          */

340         public MenuItemInfo(MenuItem menuItem, HttpServletRequest JavaDoc request) {
341             Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
342             this.title = WebappUtil.getFieldValue(menuItem.getTitle(), locale.getLanguage(), request, true);
343             this.ownerInfo = WebappUtil.getLocalizableInfo(menuItem.getOwner(), request);
344             this.ownerId = null;
345             if ( menuItem.getOwner() != null ) {
346                 this.ownerId = menuItem.getOwner().getId();
347             }
348             this.menuItemId = menuItem.getId();
349             this.dynamic = menuItem.isDynamic();
350         }
351
352         /**
353          * Returns title
354          *
355          * @return title
356          */

357         public String JavaDoc getTitle() {
358             return title;
359         }
360
361         /**
362          * Sets title
363          *
364          * @param title title to set
365          */

366         public void setTitle(String JavaDoc title) {
367             this.title = title;
368         }
369
370         /**
371          * Returns owner info
372          *
373          * @return owner info
374          */

375         public String JavaDoc getOwnerInfo() {
376             return ownerInfo;
377         }
378
379         /**
380          * Sets owner info
381          *
382          * @param ownerInfo owner info to set
383          */

384         public void setOwnerInfo(String JavaDoc ownerInfo) {
385             this.ownerInfo = ownerInfo;
386         }
387
388         /**
389          * Returns menu item ID
390          *
391          * @return menu item ID
392          */

393         public Long JavaDoc getMenuItemId() {
394             return menuItemId;
395         }
396
397         /**
398          * Sets menu item ID
399          *
400          * @param menuItemId menu item ID to set
401          */

402         public void setMenuItemId(Long JavaDoc menuItemId) {
403             this.menuItemId = menuItemId;
404         }
405
406         /**
407          * Returns owner ID
408          *
409          * @return owner ID
410          */

411         public Long JavaDoc getOwnerId() {
412             return ownerId;
413         }
414
415         /**
416          * Sets owner ID
417          *
418          * @param ownerId owner ID to set
419          */

420         public void setOwnerId(Long JavaDoc ownerId) {
421             this.ownerId = ownerId;
422         }
423
424         /**
425          * Return whether menu item is dynamic
426          *
427          * @return whether menu item is dynamic
428          */

429         public boolean isDynamic() {
430             return dynamic;
431         }
432
433         /**
434          * Sets whether menu item is dynamic
435          *
436          * @param dynamic whether menu item is dynamic
437          */

438         public void setDynamic(boolean dynamic) {
439             this.dynamic = dynamic;
440         }
441     }
442 }
Popular Tags