KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.core.ContentField;
20 import com.blandware.atleap.model.core.ContentFieldValue;
21 import com.blandware.atleap.model.core.ContentPage;
22 import com.blandware.atleap.model.core.Layout;
23 import com.blandware.atleap.search.SearchManager;
24 import com.blandware.atleap.service.core.ContentFieldManager;
25 import com.blandware.atleap.service.core.LayoutManager;
26 import com.blandware.atleap.service.core.PageManager;
27 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
28 import com.blandware.atleap.service.exception.OwnerNotFoundException;
29 import com.blandware.atleap.webapp.action.core.BaseAction;
30 import com.blandware.atleap.webapp.form.ContentPageForm;
31 import com.blandware.atleap.webapp.struts.HeritableComponentDefinition;
32 import com.blandware.atleap.webapp.util.core.CacheUtil;
33 import com.blandware.atleap.webapp.util.core.WebappConstants;
34 import com.blandware.atleap.webapp.util.core.WebappUtil;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38 import org.apache.struts.action.ActionMessage;
39 import org.apache.struts.action.ActionMessages;
40 import org.apache.struts.config.ModuleConfig;
41 import org.apache.struts.tiles.TilesUtil;
42 import org.apache.struts.util.ModuleUtils;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collection JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.LinkedList JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53 import java.util.Set JavaDoc;
54
55 /**
56  * <p>Creates new content page
57  * </p>
58  * <p><a HREF="CreateContentPageAction.java.htm"><i>View Source</i></a></p>
59  * <p/>
60  *
61  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
62  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
63  * @version $Revision: 1.35 $ $Date: 2006/03/10 17:10:20 $
64  * @struts.action path="/core/contentPage/create"
65  * name="contentPageForm"
66  * scope="request"
67  * input="inputForward"
68  * validate="true"
69  * roles="core-contentPage-create"
70  * @struts.action-forward name="inputForward"
71  * path=".core.contentPage.create"
72  * @struts.action-forward name="callAssignRoles"
73  * path="/core/contentPage/callAssignRoles.do"
74  * redirect="false"
75  * @struts.action-forward name="listContentPages"
76  * path="/core/contentPage/list.do"
77  * redirect="true"
78  * @struts.action-forward name="viewContentPage"
79  * path="/core/contentPage/view.do"
80  * redirect="true"
81  * @struts.action-forward name="unsatisfiable"
82  * path="/core/contentPage/list.do"
83  */

84 public final class CreateContentPageAction extends BaseAction {
85     /**
86      * @param mapping The ActionMapping used to select this instance
87      * @param form The optional ActionForm bean for this request (if any)
88      * @param request The HTTP request we are proceeding
89      * @param response The HTTP response we are creating
90      * @return an ActionForward instance describing where and how
91      * control should be forwarded, or null if response
92      * has already been completed
93      */

94     public ActionForward execute(ActionMapping mapping, ActionForm form,
95                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
96
97         if ( !isCancelled(request) ) {
98             ContentPageForm contentPageForm = (ContentPageForm) form;
99
100             if ( !WebappUtil.hasCorrectValues(contentPageForm.getTitleMap()) ) {
101                 // title must be specified
102
ActionMessages errors = new ActionMessages();
103                 errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "core.page.form.title")));
104                 saveErrors(request, errors, false);
105                 saveToken(request);
106                 return mapping.getInputForward();
107             }
108
109             // check URI. If there is an action with the same path, return error
110
String JavaDoc uri = WebappConstants.CONTENT_PAGES_URI_PREFIX + contentPageForm.getUri();
111             ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, getServlet().getServletContext());
112             String JavaDoc prefix = moduleConfig.getPrefix();
113             if ( log.isDebugEnabled() ) {
114                 log.debug("Prefix: " + prefix);
115             }
116
117             String JavaDoc actionPath = new String JavaDoc();
118             actionPath = prefix + uri;
119
120             if ( log.isDebugEnabled() ) {
121                 log.debug("Searching for action with path=" + actionPath);
122             }
123
124             if ( moduleConfig.findActionConfig(actionPath) != null ) {
125                 // action with same uri exists
126
if ( log.isWarnEnabled() ) {
127                     log.warn("Action was found - returning input");
128                 }
129                 ActionMessages errors = new ActionMessages();
130                 errors.add("contentPageIncorrectUri", new ActionMessage("core.contentPage.errors.incorrectUri"));
131                 saveErrors(request, errors);
132                 saveToken(request);
133                 return mapping.getInputForward();
134             }
135
136             ContentPage contentPage = new ContentPage();
137             WebappUtil.copyProperties(contentPage, contentPageForm, request);
138             contentPage.setActive(Boolean.FALSE);
139             contentPage.setTitle(contentPageForm.getTitleMap());
140             contentPage.setDescription(contentPageForm.getDescriptionMap());
141             contentPage.setKeywords(contentPageForm.getKeywordsMap());
142             contentPage.setUri(uri);
143             PageManager pageManager = (PageManager) getBean(Constants.PAGE_MANAGER_BEAN);
144             Long JavaDoc layoutId = Long.valueOf(contentPageForm.getLayoutId());
145             Long JavaDoc contentPageId = null;
146             try {
147                 contentPageId = pageManager.createContentPage(contentPage, layoutId);
148
149                 //index content page
150
LayoutManager layoutManager = (LayoutManager) getBean(Constants.LAYOUT_MANAGER_BEAN);
151                 Layout layout = layoutManager.retrieveLayout(layoutId);
152
153                 if ( contentPage.getActive().booleanValue() ) {
154                     SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
155                     searchManager.reIndexPage(contentPage, request);
156                 }
157
158                 String JavaDoc overrideFields = contentPageForm.getOverrideFields();
159                 if ( !"none".equalsIgnoreCase(overrideFields) ) {
160                     boolean all = "all".equalsIgnoreCase(overrideFields);
161                     boolean empty = "empty".equalsIgnoreCase(overrideFields);
162                     if ( all || empty ) {
163
164                         // get sequence of layouts
165
LinkedList JavaDoc layouts = new LinkedList JavaDoc();
166                         layouts.addFirst(layout);
167                         String JavaDoc definition = layout.getDefinition();
168                         while ( definition != null ) {
169                             definition = ((HeritableComponentDefinition) TilesUtil.getDefinition(definition, request, servlet.getServletContext())).getExtends();
170                             if ( definition != null ) {
171                                 layout = layoutManager.findLayoutByDefinition(definition);
172                                 if ( layout != null ) {
173                                     layouts.addFirst(layout);
174                                 }
175                             }
176                         }
177
178                         // get content fields
179
Map JavaDoc contentFields = new HashMap JavaDoc();
180                         for ( Iterator JavaDoc i = layouts.iterator(); i.hasNext(); ) {
181                             layout = (Layout) i.next();
182                             contentFields.putAll(layout.getContentFieldsMap());
183                         }
184
185                         // retrieve created content page in order to get its internal fields
186
contentPage = pageManager.retrieveContentPage(contentPageId);
187
188                         // remove internal fields of content page from list of layout fields
189
Set JavaDoc pageFieldIdentifiers = contentPage.getContentFieldsMap().keySet();
190                         for ( Iterator JavaDoc i = pageFieldIdentifiers.iterator(); i.hasNext(); ) {
191                             String JavaDoc fieldIdentifier = (String JavaDoc) i.next();
192                             contentFields.remove(fieldIdentifier);
193                         }
194
195                         // create content fields for this content page
196

197                         ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN);
198
199                         Collection JavaDoc layoutFields = contentFields.values();
200                         for ( Iterator JavaDoc i = layoutFields.iterator(); i.hasNext(); ) {
201                             ContentField layoutField = (ContentField) i.next();
202                             boolean createField = all || layoutField.getContentFieldValues().isEmpty();
203                             if ( createField ) {
204                                 ContentField pageField = new ContentField();
205                                 WebappUtil.copyProperties(pageField, layoutField, request);
206                                 pageField.setId(null);
207                                 pageField.setContentFieldValues(new ArrayList JavaDoc());
208                                 pageField.setInternal(Boolean.FALSE);
209                                 contentFieldManager.createContentField(pageField, contentPageId);
210                             }
211                         }
212                     }
213                 }
214
215                 // put CP and internal fields in cache
216
CacheUtil cacheUtil = CacheUtil.getInstance(request);
217                 //we do not need to put ContentPageData into cache here because the just created page in not active
218

219                 // title
220
ContentField titleField = (ContentField) contentPage.getContentFieldsMap().get("title");
221                 List JavaDoc titleValues = titleField.getContentFieldValues();
222                 for ( Iterator JavaDoc i = titleValues.iterator(); i.hasNext(); ) {
223                     ContentFieldValue fieldValue = (ContentFieldValue) i.next();
224                     // title field is single-lined
225
String JavaDoc content = fieldValue.getSimpleValue();
226                     CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, titleField.getId(), titleField.getType(), fieldValue.getId());
227                     cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), titleField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
228                 }
229
230                 // description
231
ContentField descriptionField = (ContentField) contentPage.getContentFieldsMap().get("description");
232                 List JavaDoc descriptionValues = descriptionField.getContentFieldValues();
233                 for ( Iterator JavaDoc i = descriptionValues.iterator(); i.hasNext(); ) {
234                     ContentFieldValue fieldValue = (ContentFieldValue) i.next();
235                     // description field is single-lined
236
String JavaDoc content = fieldValue.getSimpleValue();
237                     CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, descriptionField.getId(), descriptionField.getType(), fieldValue.getId());
238                     cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), descriptionField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
239                 }
240
241                 // keywords
242
ContentField keywordsField = (ContentField) contentPage.getContentFieldsMap().get("keywords");
243                 List JavaDoc keywordsValues = keywordsField.getContentFieldValues();
244                 for ( Iterator JavaDoc i = keywordsValues.iterator(); i.hasNext(); ) {
245                     ContentFieldValue fieldValue = (ContentFieldValue) i.next();
246                     // keywords field is single-lined
247
String JavaDoc content = fieldValue.getSimpleValue();
248                     CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, keywordsField.getId(), keywordsField.getType(), fieldValue.getId());
249                     cacheUtil.putPageFieldValueInCache(cfvData, contentPage.getUri(), keywordsField.getIdentifier(), fieldValue.getContentLocale().getIdentifier());
250                 }
251
252             } catch ( OwnerNotFoundException e ) {
253                 // layout not found. it might has already been deleted by another transaction
254
ActionMessages errors = new ActionMessages();
255                 errors.add("ownerNotFound", new ActionMessage("core.contentPage.errors.ownerNotFound"));
256                 saveErrors(request, errors);
257                 return mapping.findForward("listContentPages");
258             } catch ( BeanAlreadyExistsException e ) {
259                 // contentPage already exists
260
ActionMessages errors = new ActionMessages();
261                 errors.add("contentPageAlreadyExists", new ActionMessage("core.contentPage.errors.alreadyExists"));
262                 saveErrors(request, errors);
263                 saveToken(request);
264                 return mapping.getInputForward();
265             }
266             request.getSession().setAttribute(WebappConstants.CONTENT_PAGE_ID_KEY, contentPageId);
267             return mapping.findForward("viewContentPage");
268         } else {
269             return mapping.findForward("listContentPages");
270         }
271     }
272 }
Popular Tags