KickJava   Java API By Example, From Geeks To Geeks.

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


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.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.LayoutForm;
23 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28 import org.apache.struts.action.ActionMessage;
29 import org.apache.struts.action.ActionMessages;
30 import org.apache.struts.tiles.TilesUtil;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 /**
36  * <p>Exposes bean to request to view its properties on JSP page (layout is to
37  * be viewed)
38  * </p>
39  * <p><a HREF="ViewLayoutAction.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.19 $ $Date: 2006/03/10 17:10:26 $
44  * @struts.action path="/core/layout/view"
45  * name="layoutForm"
46  * scope="request"
47  * validate="false"
48  * roles="core-layout-view"
49  * @struts.action-forward name="viewLayout"
50  * path=".core.layout.view"
51  * @struts.action-forward name="listLayouts"
52  * path="/core/layout/list.do"
53  * redirect="true"
54  */

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

65     public ActionForward execute(ActionMapping mapping, ActionForm form,
66                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
67
68         LayoutForm layoutForm = (LayoutForm) form;
69         Long JavaDoc layoutId = null;
70         if ( layoutForm.getId() != null ) {
71             layoutId = Long.valueOf(layoutForm.getId());
72         } else if ( request.getSession().getAttribute(WebappConstants.LAYOUT_ID_KEY) != null ) {
73             layoutId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.LAYOUT_ID_KEY);
74         } else {
75             if ( log.isWarnEnabled() ) {
76                 log.warn("Missing layout ID. Returning to list...");
77             }
78             return mapping.findForward("listLayouts");
79         }
80         LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN);
81         Layout layout = layoutManager.retrieveLayout(layoutId);
82         if ( layout == null ) {
83             // layout not found. it might be deleted by someone else
84
ActionMessages errors = new ActionMessages();
85             errors.add("layoutNotFound", new ActionMessage("core.layout.errors.notFound"));
86             saveErrors(request, errors);
87             return mapping.findForward("listLayouts");
88         }
89
90         String JavaDoc parentDefinition = layout.getDefinition();
91         Layout parentLayout = null;
92
93         do {
94             parentDefinition = ((HeritableComponentDefinition) TilesUtil.getDefinition(parentDefinition, request, getServlet().getServletContext())).getExtends();
95             if ( parentDefinition != null ) {
96                 parentLayout = layoutManager.findLayoutByDefinition(parentDefinition);
97                 if ( parentLayout != null ) {
98                     break;
99                 }
100             }
101         } while ( parentDefinition != null );
102
103         if ( parentLayout != null ) {
104             request.setAttribute("parentLayoutId", parentLayout.getId());
105         }
106
107         // save owner ID in session
108
request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, layout.getId());
109         
110         request.setAttribute("layout", layout);
111         saveToken(request);
112         return mapping.findForward("viewLayout");
113     }
114 }
Popular Tags