2 * ContentAction.java created on 05.09.2003
4 * Copyright (C) 2001, 2002, 2003 The Mir-coders group
6 * This file is part of Mir.
8 * Mir is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Mir is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Mir; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * In addition, as a special exception, The Mir-coders gives permission to link
23 * the code of this program with any library licensed under the Apache Software License,
24 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
25 * (or with modified versions of the above that use the same license as the above),
26 * and distribute linked combinations including the two. You must obey the
27 * GNU General Public License in all respects for all of the code used other than
28 * the above mentioned libraries. If you modify this file, you may extend this
29 * exception to your version of the file, but you are not obligated to do so.
30 * If you do not wish to do so, delete this exception statement from your version.
32 package mir.core.ui.action.admin;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.List;
38 import javax.servlet.ServletContext;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.http.HttpSession;
43 import mir.config.MirPropertiesConfiguration;
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
45 import mir.core.model.ArticleType;
46 import mir.core.model.Content;
47 import mir.core.model.IContent;
48 import mir.core.model.Language;
49 import mir.core.model.MirUser;
50 import mir.core.service.storage.ArticleTypeService;
51 import mir.core.service.storage.ContentService;
52 import mir.core.service.storage.LanguageService;
53 import mir.core.ui.action.DispatchAction;
54 import mir.core.ui.servlet.ServletConstants;
55 import multex.Failure;
56 import net.sf.hibernate.SessionFactory;
57 import net.sf.hibernate.expression.Expression;
58 import net.sf.hibernate.expression.Order;
60 import org.apache.struts.action.ActionForm;
61 import org.apache.struts.action.ActionForward;
62 import org.apache.struts.action.ActionMapping;
63 import org.apache.struts.action.DynaActionForm;
68 * @version $Id: ContentAction.java,v 1.1 2003/12/20 20:27:09 idfx Exp $
70 public class ContentAction extends DispatchAction {
71 private MirPropertiesConfiguration _configuration;
73 public ContentAction(){
76 _configuration = MirPropertiesConfiguration.instance();
77 } catch (PropertiesConfigExc e) {
78 throw new Failure("could not load config", e);
82 private ActionForward add(ActionMapping actionMapping, ActionForm actionForm,
83 HttpServletRequest request, HttpServletResponse response)
85 //access to persistence
86 ServletContext context = getServlet().getServletContext();
87 ContentService contentService =
88 new ContentService((SessionFactory)context
89 .getAttribute(ServletConstants.SESSION_FACTORY));
91 request.setAttribute(ServletConstants.NEW, new Boolean(true));
92 request.setAttribute(ServletConstants.ID, "");
93 request.setAttribute(ServletConstants.OFFSET, "");
95 //setting standard-values
96 Content content = new Content();
97 request.setAttribute("article", content);
100 return actionMapping.findForward("success");
103 private ActionForward save(ActionMapping actionMapping, ActionForm actionForm,
104 HttpServletRequest request, HttpServletResponse response)
106 //access to persistence
107 ServletContext context = getServlet().getServletContext();
108 HttpSession httpSession = request.getSession();
109 ContentService contentService =
110 new ContentService((SessionFactory)context
111 .getAttribute(ServletConstants.SESSION_FACTORY));
112 ArticleTypeService articleTypeService =
113 new ArticleTypeService((SessionFactory)context
114 .getAttribute(ServletConstants.SESSION_FACTORY));
115 LanguageService languageService =
116 new LanguageService((SessionFactory)context
117 .getAttribute(ServletConstants.SESSION_FACTORY));
120 DynaActionForm form = (DynaActionForm) actionForm;
122 return actionMapping.findForward("failed");
126 Boolean isnew = (Boolean) form.get("new");
129 IContent content = null;
130 if(isnew.booleanValue()){
131 content = new Content();
132 //set change and create date
133 content.setWebdbCreate(new Date());
134 content.setWebdbLastchange(new Date());
136 Integer id = new Integer(request.getParameter(ServletConstants.ID));
137 content = (IContent) contentService.load(id);
139 content.setWebdbLastchange(new Date());
143 return actionMapping.findForward("failed");
146 String webDbCreate = (String) form.get("date");
147 if(webDbCreate == null || webDbCreate.length() == 0){
148 Date date = new Date();
149 SimpleDateFormat dateFormat = new SimpleDateFormat();
150 dateFormat.applyPattern("yyyyMMdd");
151 webDbCreate = dateFormat.format(date);
153 content.setDate(webDbCreate);
156 Integer articleTypeId = (Integer) form.get("to_article_type");
157 content.setArticleType(
158 (ArticleType) articleTypeService.load(articleTypeId));
161 Integer languageTypeId = (Integer) form.get("to_language");
163 (Language) languageService.load(languageTypeId));
166 content.setPublisher(
167 (MirUser) httpSession.getAttribute(ServletConstants.USER));
168 content.setTitle((String) form.get("title"));
169 content.setSubtitle((String) form.get("subtitle"));
170 content.setComment((String) form.get("comment"));
171 content.setContentData((String) form.get("content_data"));
172 content.setCreator((String) form.get("creator"));
173 content.setCreatorAddress((String)form.get("creator_address"));
174 content.setCreatorEmail((String)form.get("creator_email"));
175 content.setCreatorPhone((String)form.get("creator_phone"));
176 content.setCreatorMainUrl((String)form.get("creator_main_url"));
177 content.setDescription((String)form.get("description"));
178 content.setEdittitle((String)form.get("edittitle"));
179 if(form.get("is_html") != null){
180 content.setHtml(true);
182 content.setHtml(false);
184 if(form.get("is_published") != null){
185 content.setPublished(true);
187 content.setPublished(false);
190 // Integer parentId = (Integer)form.get("parentContent");
191 // if(parentId != null && parentId.intValue() > -1){
192 // Topic parentTopic = (Topic) topicService.load(parentId);
193 // topic.setParentTopic(parentTopic);
197 if(isnew.booleanValue()){
198 Integer id = contentService.save(content);
199 request.setAttribute(ServletConstants.OFFSET, "0");
201 contentService.update(content);
205 return actionMapping.findForward("success");
208 private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm,
209 HttpServletRequest request, HttpServletResponse response)
211 //access to persistence
212 ServletContext context = getServlet().getServletContext();
213 ContentService contentService =
214 new ContentService((SessionFactory)context
215 .getAttribute(ServletConstants.SESSION_FACTORY));
218 Integer id = new Integer(request.getParameter(ServletConstants.ID));
220 //confirm the request
221 System.out.println(request.getAttribute(ServletConstants.DELETE));
222 if(request.getAttribute(ServletConstants.DELETE) == null){
223 return actionMapping.findForward("confirm");
226 Content content = (Content) contentService.load(id);
229 return actionMapping.findForward("failed");
232 contentService.delete(content);
235 return actionMapping.findForward("success");
238 private ActionForward list(ActionMapping actionMapping, ActionForm actionForm,
239 HttpServletRequest request, HttpServletResponse response)
241 //retrieve parameters
242 String offsetString = request.getParameter(ServletConstants.OFFSET);
244 if(offsetString != null && !offsetString.equals("")){
245 offset = new Integer(offsetString).intValue();
247 String articleTypeString = request.getParameter("articletype");
248 Integer articleTypeId = new Integer(-1);
249 if(articleTypeString != null && !articleTypeString.equals("")){
250 articleTypeId = new Integer(articleTypeString);
253 //access to persistence
254 ServletContext context = getServlet().getServletContext();
255 ContentService contentService =
256 new ContentService((SessionFactory)context
257 .getAttribute(ServletConstants.SESSION_FACTORY));
261 Order order = Order.desc("id");
263 if(articleTypeId.intValue() >= 0){
264 ArticleTypeService articleTypeService =
265 new ArticleTypeService((SessionFactory)context
266 .getAttribute(ServletConstants.SESSION_FACTORY));
267 Expression expression =
268 Expression.eq("articleType",
269 articleTypeService.load(articleTypeId));
270 contents = contentService.list(offset, expression, order);
272 contents = contentService.list(offset, order);
276 //configure the data to send to view
277 int listSize = _configuration.getInt("ServletModule.Default.ListSize");
279 if(offset-listSize < 0){
280 lastOffset = new Integer(0);
282 lastOffset = new Integer(offset-listSize);
284 request.setAttribute(ServletConstants.LAST_OFFSET, lastOffset);
285 request.setAttribute(ServletConstants.NEXT_OFFSET,
286 new Integer(offset + listSize));
287 request.setAttribute(ServletConstants.OFFSET,
288 new Integer(offset));
289 request.setAttribute("articles", contents);
292 return actionMapping.findForward("success");
295 private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm,
296 HttpServletRequest request, HttpServletResponse response)
298 //retrieve parameters
299 Integer id = new Integer(request.getParameter(ServletConstants.ID));
300 String offset = request.getParameter(ServletConstants.OFFSET);
302 //access to persistence
303 ServletContext context = getServlet().getServletContext();
304 ContentService contentService =
305 new ContentService((SessionFactory)context
306 .getAttribute(ServletConstants.SESSION_FACTORY));
309 IContent content = (IContent) contentService.load(id);
311 //configure the data to send to view
312 request.setAttribute(ServletConstants.OFFSET, offset);
313 request.setAttribute(ServletConstants.NEW,"0");
314 request.setAttribute("article", content);
317 return actionMapping.findForward("success");