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.50 2003/04/21 12:42:51 idfx 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 EntityUsers user = _getUser(req);
207 String idParam = req.getParameter("id");
208 if (idParam == null) throw new ServletModuleExc("Invalid call: id missing");
210 String confirmParam = req.getParameter("confirm");
211 String cancelParam = req.getParameter("cancel");
213 logger.info("where = " + req.getParameter("where"));
215 if (confirmParam == null && cancelParam == null) {
217 SimpleHash mergeData = new SimpleHash();
218 mergeData.put("module", "Content");
219 mergeData.put("infoString", "Content: " + idParam);
220 mergeData.put("id", idParam);
221 mergeData.put("where", req.getParameter("where"));
222 mergeData.put("order", req.getParameter("order"));
223 mergeData.put("offset", req.getParameter("offset"));
224 deliver(req, res, mergeData, templateConfirmString);
227 if (confirmParam!= null && !confirmParam.equals("")) {
229 mainModule.deleteById(idParam);
231 /** @todo the following two should be implied in
233 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
234 DatabaseComment.getInstance().deleteByContentId(idParam);
235 DatabaseContentToMedia.getInstance().deleteByContentId(idParam);
238 //delete from lucene index, if any
239 String index = configuration.getString("IndexPath");
240 if (IndexReader.indexExists(index)){
241 IndexUtil.unindexID(idParam,index);
245 catch (Throwable e) {
246 throw new ServletModuleFailure(e);
251 // Datensatz anzeigen
252 _showObject(idParam, req, res);
257 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
259 String idParam = req.getParameter("id");
261 throw new ServletModuleExc("Invalid call: id not supplied ");
262 _showObject(idParam, req, res);
265 // methods for attaching media file
266 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
268 String mediaIdParam = req.getParameter("mid");
269 String articleId = req.getParameter("articleid");
271 if (articleId == null || mediaIdParam==null)
272 throw new ServletModuleExc("smod content :: attach :: articleid/mid missing");
275 EntityContent entContent = (EntityContent) mainModule.getById(articleId);
276 entContent.attach(mediaIdParam);
279 throw new ServletModuleFailure(e);
282 _showObject(articleId, req, res);
285 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
287 String articleId = req.getParameter("articleid");
288 String midParam = req.getParameter("mid");
289 if (articleId == null)
290 throw new ServletModuleExc("smod content :: dettach :: articleid missing");
291 if (midParam == null)
292 throw new ServletModuleExc("smod content :: dettach :: mid missing");
295 EntityContent entContent = (EntityContent)mainModule.getById(articleId);
296 entContent.dettach(articleId, midParam);
299 throw new ServletModuleFailure(e);
302 _showObject(articleId, req, res);
305 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
308 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
310 String returnUrl = requestParser.getParameter("returnurl");
312 String idParam = aRequest.getParameter("id");
314 throw new ServletModuleExc("Wrong call: (id) is missing");
316 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
318 String content_id = aRequest.getParameter("id");
320 withValues.put("is_produced", "0");
321 if (!withValues.containsKey("is_published"))
322 withValues.put("is_published","0");
323 if (!withValues.containsKey("is_html"))
324 withValues.put("is_html","0");
326 String id = mainModule.set(withValues);
327 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"), aRequest.getParameterValues("to_topic"));
329 String whereParam = aRequest.getParameter("where");
330 String orderParam = aRequest.getParameter("order");
332 if (returnUrl!=null){
333 redirect(aResponse, returnUrl);
336 _showObject(idParam, aRequest, aResponse);
338 catch (Throwable e) {
339 throw new ServletModuleFailure(e);
345 * HelperMethod shows the basic article editing form.
347 * if the "id" parameter is null, it means show an empty form to add a new
353 * @throws ServletModuleExc
355 public void _showObject(String id, HttpServletRequest aRequest, HttpServletResponse aResponse)
356 throws ServletModuleExc {
358 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
359 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
360 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
362 URLBuilder urlBuilder = new URLBuilder();
364 urlBuilder.setValue("module", "Content");
365 urlBuilder.setValue("do", "edit");
366 urlBuilder.setValue("id", id);
367 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
370 responseData.put("new", Boolean.FALSE);
371 article = model.makeEntityAdapter("content", mainModule.getById(id));
374 List fields = DatabaseContent.getInstance().getFields();
375 responseData.put("new", Boolean.TRUE);
376 article = new HashMap();
377 Iterator i = fields.iterator();
378 while (i.hasNext()) {
379 article.put(i.next(), null);
382 article.put("to_topics", null);
384 MirGlobal.localizer().adminInterface().initializeArticle(article);
386 responseData.put("article", article);
388 responseData.put("topics",
389 new EntityIteratorAdapter("", configuration.getString("Mir.Localizer.Admin.TopicListOrder"),
390 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
394 responseData.put("returnurl", requestParser.getParameter("returnurl"));
395 responseData.put("thisurl", urlBuilder.getQuery());
397 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editTemplate);
399 catch (Throwable e) {
400 throw new ServletModuleFailure(e);
404 public void returnArticleList(
405 HttpServletRequest aRequest,
406 HttpServletResponse aResponse,
408 String anOrderByClause,
410 String aSelectArticleUrl) throws ServletModuleExc {
412 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
413 URLBuilder urlBuilder = new URLBuilder();
414 EntityAdapterModel model;
415 int nrArticlesPerPage = 20;
419 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
420 model = MirGlobal.localizer().dataModel().adapterModel();
423 new CachingRewindableIterator(
424 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
425 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
428 responseData.put("nexturl", null);
429 responseData.put("prevurl", null);
431 count=mainModule.getSize(aWhereClause);
433 urlBuilder.setValue("module", "Content");
434 urlBuilder.setValue("do", "list");
435 urlBuilder.setValue("where", aWhereClause);
436 urlBuilder.setValue("order", anOrderByClause);
439 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
440 urlBuilder.setValue("searchvalue", requestParser.getParameter("searchvalue"));
441 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
442 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
443 urlBuilder.setValue("searcharticletype", requestParser.getParameter("searcharticletype"));
444 urlBuilder.setValue("selectarticleurl", aSelectArticleUrl);
446 responseData.put("searchfield", requestParser.getParameter("searchfield"));
447 responseData.put("searchvalue", requestParser.getParameter("searchvalue"));
448 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
449 responseData.put("searchorder", requestParser.getParameter("searchorder"));
450 responseData.put("searcharticletype", requestParser.getParameter("searcharticletype"));
451 responseData.put("selectarticleurl", aSelectArticleUrl);
453 urlBuilder.setValue("offset", anOffset);
454 responseData.put("offset" , new Integer(anOffset).toString());
455 responseData.put("thisurl" , urlBuilder.getQuery());
457 if (count>=anOffset+nrArticlesPerPage) {
458 urlBuilder.setValue("offset", (anOffset + nrArticlesPerPage));
459 responseData.put("nexturl" , urlBuilder.getQuery());
463 urlBuilder.setValue("offset", Math.max(anOffset - nrArticlesPerPage, 0));
464 responseData.put("prevurl" , urlBuilder.getQuery());
467 responseData.put("articles", articleList);
469 responseData.put("from" , Integer.toString(anOffset+1));
470 responseData.put("count", Integer.toString(count));
471 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
472 responseData.put("offset" , Integer.toString(anOffset));
473 responseData.put("order", anOrderByClause);
474 responseData.put("where" , aWhereClause);
476 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listTemplate);
478 catch (Throwable e) {
479 throw new ServletModuleFailure(e);
483 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
486 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
487 URLBuilder urlBuilder = new URLBuilder();
489 urlBuilder.setValue("module", "Content");
490 urlBuilder.setValue("do", "setparent");
491 urlBuilder.setValue("childid", requestParser.getParameter("id"));
492 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
494 returnArticleList(aRequest, aResponse, "", "", 0, urlBuilder.getQuery());
496 catch (Throwable e) {
497 throw new ServletModuleFailure(e);
501 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
504 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
505 String articleId = requestParser.getParameter("article_id");
507 if (articleId == null)
508 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
510 returnArticleList(aRequest, aResponse, "to_content = " + articleId, "", 0, null);
512 catch (Throwable e) {
513 throw new ServletModuleFailure(e);
517 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
519 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
520 String articleId = aRequest.getParameter("childid");
521 String parentId = aRequest.getParameter("id");
522 String returnUrl = aRequest.getParameter("returnurl");
525 EntityContent article = (EntityContent) mainModule.getById(articleId);
526 article.setValueForProperty("to_content", parentId);
527 article.setProduced(false);
531 logger.error("ServletModuleContent.setparent: " + e.getMessage());
532 throw new ServletModuleFailure(e);
535 redirect(aResponse, returnUrl);
538 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
540 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
541 String articleId = requestParser.getParameter("id");
542 String returnUrl = requestParser.getParameter("returnurl");
545 EntityContent article = (EntityContent) mainModule.getById(articleId);
546 article.setValueForProperty("to_content", "");
547 article.setProduced(false);
551 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
552 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
554 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
557 redirect(aResponse, returnUrl);
560 private EntityUsers _getUser(HttpServletRequest req)
562 HttpSession session=req.getSession(false);
564 return (EntityUsers)session.getAttribute("login.uid");