Topic administration
[mir.git] / source / mir / core / ui / action / admin / TopicAction.java
diff --git a/source/mir/core/ui/action/admin/TopicAction.java b/source/mir/core/ui/action/admin/TopicAction.java
new file mode 100755 (executable)
index 0000000..df93a31
--- /dev/null
@@ -0,0 +1,255 @@
+/*
+ * TopicAction.java created on 05.09.2003
+ * 
+ * Copyright (C) 2001, 2002, 2003 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package mir.core.ui.action.admin;
+
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.core.model.Topic;
+import mir.core.service.storage.TopicService;
+import mir.core.ui.action.DispatchAction;
+import mir.core.ui.servlet.ServletConstants;
+import multex.Failure;
+import net.sf.hibernate.SessionFactory;
+import net.sf.hibernate.expression.Order;
+
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.action.DynaActionForm;
+
+/**
+ * TopicAction
+ * @author idefix
+ * @version $Id: TopicAction.java,v 1.1 2003/09/30 19:29:32 idfx Exp $
+ */
+public class TopicAction extends DispatchAction {
+       private MirPropertiesConfiguration _configuration;
+
+       public TopicAction(){
+               super();
+               try {
+                       _configuration = MirPropertiesConfiguration.instance();
+               } catch (PropertiesConfigExc e) {
+                       throw new Failure("could not load config", e);
+               }               
+       }
+
+       private ActionForward add(ActionMapping actionMapping, ActionForm actionForm, 
+               HttpServletRequest request, HttpServletResponse response)
+               throws Exception {
+               //access to persistence
+               ServletContext context = getServlet().getServletContext();
+               TopicService topicService = 
+                       new TopicService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+               List topics = topicService.list();
+               Topic defaultParent = new Topic();
+               defaultParent.setId(new Integer(-1));
+               defaultParent.setTitle("----");
+               topics.add(0, defaultParent);
+                                       
+               request.setAttribute(ServletConstants.NEW, new Boolean(true));
+               request.setAttribute(ServletConstants.ID, "");
+               request.setAttribute(ServletConstants.OFFSET, "");
+               request.setAttribute("topics", topics);
+               
+               //setting standard-values
+               Topic topic = new Topic();
+               request.setAttribute("topic", topic);
+               
+               //              show the view
+               return actionMapping.findForward("success");
+       }
+       
+       private ActionForward save(ActionMapping actionMapping, ActionForm actionForm, 
+               HttpServletRequest request, HttpServletResponse response)
+               throws Exception {
+               //access to persistence
+               ServletContext context = getServlet().getServletContext();
+               TopicService topicService = 
+                       new TopicService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+               DynaActionForm form = (DynaActionForm) actionForm;
+               if(form == null){
+                       return actionMapping.findForward("failed");     
+               }
+               //check if new
+               Boolean isnew = (Boolean) form.get("new");
+               
+               //retrieve id
+               Topic topic = null;
+               if(isnew.booleanValue()){
+                       topic = new Topic();
+               } else {
+                       Integer id = new Integer(request.getParameter(ServletConstants.ID));
+                       topic = (Topic) topicService.load(id);
+               }
+               
+               if(topic == null){
+                       return actionMapping.findForward("failed");     
+               }
+               
+               topic.setTitle((String)form.get("title"));
+               topic.setDescription((String)form.get("title"));
+               topic.setFilename((String)form.get("filename"));
+               topic.setMainUrl((String)form.get("mainUrl"));
+               topic.setArchivUrl((String)form.get("archivUrl"));
+               topic.setParentTopic(null);
+               
+               Integer parentId = (Integer)form.get("parentTopic");
+               if(parentId != null && parentId.intValue() > -1){
+                       Topic parentTopic = (Topic) topicService.load(parentId);
+                       topic.setParentTopic(parentTopic);
+               } 
+
+               if(isnew.booleanValue()){
+                       System.out.println(topic);
+                       Integer id = topicService.save(topic);
+                       request.setAttribute(ServletConstants.OFFSET, "0");
+               } else {
+                       topicService.update(topic);
+               }
+               
+               //              show the view
+               return actionMapping.findForward("success");
+       }
+               
+       private ActionForward delete(ActionMapping actionMapping, ActionForm actionForm, 
+               HttpServletRequest request, HttpServletResponse response)
+               throws Exception {
+               //access to persistence
+               ServletContext context = getServlet().getServletContext();
+               TopicService topicService = 
+                       new TopicService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+               //retrieve id
+               Integer id = new Integer(request.getParameter(ServletConstants.ID));
+               
+               //confirm the request
+               System.out.println(request.getAttribute(ServletConstants.DELETE));
+               if(request.getAttribute(ServletConstants.DELETE) == null){
+                       return actionMapping.findForward("confirm");
+               }
+               //load object
+               Topic topic = (Topic) topicService.load(id);
+               
+               if(topic == null){
+                       return actionMapping.findForward("failed");     
+               }
+                                       
+               topicService.delete(topic);
+               
+               //              show the view
+               return actionMapping.findForward("success");
+       }
+
+       private ActionForward list(ActionMapping actionMapping, ActionForm actionForm, 
+               HttpServletRequest request, HttpServletResponse response)
+               throws Exception {
+               //retrieve parameters
+               String offsetString = request.getParameter(ServletConstants.OFFSET);
+               int offset = 0;
+               if(offsetString != null && !offsetString.equals("")){
+                       offset = new Integer(offsetString).intValue();
+               }
+               
+               //access to persistence
+               ServletContext context = getServlet().getServletContext();
+               TopicService topicService = 
+                       new TopicService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+               //retrieve entities
+               Order order = Order.desc("id");
+               List topics = topicService.list(offset, order);
+               
+               //configure the data to send to view
+               int listSize = _configuration.getInt("ServletModule.Default.ListSize");
+               Integer lastOffset;
+               if(offset-listSize < 0){
+                       lastOffset = new Integer(0);
+               } else {
+                       lastOffset = new Integer(offset-listSize);
+               }
+               request.setAttribute(ServletConstants.LAST_OFFSET, lastOffset);
+               request.setAttribute(ServletConstants.NEXT_OFFSET, 
+                       new Integer(offset + listSize));
+               request.setAttribute(ServletConstants.OFFSET, 
+                       new Integer(offset));
+               request.setAttribute("topics", topics);
+               
+               //show the view
+               return actionMapping.findForward("success");    
+       }
+       
+       private ActionForward edit(ActionMapping actionMapping, ActionForm actionForm, 
+               HttpServletRequest request, HttpServletResponse response)
+               throws Exception {
+               //retrieve parameters
+               Integer id = new Integer(request.getParameter(ServletConstants.ID));
+               String offset = request.getParameter(ServletConstants.OFFSET);
+
+               
+               //access to persistence
+               ServletContext context = getServlet().getServletContext();
+               TopicService topicService = 
+                       new TopicService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+               //retrieve entities
+               Topic topic = (Topic) topicService.load(id);
+               
+               List topics = topicService.list();
+               Topic defaultParent = new Topic();
+               defaultParent.setId(new Integer(-1));
+               defaultParent.setTitle("----");
+               topics.add(0, defaultParent);
+               
+               //configure the data to send to view
+               request.setAttribute(ServletConstants.OFFSET, offset);
+               request.setAttribute(ServletConstants.NEW,"0");
+               request.setAttribute("topic", topic);
+               request.setAttribute("topics", topics);
+               
+               //show the view
+               return actionMapping.findForward("success");    
+       }
+}