KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > menuItem > CreateMenuItemAction


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.menuItem;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.*;
20 import com.blandware.atleap.service.core.MenuManager;
21 import com.blandware.atleap.service.core.RoleManager;
22 import com.blandware.atleap.service.exception.OwnerNotFoundException;
23 import com.blandware.atleap.service.exception.ParentItemNotFoundException;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.MenuItemForm;
26 import com.blandware.atleap.webapp.util.core.CacheUtil;
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.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * <p>Creates new menu item
44  * </p>
45  * <p><a HREF="CreateMenuItemAction.java.htm"><i>View Source</i></a></p>
46  * <p/>
47  *
48  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
49  * @version $Revision: 1.36 $ $Date: 2006/03/10 17:10:28 $
50  * @struts.action path="/core/menuItem/create"
51  * name="menuItemForm"
52  * scope="request"
53  * input="inputForward"
54  * validate="true"
55  * roles="core-menuItem-create"
56  * @struts.action-forward name="inputForward"
57  * path=".core.menuItem.create"
58  * @struts.action-forward name="listMenuItems"
59  * path="/core/menuItem/list.do"
60  * redirect="false"
61  * @struts.action-forward name="unsatisfiable"
62  * path="/core/menuItem/list.do"
63  */

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

74     public ActionForward execute(ActionMapping mapping, ActionForm form,
75                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
76
77         MenuItemForm menuItemForm = (MenuItemForm) form;
78         Long JavaDoc ownerId = null;
79         if ( !GenericValidator.isBlankOrNull(menuItemForm.getOwnerId()) ) {
80             ownerId = Long.valueOf(menuItemForm.getOwnerId());
81         } else {
82             if ( log.isWarnEnabled() ) {
83                 log.warn("Missing owner ID. Returning to index...");
84             }
85             return mapping.findForward("admin");
86         }
87
88         request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId);
89
90         MenuManager menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
91
92         if ( !isCancelled(request) ) {
93
94             MenuItem menuItem = new MenuItem();
95
96             // create refs
97
List JavaDoc linkedPages = new ArrayList JavaDoc();
98             List JavaDoc linkedResources = new ArrayList JavaDoc();
99             List JavaDoc roles = null;
100             boolean titleIsSet = WebappUtil.hasCorrectValues(menuItemForm.getTitleMap());
101             boolean toolTipIsSet = WebappUtil.hasCorrectValues(menuItemForm.getToolTipMap());
102             String JavaDoc image = menuItemForm.getImage();
103             BaseObject linkedObject = WebappUtil.lookupObject(image, request.getSession().getServletContext(), request.getContextPath(), request);
104             if ( linkedObject != null && linkedObject instanceof ContentImage ) {
105                 linkedResources.add(linkedObject);
106             }
107
108             String JavaDoc altImage = menuItemForm.getAltImage();
109             if ( !GenericValidator.isBlankOrNull(altImage) && !altImage.equals(image) ) {
110                 linkedObject = WebappUtil.lookupObject(menuItemForm.getAltImage(), request.getSession().getServletContext(), request.getContextPath(), request);
111                 if ( linkedObject != null && linkedObject instanceof ContentImage ) {
112                     linkedResources.add(linkedObject);
113                 }
114             }
115
116             linkedObject = WebappUtil.lookupObject(menuItemForm.getLocation(), request.getSession().getServletContext(), request.getContextPath(), request);
117             if ( linkedObject != null ) {
118                 if ( linkedObject instanceof ContentResource ) {
119                     ContentResource resource = (ContentResource) linkedObject;
120                     roles = new ArrayList JavaDoc(resource.getRoles());
121                     linkedResources.add(resource);
122                 } else if ( linkedObject instanceof Page ) {
123                     Page page = (Page) linkedObject;
124                     linkedPages.add(page);
125
126                     // fill in title and tool tip
127
if ( !titleIsSet ) {
128                         ContentField titleField = (ContentField) page.getContentFieldsMap().get("title");
129                         List JavaDoc titleValues = titleField.getContentFieldValues();
130                         for ( Iterator JavaDoc i = titleValues.iterator(); i.hasNext(); ) {
131                             ContentFieldValue value = (ContentFieldValue) i.next();
132                             menuItemForm.setTitle(value.getContentLocale().getIdentifier(), value.getSimpleValue());
133                         }
134                         titleIsSet = true;
135                     }
136
137                     if ( !toolTipIsSet ) {
138                         ContentField descriptionField = (ContentField) page.getContentFieldsMap().get("description");
139                         if ( descriptionField != null ) {
140                             List JavaDoc descriptionValues = descriptionField.getContentFieldValues();
141                             for ( Iterator JavaDoc i = descriptionValues.iterator(); i.hasNext(); ) {
142                                 ContentFieldValue value = (ContentFieldValue) i.next();
143                                 menuItemForm.setToolTip(value.getContentLocale().getIdentifier(), value.getSimpleValue());
144                             }
145                             toolTipIsSet = true;
146                         }
147                     }
148
149                     // set roles
150
if ( page instanceof ContentPage ) {
151                         ContentPage cp = (ContentPage) page;
152                         roles = new ArrayList JavaDoc(cp.getRoles());
153                     } else if ( page instanceof ActionPage ) {
154                         ActionPage ap = (ActionPage) page;
155                         String JavaDoc[] roleNames = WebappUtil.getAPRoleNames(ap.getUri(), request);
156                         roles = new ArrayList JavaDoc();
157                         RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
158                         for ( int i = 0; i < roleNames.length; i++ ) {
159                             String JavaDoc roleName = roleNames[i];
160                             Role role = roleManager.retrieveRole(roleName);
161                             if ( role != null ) {
162                                 roles.add(role);
163                             } else {
164                                 // can it happen?
165
if ( log.isWarnEnabled() ) {
166                                     log.warn("Role with name '" + roleName + "', specified for Action with URI '" + ap.getUri() + "' could not be found. Skipped.");
167                                 }
168                             }
169                         }
170                     }
171                 }
172             } else {
173                 // set external location flag
174
menuItem.setExternalLocation(Boolean.TRUE);
175             }
176
177             if ( !titleIsSet ) {
178                 // title must be specified
179
ActionMessages errors = new ActionMessages();
180                 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.menuItem.form.title")));
181                 saveErrors(request, errors, false);
182                 saveToken(request);
183                 return mapping.getInputForward();
184             }
185
186             Long JavaDoc parentItemId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.MENU_ITEM_PARENT_ID_KEY);
187
188             menuItem.setLinkedResources(linkedResources);
189             menuItem.setLinkedPages(linkedPages);
190             menuItem.setRoles(roles);
191
192             WebappUtil.copyProperties(menuItem, menuItemForm, request);
193             menuItem.setTitle(menuItemForm.getTitleMap());
194             menuItem.setToolTip(menuItemForm.getToolTipMap());
195             menuItem.setOrigItem(null);
196             menuItem.setIdentifier(null);
197             menuItem.setPosition(null);
198
199             // set to null properties related only to hard-coded elements
200

201             menuItem.setAnchor(null);
202             menuItem.setAction(null);
203             menuItem.setForward(null);
204             menuItem.setOnmouseover(null);
205             menuItem.setOnmouseout(null);
206             menuItem.setOnclick(null);
207
208             // set to default properties which were not specified in form
209
if ( GenericValidator.isBlankOrNull(menuItemForm.getStyle()) ) {
210                 menuItem.setStyle(null);
211             }
212
213             if ( GenericValidator.isBlankOrNull(menuItemForm.getStyleClass()) ) {
214                 menuItem.setStyleClass(null);
215             }
216
217             if ( GenericValidator.isBlankOrNull(menuItemForm.getAlign()) ) {
218                 menuItem.setAlign(null);
219             }
220
221             if ( GenericValidator.isBlankOrNull(menuItemForm.getImage()) ) {
222                 menuItem.setImage(null);
223             }
224
225             if ( GenericValidator.isBlankOrNull(menuItemForm.getAltImage()) ) {
226                 menuItem.setAltImage(null);
227             }
228
229
230             try {
231                 menuManager.createMenuItem(menuItem, parentItemId, ownerId);
232             } catch ( OwnerNotFoundException e ) {
233                 ActionMessages errors = new ActionMessages();
234                 errors.add("ownerNotFound", new ActionMessage("core.menuItem.errors.ownerNotFound"));
235                 saveErrors(request, errors);
236                 return mapping.findForward("admin");
237             } catch ( ParentItemNotFoundException e ) {
238                 ActionMessages errors = new ActionMessages();
239                 errors.add("parentItemNotFound", new ActionMessage("core.menuItem.errors.parentItemNotFound"));
240                 saveErrors(request, errors);
241                 return mapping.findForward("listMenuItems");
242             }
243
244         }
245
246         // flush cache
247
CacheUtil cacheUtil = CacheUtil.getInstance(request);
248         cacheUtil.flushMenuCache();
249         cacheUtil.flushContentPageCache();
250
251         return mapping.findForward("listMenuItems");
252     }
253 }
Popular Tags