KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentPage > CallUpdateContentPageAction


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.contentPage;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.PartialCollection;
20 import com.blandware.atleap.model.core.ContentPage;
21 import com.blandware.atleap.model.core.Layout;
22 import com.blandware.atleap.service.core.LayoutManager;
23 import com.blandware.atleap.service.core.PageManager;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ContentPageForm;
26 import com.blandware.atleap.webapp.util.core.LocaleUtil;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * <p>Prepares form bean to update content page's data
42  * </p>
43  * <p><a HREF="CallUpdateContentPageAction.java.htm"><i>View Source</i></a></p>
44  * <p/>
45  *
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.22 $ $Date: 2006/03/10 17:10:20 $
48  * @struts.action path="/core/contentPage/callUpdate"
49  * name="contentPageForm"
50  * scope="request"
51  * validate="false"
52  * roles="core-contentPage-update, core-contentPage-view"
53  * @struts.action-forward name="updateContentPage"
54  * path=".core.contentPage.update"
55  * @struts.action-forward name="listContentPages"
56  * path="/core/contentPage/list.do"
57  * redirect="true"
58  */

59 public final class CallUpdateContentPageAction 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         if ( isCancelled(request) ) {
73             return mapping.findForward("listContentPages");
74         }
75
76         if (!request.isUserInRole("core-contentPage-update")) {
77             response.sendError(HttpServletResponse.SC_FORBIDDEN);
78             return null;
79         }
80
81         ContentPageForm contentPageForm = (ContentPageForm) form;
82         Long JavaDoc contentPageId = null;
83         if ( contentPageForm.getId() != null ) {
84             contentPageId = Long.valueOf(contentPageForm.getId());
85         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_PAGE_ID_KEY) != null ) {
86             contentPageId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_PAGE_ID_KEY);
87         } else {
88             if ( log.isWarnEnabled() ) {
89                 log.warn("Missing content page ID. Returning to list...");
90             }
91             return mapping.findForward("listContentPages");
92         }
93
94         PageManager contentPageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
95         ContentPage contentPage = contentPageManager.retrieveContentPage(contentPageId);
96         if ( contentPage == null ) {
97             // content page not found. it might be deleted by someone else
98
ActionMessages errors = new ActionMessages();
99             errors.add("contentPageNotFound", new ActionMessage("core.contentPage.errors.notFound"));
100             saveErrors(request, errors);
101             return mapping.findForward("listContentPages");
102         }
103         WebappUtil.copyProperties(contentPageForm, contentPage, request);
104         contentPageForm.setLayoutId(String.valueOf(contentPage.getLayout().getId()));
105         contentPageForm.setTitleMap(contentPage.getTitle());
106         contentPageForm.setDescriptionMap(contentPage.getDescription());
107         contentPageForm.setKeywordsMap(contentPage.getKeywords());
108
109         String JavaDoc uri = contentPage.getUri();
110         if ( uri.startsWith(WebappConstants.CONTENT_PAGES_URI_PREFIX) ) {
111             uri = uri.substring(WebappConstants.CONTENT_PAGES_URI_PREFIX.length());
112         }
113         contentPageForm.setUri(uri);
114
115         // Populate layout drop-down list
116
LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN);
117         PartialCollection layouts = null;
118         layouts = layoutManager.listLayouts(null);
119
120         if ( layouts != null ) {
121             for ( Iterator JavaDoc i = layouts.iterator(); i.hasNext(); ) {
122                 Layout layout = (Layout) i.next();
123                 if ( layout.getCpDefinition() == null || layout.getCpDefinition().length() == 0 ) {
124                     i.remove();
125                 }
126             }
127         }
128
129         if ( layouts == null || layouts.size() == 0 ) {
130             saveToken(request);
131             ActionMessages errors = new ActionMessages();
132             errors.add("noLayoutFound", new ActionMessage("core.contentPage.errors.noLayoutFound"));
133             saveErrors(request, errors);
134             return mapping.findForward("listContentPages");
135         }
136
137         request.getSession().setAttribute(WebappConstants.LAYOUT_COLLECTION_KEY, layouts);
138
139         List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
140
141         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
142
143         // save transaction token in request
144
saveToken(request);
145         return mapping.findForward("updateContentPage");
146     }
147
148 }
Popular Tags