KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.core.ContentLocale;
19 import com.blandware.atleap.webapp.action.core.BaseAction;
20 import com.blandware.atleap.webapp.form.ContentLocaleForm;
21 import com.blandware.atleap.webapp.util.core.LocaleUtil;
22 import org.apache.commons.validator.GenericValidator;
23 import org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26 import org.apache.struts.action.ActionMessage;
27 import org.apache.struts.action.ActionMessages;
28 import org.springframework.orm.ObjectOptimisticLockingFailureException;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 /**
34  * <p>Sets whether locale is active or not
35  * </p>
36  * <p><a HREF="ChangeContentLocaleActivityAction.java.htm"><i>View Source</i></a></p>
37  * <p/>
38  *
39  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
40  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
41  * @version $Revision: 1.13 $ $Date: 2006/03/10 17:10:19 $
42  * @struts.action path="/core/contentLocale/changeActivity"
43  * name="contentLocaleForm"
44  * scope="request"
45  * validate="false"
46  * roles="core-contentLocale-changeActivity"
47  * @struts.action-forward name="listContentLocales"
48  * path="/core/contentLocale/list.do"
49  * redirect="true"
50  * @struts.action-forward name="unsatisfiable"
51  * path="/core/contentLocale/list.do"
52  */

53 public final class ChangeContentLocaleActivityAction extends BaseAction {
54     /**
55      * @param mapping The ActionMapping used to select this instance
56      * @param form The optional ActionForm bean for this request (if any)
57      * @param request The HTTP request we are proceeding
58      * @param response The HTTP response we are creating
59      * @return an ActionForward instance describing where and how
60      * control should be forwarded, or null if response
61      * has already been completed
62      */

63     public ActionForward execute(ActionMapping mapping, ActionForm form,
64                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
65
66         ContentLocaleForm contentLocaleForm = (ContentLocaleForm) form;
67
68         String JavaDoc localeIdentifier = contentLocaleForm.getIdentifier();
69         if ( GenericValidator.isBlankOrNull(localeIdentifier) ) {
70             if ( log.isWarnEnabled() ) {
71                 log.warn("Missing content locale identifier. Returning to list.");
72             }
73             return mapping.findForward("listContentLocales");
74         }
75
76         ContentLocale contentLocale = LocaleUtil.getInstance(servlet.getServletContext()).retrieveContentLocale(localeIdentifier);
77
78         if ( contentLocale == null ) {
79             // content locale not found. it might be deleted by someone else
80
ActionMessages errors = new ActionMessages();
81             errors.add("contentLocaleNotFound", new ActionMessage("core.contentLocale.errors.notFound"));
82             saveErrors(request, errors);
83             return mapping.findForward("listContentLocales");
84         }
85
86         String JavaDoc mode = request.getParameter("mode");
87         boolean alreadyActive = contentLocale.getActive() != null ? contentLocale.getActive().booleanValue() : false;
88         boolean active = true;
89
90         if ( !"active".equalsIgnoreCase(mode) ) {
91             active = false;
92         }
93
94         try {
95             if ( ((alreadyActive && !active) || (!alreadyActive && active)) && !contentLocale.getDefaultInstance().booleanValue() ) {
96                 contentLocale.setActive(Boolean.valueOf(active));
97                 LocaleUtil.getInstance(servlet.getServletContext()).updateContentLocale(contentLocale);
98             }
99         } catch ( ObjectOptimisticLockingFailureException e ) {
100             // content locale was updated or deleted by another transaction
101
ActionMessages errors = new ActionMessages();
102             errors.add("updateFailed", new ActionMessage("core.contentLocale.errors.updateFailed"));
103             saveErrors(request, errors);
104             return mapping.findForward("listContentLocales");
105         }
106
107         return mapping.findForward("listContentLocales");
108     }
109 }
Popular Tags