2 * AuthenticationAction.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.Date;
35 import java.util.List;
37 import javax.servlet.ServletContext;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import javax.servlet.http.HttpSession;
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;
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;
59 * AuthenticationAction
61 * @version $Id: MessageAction.java,v 1.2 2003/09/18 21:42:16 idfx Exp $
63 public class MessageAction extends DispatchAction {
64 private MirPropertiesConfiguration _configuration;
66 public MessageAction(){
69 _configuration = MirPropertiesConfiguration.instance();
70 } catch (PropertiesConfigExc e) {
71 throw new Failure("could not load config", e);
75 private ActionForward add(ActionMapping actionMapping, ActionForm actionForm,
76 HttpServletRequest request, HttpServletResponse response)
79 request.setAttribute(ServletConstants.NEW, new Boolean(true));
80 request.setAttribute(ServletConstants.ID, "");
81 request.setAttribute(ServletConstants.OFFSET, "");
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);
91 return actionMapping.findForward("success");
94 private ActionForward save(ActionMapping actionMapping, ActionForm actionForm,
95 HttpServletRequest request, HttpServletResponse response)
97 //access to persistence
98 ServletContext context = getServlet().getServletContext();
99 MessageService messageService =
100 new MessageService((SessionFactory)context
101 .getAttribute(ServletConstants.SESSION_FACTORY));
103 DynaActionForm form = (DynaActionForm) actionForm;
105 return actionMapping.findForward("failed");
108 Boolean isnew = (Boolean) form.get("new");
111 Message message = null;
112 if(isnew.booleanValue()){
113 message = new Message();
115 Integer id = new Integer(request.getParameter(ServletConstants.ID));
116 message = (Message) messageService.load(id);
120 return actionMapping.findForward("failed");
123 message.setTitle((String)form.get("title"));
124 message.setDescription((String)form.get("description"));
125 message.setCreator((String)form.get("creator"));
127 if(isnew.booleanValue()){
128 message.setWebdbCreate(new Date());
129 Integer id = messageService.save(message);
130 request.setAttribute(ServletConstants.OFFSET, "0");
132 messageService.update(message);
136 return actionMapping.findForward("success");
139 private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm,
140 HttpServletRequest request, HttpServletResponse response)
142 //access to persistence
143 ServletContext context = getServlet().getServletContext();
144 MessageService messageService =
145 new MessageService((SessionFactory)context
146 .getAttribute(ServletConstants.SESSION_FACTORY));
149 Integer id = new Integer(request.getParameter(ServletConstants.ID));
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");
157 System.out.println("komish confirm");
159 Message message = (Message) messageService.load(id);
162 return actionMapping.findForward("failed");
165 messageService.delete(message);
168 return actionMapping.findForward("success");
171 private ActionForward list(ActionMapping actionMapping, ActionForm actionForm,
172 HttpServletRequest request, HttpServletResponse response)
174 //retrieve parameters
175 String offsetString = request.getParameter(ServletConstants.OFFSET);
177 if(offsetString != null && !offsetString.equals("")){
178 offset = new Integer(offsetString).intValue();
181 //access to persistence
182 ServletContext context = getServlet().getServletContext();
183 MessageService messageService =
184 new MessageService((SessionFactory)context
185 .getAttribute(ServletConstants.SESSION_FACTORY));
188 Order order = Order.desc("webdbCreate");
189 List messages = messageService.list(offset, order);
191 //configure the data to send to view
192 int listSize = _configuration.getInt("ServletModule.Default.ListSize");
194 if(offset-listSize < 0){
195 lastOffset = new Integer(0);
197 lastOffset = new Integer(offset-listSize);
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);
207 return actionMapping.findForward("success");
210 private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm,
211 HttpServletRequest request, HttpServletResponse response)
213 //retrieve parameters
214 Integer id = new Integer(request.getParameter(ServletConstants.ID));
215 String offset = request.getParameter(ServletConstants.OFFSET);
218 //access to persistence
219 ServletContext context = getServlet().getServletContext();
220 MessageService messageService =
221 new MessageService((SessionFactory)context
222 .getAttribute(ServletConstants.SESSION_FACTORY));
225 Message message = (Message) messageService.load(id);
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);
233 return actionMapping.findForward("success");