KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > actionPage > CreateActionPageAction


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.actionPage;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ActionPage;
20 import com.blandware.atleap.model.core.ContentField;
21 import com.blandware.atleap.model.core.ContentFieldValue;
22 import com.blandware.atleap.search.SearchManager;
23 import com.blandware.atleap.service.core.PageManager;
24 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
25 import com.blandware.atleap.webapp.action.core.BaseAction;
26 import com.blandware.atleap.webapp.form.ActionPageForm;
27 import com.blandware.atleap.webapp.util.core.CacheUtil;
28 import com.blandware.atleap.webapp.util.core.WebappConstants;
29 import com.blandware.atleap.webapp.util.core.WebappUtil;
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.config.ModuleConfig;
36 import org.apache.struts.util.ModuleUtils;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * <p>Creates new content actionPage
45  * </p>
46  * <p><a HREF="CreateActionPageAction.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  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
51  * @version $Revision: 1.26 $ $Date: 2006/03/10 17:10:15 $
52  * @struts.action name="actionPageForm"
53  * path="/core/actionPage/create"
54  * scope="request"
55  * input="inputForward"
56  * validate="true"
57  * roles="core-actionPage-create"
58  * @struts.action-forward name="inputForward"
59  * path=".core.actionPage.create"
60  * @struts.action-forward name="viewActionPage"
61  * path="/core/actionPage/view.do"
62  * redirect="true"
63  * @struts.action-forward name="listActionPages"
64  * path="/core/actionPage/list.do"
65  * redirect="true"
66  * @struts.action-forward name="unsatisfiable"
67  * path="/core/actionPage/list.do"
68  */

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

79     public ActionForward execute(ActionMapping mapping, ActionForm form,
80                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
81
82         if ( !isCancelled(request) ) {
83             ActionPageForm actionPageForm = (ActionPageForm) form;
84
85             if ( !WebappUtil.hasCorrectValues(actionPageForm.getTitleMap()) ) {
86                 // title must be specified
87
ActionMessages errors = new ActionMessages();
88                 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.page.form.title")));
89                 saveErrors(request, errors, false);
90                 saveToken(request);
91                 return mapping.getInputForward();
92             }
93
94             // check URI for existence
95
String JavaDoc uri = actionPageForm.getUri();
96             ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, getServlet().getServletContext());
97             String JavaDoc prefix = moduleConfig.getPrefix();
98             if ( log.isDebugEnabled() ) {
99                 log.debug("Prefix: " + prefix);
100             }
101
102             String JavaDoc actionPath = new String JavaDoc();
103             actionPath = prefix + uri;
104
105             if ( log.isDebugEnabled() ) {
106                 log.debug("Searching for action with path=" + actionPath);
107             }
108
109             if ( moduleConfig.findActionConfig(actionPath) == null ) {
110                 // action with this uri does not exist
111
if ( log.isDebugEnabled() ) {
112                     log.debug("Action with uri '" + actionPath + "' does not exist");
113                 }
114                 ActionMessages errors = new ActionMessages();
115                 errors.add("actionPageIncorrectUri", new ActionMessage("core.actionPage.errors.incorrectUri"));
116                 saveErrors(request, errors);
117                 saveToken(request);
118                 return mapping.getInputForward();
119             }
120
121             ActionPage actionPage = new ActionPage();
122             WebappUtil.copyProperties(actionPage, actionPageForm, request);
123             actionPage.setTitle(actionPageForm.getTitleMap());
124             actionPage.setDescription(actionPageForm.getDescriptionMap());
125             actionPage.setKeywords(actionPageForm.getKeywordsMap());
126
127             PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
128             Long JavaDoc actionPageId = null;
129             try {
130                 actionPageId = pageManager.createActionPage(actionPage);
131
132                 if ( actionPage.getActive().booleanValue() ) {
133                     //index action page
134
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
135                     searchManager.reIndexPage(actionPage, request);
136                 }
137
138                 // put internal fields in cache
139
CacheUtil cacheUtil = CacheUtil.getInstance(request);
140
141                 // title
142
ContentField titleField = (ContentField) actionPage.getContentFieldsMap().get("title");
143                 List JavaDoc titleValues = titleField.getContentFieldValues();
144                 for ( Iterator JavaDoc i = titleValues.iterator(); i.hasNext(); ) {
145                     ContentFieldValue fieldValue = (ContentFieldValue) i.next();
146                     // title field is single-lined
147
String JavaDoc content = fieldValue.getSimpleValue();
148                     CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, titleField.getId(), titleField.getType(), fieldValue.getId());
149                     cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), titleField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
150                 }
151
152                 // description
153
ContentField descriptionField = (ContentField) actionPage.getContentFieldsMap().get("description");
154                 List JavaDoc descriptionValues = descriptionField.getContentFieldValues();
155                 for ( Iterator JavaDoc i = descriptionValues.iterator(); i.hasNext(); ) {
156                     ContentFieldValue fieldValue = (ContentFieldValue) i.next();
157                     // description field is single-lined
158
String JavaDoc content = fieldValue.getSimpleValue();
159                     CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, descriptionField.getId(), descriptionField.getType(), fieldValue.getId());
160                     cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), descriptionField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
161                 }
162
163                 // keywords
164
ContentField keywordsField = (ContentField) actionPage.getContentFieldsMap().get("keywords");
165                 List JavaDoc keywordsValues = keywordsField.getContentFieldValues();
166                 for ( Iterator JavaDoc i = keywordsValues.iterator(); i.hasNext(); ) {
167                     ContentFieldValue fieldValue = (ContentFieldValue) i.next();
168                     // keywords field is single-lined
169
String JavaDoc content = fieldValue.getSimpleValue();
170                     CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, keywordsField.getId(), keywordsField.getType(), fieldValue.getId());
171                     cacheUtil.putPageFieldValueInCache(cfvData, actionPage.getUri(), keywordsField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
172                 }
173
174             } catch ( BeanAlreadyExistsException e ) {
175                 // actionPage already exists
176
ActionMessages errors = new ActionMessages();
177                 errors.add("actionPageAlreadyExists", new ActionMessage("core.actionPage.errors.alreadyExists"));
178                 saveErrors(request, errors);
179                 saveToken(request);
180                 return mapping.getInputForward();
181             }
182             request.getSession().setAttribute(WebappConstants.ACTION_PAGE_ID_KEY, actionPageId);
183             return mapping.findForward("viewActionPage");
184         } else {
185             return mapping.findForward("listActionPages");
186         }
187     }
188 }
Popular Tags