KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > layout > UpdateLayoutAction


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.layout;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.Layout;
20 import com.blandware.atleap.service.core.LayoutManager;
21 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.LayoutForm;
24 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition;
25 import com.blandware.atleap.webapp.util.core.CacheUtil;
26 import com.blandware.atleap.webapp.util.core.WebappConstants;
27 import com.blandware.atleap.webapp.util.core.WebappUtil;
28 import com.blandware.atleap.persistence.exception.UpdateException;
29 import org.apache.commons.validator.GenericValidator;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35 import org.apache.struts.tiles.ComponentDefinition;
36 import org.apache.struts.tiles.TilesUtil;
37 import org.springframework.orm.ObjectOptimisticLockingFailureException;
38
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42
43 /**
44  * <p>Updates layout
45  * </p>
46  * <p><a HREF="UpdateLayoutAction.java.htm"><i>View Source</i></a></p>
47  * <p/>
48  *
49  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
50  * @version $Revision: 1.27 $ $Date: 2006/03/10 17:10:26 $
51  * @struts.action path="/core/layout/update"
52  * name="layoutForm"
53  * scope="request"
54  * input="inputForward"
55  * validate="true"
56  * roles="core-layout-update"
57  * @struts.action-forward name="inputForward"
58  * path=".core.layout.update"
59  * @struts.action-forward name="viewLayout"
60  * path="/core/layout/view.do"
61  * redirect="true"
62  * @struts.action-forward name="listLayouts"
63  * path="/core/layout/list.do"
64  * redirect="true"
65  * @struts.action-forward name="callUpdateLayout"
66  * path="/core/layout/callUpdate.do"
67  * redirect="false"
68  * @struts.action-forward name="unsatisfiable"
69  * path="/core/layout/list.do"
70  */

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

81     public ActionForward execute(ActionMapping mapping, ActionForm form,
82                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
83
84         if ( !isCancelled(request) ) {
85             LayoutForm layoutForm = (LayoutForm) form;
86             ServletContext JavaDoc servletContext = request.getSession().getServletContext();
87
88             // check for definition existence
89
ComponentDefinition definition = TilesUtil.getDefinition(layoutForm.getDefinition(), request, getServlet().getServletContext());
90             if ( definition == null ) {
91                 // definition not found
92
ActionMessages errors = new ActionMessages();
93                 errors.add("noSuchDefinition", new ActionMessage("core.layout.errors.noSuchDefinition"));
94                 saveErrors(request, errors);
95                 saveToken(request);
96                 return mapping.getInputForward();
97             }
98
99             if ( layoutForm.getCpDefinition() != null && layoutForm.getCpDefinition().length() > 0 ) {
100                 // check for cpDefinition existence
101
ComponentDefinition cpDefinition = TilesUtil.getDefinition(layoutForm.getCpDefinition(), request, getServlet().getServletContext());
102                 if ( cpDefinition == null ) {
103                     // cpDefinition not found
104
ActionMessages errors = new ActionMessages();
105                     errors.add("noSuchDefinition", new ActionMessage("core.layout.errors.noSuchDefinition"));
106                     saveErrors(request, errors);
107                     saveToken(request);
108                     return mapping.getInputForward();
109                 }
110
111                 //check cpDefinition is the same or child defintion
112
boolean isChild = false;
113                 String JavaDoc tempDefinition = cpDefinition.getName();
114                 do {
115                     if ( tempDefinition.equalsIgnoreCase(definition.getName()) ) {
116                         isChild = true;
117                         break;
118                     }
119                     tempDefinition = ((HeritableComponentDefinition) TilesUtil.getDefinition(tempDefinition, request, servletContext)).getExtends();
120                 } while ( tempDefinition != null );
121                 if ( !isChild ) {
122                     ActionMessages errors = new ActionMessages();
123                     errors.add("isNotChildDefinition", new ActionMessage("core.layout.errors.isNotChildDefinition",
124                             cpDefinition.getName(),
125                             definition.getName()));
126                     saveErrors(request, errors);
127                     saveToken(request);
128                     return mapping.getInputForward();
129                 }
130             }
131
132             Long JavaDoc layoutId = null;
133             if ( !GenericValidator.isBlankOrNull(layoutForm.getId()) ) {
134                 layoutId = Long.valueOf(layoutForm.getId());
135             } else {
136                 if ( log.isWarnEnabled() ) {
137                     log.warn("Missing layout ID. Returning to list...");
138                 }
139                 return mapping.findForward("listLayouts");
140             }
141
142
143             LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN);
144             Layout layout = layoutManager.retrieveLayout(layoutId);
145
146             if ( layout == null ) {
147                 // layout not found. it might be deleted by someone else
148
ActionMessages errors = new ActionMessages();
149                 errors.add("layoutNotFound", new ActionMessage("core.layout.errors.notFound"));
150                 saveErrors(request, errors);
151                 return mapping.findForward("listLayouts");
152             }
153
154             WebappUtil.copyProperties(layout, layoutForm, request);
155             try {
156                 layoutManager.updateLayout(layout);
157
158                 //flush cache
159
CacheUtil cacheUtil = CacheUtil.getInstance(request);
160                 cacheUtil.flushLayoutFieldValueCache(layoutForm.getDefinition());
161                 cacheUtil.flushFieldIndices();
162
163             } catch ( BeanAlreadyExistsException e ) {
164                 // layout already exists
165
ActionMessages errors = new ActionMessages();
166                 errors.add("layoutAlreadyExists", new ActionMessage("core.layout.errors.alreadyExists"));
167                 saveErrors(request, errors);
168                 saveToken(request);
169                 return mapping.getInputForward();
170             } catch ( ObjectOptimisticLockingFailureException e ) {
171                 // layout was updated or deleted by another transaction
172
ActionMessages errors = new ActionMessages();
173                 errors.add("updateFailed", new ActionMessage("core.layout.errors.updateFailed"));
174                 saveErrors(request, errors);
175                 return mapping.findForward("callUpdateLayout");
176             } catch( UpdateException e ) {
177                 // tried to set cpDefinition to empty value, while there're some
178
// content pages based on this layout
179
ActionMessages errors = new ActionMessages();
180                 errors.add("updateFailed", new ActionMessage("core.layout.errors.cannotMakeCpDefinitionEmpty"));
181                 saveErrors(request, errors);
182                 return mapping.findForward("callUpdateLayout");
183             }
184             request.getSession().setAttribute(WebappConstants.LAYOUT_ID_KEY, layout.getId());
185             return mapping.findForward("viewLayout");
186         } else {
187             return mapping.findForward("listLayouts");
188         }
189     }
190 }
Popular Tags