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 2003/05/08 02:43:42 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 id = mainModule.add(withValues);
194 DatabaseContentToTopics.getInstance().setTopics(id, req.getParameterValues("to_topic"));
196 _showObject(id, req, res);
198 catch (Throwable e) {
199 throw new ServletModuleFailure(e);
203 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
205 if (!configuration.getString("Mir.Localizer.Admin.AllowDeleteArticle", "0").equals("1"))
206 throw new ServletModuleExc("Operation not permitted");
208 EntityUsers user = _getUser(req);
210 String idParam = req.getParameter("id");
211 if (idParam == null) throw new ServletModuleExc("Invalid call: id missing");
213 String confirmParam = req.getParameter("confirm");
214 String cancelParam = req.getParameter("cancel");
216 logger.info("where = " + req.getParameter("where"));
218 if (confirmParam == null && cancelParam == null) {
220 SimpleHash mergeData = new SimpleHash();
221 mergeData.put("module", "Content");
222 mergeData.put("infoString", "Content: " + idParam);
223 mergeData.put("id", idParam);
224 mergeData.put("where", req.getParameter("where"));
225 mergeData.put("order", req.getParameter("order"));
226 mergeData.put("offset", req.getParameter("offset"));
227 deliver(req, res, mergeData, templateConfirmString);
230 if (confirmParam!= null && !confirmParam.equals("")) {
232 mainModule.deleteById(idParam);
234 /** @todo the following two should be implied in
236 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
237 DatabaseComment.getInstance().deleteByContentId(idParam);
238 DatabaseContentToMedia.getInstance().deleteByContentId(idParam);
241 //delete from lucene index, if any
242 String index = configuration.getString("IndexPath");
243 if (IndexReader.indexExists(index)){
244 IndexUtil.unindexID(idParam,index);
248 catch (Throwable e) {
249 throw new ServletModuleFailure(e);
254 // Datensatz anzeigen
255 _showObject(idParam, req, res);
260 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
262 String idParam = req.getParameter("id");
264 throw new ServletModuleExc("Invalid call: id not supplied ");
265 _showObject(idParam, req, res);
268 // methods for attaching media file
269 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
271 String mediaIdParam = req.getParameter("mid");
272 String articleId = req.getParameter("articleid");
274 if (articleId == null || mediaIdParam==null)
275 throw new ServletModuleExc("smod content :: attach :: articleid/mid missing");
278 EntityContent entContent = (EntityContent) mainModule.getById(articleId);
279 entContent.attach(mediaIdParam);
282 throw new ServletModuleFailure(e);
285 _showObject(articleId, req, res);
288 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
290 String articleId = req.getParameter("articleid");
291 String midParam = req.getParameter("mid");
292 if (articleId == null)
293 throw new ServletModuleExc("smod content :: dettach :: articleid missing");
294 if (midParam == null)
295 throw new ServletModuleExc("smod content :: dettach :: mid missing");
298 EntityContent entContent = (EntityContent)mainModule.getById(articleId);
299 entContent.dettach(articleId, midParam);
302 throw new ServletModuleFailure(e);
305 _showObject(articleId, req, res);
308 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
311 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
313 String returnUrl = requestParser.getParameter("returnurl");
315 String idParam = aRequest.getParameter("id");
317 throw new ServletModuleExc("Wrong call: (id) is missing");
319 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
321 String content_id = aRequest.getParameter("id");
323 withValues.put("is_produced", "0");
324 if (!withValues.containsKey("is_published"))
325 withValues.put("is_published","0");
326 if (!withValues.containsKey("is_html"))
327 withValues.put("is_html","0");
329 String id = mainModule.set(withValues);
330 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"), aRequest.getParameterValues("to_topic"));
332 String whereParam = aRequest.getParameter("where");
333 String orderParam = aRequest.getParameter("order");
335 if (returnUrl!=null){
336 redirect(aResponse, returnUrl);
339 _showObject(idParam, aRequest, aResponse);
341 catch (Throwable e) {
342 throw new ServletModuleFailure(e);
348 * HelperMethod shows the basic article editing form.
350 * if the "id" parameter is null, it means show an empty form to add a new
356 * @throws ServletModuleExc
358 public void _showObject(String id, HttpServletRequest aRequest, HttpServletResponse aResponse)
359 throws ServletModuleExc {
361 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
362 Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
363 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
365 URLBuilder urlBuilder = new URLBuilder();
367 urlBuilder.setValue("module", "Content");
368 urlBuilder.setValue("do", "edit");
369 urlBuilder.setValue("id", id);
370 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
373 responseData.put("new", Boolean.FALSE);
374 article = model.makeEntityAdapter("content", mainModule.getById(id));
377 List fields = DatabaseContent.getInstance().getFields();
378 responseData.put("new", Boolean.TRUE);
379 article = new HashMap();
380 Iterator i = fields.iterator();
381 while (i.hasNext()) {
382 article.put(i.next(), null);
385 article.put("to_topics", null);
387 MirGlobal.localizer().adminInterface().initializeArticle(article);
389 responseData.put("article", article);
391 responseData.put("topics",
392 new EntityIteratorAdapter("", configuration.getString("Mir.Localizer.Admin.TopicListOrder"),
393 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
397 responseData.put("returnurl", requestParser.getParameter("returnurl"));
398 responseData.put("thisurl", urlBuilder.getQuery());
400 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editTemplate);
402 catch (Throwable e) {
403 throw new ServletModuleFailure(e);
407 public void returnArticleList(
408 HttpServletRequest aRequest,
409 HttpServletResponse aResponse,
411 String anOrderByClause,
413 String aSelectArticleUrl) throws ServletModuleExc {
415 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
416 URLBuilder urlBuilder = new URLBuilder();
417 EntityAdapterModel model;
418 int nrArticlesPerPage = 20;
422 Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
423 model = MirGlobal.localizer().dataModel().adapterModel();
426 new CachingRewindableIterator(
427 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
428 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
431 responseData.put("nexturl", null);
432 responseData.put("prevurl", null);
434 count=mainModule.getSize(aWhereClause);
436 urlBuilder.setValue("module", "Content");
437 urlBuilder.setValue("do", "list");
438 urlBuilder.setValue("where", aWhereClause);
439 urlBuilder.setValue("order", anOrderByClause);
442 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
443 urlBuilder.setValue("searchvalue", requestParser.getParameter("searchvalue"));
444 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
445 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
446 urlBuilder.setValue("searcharticletype", requestParser.getParameter("searcharticletype"));
447 urlBuilder.setValue("selectarticleurl", aSelectArticleUrl);
449 responseData.put("searchfield", requestParser.getParameter("searchfield"));
450 responseData.put("searchvalue", requestParser.getParameter("searchvalue"));
451 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
452 responseData.put("searchorder", requestParser.getParameter("searchorder"));
453 responseData.put("searcharticletype", requestParser.getParameter("searcharticletype"));
454 responseData.put("selectarticleurl", aSelectArticleUrl);
456 urlBuilder.setValue("offset", anOffset);
457 responseData.put("offset" , new Integer(anOffset).toString());
458 responseData.put("thisurl" , urlBuilder.getQuery());
460 if (count>=anOffset+nrArticlesPerPage) {
461 urlBuilder.setValue("offset", (anOffset + nrArticlesPerPage));
462 responseData.put("nexturl" , urlBuilder.getQuery());
466 urlBuilder.setValue("offset", Math.max(anOffset - nrArticlesPerPage, 0));
467 responseData.put("prevurl" , urlBuilder.getQuery());
470 responseData.put("articles", articleList);
472 responseData.put("from" , Integer.toString(anOffset+1));
473 responseData.put("count", Integer.toString(count));
474 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
475 responseData.put("offset" , Integer.toString(anOffset));
476 responseData.put("order", anOrderByClause);
477 responseData.put("where" , aWhereClause);
479 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listTemplate);
481 catch (Throwable e) {
482 throw new ServletModuleFailure(e);
486 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
489 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
490 URLBuilder urlBuilder = new URLBuilder();
492 urlBuilder.setValue("module", "Content");
493 urlBuilder.setValue("do", "setparent");
494 urlBuilder.setValue("childid", requestParser.getParameter("id"));
495 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
497 returnArticleList(aRequest, aResponse, "", "", 0, urlBuilder.getQuery());
499 catch (Throwable e) {
500 throw new ServletModuleFailure(e);
504 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
507 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
508 String articleId = requestParser.getParameter("article_id");
510 if (articleId == null)
511 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
513 returnArticleList(aRequest, aResponse, "to_content = " + articleId, "", 0, null);
515 catch (Throwable e) {
516 throw new ServletModuleFailure(e);
520 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
522 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
523 String articleId = aRequest.getParameter("childid");
524 String parentId = aRequest.getParameter("id");
525 String returnUrl = aRequest.getParameter("returnurl");
528 EntityContent article = (EntityContent) mainModule.getById(articleId);
529 article.setValueForProperty("to_content", parentId);
530 article.setProduced(false);
534 logger.error("ServletModuleContent.setparent: " + e.getMessage());
535 throw new ServletModuleFailure(e);
538 redirect(aResponse, returnUrl);
541 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
543 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
544 String articleId = requestParser.getParameter("id");
545 String returnUrl = requestParser.getParameter("returnurl");
548 EntityContent article = (EntityContent) mainModule.getById(articleId);
549 article.setValueForProperty("to_content", "");
550 article.setProduced(false);
554 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
555 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
557 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
560 redirect(aResponse, returnUrl);
563 private EntityUsers _getUser(HttpServletRequest req)
565 HttpSession session=req.getSession(false);
567 return (EntityUsers)session.getAttribute("login.uid");