a new servletfilter, which controls the caching-values in the http-header.\ra new...
[mir.git] / source / mir / core / ui / action / admin / MessageAction.java
1 /*
2  * AuthenticationAction.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.Date;
35 import java.util.List;
36
37 import javax.servlet.ServletContext;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession;
41
42 import mir.config.MirPropertiesConfiguration;
43 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
44 import mir.core.model.Message;
45 import mir.core.model.MirUser;
46 import mir.core.service.storage.MessageService;
47 import mir.core.ui.action.DispatchAction;
48 import mir.core.ui.servlet.ServletConstants;
49 import multex.Failure;
50 import net.sf.hibernate.SessionFactory;
51 import net.sf.hibernate.expression.Order;
52
53 import org.apache.struts.action.ActionForm;
54 import org.apache.struts.action.ActionForward;
55 import org.apache.struts.action.ActionMapping;
56 import org.apache.struts.action.DynaActionForm;
57
58 /**
59  * AuthenticationAction
60  * @author idefix
61  * @version $Id: MessageAction.java,v 1.2 2003/09/18 21:42:16 idfx Exp $
62  */
63 public class MessageAction extends DispatchAction {
64         private MirPropertiesConfiguration _configuration;
65
66         public MessageAction(){
67                 super();
68                 try {
69                         _configuration = MirPropertiesConfiguration.instance();
70                 } catch (PropertiesConfigExc e) {
71                         throw new Failure("could not load config", e);
72                 }               
73         }
74
75         private ActionForward add(ActionMapping actionMapping, ActionForm actionForm, 
76                 HttpServletRequest request, HttpServletResponse response)
77                 throws Exception {
78
79                 request.setAttribute(ServletConstants.NEW, new Boolean(true));
80                 request.setAttribute(ServletConstants.ID, "");
81                 request.setAttribute(ServletConstants.OFFSET, "");
82                 
83                 //setting standard-values
84                 Message message = new Message();
85                 HttpSession session = request.getSession();
86                 MirUser user = (MirUser) session.getAttribute(ServletConstants.USER);
87                 message.setCreator(user.getLogin());
88                 request.setAttribute("message", message);
89                 
90                 //              show the view
91                 return actionMapping.findForward("success");
92         }
93         
94         private ActionForward save(ActionMapping actionMapping, ActionForm actionForm, 
95                 HttpServletRequest request, HttpServletResponse response)
96                 throws Exception {
97                 //access to persistence
98                 ServletContext context = getServlet().getServletContext();
99                 MessageService messageService = 
100                         new MessageService((SessionFactory)context
101                                 .getAttribute(ServletConstants.SESSION_FACTORY));
102                 
103                 DynaActionForm form = (DynaActionForm) actionForm;
104                 if(form == null){
105                         return actionMapping.findForward("failed");     
106                 }
107                 //check if new
108                 Boolean isnew = (Boolean) form.get("new");
109                 
110                 //retrieve id
111                 Message message = null;
112                 if(isnew.booleanValue()){
113                         message = new Message();
114                 } else {
115                         Integer id = new Integer(request.getParameter(ServletConstants.ID));
116                         message = (Message) messageService.load(id);
117                 }
118                 
119                 if(message == null){
120                         return actionMapping.findForward("failed");     
121                 }
122                                         
123                 message.setTitle((String)form.get("title"));
124                 message.setDescription((String)form.get("description"));
125                 message.setCreator((String)form.get("creator"));
126                 
127                 if(isnew.booleanValue()){
128                         message.setWebdbCreate(new Date());
129                         Integer id = messageService.save(message);
130                         request.setAttribute(ServletConstants.OFFSET, "0");
131                 } else {
132                         messageService.update(message);
133                 }
134                 
135                 //              show the view
136                 return actionMapping.findForward("success");
137         }
138                 
139         private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm, 
140                 HttpServletRequest request, HttpServletResponse response)
141                 throws Exception {
142                 //access to persistence
143                 ServletContext context = getServlet().getServletContext();
144                 MessageService messageService = 
145                         new MessageService((SessionFactory)context
146                                 .getAttribute(ServletConstants.SESSION_FACTORY));
147                 
148                 //retrieve id
149                 Integer id = new Integer(request.getParameter(ServletConstants.ID));
150                 
151                 //confirm the request
152                 System.out.println(request.getAttribute(ServletConstants.DELETE));
153                 if(request.getAttribute(ServletConstants.DELETE) == null){
154                         System.out.println("call confirm");
155                         return actionMapping.findForward("confirm");
156                 }
157                 System.out.println("komish confirm");
158                 //load object
159                 Message message = (Message) messageService.load(id);
160                 
161                 if(message == null){
162                         return actionMapping.findForward("failed");     
163                 }
164                                         
165                 messageService.delete(message);
166                 
167                 //              show the view
168                 return actionMapping.findForward("success");
169         }
170
171         private ActionForward list(ActionMapping actionMapping, ActionForm actionForm, 
172                 HttpServletRequest request, HttpServletResponse response)
173                 throws Exception {
174                 //retrieve parameters
175                 String offsetString = request.getParameter(ServletConstants.OFFSET);
176                 int offset = 0;
177                 if(offsetString != null && !offsetString.equals("")){
178                         offset = new Integer(offsetString).intValue();
179                 }
180                 
181                 //access to persistence
182                 ServletContext context = getServlet().getServletContext();
183                 MessageService messageService = 
184                         new MessageService((SessionFactory)context
185                                 .getAttribute(ServletConstants.SESSION_FACTORY));
186                 
187                 //retrieve entities
188                 Order order = Order.desc("webdbCreate");
189                 List messages = messageService.list(offset, order);
190                 
191                 //configure the data to send to view
192                 int listSize = _configuration.getInt("ServletModule.Default.ListSize");
193                 Integer lastOffset;
194                 if(offset-listSize < 0){
195                         lastOffset = new Integer(0);
196                 } else {
197                         lastOffset = new Integer(offset-listSize);
198                 }
199                 request.setAttribute(ServletConstants.LAST_OFFSET, lastOffset);
200                 request.setAttribute(ServletConstants.NEXT_OFFSET, 
201                         new Integer(offset + listSize));
202                 request.setAttribute(ServletConstants.OFFSET, 
203                         new Integer(offset));
204                 request.setAttribute("messages", messages);
205                 
206                 //show the view
207                 return actionMapping.findForward("success");    
208         }
209         
210         private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm, 
211                 HttpServletRequest request, HttpServletResponse response)
212                 throws Exception {
213                 //retrieve parameters
214                 Integer id = new Integer(request.getParameter(ServletConstants.ID));
215                 String offset = request.getParameter(ServletConstants.OFFSET);
216
217                 
218                 //access to persistence
219                 ServletContext context = getServlet().getServletContext();
220                 MessageService messageService = 
221                         new MessageService((SessionFactory)context
222                                 .getAttribute(ServletConstants.SESSION_FACTORY));
223                 
224                 //retrieve entities
225                 Message message = (Message) messageService.load(id);
226                 
227                 //configure the data to send to view
228                 request.setAttribute(ServletConstants.OFFSET, offset);
229                 request.setAttribute(ServletConstants.NEW,"0");
230                 request.setAttribute("message", message);
231                 
232                 //show the view
233                 return actionMapping.findForward("success");    
234         }
235 }