KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentLocale > SetContentLocaleDefaultAction


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.contentLocale;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentLocale;
20 import com.blandware.atleap.model.core.MenuItem;
21 import com.blandware.atleap.service.core.MenuManager;
22 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
23 import com.blandware.atleap.service.exception.BeanNotFoundException;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ContentLocaleForm;
26 import com.blandware.atleap.webapp.util.core.LocaleUtil;
27 import org.apache.commons.validator.GenericValidator;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionForward;
30 import org.apache.struts.action.ActionMapping;
31 import org.apache.struts.action.ActionMessage;
32 import org.apache.struts.action.ActionMessages;
33 import org.springframework.orm.ObjectOptimisticLockingFailureException;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * <p>Sets locale default if it is not
42  * </p>
43  * <p><a HREF="SetContentLocaleDefaultAction.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  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
48  * @version $Revision: 1.13 $ $Date: 2006/03/10 17:10:19 $
49  * @struts.action path="/core/contentLocale/setDefault"
50  * name="contentLocaleForm"
51  * scope="request"
52  * validate="false"
53  * roles="core-contentLocale-setDefault"
54  * @struts.action-forward name="listContentLocales"
55  * path="/core/contentLocale/list.do"
56  * redirect="true"
57  * @struts.action-forward name="unsatisfiable"
58  * path="/core/contentLocale/list.do"
59  */

60 public final class SetContentLocaleDefaultAction extends BaseAction {
61
62     private transient MenuManager menuManager = null;
63
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         ContentLocaleForm contentLocaleForm = (ContentLocaleForm) form;
77
78         String JavaDoc contentLocaleIdentifier = null;
79         if ( !GenericValidator.isBlankOrNull(contentLocaleForm.getIdentifier()) ) {
80             contentLocaleIdentifier = contentLocaleForm.getIdentifier();
81         } else {
82             if ( log.isWarnEnabled() ) {
83                 log.warn("Missing content locale ID. Returning to list.");
84             }
85             return mapping.findForward("listContentLocales");
86         }
87
88         if ( menuManager == null ) {
89             menuManager = (MenuManager) getBean(Constants.MENU_MANAGER_BEAN);
90         }
91
92         ContentLocale contentLocale = LocaleUtil.getInstance(servlet.getServletContext()).retrieveContentLocale(contentLocaleIdentifier);
93
94         if ( contentLocale == null ) {
95             // content locale not found. it might be deleted by someone else
96
ActionMessages errors = new ActionMessages();
97             errors.add("contentLocaleNotFound", new ActionMessage("core.contentLocale.errors.notFound"));
98             saveErrors(request, errors);
99             return mapping.findForward("listContentLocales");
100         }
101
102         boolean alreadyDefault = contentLocale.getDefaultInstance() != null ? contentLocale.getDefaultInstance().booleanValue() : false;
103
104         try {
105             if ( !alreadyDefault ) {
106                 ContentLocale defaultLocale = LocaleUtil.getInstance(servlet.getServletContext()).getDefaultLocale();
107                 if ( defaultLocale != null ) {
108                     defaultLocale.setDefaultInstance(Boolean.FALSE);
109                     LocaleUtil.getInstance(servlet.getServletContext()).updateContentLocale(defaultLocale);
110
111                     // we must change locale identifier of all hard-coded menu items
112
List JavaDoc hardcodedItems = menuManager.listMenuItems(null).asList();
113                     for ( Iterator JavaDoc i = hardcodedItems.iterator(); i.hasNext(); ) {
114                         MenuItem menuItem = (MenuItem) i.next();
115                         processFieldValues(menuItem, contentLocale.getIdentifier());
116                     }
117                 }
118
119                 contentLocale.setDefaultInstance(Boolean.TRUE);
120                 contentLocale.setActive(Boolean.TRUE);
121                 LocaleUtil.getInstance(servlet.getServletContext()).updateContentLocale(contentLocale);
122             }
123         } catch ( ObjectOptimisticLockingFailureException e ) {
124             // content locale was updated or deleted by another transaction
125
ActionMessages errors = new ActionMessages();
126             errors.add("updateFailed", new ActionMessage("core.contentLocale.errors.updateFailed"));
127             saveErrors(request, errors);
128             return mapping.findForward("listContentLocales");
129         }
130
131         return mapping.findForward("listContentLocales");
132     }
133
134     /**
135      * Changes locale identifier of title and tooltip fields of menu item
136      *
137      * @param menuItem Menu item to process field values of
138      * @param localeIdentifier Locale identifier to set
139      */

140     protected void processFieldValues(MenuItem menuItem, String JavaDoc localeIdentifier) throws BeanNotFoundException, BeanAlreadyExistsException {
141
142         if ( menuItem.getTitle() != null && !menuItem.getTitle().isEmpty() ) {
143             String JavaDoc title = (String JavaDoc) menuItem.getTitle().values().iterator().next();
144             menuItem.getTitle().clear();
145             menuItem.getTitle().put(localeIdentifier, title);
146         }
147
148         if ( menuItem.getToolTip() != null && !menuItem.getToolTip().isEmpty() ) {
149             String JavaDoc toolTip = (String JavaDoc) menuItem.getToolTip().values().iterator().next();
150             menuItem.getToolTip().clear();
151             menuItem.getToolTip().put(localeIdentifier, toolTip);
152         }
153
154         // recursive call of this method on child items
155
List JavaDoc childItems = menuItem.getChildItems();
156         for ( Iterator JavaDoc i = childItems.iterator(); i.hasNext(); ) {
157             MenuItem childItem = (MenuItem) i.next();
158             if ( !childItem.isDynamic() ) {
159                 processFieldValues(childItem, localeIdentifier);
160             }
161         }
162
163     }
164 }
Popular Tags