whoops
[mir.git] / source / mircoders / servlet / ServletModuleComment.java
index 1e1c652..de606b0 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * 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 the com.oreilly.servlet library, 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.
+ * 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 mircoders.servlet;
 
-import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import mir.config.MirPropertiesConfiguration;
 import mir.entity.adapter.EntityAdapterModel;
 import mir.entity.adapter.EntityIteratorAdapter;
-import mir.log.LoggerToWriterAdapter;
 import mir.log.LoggerWrapper;
 import mir.servlet.ServletModule;
 import mir.servlet.ServletModuleExc;
 import mir.servlet.ServletModuleFailure;
+import mir.servlet.ServletModuleUserExc;
 import mir.util.CachingRewindableIterator;
 import mir.util.HTTPRequestParser;
 import mir.util.JDBCStringRoutines;
 import mir.util.SQLQueryBuilder;
 import mir.util.URLBuilder;
+import mircoders.entity.EntityComment;
 import mircoders.global.MirGlobal;
 import mircoders.module.ModuleComment;
 import mircoders.module.ModuleContent;
 import mircoders.storage.DatabaseComment;
-import mircoders.storage.DatabaseCommentStatus;
 import mircoders.storage.DatabaseContent;
-import mircoders.storage.DatabaseLanguage;
-import freemarker.template.SimpleHash;
-import freemarker.template.TemplateModelRoot;
 
 /*
  *  ServletModuleComment - controls navigation for Comments
  *
  *
- * @author RK
+ *  @author the mir-coders
  */
 
 public class ServletModuleComment extends ServletModule
 {
+  private ModuleContent  moduleContent;
 
-  private ModuleContent     moduleContent;
-
-  // Singelton / Kontruktor
   private static ServletModuleComment instance = new ServletModuleComment();
   public static ServletModule getInstance() { return instance; }
 
   private ServletModuleComment() {
     logger = new LoggerWrapper("ServletModule.Comment");
-
-
     try {
-      configuration = MirPropertiesConfiguration.instance();
-      templateListString = configuration.getString("ServletModule.Comment.ListTemplate");
-      templateObjektString = configuration.getString("ServletModule.Comment.ObjektTemplate");
-      templateConfirmString = configuration.getString("ServletModule.Comment.ConfirmTemplate");
-
       mainModule = new ModuleComment(DatabaseComment.getInstance());
       moduleContent = new ModuleContent(DatabaseContent.getInstance());
     }
@@ -94,33 +83,107 @@ public class ServletModuleComment extends ServletModule
     }
   }
 
-  public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
+  public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
+    try {
+      MirGlobal.accessControl().general().assertMayDeleteComments(ServletHelper.getUser(aRequest));
+
+      super.delete(aRequest, aResponse);
+    }
+    catch (Throwable t) {
+      throw new ServletModuleFailure(t);
+    }
+  }
+
+  public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
   {
-    String idParam = req.getParameter("id");
+    String idParam = aRequest.getParameter("id");
 
     if (idParam == null)
       throw new ServletModuleExc("Invalid call: id not supplied ");
 
-    showComment(idParam, req, res);
+    editObject(aRequest, aResponse, idParam);
   }
 
-  public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
+  public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
     try {
-      SimpleHash extraInfo = new SimpleHash();
-      TemplateModelRoot data;
+      HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+      Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
+      EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
+      Map comment;
+      URLBuilder urlBuilder = new URLBuilder();
 
-      data = (TemplateModelRoot) mainModule.getById(anId);
+      urlBuilder.setValue("module", "Comment");
+      urlBuilder.setValue("do", "edit");
+      urlBuilder.setValue("id", anId);
+      urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
+
+      if (anId != null) {
+        responseData.put("new", Boolean.FALSE);
+        comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
+      }
+      else {
+        List fields = DatabaseComment.getInstance().getFields();
+        responseData.put("new", Boolean.TRUE);
+        comment = new HashMap();
+        Iterator i = fields.iterator();
+        while (i.hasNext()) {
+          comment.put(i.next(), null);
+        }
+      }
+      responseData.put("comment", comment);
 
-      extraInfo.put("languages", DatabaseLanguage.getInstance().getPopupData());
-      extraInfo.put("comment_status_values", DatabaseCommentStatus.getInstance().getPopupData());
+      responseData.put("returnurl", requestParser.getParameter("returnurl"));
+      responseData.put("thisurl", urlBuilder.getQuery());
 
-      deliver(aRequest, aResponse, data, extraInfo, templateObjektString);
+      ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
     }
     catch (Throwable e) {
       throw new ServletModuleFailure(e);
     }
   }
 
+  public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+  {
+    String  mediaIdParam = aRequest.getParameter("mid");
+    String  commentId = aRequest.getParameter("commentid");
+
+    if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
+
+    try {
+      EntityComment comment = (EntityComment) mainModule.getById(commentId);
+      comment.attach(mediaIdParam);
+    }
+    catch(Throwable e) {
+      throw new ServletModuleFailure(e);
+    }
+
+    logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
+
+    editObject(aRequest, aResponse, commentId);
+  }
+
+  public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+  {
+    String  commentId = aRequest.getParameter("commentid");
+    String  midParam = aRequest.getParameter("mid");
+    if (commentId == null)
+      throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
+    if (midParam == null)
+      throw new ServletModuleExc("smod comment :: dettach :: mid missing");
+
+    try {
+      EntityComment comment = (EntityComment)mainModule.getById(commentId);
+      comment.dettach(commentId, midParam);
+    }
+    catch(Throwable e) {
+      throw new ServletModuleFailure(e);
+    }
+
+    logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
+
+    editObject(aRequest, aResponse, commentId);
+  }
+
 
   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
   {
@@ -130,7 +193,7 @@ public class ServletModuleComment extends ServletModule
     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
     int offset = requestParser.getIntegerWithDefault("offset", 0);
 
-    returnCommentList(aRequest, aResponse, where, order, offset);
+    returnList(aRequest, aResponse, where, order, offset);
   }
 
   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
@@ -164,52 +227,49 @@ public class ServletModuleComment extends ServletModule
 
     if (searchOrder.length()>0) {
       if (searchOrder.equals("datedesc"))
-        queryBuilder.appendAscendingOrder("webdb_create");
-      else if (searchOrder.equals("dateasc"))
         queryBuilder.appendDescendingOrder("webdb_create");
+      else if (searchOrder.equals("dateasc"))
+        queryBuilder.appendAscendingOrder("webdb_create");
       else if (searchOrder.equals("articletitle"))
         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
       else if (searchOrder.equals("creator"))
         queryBuilder.appendDescendingOrder("creator");
     }
 
-    returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
+    returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
   }
 
-  public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
+  public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
   {
-    String articleIdString = req.getParameter("articleid");
+    String articleIdString = aRequest.getParameter("articleid");
     int articleId;
 
     try {
       articleId  = Integer.parseInt(articleIdString);
 
-      returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
+      returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
     }
     catch (Throwable e) {
       throw new ServletModuleFailure(e);
     }
   }
 
-  public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
+  public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
      String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
-    // ML: experiment in using the producer's generation system instead of the
-    //     old one...
 
     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
     URLBuilder urlBuilder = new URLBuilder();
     EntityAdapterModel model;
-    int nrCommentsPerPage = 20;
     int count;
 
     try {
-      Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
+      Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
       model = MirGlobal.localizer().dataModel().adapterModel();
 
       Object commentList =
           new CachingRewindableIterator(
-            new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
-              MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
+            new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrEntitiesPerListPage,
+              MirGlobal.localizer().dataModel().adapterModel(), "comment", nrEntitiesPerListPage, anOffset)
       );
 
       responseData.put("nexturl", null);
@@ -238,26 +298,64 @@ public class ServletModuleComment extends ServletModule
       responseData.put("offset" , new Integer(anOffset).toString());
       responseData.put("thisurl" , urlBuilder.getQuery());
 
-      if (count>=anOffset+nrCommentsPerPage) {
-        urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
+      if (count>=anOffset+nrEntitiesPerListPage) {
+        urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
         responseData.put("nexturl" , urlBuilder.getQuery());
       }
 
       if (anOffset>0) {
-        urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
+        urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
         responseData.put("prevurl" , urlBuilder.getQuery());
       }
 
       responseData.put("comments", commentList);
       responseData.put("from" , Integer.toString(anOffset+1));
       responseData.put("count", Integer.toString(count));
-      responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
+      responseData.put("to", Integer.toString(Math.min(anOffset+nrEntitiesPerListPage, count)));
 
-      ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
+      ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
     }
     catch (Throwable e) {
       throw new ServletModuleFailure(e);
     }
   }
-}
 
+  public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+  {
+    try {
+      HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+
+      String returnUrl = requestParser.getParameter("returnurl");
+
+      String idParam = aRequest.getParameter("id");
+      if (idParam == null)
+        throw new ServletModuleExc("Wrong call: (id) is missing");
+
+      Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
+
+      String content_id = aRequest.getParameter("id");
+
+      if (!withValues.containsKey("is_published"))
+        withValues.put("is_published","0");
+      if (!withValues.containsKey("is_html"))
+        withValues.put("is_html","0");
+
+      String webdbCreate = (String) withValues.get("webdb_create");
+      if (webdbCreate==null || webdbCreate.trim().length()==0)
+        withValues.remove("webdb_create");
+
+      String id = mainModule.set(withValues);
+
+      logAdminUsage(aRequest, id, "object modified");
+
+      if (returnUrl!=null){
+        redirect(aResponse, returnUrl);
+      }
+      else
+        editObject(aRequest, aResponse, idParam);
+    }
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
+    }
+  }
+}
\ No newline at end of file