KickJava   Java API By Example, From Geeks To Geeks.

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


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.service.exception.BeanNotFoundException;
19 import com.blandware.atleap.webapp.action.core.BaseAction;
20 import com.blandware.atleap.webapp.form.core.ApplicationResourceForm;
21 import com.blandware.atleap.webapp.util.core.ApplicationResources;
22 import com.blandware.atleap.webapp.util.core.WebappConstants;
23 import org.apache.commons.validator.GenericValidator;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionMessage;
28 import org.apache.struts.action.ActionMessages;
29 import org.springframework.orm.ObjectOptimisticLockingFailureException;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * <p>Updates application resource
38  * </p>
39  * <p><a HREF="UpdateApplicationResourceAction.java.htm"><i>View Source</i></a></p>
40  * <p/>
41  *
42  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
43  * @version $Revision: 1.9 $ $Date: 2006/03/18 17:00:55 $
44  * @struts.action name="applicationResourceForm"
45  * path="/core/applicationResource/update"
46  * scope="request"
47  * input="inputForward"
48  * validate="true"
49  * roles="core-applicationResource-update"
50  * @struts.action-forward name="inputForward"
51  * path=".core.applicationResource.update"
52  * @struts.action-forward name="listApplicationResources"
53  * path="/core/applicationResource/list.do"
54  * redirect="true"
55  * @struts.action-forward name="callUpdateApplicationResource"
56  * path="/core/applicationResource/callUpdate.do"
57  * redirect="false"
58  * @struts.action-forward name="unsatisfiable"
59  * path="/core/applicationResource/list.do"
60  */

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

71     public ActionForward execute(ActionMapping mapping, ActionForm form,
72                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
73
74         if ( !isCancelled(request) ) {
75             ApplicationResourceForm applicationResourceForm = (ApplicationResourceForm) form;
76             String JavaDoc key = applicationResourceForm.getKey();
77             if ( GenericValidator.isBlankOrNull(key) ) {
78                 if ( log.isWarnEnabled() ) {
79                     log.warn("Missing application resource key. Returning to list...");
80                 }
81                 return mapping.findForward("listApplicationResources");
82             }
83
84             Map JavaDoc valueMap = applicationResourceForm.getValueMap();
85
86             boolean resourceExists = true;
87             boolean alreadyUpdatedExist = false;
88             ApplicationResources applicationResources = ApplicationResources.getInstance(servlet.getServletContext());
89             for ( Iterator JavaDoc i = valueMap.entrySet().iterator(); i.hasNext(); ) {
90                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
91                 String JavaDoc localeIdentifier = (String JavaDoc) entry.getKey();
92                 String JavaDoc value = (String JavaDoc) entry.getValue();
93
94                 String JavaDoc versionString = (String JavaDoc)applicationResourceForm.getVersion(localeIdentifier);
95                 Long JavaDoc version;
96                 if (GenericValidator.isBlankOrNull(versionString)) {
97                     version = null;
98                 } else {
99                     version = Long.valueOf(versionString);
100                 }
101                 try {
102                     applicationResources.updateResource(localeIdentifier, key, value, version);
103                 } catch ( BeanNotFoundException e ) {
104                     resourceExists = false;
105                 } catch ( ObjectOptimisticLockingFailureException e ) {
106                     // action page was updated by another transaction
107
alreadyUpdatedExist = true;
108                 }
109             }
110
111             if ( !resourceExists ) {
112                 // some resource not found. it might be deleted by someone else
113
ActionMessages errors = new ActionMessages();
114                 errors.add("applicationResourceNotFound", new ActionMessage("core.applicationResource.errors.notFound", key));
115                 saveErrors(request, errors);
116                 return mapping.findForward("listApplicationResources");
117             }
118
119             if ( alreadyUpdatedExist ) {
120                 ActionMessages errors = new ActionMessages();
121                 errors.add("updateFailed", new ActionMessage("core.applicationResource.errors.updateFailed"));
122                 saveErrors(request, errors);
123                 request.setAttribute(WebappConstants.APPLICATION_RESOURCE_KEY_KEY, key);
124                 return mapping.findForward("callUpdateApplicationResource");
125             }
126
127         }
128         return mapping.findForward("listApplicationResources");
129     }
130 }
Popular Tags