KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.blandware.atleap.common.util.DateUtil;
19 import com.blandware.atleap.webapp.action.core.BaseAction;
20 import com.blandware.atleap.webapp.form.NewsItemForm;
21 import com.blandware.atleap.webapp.util.core.LocaleUtil;
22 import com.blandware.atleap.webapp.util.core.WebappConstants;
23 import com.blandware.atleap.webapp.util.core.WebappUtil;
24 import com.blandware.atleap.webapp.util.news.NewsModuleWebConstants;
25 import org.apache.struts.action.*;
26 import org.apache.struts.util.RequestUtils;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import java.text.ParseException JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.List JavaDoc;
33
34
35 /**
36  * <p>Returns page which allows creating of news item annotation in selected
37  * languages</p>
38  *
39  * <p><a HREF="CallCreateNewsItemAnnotationAction.java.htm"></a><i>View source</i></p>
40  *
41  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
42  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
43  * @version $Revision: 1.12 $ $Date: 2006/03/16 11:09:42 $
44  * @struts.action path="/news/callCreateItemAnnotation"
45  * input="inputForward"
46  * name="newsItemForm"
47  * validate="false"
48  * roles="news-item-create"
49  * scope="request"
50  * @struts.action-forward name="inputForward"
51  * path=".news.createItem"
52  * @struts.action-forward name="createNewsItemAnnotation"
53  * path=".news.createItemAnnotation"
54  * @struts.action-forward name="listNewsItems"
55  * path="/news/listItems.do"
56  * redirect="true"
57  */

58 public final class CallCreateNewsItemAnnotationAction extends BaseAction {
59     /**
60      * @param mapping The ActionMapping used to select this instance
61      * @param form The optional ActionForm bean for this request (if any)
62      * @param request The HTTP request we are proceeding
63      * @param response The HTTP response we are creating
64      * @return an ActionForward instance describing where and how
65      * control should be forwarded, or null if response
66      * has already been completed
67      */

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

100             // check 'check all' ckeckbox if needed
101
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
102
103             ActionMessages errors = new ActionMessages();
104             errors.add("title", new ActionMessage("core.commons.errors.required", getMessage(request, "news.form.title")));
105             saveErrors(request, errors, false);
106             saveToken(request);
107             return mapping.getInputForward();
108         }
109
110         // check publication and expiration dates
111
Date JavaDoc publicationDate = null;
112         Date JavaDoc expirationDate = null;
113         boolean publicationDateIsValid = false;
114         try {
115             publicationDate = DateUtil.parseDate(newsItemForm.getPublicationDate(), RequestUtils.getUserLocale(request, null));
116             publicationDateIsValid = true;
117             expirationDate = DateUtil.parseDate(newsItemForm.getExpirationDate(), RequestUtils.getUserLocale(request, null));
118         } catch (ParseException JavaDoc e) {
119             ActionMessages errors = new ActionMessages();
120             errors.add("newsItemInvalidDate", new ActionMessage("core.commons.errors.customDate",
121                         publicationDateIsValid ? newsItemForm.getExpirationDate() : newsItemForm.getPublicationDate()));
122             saveErrors(request, errors);
123             saveToken(request);
124             return mapping.getInputForward();
125         }
126
127         if ( publicationDate.compareTo(expirationDate) >= 0 ) {
128             // check 'check all' ckeckbox if needed
129
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
130
131             ActionMessages errors = new ActionMessages();
132             errors.add("newsItemInvalidDate", new ActionMessage("news.errors.invalidPublicationDate"));
133             saveErrors(request, errors);
134             saveToken(request);
135             return mapping.getInputForward();
136         }
137
138         Date JavaDoc now = new Date JavaDoc();
139         if (expirationDate.compareTo(now) <= 0) {
140             // check 'check all' ckeckbox if needed
141
newsItemForm.setAllCheckedFlagForAnnotationCheckboxes(request, localesMode);
142
143             ActionMessages errors = new ActionMessages();
144             errors.add("newsItemInvalidDate", new ActionMessage("news.errors.invalidExpirationDate"));
145             saveErrors(request, errors);
146             saveToken(request);
147             return mapping.getInputForward();
148         }
149
150         if (newsItemForm.getAnnotationCheckedBoxes().size() != 0) {
151             newsItemForm.setBodyCheckedBoxes(newsItemForm.getAnnotationCheckedBoxes());
152         }
153         newsItemForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
154
155         saveToken(request);
156         return mapping.findForward("createNewsItemAnnotation");
157     }
158 }
159
Popular Tags