df93a31bbb8176fe3fb717d9a41b28a8b02d1fc6
[mir.git] / source / mir / core / ui / action / admin / TopicAction.java
1 /*
2  * TopicAction.java created on 05.09.2003
3  * 
4  * Copyright (C) 2001, 2002, 2003 The Mir-coders group
5  *
6  * This file is part of Mir.
7  *
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.
12  *
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.
17  *
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
21  *
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.
31  */
32 package mir.core.ui.action.admin;
33
34 import java.util.List;
35
36 import javax.servlet.ServletContext;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
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;
49
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;
54
55 /**
56  * TopicAction
57  * @author idefix
58  * @version $Id: TopicAction.java,v 1.1 2003/09/30 19:29:32 idfx Exp $
59  */
60 public class TopicAction extends DispatchAction {
61         private MirPropertiesConfiguration _configuration;
62
63         public TopicAction(){
64                 super();
65                 try {
66                         _configuration = MirPropertiesConfiguration.instance();
67                 } catch (PropertiesConfigExc e) {
68                         throw new Failure("could not load config", e);
69                 }               
70         }
71
72         private ActionForward add(ActionMapping actionMapping, ActionForm actionForm, 
73                 HttpServletRequest request, HttpServletResponse response)
74                 throws Exception {
75                 //access to persistence
76                 ServletContext context = getServlet().getServletContext();
77                 TopicService topicService = 
78                         new TopicService((SessionFactory)context
79                                 .getAttribute(ServletConstants.SESSION_FACTORY));
80                 
81                 List topics = topicService.list();
82                 Topic defaultParent = new Topic();
83                 defaultParent.setId(new Integer(-1));
84                 defaultParent.setTitle("----");
85                 topics.add(0, defaultParent);
86                                         
87                 request.setAttribute(ServletConstants.NEW, new Boolean(true));
88                 request.setAttribute(ServletConstants.ID, "");
89                 request.setAttribute(ServletConstants.OFFSET, "");
90                 request.setAttribute("topics", topics);
91                 
92                 //setting standard-values
93                 Topic topic = new Topic();
94                 request.setAttribute("topic", topic);
95                 
96                 //              show the view
97                 return actionMapping.findForward("success");
98         }
99         
100         private ActionForward save(ActionMapping actionMapping, ActionForm actionForm, 
101                 HttpServletRequest request, HttpServletResponse response)
102                 throws Exception {
103                 //access to persistence
104                 ServletContext context = getServlet().getServletContext();
105                 TopicService topicService = 
106                         new TopicService((SessionFactory)context
107                                 .getAttribute(ServletConstants.SESSION_FACTORY));
108                 
109                 DynaActionForm form = (DynaActionForm) actionForm;
110                 if(form == null){
111                         return actionMapping.findForward("failed");     
112                 }
113                 //check if new
114                 Boolean isnew = (Boolean) form.get("new");
115                 
116                 //retrieve id
117                 Topic topic = null;
118                 if(isnew.booleanValue()){
119                         topic = new Topic();
120                 } else {
121                         Integer id = new Integer(request.getParameter(ServletConstants.ID));
122                         topic = (Topic) topicService.load(id);
123                 }
124                 
125                 if(topic == null){
126                         return actionMapping.findForward("failed");     
127                 }
128                 
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);
135                 
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);
140                 } 
141
142                 if(isnew.booleanValue()){
143                         System.out.println(topic);
144                         Integer id = topicService.save(topic);
145                         request.setAttribute(ServletConstants.OFFSET, "0");
146                 } else {
147                         topicService.update(topic);
148                 }
149                 
150                 //              show the view
151                 return actionMapping.findForward("success");
152         }
153                 
154         private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm, 
155                 HttpServletRequest request, HttpServletResponse response)
156                 throws Exception {
157                 //access to persistence
158                 ServletContext context = getServlet().getServletContext();
159                 TopicService topicService = 
160                         new TopicService((SessionFactory)context
161                                 .getAttribute(ServletConstants.SESSION_FACTORY));
162                 
163                 //retrieve id
164                 Integer id = new Integer(request.getParameter(ServletConstants.ID));
165                 
166                 //confirm the request
167                 System.out.println(request.getAttribute(ServletConstants.DELETE));
168                 if(request.getAttribute(ServletConstants.DELETE) == null){
169                         return actionMapping.findForward("confirm");
170                 }
171                 //load object
172                 Topic topic = (Topic) topicService.load(id);
173                 
174                 if(topic == null){
175                         return actionMapping.findForward("failed");     
176                 }
177                                         
178                 topicService.delete(topic);
179                 
180                 //              show the view
181                 return actionMapping.findForward("success");
182         }
183
184         private ActionForward list(ActionMapping actionMapping, ActionForm actionForm, 
185                 HttpServletRequest request, HttpServletResponse response)
186                 throws Exception {
187                 //retrieve parameters
188                 String offsetString = request.getParameter(ServletConstants.OFFSET);
189                 int offset = 0;
190                 if(offsetString != null && !offsetString.equals("")){
191                         offset = new Integer(offsetString).intValue();
192                 }
193                 
194                 //access to persistence
195                 ServletContext context = getServlet().getServletContext();
196                 TopicService topicService = 
197                         new TopicService((SessionFactory)context
198                                 .getAttribute(ServletConstants.SESSION_FACTORY));
199                 
200                 //retrieve entities
201                 Order order = Order.desc("id");
202                 List topics = topicService.list(offset, order);
203                 
204                 //configure the data to send to view
205                 int listSize = _configuration.getInt("ServletModule.Default.ListSize");
206                 Integer lastOffset;
207                 if(offset-listSize < 0){
208                         lastOffset = new Integer(0);
209                 } else {
210                         lastOffset = new Integer(offset-listSize);
211                 }
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);
218                 
219                 //show the view
220                 return actionMapping.findForward("success");    
221         }
222         
223         private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm, 
224                 HttpServletRequest request, HttpServletResponse response)
225                 throws Exception {
226                 //retrieve parameters
227                 Integer id = new Integer(request.getParameter(ServletConstants.ID));
228                 String offset = request.getParameter(ServletConstants.OFFSET);
229
230                 
231                 //access to persistence
232                 ServletContext context = getServlet().getServletContext();
233                 TopicService topicService = 
234                         new TopicService((SessionFactory)context
235                                 .getAttribute(ServletConstants.SESSION_FACTORY));
236                 
237                 //retrieve entities
238                 Topic topic = (Topic) topicService.load(id);
239                 
240                 List topics = topicService.list();
241                 Topic defaultParent = new Topic();
242                 defaultParent.setId(new Integer(-1));
243                 defaultParent.setTitle("----");
244                 topics.add(0, defaultParent);
245                 
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);
251                 
252                 //show the view
253                 return actionMapping.findForward("success");    
254         }
255 }