working on struts/hibernate for content-admin
[mir.git] / source / mir / core / ui / action / admin / ContentAction.java
diff --git a/source/mir/core/ui/action/admin/ContentAction.java b/source/mir/core/ui/action/admin/ContentAction.java
new file mode 100755 (executable)
index 0000000..2f8a0ca
--- /dev/null
@@ -0,0 +1,319 @@
+/*
+ * ContentAction.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.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.core.model.ArticleType;
+import mir.core.model.Content;
+import mir.core.model.IContent;
+import mir.core.model.Language;
+import mir.core.model.MirUser;
+import mir.core.service.storage.ArticleTypeService;
+import mir.core.service.storage.ContentService;
+import mir.core.service.storage.LanguageService;
+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.Expression;
+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: ContentAction.java,v 1.1 2003/12/20 20:27:09 idfx Exp $
+ */
+public class ContentAction extends DispatchAction {
+       private MirPropertiesConfiguration _configuration;
+
+       public ContentAction(){
+               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();
+               ContentService contentService = 
+                       new ContentService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+                                                       
+               request.setAttribute(ServletConstants.NEW, new Boolean(true));
+               request.setAttribute(ServletConstants.ID, "");
+               request.setAttribute(ServletConstants.OFFSET, "");
+               
+               //setting standard-values
+               Content content = new Content();
+               request.setAttribute("article", content);
+               
+               //              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();
+    HttpSession httpSession = request.getSession();
+               ContentService contentService = 
+                       new ContentService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+    ArticleTypeService articleTypeService = 
+      new ArticleTypeService((SessionFactory)context
+        .getAttribute(ServletConstants.SESSION_FACTORY));
+    LanguageService languageService = 
+      new LanguageService((SessionFactory)context
+        .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+    //retrieve the form
+               DynaActionForm form = (DynaActionForm) actionForm;
+               if(form == null){
+                       return actionMapping.findForward("failed");     
+               }
+    
+               //check if new
+               Boolean isnew = (Boolean) form.get("new");
+               
+               //retrieve id
+               IContent content = null;
+               if(isnew.booleanValue()){
+                       content = new Content();
+      //set change and create date
+      content.setWebdbCreate(new Date());
+      content.setWebdbLastchange(new Date());
+               } else {
+                       Integer id = new Integer(request.getParameter(ServletConstants.ID));
+                       content = (IContent) contentService.load(id);
+      //set last edit date
+      content.setWebdbLastchange(new Date());
+               }
+               
+               if(content == null){
+                       return actionMapping.findForward("failed");     
+               }
+
+               String webDbCreate = (String) form.get("date");
+    if(webDbCreate == null || webDbCreate.length() == 0){
+      Date date = new Date();
+      SimpleDateFormat dateFormat = new SimpleDateFormat();
+      dateFormat.applyPattern("yyyyMMdd");
+      webDbCreate = dateFormat.format(date);
+    }
+    content.setDate(webDbCreate);
+    
+    //set articletype
+    Integer articleTypeId = (Integer) form.get("to_article_type");
+    content.setArticleType(
+      (ArticleType) articleTypeService.load(articleTypeId));
+    
+    //set language
+    Integer languageTypeId = (Integer) form.get("to_language");
+    content.setLanguage(
+      (Language) languageService.load(languageTypeId)); 
+    
+    //set form data
+    content.setPublisher(
+      (MirUser) httpSession.getAttribute(ServletConstants.USER));
+    content.setTitle((String) form.get("title"));
+    content.setSubtitle((String) form.get("subtitle"));
+               content.setComment((String) form.get("comment"));
+               content.setContentData((String) form.get("content_data"));
+               content.setCreator((String) form.get("creator"));
+               content.setCreatorAddress((String)form.get("creator_address"));
+               content.setCreatorEmail((String)form.get("creator_email"));
+               content.setCreatorPhone((String)form.get("creator_phone"));
+               content.setCreatorMainUrl((String)form.get("creator_main_url"));
+               content.setDescription((String)form.get("description"));
+               content.setEdittitle((String)form.get("edittitle"));
+               if(form.get("is_html") != null){
+                       content.setHtml(true);
+               } else {
+                       content.setHtml(false);
+               }
+               if(form.get("is_published") != null){
+                       content.setPublished(true);
+               } else {
+                       content.setPublished(false);
+               }
+                               
+//             Integer parentId = (Integer)form.get("parentContent");
+//             if(parentId != null && parentId.intValue() > -1){
+//                     Topic parentTopic = (Topic) topicService.load(parentId);
+//                     topic.setParentTopic(parentTopic);
+//             } 
+    
+    //save the article
+               if(isnew.booleanValue()){
+                       Integer id = contentService.save(content);
+                       request.setAttribute(ServletConstants.OFFSET, "0");
+               } else {
+                       contentService.update(content);
+               }
+               
+               //              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();
+               ContentService contentService = 
+                       new ContentService((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
+               Content content = (Content) contentService.load(id);
+               
+               if(content == null){
+                       return actionMapping.findForward("failed");     
+               }
+                                       
+               contentService.delete(content);
+               
+               //              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();
+               }
+    String articleTypeString = request.getParameter("articletype");
+    Integer articleTypeId = new Integer(-1);
+    if(articleTypeString != null && !articleTypeString.equals("")){
+      articleTypeId = new Integer(articleTypeString);
+    }  
+       
+               //access to persistence
+               ServletContext context = getServlet().getServletContext();
+               ContentService contentService = 
+                       new ContentService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+
+               
+               //retrieve entities
+               Order order = Order.desc("id");
+    List contents;
+    if(articleTypeId.intValue() >= 0){
+      ArticleTypeService articleTypeService = 
+        new ArticleTypeService((SessionFactory)context
+          .getAttribute(ServletConstants.SESSION_FACTORY));
+      Expression expression = 
+        Expression.eq("articleType", 
+          articleTypeService.load(articleTypeId));
+      contents = contentService.list(offset, expression, order);
+    } else {
+      contents = contentService.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("articles", contents);
+               
+               //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();
+               ContentService contentService = 
+                       new ContentService((SessionFactory)context
+                               .getAttribute(ServletConstants.SESSION_FACTORY));
+               
+               //retrieve entities
+    IContent content = (IContent) contentService.load(id);
+               
+               //configure the data to send to view
+               request.setAttribute(ServletConstants.OFFSET, offset);
+               request.setAttribute(ServletConstants.NEW,"0");
+               request.setAttribute("article", content);
+                                               
+               //show the view
+               return actionMapping.findForward("success");    
+       }
+}