KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 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 org.apache.struts.action.*;
19 import org.apache.struts.util.RequestUtils;
20 import org.apache.commons.validator.GenericValidator;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants;
26 import com.blandware.atleap.webapp.util.core.WebappConstants;
27 import com.blandware.atleap.webapp.util.core.LocaleUtil;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import com.blandware.atleap.webapp.action.core.BaseAction;
30 import com.blandware.atleap.webapp.form.NewsItemForm;
31 import com.blandware.atleap.service.news.NewsManager;
32 import com.blandware.atleap.common.NewsModuleConstants;
33 import com.blandware.atleap.common.util.DateUtil;
34 import com.blandware.atleap.model.news.NewsItem;
35
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.text.ParseException JavaDoc;
40
41
42 /**
43  * <p>Returns page which allows editing of news item annotation in selected
44  * languages</p>
45  *
46  * <p><a HREF="CallUpdateNewsItemAnnotationAction.java.htm"></a><i>View source</i></p>
47  *
48  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
49  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
50  * @version $Revision: 1.11 $ $Date: 2006/03/10 17:10:31 $
51  * @struts.action path="/news/callUpdateItemAnnotation"
52  * input="inputForward"
53  * name="newsItemForm"
54  * validate="false"
55  * roles="news-item-update"
56  * scope="request"
57  * @struts.action-forward name="inputForward"
58  * path=".news.updateItem"
59  * @struts.action-forward name="updateNewsItemAnnotation"
60  * path=".news.updateItemAnnotation"
61  * @struts.action-forward name="listNewsItems"
62  * path="/news/listItems.do"
63  * redirect="true"
64  */

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

75     public ActionForward execute(ActionMapping mapping, ActionForm form,
76                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
77
78         request.getSession().removeAttribute(NewsModuleWebConstants.NEWS_ITEM_SUBMITTED_ACTION_KEY);
79
80         if ( isCancelled(request) ) {
81             if ( request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY) != null ) {
82                 String JavaDoc redirectUrl = (String JavaDoc) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY);
83                 ActionForward redirect = new ActionForward(redirectUrl, true);
84                 return redirect;
85             }
86             return mapping.findForward("listNewsItems");
87         }
88
89         if ( request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null ) {
90             List JavaDoc contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
91             request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
92         }
93
94         NewsItemForm newsItemForm = (NewsItemForm) form;
95
96         // Check whether user came here not from previous page (or he switched
97
// edit mode/language during editing news item)
98
String JavaDoc localesMode = newsItemForm.getLocalesMode();
99         if (!"current".equalsIgnoreCase(localesMode) && !"all".equalsIgnoreCase(localesMode) && newsItemForm.getAnnotationCheckedBoxes().size() == 0) {
100             request.setAttribute("beginPage", mapping.findForward("listNewsItems").getPath());
101             return mapping.findForward("unsatisfiablePage");
102         }
103
104         if ( !WebappUtil.hasCorrectValues(newsItemForm.getTitleMap()) ) {
105             // title must be specified
106

107             // check 'check all' ckeckbox if needed
108
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
109
110             ActionMessages errors = new ActionMessages();
111             errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "news.form.title")));
112             saveErrors(request, errors, false);
113             saveToken(request);
114             return mapping.getInputForward();
115         }
116
117         // check news item existence, because it might be deleted by another transaction
118
Long JavaDoc newsItemId = null;
119         if ( !GenericValidator.isBlankOrNull(newsItemForm.getId()) ) {
120             newsItemId = Long.valueOf(newsItemForm.getId());
121         } else {
122             if ( log.isWarnEnabled() ) {
123                 log.warn("Missing news item ID. Returning to list");
124             }
125             return mapping.findForward("listNewsItems");
126         }
127
128         NewsManager newsManager = (NewsManager) getBean(NewsModuleConstants.NEWS_MANAGER_BEAN);
129         NewsItem newsItem = newsManager.retrieveNewsItem(newsItemId);
130         if ( newsItem == null ) {
131             ActionMessages errors = new ActionMessages();
132             errors.add("newsItemNotFound", new ActionMessage("news.errors.notFound"));
133             saveErrors(request, errors);
134             return mapping.findForward("listNewsItems");
135         }
136
137         // check publication and expiration dates
138
Date JavaDoc publicationDate = null;
139         Date JavaDoc expirationDate = null;
140         boolean publicationDateIsValid = false;
141         try {
142             publicationDate = DateUtil.parseDate(newsItemForm.getPublicationDate(), RequestUtils.getUserLocale(request, null));
143             publicationDateIsValid = true;
144             expirationDate = DateUtil.parseDate(newsItemForm.getExpirationDate(), RequestUtils.getUserLocale(request, null));
145         } catch (ParseException JavaDoc e) {
146             ActionMessages errors = new ActionMessages();
147             errors.add("newsItemInvalidDate", new ActionMessage("core.commons.errors.customDate",
148                         publicationDateIsValid ? newsItemForm.getExpirationDate() : newsItemForm.getPublicationDate()));
149             saveErrors(request, errors);
150             saveToken(request);
151             return mapping.getInputForward();
152         }
153
154
155         if ( publicationDate.compareTo(expirationDate) >= 0 ) {
156             // check 'check all' ckeckbox if needed
157
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
158
159             ActionMessages errors = new ActionMessages();
160             errors.add("newsItemInvalidDate", new ActionMessage("news.errors.invalidPublicationDate"));
161             saveErrors(request, errors);
162             saveToken(request);
163             return mapping.getInputForward();
164         }
165
166         Date JavaDoc now = new Date JavaDoc();
167         if (expirationDate.compareTo(now) <= 0) {
168             // check 'check all' ckeckbox if needed
169
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
170
171             ActionMessages errors = new ActionMessages();
172             errors.add("newsItemInvalidDate", new ActionMessage("news.errors.invalidExpirationDate"));
173             saveErrors(request, errors);
174             saveToken(request);
175             return mapping.getInputForward();
176         }
177
178         if (newsItemForm.getAnnotationCheckedBoxes().size() != 0) {
179             newsItemForm.setBodyCheckedBoxes(newsItemForm.getAnnotationCheckedBoxes());
180         }
181         newsItemForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
182
183         if ( request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_ANNOTATION_KEY) != null ) {
184             Map JavaDoc mergedAnnotation = (Map JavaDoc) request.getSession().getAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_ANNOTATION_KEY);
185             newsItemForm.setAnnotationMap(mergedAnnotation);
186             request.getSession().removeAttribute(NewsModuleWebConstants.NEWS_ITEM_MERGED_ANNOTATION_KEY);
187         }
188
189         saveToken(request);
190         return mapping.findForward("updateNewsItemAnnotation");
191     }
192 }
193
Popular Tags