2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.servlet;
33 import java.util.GregorianCalendar;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Locale;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import javax.servlet.http.HttpSession;
44 import mir.entity.adapter.EntityAdapterModel;
45 import mir.entity.adapter.EntityIteratorAdapter;
46 import mir.log.LoggerWrapper;
47 import mir.misc.StringUtil;
48 import mir.servlet.ServletModule;
49 import mir.servlet.ServletModuleExc;
50 import mir.servlet.ServletModuleFailure;
51 import mir.util.CachingRewindableIterator;
52 import mir.util.HTTPRequestParser;
53 import mir.util.JDBCStringRoutines;
54 import mir.util.SQLQueryBuilder;
55 import mir.util.URLBuilder;
56 import mircoders.entity.EntityContent;
57 import mircoders.entity.EntityUsers;
58 import mircoders.global.MirGlobal;
59 import mircoders.module.ModuleContent;
60 import mircoders.search.IndexUtil;
61 import mircoders.storage.DatabaseComment;
62 import mircoders.storage.DatabaseContent;
63 import mircoders.storage.DatabaseContentToMedia;
64 import mircoders.storage.DatabaseContentToTopics;
66 import org.apache.lucene.index.IndexReader;
68 import freemarker.template.SimpleHash;
71 * ServletModuleContent -
72 * deliver html for the article admin form.
74 * @version $Id: ServletModuleContent.java,v 1.52.2.1 2003/05/24 15:56:38 zapata Exp $
75 * @author rk, mir-coders
79 public class ServletModuleContent extends ServletModule
81 private String editTemplate = configuration.getString("ServletModule.Content.ObjektTemplate");;
82 private String listTemplate = configuration.getString("ServletModule.Content.ListTemplate");
84 private static ServletModuleContent instance = new ServletModuleContent();
85 public static ServletModule getInstance() { return instance; }
87 private ServletModuleContent() {
89 logger = new LoggerWrapper("ServletModule.Content");
92 templateListString = configuration.getString("ServletModule.Content.ListTemplate");
93 templateObjektString = configuration.getString("ServletModule.Content.ObjektTemplate");
94 templateConfirmString = configuration.getString("ServletModule.Content.ConfirmTemplate");
96 mainModule = new ModuleContent(DatabaseContent.getInstance());
99 logger.fatal("ServletModuleContent could not be initialized: " + e.toString());
103 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
105 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
107 String where = requestParser.getParameter("where");
108 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
109 int offset = requestParser.getIntegerWithDefault("offset", 0);
110 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
112 returnArticleList(aRequest, aResponse, where, order, offset, selectArticleUrl);
115 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {
117 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
118 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
119 String searchField = requestParser.getParameterWithDefault("searchfield", "");
120 String searchValue = requestParser.getParameterWithDefault("searchvalue", "").trim();
121 String searchOrder = requestParser.getParameterWithDefault("searchorder", "");
122 String searchispublished = requestParser.getParameterWithDefault("searchispublished", "");
123 String searchArticleType = requestParser.getParameterWithDefault("searcharticletype", "");
124 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
126 if (searchValue.length()>0) {
127 if (searchField.equals("id"))
128 queryBuilder.appendAndCondition(
129 "id='"+JDBCStringRoutines.escapeStringLiteral(searchValue)+"'");
130 else if (searchField.equals("contents"))
131 queryBuilder.appendAndCondition(
132 "(lower(content_data) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')"+
133 " or (lower(description) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')");
135 queryBuilder.appendAndCondition(
136 "lower("+ searchField + ") like " +
137 "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%'");
140 if (searchispublished.length()>0) {
141 if (searchispublished.equals("0"))
142 queryBuilder.appendAndCondition("is_published='f'");
144 queryBuilder.appendAndCondition("is_published='t'");
147 if (searchArticleType.length()>0) {
148 queryBuilder.appendAndCondition("to_article_type="+Integer.parseInt(searchArticleType));
151 if (searchOrder.length()>0) {
152 if (searchOrder.equals("datedesc"))
153 queryBuilder.appendAscendingOrder("webdb_create");
154 else if (searchOrder.equals("dateasc"))
155 queryBuilder.appendDescendingOrder("webdb_create");
156 else if (searchOrder.equals("title"))
157 queryBuilder.appendDescendingOrder("title");
158 else if (searchOrder.equals("creator"))
159 queryBuilder.appendDescendingOrder("creator");
162 returnArticleList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0, selectArticleUrl);
164 catch (Throwable e) {
165 throw new ServletModuleFailure(e);
169 public void add(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
170 _showObject(null, req, res);
174 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
176 //theLog.printDebugInfo(":: content :: trying to insert");
178 EntityUsers user = _getUser(req);
179 Map withValues = getIntersectingValues(req, DatabaseContent.getInstance());
181 String now = StringUtil.date2webdbDate(new GregorianCalendar());
182 withValues.put("date", now);
183 withValues.put("publish_path", StringUtil.webdbDate2path(now));
184 withValues.put("to_publisher", user.getId());
185 withValues.put("is_produced", "0");
186 if (!withValues.containsKey("is_published"))
187 withValues.put("is_published","0");
188 if (!withValues.containsKey("is_html"))
189 withValues.put("is_html","0");
191 String webdbCreate = (String) withValues.get("webdb_create");
192 if (webdbCreate==null || webdbCreate.trim().length()==0)
193 withValues.remove("webdb_create");
195 String id = mainModule.add(withValues);
198 DatabaseContentToTopics.getInstance().setTopics(id, req.getParameterValues("to_topic"));
200 _showObject(id, req, res);
202 catch (Throwable e) {
203 throw new ServletModuleFailure(e);
207 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
209 if (!configuration.getString("Mir.Localizer.Admin.AllowDeleteArticle", "0").equals("1"))
210 throw new ServletModuleExc("Operation not permitted");
212 EntityUsers user = _getUser(req);
214 String idParam = req.getParameter("id");
215 if (idParam == null) throw new ServletModuleExc("Invalid call: id missing");
217 String confirmParam = req.getParameter("confirm");
218 String cancelParam = req.getParameter("cancel");
220 logger.info("where = " + req.getParameter("where"));
222 if (confirmParam == null && cancelParam == null) {
224 SimpleHash mergeData = new SimpleHash();
225 mergeData.put("module", "Content");
226 mergeData.put("infoString", "Content: " + idParam);
227 mergeData.put("id", idParam);
228 mergeData.put("where", req.getParameter("where"));
229 mergeData.put("order", req.getParameter("order"));
230 mergeData.put("offset", req.getParameter("offset"));
231 deliver(req, res, mergeData, templateConfirmString);
234 if (confirmParam!= null && !confirmParam.equals("")) {
236 mainModule.deleteById(idParam);
238 /** @todo the following two should be implied in
240 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
241 DatabaseComment.getInstance().deleteByContentId(idParam);
242 DatabaseContentToMedia.getInstance().deleteByContentId(idParam);
245 //delete from lucene index, if any
246 String index = configuration.getString("IndexPath");
247 if (IndexReader.indexExists(index)){
248 IndexUtil.unindexID(idParam,index);
252 catch (Throwable e) {
253 throw new ServletModuleFailure(e);
258 // Datensatz anzeigen
259 _showObject(idParam, req, res);
264 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
266 String idParam = req.getParameter("id");
268 throw new ServletModuleExc("Invalid call: id not supplied ");
269 _showObject(idParam, req, res);
272 // methods for attaching media file
273 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
275 String mediaIdParam = req.getParameter("mid");
276 String articleId = req.getParameter("articleid");
278 if (articleId == null || mediaIdParam==null)
279 throw new ServletModuleExc("smod content :: attach :: articleid/mid missing");
282 EntityContent entContent = (EntityContent) mainModule.getById(articleId);
283 entContent.attach(mediaIdParam);
286 throw new ServletModuleFailure(e);
289 _showObject(articleId, req, res);
292 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
294 String articleId = req.getParameter("articleid");
295 String midParam = req.getParameter("mid");
296 if (articleId == null)
297 throw new ServletModuleExc("smod content :: dettach :: articleid missing");
298 if (midParam == null)
299 throw new ServletModuleExc("smod content :: dettach :: mid missing");
302 EntityContent entContent = (EntityContent)mainModule.getById(articleId);
303 entContent.dettach(articleId, midParam);
306 throw new ServletModuleFailure(e);
309 _showObject(articleId, req, res);
312 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
315 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
317 String returnUrl = requestParser.getParameter("returnurl");
319 String idParam = aRequest.getParameter("id");
321 throw new ServletModuleExc("Wrong call: (id) is missing");
323 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
325 String content_id = aRequest.getParameter("id");
327 withValues.put("is_produced", "0");
328 if (!withValues.containsKey("is_published"))
329 withValues.put("is_published","0");
330 if (!withValues.containsKey("is_html"))
331 withValues.put("is_html","0");
333 String webdbCreate = (String) withValues.get("webdb_create");
334 if (webdbCreate==null || webdbCreate.trim().length()==0)
335 withValues.remove("webdb_create");
337 String id = mainModule.set(withValues);
338 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"), aRequest.getParameterValues("to_topic"));
340 String whereParam = aRequest.getParameter("where");
341 String orderParam = aRequest.getParameter("order");
343 if (returnUrl!=null){
344 redirect(aResponse, returnUrl);
347 _showObject(idParam, aRequest, aResponse);
349 catch (Throwable e) {
350 throw new ServletModuleFailure(e);
356 * HelperMethod shows the basic article editing form.
358 * if the "id" parameter is null, it means show an empty form to add a new
364 * @throws ServletModuleExc
366 public void _showObject(String id, HttpServletRequest aRequest, HttpServletResponse aResponse)
367 throws ServletModuleExc {
369 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
370 Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
371 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
373 URLBuilder urlBuilder = new URLBuilder();
375 urlBuilder.setValue("module", "Content");
376 urlBuilder.setValue("do", "edit");
377 urlBuilder.setValue("id", id);
378 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
381 responseData.put("new", Boolean.FALSE);
382 article = model.makeEntityAdapter("content", mainModule.getById(id));
385 List fields = DatabaseContent.getInstance().getFields();
386 responseData.put("new", Boolean.TRUE);
387 article = new HashMap();
388 Iterator i = fields.iterator();
389 while (i.hasNext()) {
390 article.put(i.next(), null);
393 article.put("to_topics", null);
395 MirGlobal.localizer().adminInterface().initializeArticle(article);
397 responseData.put("article", article);
399 responseData.put("topics",
400 new EntityIteratorAdapter("", configuration.getString("Mir.Localizer.Admin.TopicListOrder"),
401 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
405 responseData.put("returnurl", requestParser.getParameter("returnurl"));
406 responseData.put("thisurl", urlBuilder.getQuery());
408 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editTemplate);
410 catch (Throwable e) {
411 throw new ServletModuleFailure(e);
415 public void returnArticleList(
416 HttpServletRequest aRequest,
417 HttpServletResponse aResponse,
419 String anOrderByClause,
421 String aSelectArticleUrl) throws ServletModuleExc {
423 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
424 URLBuilder urlBuilder = new URLBuilder();
425 EntityAdapterModel model;
426 int nrArticlesPerPage = 20;
430 Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
431 model = MirGlobal.localizer().dataModel().adapterModel();
434 new CachingRewindableIterator(
435 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
436 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
439 responseData.put("nexturl", null);
440 responseData.put("prevurl", null);
442 count=mainModule.getSize(aWhereClause);
444 urlBuilder.setValue("module", "Content");
445 urlBuilder.setValue("do", "list");
446 urlBuilder.setValue("where", aWhereClause);
447 urlBuilder.setValue("order", anOrderByClause);
450 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
451 urlBuilder.setValue("searchvalue", requestParser.getParameter("searchvalue"));
452 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
453 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
454 urlBuilder.setValue("searcharticletype", requestParser.getParameter("searcharticletype"));
455 urlBuilder.setValue("selectarticleurl", aSelectArticleUrl);
457 responseData.put("searchfield", requestParser.getParameter("searchfield"));
458 responseData.put("searchvalue", requestParser.getParameter("searchvalue"));
459 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
460 responseData.put("searchorder", requestParser.getParameter("searchorder"));
461 responseData.put("searcharticletype", requestParser.getParameter("searcharticletype"));
462 responseData.put("selectarticleurl", aSelectArticleUrl);
464 urlBuilder.setValue("offset", anOffset);
465 responseData.put("offset" , new Integer(anOffset).toString());
466 responseData.put("thisurl" , urlBuilder.getQuery());
468 if (count>=anOffset+nrArticlesPerPage) {
469 urlBuilder.setValue("offset", (anOffset + nrArticlesPerPage));
470 responseData.put("nexturl" , urlBuilder.getQuery());
474 urlBuilder.setValue("offset", Math.max(anOffset - nrArticlesPerPage, 0));
475 responseData.put("prevurl" , urlBuilder.getQuery());
478 responseData.put("articles", articleList);
480 responseData.put("from" , Integer.toString(anOffset+1));
481 responseData.put("count", Integer.toString(count));
482 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
483 responseData.put("offset" , Integer.toString(anOffset));
484 responseData.put("order", anOrderByClause);
485 responseData.put("where" , aWhereClause);
487 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listTemplate);
489 catch (Throwable e) {
490 throw new ServletModuleFailure(e);
494 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
497 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
498 URLBuilder urlBuilder = new URLBuilder();
500 urlBuilder.setValue("module", "Content");
501 urlBuilder.setValue("do", "setparent");
502 urlBuilder.setValue("childid", requestParser.getParameter("id"));
503 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
505 returnArticleList(aRequest, aResponse, "", "", 0, urlBuilder.getQuery());
507 catch (Throwable e) {
508 throw new ServletModuleFailure(e);
512 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
515 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
516 String articleId = requestParser.getParameter("article_id");
518 if (articleId == null)
519 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
521 returnArticleList(aRequest, aResponse, "to_content = " + articleId, "", 0, null);
523 catch (Throwable e) {
524 throw new ServletModuleFailure(e);
528 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
530 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
531 String articleId = aRequest.getParameter("childid");
532 String parentId = aRequest.getParameter("id");
533 String returnUrl = aRequest.getParameter("returnurl");
536 EntityContent article = (EntityContent) mainModule.getById(articleId);
537 article.setValueForProperty("to_content", parentId);
538 article.setProduced(false);
542 logger.error("ServletModuleContent.setparent: " + e.getMessage());
543 throw new ServletModuleFailure(e);
546 redirect(aResponse, returnUrl);
549 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
551 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
552 String articleId = requestParser.getParameter("id");
553 String returnUrl = requestParser.getParameter("returnurl");
556 EntityContent article = (EntityContent) mainModule.getById(articleId);
557 article.setValueForProperty("to_content", "");
558 article.setProduced(false);
562 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
563 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
565 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
568 redirect(aResponse, returnUrl);
571 private EntityUsers _getUser(HttpServletRequest req)
573 HttpSession session=req.getSession(false);
575 return (EntityUsers)session.getAttribute("login.uid");