KickJava   Java API By Example, From Geeks To Geeks.

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


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.WebappConstants;
26 import com.blandware.atleap.webapp.util.core.WebappUtil;
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.apache.struts.tiles.ComponentDefinition;
33 import org.apache.struts.tiles.TilesUtil;
34
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 /**
40  * <p>Creates new layout
41  * </p>
42  * <p><a HREF="CreateLayoutAction.java.htm"><i>View Source</i></a></p>
43  * <p/>
44  *
45  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
46  * @version $Revision: 1.19 $ $Date: 2006/03/10 17:10:25 $
47  * @struts.action path="/core/layout/create"
48  * name="layoutForm"
49  * scope="request"
50  * input="inputForward"
51  * validate="true"
52  * roles="core-layout-create"
53  * @struts.action-forward name="inputForward"
54  * path=".core.layout.create"
55  * @struts.action-forward name="viewLayout"
56  * path="/core/layout/view.do"
57  * redirect="true"
58  * @struts.action-forward name="listLayouts"
59  * path="/core/layout/list.do"
60  * redirect="true"
61  * @struts.action-forward name="unsatisfiable"
62  * path="/core/layout/list.do"
63  */

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

74     public ActionForward execute(ActionMapping mapping, ActionForm form,
75                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
76
77         if ( !isCancelled(request) ) {
78             LayoutForm layoutForm = (LayoutForm) form;
79
80             ServletContext JavaDoc servletContext = request.getSession().getServletContext();
81             // check for definition existence
82
ComponentDefinition definition = TilesUtil.getDefinition(layoutForm.getDefinition(), request, getServlet().getServletContext());
83             if ( definition == null ) {
84                 // definition not found
85
ActionMessages errors = new ActionMessages();
86                 errors.add("noSuchDefinition", new ActionMessage("core.layout.errors.noSuchDefinition"));
87                 saveErrors(request, errors);
88                 saveToken(request);
89                 return mapping.getInputForward();
90             }
91
92             // check for cpDefinition existence
93
if ( layoutForm.getCpDefinition() != null && layoutForm.getCpDefinition().length() > 0 ) {
94                 ComponentDefinition cpDefinition = TilesUtil.getDefinition(layoutForm.getCpDefinition(), request, getServlet().getServletContext());
95                 if ( cpDefinition == null ) {
96                     // cpDefinition not found
97
ActionMessages errors = new ActionMessages();
98                     errors.add("noSuchDefinition", new ActionMessage("core.layout.errors.noSuchDefinition"));
99                     saveErrors(request, errors);
100                     saveToken(request);
101                     return mapping.getInputForward();
102                 }
103
104
105                 //check cpDefinition is the same or child defintion
106
boolean isChild = false;
107                 String JavaDoc tempDefinition = cpDefinition.getName();
108                 do {
109                     if ( tempDefinition.equalsIgnoreCase(definition.getName()) ) {
110                         isChild = true;
111                         break;
112                     }
113                     tempDefinition = ((HeritableComponentDefinition) TilesUtil.getDefinition(tempDefinition, request, servletContext)).getExtends();
114                 } while ( tempDefinition != null );
115                 if ( !isChild ) {
116                     ActionMessages errors = new ActionMessages();
117                     errors.add("isNotChildDefinition", new ActionMessage("core.layout.errors.isNotChildDefinition",
118                             cpDefinition.getName(),
119                             definition.getName()));
120                     saveErrors(request, errors);
121                     saveToken(request);
122                     return mapping.getInputForward();
123                 }
124             }
125             Layout layout = new Layout();
126             WebappUtil.copyProperties(layout, layoutForm, request);
127             LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN);
128             Long JavaDoc layoutId = null;
129             try {
130                 layoutId = layoutManager.createLayout(layout);
131             } catch ( BeanAlreadyExistsException e ) {
132                 // layout already exists
133
ActionMessages errors = new ActionMessages();
134                 errors.add("layoutAlreadyExists", new ActionMessage("core.layout.errors.alreadyExists"));
135                 saveErrors(request, errors);
136                 saveToken(request);
137                 return mapping.getInputForward();
138             }
139             request.getSession().setAttribute(WebappConstants.LAYOUT_ID_KEY, layoutId);
140             return mapping.findForward("viewLayout");
141         } else {
142             return mapping.findForward("listLayouts");
143         }
144     }
145 }
Popular Tags