KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > applicationResource > CallUpdateApplicationResourceAction


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.applicationResource;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.ConvertUtil;
20 import com.blandware.atleap.model.core.ApplicationResource;
21 import com.blandware.atleap.model.core.ContentLocale;
22 import com.blandware.atleap.service.core.ApplicationResourceManager;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.form.core.ApplicationResourceForm;
25 import com.blandware.atleap.webapp.util.core.LocaleUtil;
26 import com.blandware.atleap.webapp.util.core.WebappConstants;
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
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.util.List JavaDoc;
37
38 /**
39  * <p>Prepares form bean to update application resource's data
40  * </p>
41  * <p><a HREF="CallUpdateApplicationResourceAction.java.htm"><i>View Source</i></a></p>
42  * <p/>
43  *
44  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
45  * @version $Revision: 1.5 $ $Date: 2006/03/10 17:10:15 $
46  * @struts.action name="applicationResourceForm"
47  * path="/core/applicationResource/callUpdate"
48  * scope="request"
49  * validate="false"
50  * roles="core-applicationResource-update"
51  * @struts.action-forward name="updateApplicationResource"
52  * path=".core.applicationResource.update"
53  * @struts.action-forward name="listApplicationResources"
54  * path="/core/applicationResource/list.do"
55  * redirect="true"
56  */

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

67     public ActionForward execute(ActionMapping mapping, ActionForm form,
68                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69
70         if ( isCancelled(request) ) {
71             return mapping.findForward("listApplicationResources");
72         }
73
74         ApplicationResourceForm applicationResourceForm = (ApplicationResourceForm) form;
75         String JavaDoc key = applicationResourceForm.getKey();
76         if ( GenericValidator.isBlankOrNull(key) ) {
77             if ( request.getAttribute(WebappConstants.APPLICATION_RESOURCE_KEY_KEY) != null ) {
78                 key = (String JavaDoc) request.getAttribute(WebappConstants.APPLICATION_RESOURCE_KEY_KEY);
79             } else {
80                 if ( log.isWarnEnabled() ) {
81                     log.warn("Missing application resource ID. Returning to list...");
82                 }
83                 return mapping.findForward("listApplicationResources");
84             }
85         }
86         ApplicationResourceManager applicationResourceManager = (ApplicationResourceManager) getBean(Constants.APPLICATION_RESOURCE_MANAGER_BEAN);
87         List JavaDoc availableLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
88         for ( int i = 0; i < availableLocales.size(); i++ ) {
89             ContentLocale locale = (ContentLocale) availableLocales.get(i);
90             String JavaDoc localeIdentifier = locale.getIdentifier();
91             ApplicationResource applicationResource = applicationResourceManager.retrieveApplicationResource(key, localeIdentifier);
92             if ( applicationResource != null ) {
93                 applicationResourceForm.setValue(localeIdentifier, ConvertUtil.convertToString(applicationResource.getValue()));
94                 applicationResourceForm.setVersion(localeIdentifier, applicationResource.getVersion());
95             }
96         }
97
98         if ( applicationResourceForm.getValueMap().isEmpty() ) {
99             // action page not found. it might be deleted by someone else
100
ActionMessages errors = new ActionMessages();
101             errors.add("applicationResourceNotFound", new ActionMessage("core.applicationResource.errors.notFound", key));
102             saveErrors(request, errors);
103             return mapping.findForward("listApplicationResources");
104         }
105
106         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, availableLocales);
107
108         // save transaction token in request
109
saveToken(request);
110         return mapping.findForward("updateApplicationResource");
111     }
112
113 }
Popular Tags