KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > globalProperty > UpdateGlobalPropertiesAction


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.globalProperty;
17
18 import com.blandware.atleap.common.util.ConvertUtil;
19 import com.blandware.atleap.model.core.GlobalProperty;
20 import com.blandware.atleap.webapp.action.core.BaseAction;
21 import com.blandware.atleap.webapp.form.core.GlobalPropertyForm;
22 import com.blandware.atleap.webapp.util.core.GlobalProperties;
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 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * <p>Updates global properties
39  * </p>
40  * <p><a HREF="UpdateGlobalPropertiesAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
44  * @version $Revision: 1.9 $ $Date: 2006/03/10 17:10:24 $
45  * @struts.action path="/core/globalProperty/update"
46  * name="globalPropertyForm"
47  * scope="request"
48  * input="inputForward"
49  * validate="true"
50  * roles="core-globalProperty-update"
51  * @struts.action-forward name="inputForward"
52  * path=".core.globalProperty.list"
53  * @struts.action-forward name="listGlobalProperties"
54  * path="/core/globalProperty/list.do"
55  * redirect="true"
56  * @struts.action-forward name="unsatisfiable"
57  * path="/core/globalProperty/list.do"
58  */

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

69     public ActionForward execute(ActionMapping mapping, ActionForm form,
70                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71
72         GlobalPropertyForm globalPropertyForm = (GlobalPropertyForm) form;
73         Map JavaDoc properties = globalPropertyForm.getPropertyMap();
74         List JavaDoc nonExistentPropertyNames = new ArrayList JavaDoc();
75         boolean alreadyUpdatedExist = false;
76         for ( Iterator JavaDoc i = properties.entrySet().iterator(); i.hasNext(); ) {
77             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
78             String JavaDoc name = (String JavaDoc) entry.getKey();
79             GlobalProperty property = GlobalProperties.getInstance(request.getSession().getServletContext()).retrieveGlobalProperty(name);
80             if ( property == null ) {
81                 nonExistentPropertyNames.add(name);
82                 continue;
83             }
84             String JavaDoc value = (String JavaDoc) entry.getValue();
85             Long JavaDoc version = Long.valueOf((String JavaDoc) globalPropertyForm.getVersion(name));
86             property.setValue(value);
87             property.setVersion(version);
88             try {
89                 GlobalProperties.getInstance(request.getSession().getServletContext()).updateProperty(property);
90             } catch ( ObjectOptimisticLockingFailureException e ) {
91                 alreadyUpdatedExist = true;
92             }
93         }
94
95         if ( !nonExistentPropertyNames.isEmpty() || alreadyUpdatedExist ) {
96             ActionMessages errors = new ActionMessages();
97             if ( !nonExistentPropertyNames.isEmpty() ) {
98                 errors.add("nonExistentProperties", new ActionMessage("core.globalProperty.errors.nonExistentProperties", ConvertUtil.convertListToString(nonExistentPropertyNames, ", ")));
99             }
100             if ( alreadyUpdatedExist ) {
101                 errors.add("alreadyUpdatedProperties", new ActionMessage("core.globalProperty.errors.updateFailed"));
102             }
103             saveErrors(request, errors);
104         } else {
105             ActionMessages messages = new ActionMessages();
106             messages.add("updateSuccessful", new ActionMessage("core.globalProperty.messages.updateSuccessful"));
107             saveMessages(request, messages);
108         }
109
110         return mapping.findForward("listGlobalProperties");
111     }
112 }
Popular Tags