KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > group > UpdateGroupAction


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.group;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.Group;
20 import com.blandware.atleap.service.core.GroupManager;
21 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.GroupForm;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
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 import org.springframework.orm.ObjectOptimisticLockingFailureException;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * <p>Updates group
39  * </p>
40  * <p><a HREF="UpdateGroupAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
44  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
45  * @version $Revision: 1.4 $ $Date: 2006/03/11 10:41:29 $
46  * @struts.action path="/core/group/update"
47  * name="groupForm"
48  * scope="request"
49  * input="inputForward"
50  * validate="true"
51  * roles="core-group-update"
52  * @struts.action-forward name="inputForward"
53  * path=".core.group.update"
54  * @struts.action-forward name="viewGroup"
55  * path="/core/group/view.do"
56  * redirect="true"
57  * @struts.action-forward name="listGroups"
58  * path="/core/group/list.do"
59  * redirect="true"
60  * @struts.action-forward name="callUpdateGroup"
61  * path="/core/group/callUpdate.do"
62  * redirect="false"
63  * @struts.action-forward name="unsatisfiable"
64  * path="/core/group/list.do"
65  */

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

76     public ActionForward execute(ActionMapping mapping, ActionForm form,
77                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
78
79         if ( !isCancelled(request) ) {
80
81             GroupForm groupForm = (GroupForm) form;
82
83             String JavaDoc groupName = groupForm.getName();
84             if ( GenericValidator.isBlankOrNull(groupName) ) {
85                 if ( log.isWarnEnabled() ) {
86                     log.warn("Missing group name. Returning to list...");
87                 }
88                 return mapping.findForward("listGroups");
89             }
90
91             request.getSession().setAttribute(WebappConstants.GROUP_NAME_KEY, groupName);
92
93             GroupManager groupManager = (GroupManager) getBean(Constants.GROUP_MANAGER_BEAN);
94
95             Group group = groupManager.retrieveGroup(groupName);
96
97             if ( group == null ) {
98                 // group not found. it might be deleted by someone else
99
ActionMessages errors = new ActionMessages();
100                 errors.add("groupNotFound", new ActionMessage("core.group.errors.notFound"));
101                 saveErrors(request, errors);
102                 return mapping.findForward("listGroups");
103             }
104
105             WebappUtil.copyProperties(group, groupForm, request);
106
107             try {
108                 groupManager.updateGroup(group);
109             } catch ( BeanAlreadyExistsException e ) {
110                 // group already exists
111
ActionMessages errors = new ActionMessages();
112                 errors.add("groupAlreadyExists", new ActionMessage("core.group.errors.alreadyExists"));
113                 saveErrors(request, errors);
114                 saveToken(request);
115                 return mapping.getInputForward();
116             } catch ( ObjectOptimisticLockingFailureException e ) {
117                 // group was updated or deleted by another transaction
118
ActionMessages errors = new ActionMessages();
119                 errors.add("updateFailed", new ActionMessage("core.group.errors.updateFailed"));
120                 saveErrors(request, errors);
121                 return mapping.findForward("callUpdateGroup");
122             }
123         }
124         return mapping.findForward("listGroups");
125     }
126 }
Popular Tags