KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > news > CreateNewsItemAction


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.news;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.NewsModuleConstants;
20 import com.blandware.atleap.common.parsers.html.HTMLPlainTextExtractor;
21 import com.blandware.atleap.common.util.ConvertUtil;
22 import com.blandware.atleap.model.news.NewsItem;
23 import com.blandware.atleap.search.SearchManager;
24 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
25 import com.blandware.atleap.service.news.NewsManager;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.NewsItemForm;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import com.blandware.atleap.webapp.util.core.WebappConstants;
30 import com.blandware.atleap.webapp.util.core.LocaleUtil;
31 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants;
32 import org.apache.commons.validator.GenericValidator;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37 import org.apache.struts.action.ActionMessages;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletResponse JavaDoc;
41 import java.io.ByteArrayInputStream JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.util.*;
44
45 /**
46  * <p>Creates new news item
47  * </p>
48  * <p><a HREF="CreateNewsItemAction.java.htm"><i>View Source</i></a></p>
49  * <p/>
50  *
51  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
52  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
53  * @version $Revision: 1.28 $ $Date: 2006/03/10 17:10:31 $
54  * @struts.action path="/news/createItem"
55  * name="newsItemForm"
56  * scope="request"
57  * input="inputForward"
58  * validate="true"
59  * roles="news-item-create"
60  * @struts.action-forward name="inputForward"
61  * path=".news.createItemAnnotation"
62  * @struts.action-forward name="listNewsItems"
63  * path="/news/listItems.do"
64  * redirect="true"
65  * @struts.action-forward name="callCreateItem"
66  * path=".news.createItem"
67  */

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

78     public ActionForward execute(ActionMapping mapping, ActionForm form,
79                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
80
81         NewsItemForm newsItemForm = (NewsItemForm) form;
82         String JavaDoc localesMode = newsItemForm.getLocalesMode();
83
84         if ( isBackPressed(request) ) {
85             // First check, may be session has been invalidated and we need to
86
// fill content locales again
87
if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) {
88                 List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
89                 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
90             }
91
92             // check 'check all' ckeckbox if needed
93
newsItemForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
94
95             return mapping.findForward("inputForward");
96         }
97
98         if ( isCancelled(request) ) {
99             return mapping.findForward("listNewsItems");
100         }
101
102         if ( request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_SUBMITTED_ACTION_KEY) == null ) {
103             request.getSession().setAttribute(NewsModuleWebConstants.NEWS_ITEM_SUBMITTED_ACTION_KEY, "create");
104
105             NewsItem newsItem = new NewsItem();
106             WebappUtil.copyProperties(newsItem, newsItemForm, request);
107
108             Date publicationDate = newsItem.getPublicationDate();
109
110             // set activity
111
Date today = new Date();
112             if ( publicationDate.compareTo(today) <= 0 ) {
113                 newsItem.setActive(Boolean.TRUE);
114             } else {
115                 newsItem.setActive(Boolean.FALSE);
116             }
117
118             Map annotationMap = newsItemForm.getAnnotationMap();
119             Map bodyMap = newsItemForm.getBodyMap();
120             Map annotationCheckedBoxes = newsItemForm.getAnnotationCheckedBoxes();
121             Map bodyCheckedBoxes = newsItemForm.getBodyCheckedBoxes();
122             for (Iterator i = annotationMap.entrySet().iterator(); i.hasNext();) {
123                 Map.Entry entry = (Map.Entry) i.next();
124                 String JavaDoc localeIdentifier = (String JavaDoc) entry.getKey();
125                 if (annotationCheckedBoxes.get(localeIdentifier) == null) {
126                     // if this locale is not checked, remove annotation
127
annotationMap.put(localeIdentifier, "");
128                 }
129             }
130             for (Iterator i = bodyMap.entrySet().iterator(); i.hasNext();) {
131                 Map.Entry entry = (Map.Entry) i.next();
132                 String JavaDoc localeIdentifier = (String JavaDoc) entry.getKey();
133                 if (bodyCheckedBoxes.get(localeIdentifier) == null) {
134                     // if this locale is not checked, remove body
135
bodyMap.put(localeIdentifier, "");
136                 }
137             }
138
139             newsItem.setTitle(newsItemForm.getTitleMap());
140             newsItem.setAnnotation(annotationMap);
141             newsItem.setBody(bodyMap);
142             NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
143
144             // get all refs
145
HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor();
146             Set refs = new HashSet();
147             for ( Iterator i = annotationMap.entrySet().iterator(); i.hasNext(); ) {
148                 Map.Entry entry = (Map.Entry) i.next();
149                 String JavaDoc locale = (String JavaDoc) entry.getKey();
150                 String JavaDoc annotation = (String JavaDoc) entry.getValue();
151                 String JavaDoc body = (String JavaDoc) bodyMap.get(locale);
152                 InputStream JavaDoc annotationIS = new ByteArrayInputStream JavaDoc(ConvertUtil.convertToByteArray(annotation));
153                 refs.addAll(extractor.extractAllRefs(annotationIS, Constants.DEFAULT_ENCODING));
154                 if ( !GenericValidator.isBlankOrNull(body) ) {
155                     InputStream JavaDoc bodyIS = new ByteArrayInputStream JavaDoc(ConvertUtil.convertToByteArray(body));
156                     refs.addAll(extractor.extractAllRefs(bodyIS, Constants.DEFAULT_ENCODING));
157                 }
158             }
159
160             Map linkedObjects = WebappUtil.mapObjectsToRefs(refs, request.getSession().getServletContext(), request.getContextPath());
161
162             Long JavaDoc newsItemId = null;
163             String JavaDoc uri = null;
164
165             try {
166                 // we need an ID to create URI of item, so we must first create item, then set URI and update item
167
newsItemId = newsManager.createNewsItem(newsItem, linkedObjects);
168
169                 uri = NewsModuleWebConstants.NEWS_ITEM_URI_PREFIX + "/item" + newsItemId;
170                 newsItem.setUri(uri);
171                 newsManager.updateNewsItem(newsItem, linkedObjects);
172
173                 if ( newsItem.getActive().booleanValue() ) {
174                     // index news item
175
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
176                     searchManager.reIndexPage(newsItem, request);
177                 }
178
179             } catch ( BeanAlreadyExistsException e ) {
180                 // news item already exists
181

182                 // check 'check all' ckeckbox if needed
183
newsItemForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
184
185                 ActionMessages errors = new ActionMessages();
186                 errors.add("newsItemAlreadyExists", new ActionMessage("news.errors.alreadyExists", uri));
187                 saveErrors(request, errors);
188                 saveToken(request);
189                 return mapping.getInputForward();
190             }
191         }
192         return mapping.findForward("listNewsItems");
193     }
194 }
Popular Tags