2 * TopicAction.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.util.List;
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
40 import mir.config.MirPropertiesConfiguration;
41 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
42 import mir.core.model.Topic;
43 import mir.core.service.storage.TopicService;
44 import mir.core.ui.action.DispatchAction;
45 import mir.core.ui.servlet.ServletConstants;
46 import multex.Failure;
47 import net.sf.hibernate.SessionFactory;
48 import net.sf.hibernate.expression.Order;
50 import org.apache.struts.action.ActionForm;
51 import org.apache.struts.action.ActionForward;
52 import org.apache.struts.action.ActionMapping;
53 import org.apache.struts.action.DynaActionForm;
58 * @version $Id: TopicAction.java,v 1.1 2003/09/30 19:29:32 idfx Exp $
60 public class TopicAction extends DispatchAction {
61 private MirPropertiesConfiguration _configuration;
66 _configuration = MirPropertiesConfiguration.instance();
67 } catch (PropertiesConfigExc e) {
68 throw new Failure("could not load config", e);
72 private ActionForward add(ActionMapping actionMapping, ActionForm actionForm,
73 HttpServletRequest request, HttpServletResponse response)
75 //access to persistence
76 ServletContext context = getServlet().getServletContext();
77 TopicService topicService =
78 new TopicService((SessionFactory)context
79 .getAttribute(ServletConstants.SESSION_FACTORY));
81 List topics = topicService.list();
82 Topic defaultParent = new Topic();
83 defaultParent.setId(new Integer(-1));
84 defaultParent.setTitle("----");
85 topics.add(0, defaultParent);
87 request.setAttribute(ServletConstants.NEW, new Boolean(true));
88 request.setAttribute(ServletConstants.ID, "");
89 request.setAttribute(ServletConstants.OFFSET, "");
90 request.setAttribute("topics", topics);
92 //setting standard-values
93 Topic topic = new Topic();
94 request.setAttribute("topic", topic);
97 return actionMapping.findForward("success");
100 private ActionForward save(ActionMapping actionMapping, ActionForm actionForm,
101 HttpServletRequest request, HttpServletResponse response)
103 //access to persistence
104 ServletContext context = getServlet().getServletContext();
105 TopicService topicService =
106 new TopicService((SessionFactory)context
107 .getAttribute(ServletConstants.SESSION_FACTORY));
109 DynaActionForm form = (DynaActionForm) actionForm;
111 return actionMapping.findForward("failed");
114 Boolean isnew = (Boolean) form.get("new");
118 if(isnew.booleanValue()){
121 Integer id = new Integer(request.getParameter(ServletConstants.ID));
122 topic = (Topic) topicService.load(id);
126 return actionMapping.findForward("failed");
129 topic.setTitle((String)form.get("title"));
130 topic.setDescription((String)form.get("title"));
131 topic.setFilename((String)form.get("filename"));
132 topic.setMainUrl((String)form.get("mainUrl"));
133 topic.setArchivUrl((String)form.get("archivUrl"));
134 topic.setParentTopic(null);
136 Integer parentId = (Integer)form.get("parentTopic");
137 if(parentId != null && parentId.intValue() > -1){
138 Topic parentTopic = (Topic) topicService.load(parentId);
139 topic.setParentTopic(parentTopic);
142 if(isnew.booleanValue()){
143 System.out.println(topic);
144 Integer id = topicService.save(topic);
145 request.setAttribute(ServletConstants.OFFSET, "0");
147 topicService.update(topic);
151 return actionMapping.findForward("success");
154 private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm,
155 HttpServletRequest request, HttpServletResponse response)
157 //access to persistence
158 ServletContext context = getServlet().getServletContext();
159 TopicService topicService =
160 new TopicService((SessionFactory)context
161 .getAttribute(ServletConstants.SESSION_FACTORY));
164 Integer id = new Integer(request.getParameter(ServletConstants.ID));
166 //confirm the request
167 System.out.println(request.getAttribute(ServletConstants.DELETE));
168 if(request.getAttribute(ServletConstants.DELETE) == null){
169 return actionMapping.findForward("confirm");
172 Topic topic = (Topic) topicService.load(id);
175 return actionMapping.findForward("failed");
178 topicService.delete(topic);
181 return actionMapping.findForward("success");
184 private ActionForward list(ActionMapping actionMapping, ActionForm actionForm,
185 HttpServletRequest request, HttpServletResponse response)
187 //retrieve parameters
188 String offsetString = request.getParameter(ServletConstants.OFFSET);
190 if(offsetString != null && !offsetString.equals("")){
191 offset = new Integer(offsetString).intValue();
194 //access to persistence
195 ServletContext context = getServlet().getServletContext();
196 TopicService topicService =
197 new TopicService((SessionFactory)context
198 .getAttribute(ServletConstants.SESSION_FACTORY));
201 Order order = Order.desc("id");
202 List topics = topicService.list(offset, order);
204 //configure the data to send to view
205 int listSize = _configuration.getInt("ServletModule.Default.ListSize");
207 if(offset-listSize < 0){
208 lastOffset = new Integer(0);
210 lastOffset = new Integer(offset-listSize);
212 request.setAttribute(ServletConstants.LAST_OFFSET, lastOffset);
213 request.setAttribute(ServletConstants.NEXT_OFFSET,
214 new Integer(offset + listSize));
215 request.setAttribute(ServletConstants.OFFSET,
216 new Integer(offset));
217 request.setAttribute("topics", topics);
220 return actionMapping.findForward("success");
223 private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm,
224 HttpServletRequest request, HttpServletResponse response)
226 //retrieve parameters
227 Integer id = new Integer(request.getParameter(ServletConstants.ID));
228 String offset = request.getParameter(ServletConstants.OFFSET);
231 //access to persistence
232 ServletContext context = getServlet().getServletContext();
233 TopicService topicService =
234 new TopicService((SessionFactory)context
235 .getAttribute(ServletConstants.SESSION_FACTORY));
238 Topic topic = (Topic) topicService.load(id);
240 List topics = topicService.list();
241 Topic defaultParent = new Topic();
242 defaultParent.setId(new Integer(-1));
243 defaultParent.setTitle("----");
244 topics.add(0, defaultParent);
246 //configure the data to send to view
247 request.setAttribute(ServletConstants.OFFSET, offset);
248 request.setAttribute(ServletConstants.NEW,"0");
249 request.setAttribute("topic", topic);
250 request.setAttribute("topics", topics);
253 return actionMapping.findForward("success");