/*
- * 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.*;
-import java.sql.*;
-import java.util.*;
-import java.net.*;
-import javax.servlet.*;
-import javax.servlet.http.*;
-import org.apache.struts.util.MessageResources;
-
-import freemarker.template.*;
-
-import mir.servlet.*;
-import mir.module.*;
-import mir.misc.*;
-import mir.entity.*;
-import mir.storage.*;
-import mir.generator.*;
-import mir.entity.*;
-import mir.entity.adapter.*;
-import mir.util.*;
-
-import mircoders.storage.*;
-import mircoders.global.*;
-import mircoders.localizer.*;
-import mircoders.module.*;
-
-/*
- * ServletModuleComment - controls navigation for Comments
- *
- *
- * @author RK
+import mir.entity.adapter.EntityAdapterEngine;
+import mir.entity.adapter.EntityAdapterModel;
+import mir.servlet.AdminServletModule;
+import mir.servlet.ServletModuleExc;
+import mir.servlet.ServletModuleFailure;
+import mir.servlet.ServletModuleUserExc;
+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.storage.DatabaseComment;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * ServletModule for managing Comments
*/
-public class ServletModuleComment extends ServletModule
-{
-
- private ModuleContent moduleContent;
-
- // Singelton / Kontruktor
- private static ServletModuleComment instance = new ServletModuleComment();
- public static ServletModule getInstance() { return instance; }
+public class ServletModuleComment extends AdminServletModule {
+ public ServletModuleComment() {
+ mainModule = new ModuleComment();
+ definition = "comment";
+ }
- private ServletModuleComment() {
- theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Comment.Logfile"));
- templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
- templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
- templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
+ public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
try {
- mainModule = new ModuleComment(DatabaseComment.getInstance());
- moduleContent = new ModuleContent(DatabaseContent.getInstance());
+ MirGlobal.accessControl().general().assertMayDeleteComments(ServletHelper.getUser(aRequest));
+
+ super.delete(aRequest, aResponse);
}
- catch (StorageObjectException e) {
- theLog.printError("servletmodule: comment could not be initialized");
+ catch (Throwable t) {
+ throw new ServletModuleFailure(t);
}
}
- public void list(HttpServletRequest req, HttpServletResponse res)
- throws ServletModuleException
- {
- // Parameter auswerten
- SimpleHash mergeData = new SimpleHash();
- String query_text = req.getParameter("query_text");
- mergeData.put("query_text",query_text);
- if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
- String query_field = req.getParameter("query_field");
- mergeData.put("query_field",query_field);
- String query_is_published = req.getParameter("query_is_published");
- mergeData.put("query_is_published",query_is_published);
-
- String offset = req.getParameter("offset");
- if (offset==null || offset.equals("")) offset="0";
- mergeData.put("offset",offset);
-
- // patching order
- String order = req.getParameter("order");
- if(order!=null) {
- mergeData.put("order", order);
- mergeData.put("order_encoded", URLEncoder.encode(order));
- if (order.equals("webdb_create")) order="webdb_create desc";
- }
+ public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
+ String idParam = aRequest.getParameter("id");
- // sql basteln
- String whereClause=""; boolean isFirst=true;
- if (query_text!=null && !query_text.equalsIgnoreCase("")) {
- whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
- if (query_is_published != null && !query_is_published.equals("")) {
- if (isFirst==false) whereClause+=" and ";
- whereClause += "is_published='"+query_is_published+"'";
- isFirst=false;
+ if (idParam == null) {
+ throw new ServletModuleExc("Invalid call: id not supplied ");
}
- theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
+ editObject(aRequest, aResponse, idParam, null);
+ }
- // fetch und ausliefern
+ public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId, String aTemplate) throws ServletModuleExc {
try {
-
- if (query_text!=null || query_is_published!=null ) {
- EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue());
- if (theList!=null && theList.size()>0) {
-
- //make articleHash for comment
- StringBuffer buf= new StringBuffer("id in (");boolean first=true;
- for(int i=0;i<theList.size();i++) {
- if (first==false) buf.append(",");
- first=false;
- buf.append(theList.elementAt(i).getValue("to_media"));
- }
- buf.append(")");
- SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
- mergeData.put("articleHash", articleHash);
-
- // get comment
- mergeData.put("contentlist",theList);
- mergeData.put("count", (new Integer(theList.getCount())).toString());
- mergeData.put("from", (new Integer(theList.getFrom())).toString());
- mergeData.put("to", (new Integer(theList.getTo())).toString());
- if (theList.hasNextBatch())
- mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
- if (theList.hasPrevBatch())
- mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+ Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
+ EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
+ Object comment;
+ URLBuilder urlBuilder = new URLBuilder();
+
+ 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().getFieldNames();
+ responseData.put("new", Boolean.TRUE);
+ Map emptyComment = new HashMap();
+ Iterator i = fields.iterator();
+ while (i.hasNext()) {
+ emptyComment.put(i.next(), null);
}
+
+ comment = emptyComment;
}
- // raus damit
- HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
+ responseData.put("comment", comment);
+
+ responseData.put("returnurl", requestParser.getParameter("returnurl"));
+ responseData.put("thisurl", urlBuilder.getQuery());
+
+ ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure(e);
}
- catch (ModuleException e) {throw new ServletModuleException(e.toString());}
- catch (IOException e) {throw new ServletModuleException(e.toString());}
- catch (Exception e) {throw new ServletModuleException(e.toString());}
}
- public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
- int nrCommentsPerPage = 20;
+ public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
+ String mediaIdParam = aRequest.getParameter("mid");
+ String commentId = aRequest.getParameter("commentid");
- Object comments;
- Map generationData;
- Generator generator;
- int totalNrComments;
- EntityAdapterModel model;
- MessageResources messages = MessageResources.getMessageResources("bundles.admin");
+ if (commentId == null || mediaIdParam==null) {
+ throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
+ }
try {
- generator = MirGlobal.localizer().generators().makeGeneratorLibrary().makeGenerator("admin/commentlist2.template");
- model = MirGlobal.localizer().dataModel().adapterModel();
-
-// commentList = mainModule.getByWhereClause(whereClause, order, offset);
-
- generationData = new HashMap();
- MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
-
- comments =
- new CachingRewindableIterator(
- new EntityIteratorAdapter( "to_media = " + anArticleId,
- "webdb_create desc",
- nrCommentsPerPage,
- model,
- "comment",
- nrCommentsPerPage,
- anOffset
- )
- );
-
- totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
-
- generationData.put( "comments", comments);
- generationData.put( "offset", new Integer(anOffset));
- generationData.put( "articleid", new Integer(anArticleId));
- generationData.put( "lang", new MessageMethodModel(aLocale, messages) );
- generationData.put( "thisurl", "module=Comment&do=listarticlecomments&offset="+anOffset+"&articleid="+anArticleId);
+ EntityComment comment = (EntityComment) mainModule.getById(commentId);
+ comment.attach(mediaIdParam);
+ }
+ catch(Throwable e) {
+ throw new ServletModuleFailure(e);
+ }
- if (anOffset>0) {
- generationData.put( "previousurl", "module=Comment&do=listarticlecomments&offset="+
- Math.max( 0, anOffset - nrCommentsPerPage )+"&articleid="+anArticleId);
- generationData.put("previous", new Integer(Math.max( 0, anOffset - nrCommentsPerPage )));
- }
+ logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
- if (anOffset + nrCommentsPerPage < totalNrComments) {
- generationData.put( "nexturl", "module=Comment&do=listarticlecomments&offset="+
- Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )+"&articleid="+anArticleId);
- generationData.put("next", new Integer(Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )));
- }
+ editObject(aRequest, aResponse, commentId, null);
+ }
- generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
+ 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");
}
- catch (Throwable t) {
- t.printStackTrace(System.out);
- throw new ServletModuleException(t.getMessage());
+ 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, null);
}
- public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
+
+ public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
{
- String articleIdString = req.getParameter("articleid");
- String offsetString = req.getParameter("offset");
- int offset = 0;
- int articleId = 0;
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
- try {
- offset = Integer.parseInt(offsetString);
+ String where = requestParser.getParameter("where");
+ String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
+ int offset = requestParser.getIntegerWithDefault("offset", 0);
+
+ returnList(aRequest, aResponse, where, order, offset);
+ }
+
+ public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
+ {
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+ SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
+
+ String searchField = requestParser.getParameter("searchfield");
+ String searchText = requestParser.getParameter("searchtext");
+ String searchIsPublished = requestParser.getParameter("searchispublished");
+ String searchStatus = requestParser.getParameter("searchstatus");
+ String searchOrder = requestParser.getParameter("searchorder");
+
+ if ("0".equals(searchIsPublished)) {
+ queryBuilder.appendAndCondition("is_published='f'");
}
- catch (Throwable t) {
+ else if ("1".equals(searchIsPublished)) {
+ queryBuilder.appendAndCondition("is_published='t'");
}
- try {
- articleId = Integer.parseInt(articleIdString);
+ if (searchText.length()>0) {
+ queryBuilder.appendAndCondition(
+ "lower("+ searchField + ") like " +
+ "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
+ }
+
+ if (searchStatus.length()>0) {
+ queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
+ }
- showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
+ if (searchOrder.length()>0) {
+ if ("datedesc".equals(searchOrder)) {
+ queryBuilder.appendDescendingOrder("webdb_create");
+ }
+ else if ("dateasc".equals(searchOrder)) {
+ queryBuilder.appendAscendingOrder("webdb_create");
+ }
+ else if ("articletitle".equals(searchOrder)) {
+ queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
+ }
+ else if ("creator".equals(searchOrder)) {
+ queryBuilder.appendDescendingOrder("creator");
+ }
}
- catch (ServletModuleException e) {
- throw e;
+
+ returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
+ }
+
+ public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
+ String articleIdString = aRequest.getParameter("articleid");
+ try {
+ int articleId = Integer.parseInt(articleIdString);
+
+ returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
}
catch (Throwable e) {
- e.printStackTrace(System.out);
- throw new ServletModuleException(e.getMessage());
+ throw new ServletModuleFailure(e);
}
}
- public void performarticlecommentsoperation(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
- String commentIdString = req.getParameter("commentid");
- String articleIdString = req.getParameter("articleid");
- String offsetString = req.getParameter("offset");
- String operation = req.getParameter("operation");
- int offset = 0;
- int articleId = 0;
+ public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
+ String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
+
+ HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
+ URLBuilder urlBuilder = new URLBuilder();
try {
- articleId = Integer.parseInt(articleIdString);
+ Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
+
+ List commentList =
+ EntityAdapterEngine.retrieveAdapterList(getModel(), definition, aWhereClause,
+ anOrderByClause, nrEntitiesPerListPage, anOffset);
+
+ responseData.put("nexturl", null);
+ responseData.put("prevurl", null);
+
+ urlBuilder.setValue("module", "Comment");
+ urlBuilder.setValue("do", "list");
+ urlBuilder.setValue("where", aWhereClause);
+ urlBuilder.setValue("order", anOrderByClause);
+
+ urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
+ urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
+ urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
+ urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
+ urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
+
+ responseData.put("searchfield", requestParser.getParameter("searchfield"));
+ responseData.put("searchtext", requestParser.getParameter("searchtext"));
+ responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
+ responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
+ responseData.put("searchorder", requestParser.getParameter("searchorder"));
+
+ urlBuilder.setValue("offset", anOffset);
+ responseData.put("offset" , Integer.toString(anOffset));
+ responseData.put("thisurl" , urlBuilder.getQuery());
+
+ if (commentList.size()>=nrEntitiesPerListPage) {
+ urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
+ responseData.put("nexturl" , urlBuilder.getQuery());
+ }
+
+ if (anOffset>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("to", Integer.toString(anOffset+commentList.size()-1));
- showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
+ ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
}
- catch (ServletModuleException e) {
- throw e;
+ 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());
+
+ 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){
+ ServletHelper.redirect(aResponse, returnUrl);
+ }
+ else {
+ editObject(aRequest, aResponse, idParam, null);
+ }
}
catch (Throwable e) {
- e.printStackTrace(System.out);
- throw new ServletModuleException(e.getMessage());
+ throw new ServletModuleFailure(e);
}
}
-}
+}
\ No newline at end of file