KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StringUtil;
19 import com.blandware.atleap.model.core.ContentLocale;
20 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.ContentLocaleForm;
23 import com.blandware.atleap.webapp.util.core.ApplicationResources;
24 import com.blandware.atleap.webapp.util.core.LocaleUtil;
25 import com.blandware.atleap.webapp.util.core.WebappUtil;
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Locale JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * <p>Creates new content locale
42  * </p>
43  * <p><a HREF="CreateContentLocaleAction.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  * @version $Revision: 1.20 $ $Date: 2006/03/10 17:10:19 $
48  * @struts.action path="/core/contentLocale/create"
49  * name="contentLocaleForm"
50  * scope="request"
51  * input="inputForward"
52  * validate="true"
53  * roles="core-contentLocale-create"
54  * @struts.action-forward name="inputForward"
55  * path=".core.contentLocale.create"
56  * @struts.action-forward name="listContentLocales"
57  * path="/core/contentLocale/list.do"
58  * redirect="true"
59  * @struts.action-forward name="unsatisfiable"
60  * path="/core/contentLocale/callCreate.do"
61  */

62 public final class CreateContentLocaleAction 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         if ( !isCancelled(request) ) {
76             ContentLocaleForm contentLocaleForm = (ContentLocaleForm) form;
77             String JavaDoc localeIdentifier = contentLocaleForm.getIdentifier();
78             LocaleUtil localeUtil = LocaleUtil.getInstance(servlet.getServletContext());
79
80             // check locales existence before setting title
81
List JavaDoc availableLocales = localeUtil.getAvailableLocales();
82             for ( Iterator JavaDoc i = availableLocales.iterator(); i.hasNext(); ) {
83                 ContentLocale locale = (ContentLocale) i.next();
84                 if ( locale.getIdentifier().equalsIgnoreCase(localeIdentifier) ) {
85                     // locale with the same identifier already exists. return input page
86
ActionMessages errors = new ActionMessages();
87                     errors.add("contentLocaleAlreadyExists", new ActionMessage("core.contentLocale.errors.alreadyExists"));
88                     saveErrors(request, errors);
89                     saveToken(request);
90                     return mapping.getInputForward();
91                 }
92             }
93
94             ContentLocale contentLocale = new ContentLocale();
95             WebappUtil.copyProperties(contentLocale, contentLocaleForm, request);
96
97             Map JavaDoc titleMap = contentLocaleForm.getTitleMap();
98
99             // put this locale identifier to get title of locale for this locale itself
100
titleMap.put(localeIdentifier, titleMap.remove("current"));
101
102             // create default titles
103
for ( Iterator JavaDoc i = titleMap.entrySet().iterator(); i.hasNext(); ) {
104                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
105                 String JavaDoc identifier = (String JavaDoc) entry.getKey();
106                 String JavaDoc title = (String JavaDoc) entry.getValue();
107                 if ( GenericValidator.isBlankOrNull(title) ) {
108                     titleMap.put(identifier, StringUtil.capitalize(new Locale(localeIdentifier).getDisplayLanguage(new Locale(identifier))));
109                 }
110             }
111
112             contentLocale.setTitle(contentLocaleForm.getTitleMap());
113             contentLocale.setActive(Boolean.FALSE);
114             contentLocale.setDefaultInstance(Boolean.FALSE);
115             try {
116
117                 localeUtil.createContentLocale(contentLocale);
118
119                 // update all another locales (available locales do not contain new locale, because list was acquired earlier
120
for ( Iterator JavaDoc i = availableLocales.iterator(); i.hasNext(); ) {
121                     ContentLocale locale = (ContentLocale) i.next();
122                     ContentLocale oldLocale = localeUtil.retrieveContentLocale(locale.getIdentifier());
123                     oldLocale.getTitle().put(localeIdentifier, StringUtil.capitalize(new Locale(locale.getIdentifier()).getDisplayLanguage(new Locale(localeIdentifier))));
124                     localeUtil.updateContentLocale(oldLocale);
125                 }
126
127                 ApplicationResources.getInstance(servlet.getServletContext()).loadBundle(contentLocale);
128
129             } catch ( BeanAlreadyExistsException e ) {
130                 // content locale already exists
131
ActionMessages errors = new ActionMessages();
132                 errors.add("contentLocaleAlreadyExists", new ActionMessage("core.contentLocale.errors.alreadyExists"));
133                 saveErrors(request, errors);
134                 saveToken(request);
135                 return mapping.getInputForward();
136             }
137
138         }
139
140         return mapping.findForward("listContentLocales");
141     }
142 }
Popular Tags