2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.servlet;
\r
34 import java.io.PrintWriter;
\r
35 import java.io.IOException;
\r
38 import javax.servlet.http.HttpServletRequest;
\r
39 import javax.servlet.http.HttpServletResponse;
\r
40 import javax.servlet.http.HttpSession;
\r
42 import mir.entity.EntityList;
\r
43 import mir.entity.adapter.EntityAdapter;
\r
44 import mir.entity.adapter.EntityAdapterModel;
\r
45 import mir.entity.adapter.EntityIteratorAdapter;
\r
46 import mir.log.LoggerWrapper;
\r
47 import mir.misc.StringUtil;
\r
48 import mir.module.ModuleException;
\r
49 import mir.servlet.ServletModule;
\r
50 import mir.servlet.ServletModuleException;
\r
51 import mir.storage.StorageObjectFailure;
\r
52 import mir.util.CachingRewindableIterator;
\r
53 import mir.util.HTMLRoutines;
\r
54 import mir.util.JDBCStringRoutines;
\r
55 import mir.util.HTTPRequestParser;
\r
56 import mir.util.URLBuilder;
\r
57 import mir.util.SQLQueryBuilder;
\r
60 import mircoders.entity.EntityContent;
\r
61 import mircoders.entity.EntityUsers;
\r
62 import mircoders.global.MirGlobal;
\r
63 import mircoders.module.ModuleContent;
\r
64 import mircoders.module.ModuleImages;
\r
65 import mircoders.module.ModuleSchwerpunkt;
\r
66 import mircoders.module.ModuleTopics;
\r
67 import mircoders.search.IndexUtil;
\r
68 import mircoders.storage.DatabaseArticleType;
\r
69 import mircoders.storage.DatabaseComment;
\r
70 import mircoders.storage.DatabaseContent;
\r
71 import mircoders.storage.DatabaseContentToTopics;
\r
72 import mircoders.storage.DatabaseFeature;
\r
73 import mircoders.storage.DatabaseImages;
\r
74 import mircoders.storage.DatabaseLanguage;
\r
75 import mircoders.storage.DatabaseTopics;
\r
77 import org.apache.lucene.index.IndexReader;
\r
79 import freemarker.template.SimpleHash;
\r
80 import freemarker.template.SimpleScalar;
\r
81 import freemarker.template.TemplateModelRoot;
\r
84 * ServletModuleContent -
\r
85 * deliver html for the article admin form.
\r
87 * @version $Id: ServletModuleContent.java,v 1.33 2003/02/20 16:05:34 zapata Exp $
\r
88 * @author rk, mir-coders
\r
92 public class ServletModuleContent extends ServletModule
\r
94 private String editTemplate = configuration.getString("ServletModule.Content.ObjektTemplate");;
\r
95 private String listTemplate = configuration.getString("ServletModule.Content.ListTemplate");
\r
97 private static ServletModuleContent instance = new ServletModuleContent();
\r
98 public static ServletModule getInstance() { return instance; }
\r
100 private ServletModuleContent() {
\r
102 logger = new LoggerWrapper("ServletModule.Content");
\r
105 templateListString = configuration.getString("ServletModule.Content.ListTemplate");
\r
106 templateObjektString = configuration.getString("ServletModule.Content.ObjektTemplate");
\r
107 templateConfirmString = configuration.getString("ServletModule.Content.ConfirmTemplate");
\r
109 mainModule = new ModuleContent(DatabaseContent.getInstance());
\r
111 catch (StorageObjectFailure e) {
\r
112 logger.error("servletmodulecontent konnte nicht initialisiert werden");
\r
116 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
118 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
120 String where = requestParser.getParameter("where");
\r
121 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
\r
122 int offset = requestParser.getIntegerWithDefault("offset", 0);
\r
123 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
\r
125 returnArticleList(aRequest, aResponse, where, order, offset, selectArticleUrl);
\r
128 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
\r
130 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
131 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
\r
132 String searchField = requestParser.getParameterWithDefault("searchfield", "");
\r
133 String searchValue = requestParser.getParameterWithDefault("searchvalue", "").trim();
\r
134 String searchOrder = requestParser.getParameterWithDefault("searchorder", "");
\r
135 String searchispublished = requestParser.getParameterWithDefault("searchispublished", "");
\r
136 String searchArticleType = requestParser.getParameterWithDefault("searcharticletype", "");
\r
137 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
\r
139 if (searchValue.length()>0) {
\r
140 if (searchField.equals("contents"))
\r
141 queryBuilder.appendAndCondition(
\r
142 "(lower(content_data) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')"+
\r
143 " or (lower(description) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')");
\r
145 queryBuilder.appendAndCondition(
\r
146 "lower("+ searchField + ") like " +
\r
147 "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%'");
\r
150 if (searchispublished.length()>0) {
\r
151 if (searchispublished.equals("0"))
\r
152 queryBuilder.appendAndCondition("is_published='f'");
\r
154 queryBuilder.appendAndCondition("is_published='t'");
\r
157 if (searchArticleType.length()>0) {
\r
158 queryBuilder.appendAndCondition("to_article_type="+searchArticleType);
\r
161 if (searchOrder.length()>0) {
\r
162 if (searchOrder.equals("datedesc"))
\r
163 queryBuilder.appendAscendingOrder("webdb_create");
\r
164 else if (searchOrder.equals("dateasc"))
\r
165 queryBuilder.appendDescendingOrder("webdb_create");
\r
166 else if (searchOrder.equals("title"))
\r
167 queryBuilder.appendDescendingOrder("title");
\r
168 else if (searchOrder.equals("creator"))
\r
169 queryBuilder.appendDescendingOrder("creator");
\r
172 returnArticleList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0, selectArticleUrl);
\r
174 catch (Throwable e) {
\r
175 throw new ServletModuleException(e.toString());
\r
179 public void add(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
\r
180 _showObject(null, req, res);
\r
184 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
186 //theLog.printDebugInfo(":: content :: trying to insert");
\r
188 EntityUsers user = _getUser(req);
\r
189 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
\r
191 String now = StringUtil.date2webdbDate(new GregorianCalendar());
\r
192 withValues.put("date", now);
\r
193 withValues.put("publish_path", StringUtil.webdbDate2path(now));
\r
194 withValues.put("to_publisher", user.getId());
\r
195 withValues.put("is_produced", "0");
\r
196 if (!withValues.containsKey("is_published"))
\r
197 withValues.put("is_published","0");
\r
198 if (!withValues.containsKey("is_html"))
\r
199 withValues.put("is_html","0");
\r
201 String id = mainModule.add(withValues);
\r
202 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
\r
204 _showObject(id, req, res);
\r
206 catch (StorageObjectFailure e) {
\r
207 throw new ServletModuleException(e.toString());
\r
209 catch (ModuleException e) {
\r
210 throw new ServletModuleException(e.toString());
\r
214 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
216 EntityUsers user = _getUser(req);
\r
218 String idParam = req.getParameter("id");
\r
219 if (idParam == null) throw new ServletModuleException("Invalid call: id missing");
\r
221 String confirmParam = req.getParameter("confirm");
\r
222 String cancelParam = req.getParameter("cancel");
\r
224 logger.info("where = " + req.getParameter("where"));
\r
226 if (confirmParam == null && cancelParam == null) {
\r
228 SimpleHash mergeData = new SimpleHash();
\r
229 mergeData.put("module", "Content");
\r
230 mergeData.put("infoString", "Content: " + idParam);
\r
231 mergeData.put("id", idParam);
\r
232 mergeData.put("where", req.getParameter("where"));
\r
233 mergeData.put("order", req.getParameter("order"));
\r
234 mergeData.put("offset", req.getParameter("offset"));
\r
235 deliver(req, res, mergeData, templateConfirmString);
\r
238 if (confirmParam!= null && !confirmParam.equals("")) {
\r
240 mainModule.deleteById(idParam);
\r
242 /** @todo the following two should be implied in
\r
243 * DatabaseContent */
\r
245 //delete rows in the content_x_topic-table
\r
246 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
\r
247 //delete rows in the comment-table
\r
248 DatabaseComment.getInstance().deleteByContentId(idParam);
\r
249 //delete from lucene index, if any
\r
250 String index = configuration.getString("IndexPath");
\r
251 if (IndexReader.indexExists(index)){
\r
252 IndexUtil.unindexID(idParam,index);
\r
256 catch (ModuleException e) {
\r
257 throw new ServletModuleException(e.toString());
\r
259 catch (StorageObjectFailure e) {
\r
260 throw new ServletModuleException(e.toString());
\r
262 catch (IOException e) {
\r
263 throw new ServletModuleException(e.toString());
\r
268 // Datensatz anzeigen
\r
269 _showObject(idParam, req, res);
\r
274 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
276 String idParam = req.getParameter("id");
\r
277 if (idParam == null)
\r
278 throw new ServletModuleException("Invalid call: id not supplied ");
\r
279 _showObject(idParam, req, res);
\r
282 // methods for attaching media file
\r
283 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
285 String mediaIdParam = req.getParameter("mid");
\r
286 String idParam = req.getParameter("cid");
\r
287 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
\r
290 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
\r
291 entContent.attach(mediaIdParam);
\r
293 catch(ModuleException e) {
\r
294 logger.error("smod content :: attach :: could not get entityContent");
\r
296 catch(StorageObjectFailure e) {
\r
297 logger.error("smod content :: attach :: could not get entityContent");
\r
300 _showObject(idParam, req, res);
\r
303 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
305 String cidParam = req.getParameter("cid");
\r
306 String midParam = req.getParameter("mid");
\r
307 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
\r
308 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
\r
311 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
\r
312 entContent.dettach(cidParam,midParam);
\r
314 catch(ModuleException e) {
\r
315 logger.error("smod content :: dettach :: could not get entityContent");
\r
317 catch(StorageObjectFailure e) {
\r
318 logger.error("smod content :: dettach :: could not get entityContent");
\r
321 _showObject(cidParam, req, res);
\r
324 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse)
\r
325 throws ServletModuleException
\r
328 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
330 String returnUrl = requestParser.getParameter("returnurl");
\r
332 String idParam = aRequest.getParameter("id");
\r
333 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
\r
335 HashMap withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
\r
336 String[] topic_id = aRequest.getParameterValues("to_topic");
\r
337 String content_id = aRequest.getParameter("id");
\r
339 withValues.put("is_produced", "0");
\r
340 if (!withValues.containsKey("is_published"))
\r
341 withValues.put("is_published","0");
\r
342 if (!withValues.containsKey("is_html"))
\r
343 withValues.put("is_html","0");
\r
345 String id = mainModule.set(withValues);
\r
346 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"),topic_id);
\r
348 String whereParam = aRequest.getParameter("where");
\r
349 String orderParam = aRequest.getParameter("order");
\r
351 if (returnUrl!=null){
\r
352 redirect(aResponse, returnUrl);
\r
355 _showObject(idParam, aRequest, aResponse);
\r
357 catch (Throwable e) {
\r
358 throw new ServletModuleException(e.toString());
\r
363 * HelperMethod shows the basic article editing form.
\r
365 * if the "id" parameter is null, it means show an empty form to add a new
\r
368 private void _showObject(String id, HttpServletRequest aRequest, HttpServletResponse aResponse)
\r
369 throws ServletModuleException {
\r
372 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
373 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
374 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
\r
376 URLBuilder urlBuilder = new URLBuilder();
\r
378 urlBuilder.setValue("module", "Content");
\r
379 urlBuilder.setValue("do", "edit");
\r
380 urlBuilder.setValue("id", id);
\r
381 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
384 responseData.put("new", Boolean.FALSE);
\r
385 article = model.makeEntityAdapter("content", mainModule.getById(id));
\r
388 List fields = DatabaseContent.getInstance().getFields();
\r
389 responseData.put("new", Boolean.TRUE);
\r
390 article = new HashMap();
\r
391 Iterator i = fields.iterator();
\r
392 while (i.hasNext()) {
\r
393 article.put(i.next(), null);
\r
396 article.put("to_topics", null);
\r
398 MirGlobal.localizer().adminInterface().initializeArticle(article);
\r
400 responseData.put("article", article);
\r
403 new CachingRewindableIterator(
\r
404 new EntityIteratorAdapter( "", "id", 30,
\r
405 MirGlobal.localizer().dataModel().adapterModel(), "language"));
\r
407 new CachingRewindableIterator(
\r
408 new EntityIteratorAdapter("", "id", 30,
\r
409 MirGlobal.localizer().dataModel().adapterModel(), "topic"));
\r
410 Object articleTypes =
\r
411 new CachingRewindableIterator(
\r
412 new EntityIteratorAdapter( "", "id", 30,
\r
413 MirGlobal.localizer().dataModel().adapterModel(), "articleType"));
\r
415 responseData.put("articleTypes", articleTypes);
\r
416 responseData.put("languages", languages);
\r
417 responseData.put("topics", topics);
\r
418 responseData.put("returnurl", requestParser.getParameter("returnurl"));
\r
419 responseData.put("thisurl", urlBuilder.getQuery());
\r
421 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editTemplate);
\r
423 catch (Exception e) {
\r
424 throw new ServletModuleException(e.toString());
\r
428 public void returnArticleList(
\r
429 HttpServletRequest aRequest,
\r
430 HttpServletResponse aResponse,
\r
431 String aWhereClause,
\r
432 String anOrderByClause,
\r
434 String aSelectArticleUrl) throws ServletModuleException {
\r
436 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
437 URLBuilder urlBuilder = new URLBuilder();
\r
438 EntityAdapterModel model;
\r
439 int nrArticlesPerPage = 20;
\r
443 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
444 model = MirGlobal.localizer().dataModel().adapterModel();
\r
446 Object articleList =
\r
447 new CachingRewindableIterator(
\r
448 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
\r
449 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
\r
452 responseData.put("nexturl", null);
\r
453 responseData.put("prevurl", null);
\r
455 count=mainModule.getSize(aWhereClause);
\r
457 urlBuilder.setValue("module", "Content");
\r
458 urlBuilder.setValue("do", "list");
\r
459 urlBuilder.setValue("where", aWhereClause);
\r
460 urlBuilder.setValue("order", anOrderByClause);
\r
463 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
\r
464 urlBuilder.setValue("searchvalue", requestParser.getParameter("searchvalue"));
\r
465 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
\r
466 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
\r
467 urlBuilder.setValue("searcharticletype", requestParser.getParameter("searcharticletype"));
\r
468 urlBuilder.setValue("selectarticleurl", aSelectArticleUrl);
\r
470 responseData.put("searchfield", requestParser.getParameter("searchfield"));
\r
471 responseData.put("searchvalue", requestParser.getParameter("searchvalue"));
\r
472 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
\r
473 responseData.put("searchorder", requestParser.getParameter("searchorder"));
\r
474 responseData.put("searcharticletype", requestParser.getParameter("searcharticletype"));
\r
475 responseData.put("selectarticleurl", aSelectArticleUrl);
\r
477 urlBuilder.setValue("offset", anOffset);
\r
478 responseData.put("offset" , new Integer(anOffset).toString());
\r
479 responseData.put("thisurl" , urlBuilder.getQuery());
\r
481 if (count>=anOffset+nrArticlesPerPage) {
\r
482 urlBuilder.setValue("offset", anOffset + nrArticlesPerPage);
\r
483 responseData.put("nexturl" , urlBuilder.getQuery());
\r
487 urlBuilder.setValue("offset", Math.max(anOffset - nrArticlesPerPage, 0));
\r
488 responseData.put("prevurl" , urlBuilder.getQuery());
\r
491 responseData.put("articles", articleList);
\r
493 responseData.put("from" , Integer.toString(anOffset+1));
\r
494 responseData.put("count", Integer.toString(count));
\r
495 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
\r
496 responseData.put("offset" , Integer.toString(anOffset));
\r
497 responseData.put("order", anOrderByClause);
\r
498 responseData.put("where" , aWhereClause);
\r
500 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listTemplate);
\r
502 catch (Throwable e) {
\r
503 throw new ServletModuleException(e.toString());
\r
507 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
510 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
511 URLBuilder urlBuilder = new URLBuilder();
\r
513 urlBuilder.setValue("module", "Content");
\r
514 urlBuilder.setValue("do", "setparent");
\r
515 urlBuilder.setValue("childid", requestParser.getParameter("id"));
\r
516 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
518 returnArticleList(aRequest, aResponse, "", "", 0, urlBuilder.getQuery());
\r
520 catch (Throwable e) {
\r
521 throw new ServletModuleException(e.getMessage());
\r
525 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
528 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
529 String articleId = requestParser.getParameter("article_id");
\r
531 if (articleId == null)
\r
532 throw new ServletModuleException("ServletModuleContent.listchildren: article_id not set!");
\r
534 returnArticleList(aRequest, aResponse, "to_content = " + articleId, "", 0, null);
\r
536 catch (Throwable e) {
\r
537 throw new ServletModuleException(e.getMessage());
\r
541 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
543 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
544 String articleId = aRequest.getParameter("childid");
\r
545 String parentId = aRequest.getParameter("id");
\r
546 String returnUrl = aRequest.getParameter("returnurl");
\r
549 EntityContent article = (EntityContent) mainModule.getById(articleId);
\r
550 article.setValueForProperty("to_content", parentId);
\r
551 article.setProduced(false);
\r
554 catch(Throwable e) {
\r
555 logger.error("ServletModuleContent.setparent: " + e.getMessage());
\r
556 throw new ServletModuleException("ServletModuleContent.setparent: " + e.getMessage());
\r
559 redirect(aResponse, returnUrl);
\r
562 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
564 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
565 String articleId = requestParser.getParameter("id");
\r
566 String returnUrl = requestParser.getParameter("returnurl");
\r
569 EntityContent article = (EntityContent) mainModule.getById(articleId);
\r
570 article.setValueForProperty("to_content", "");
\r
571 article.setProduced(false);
\r
574 catch(Throwable e) {
\r
575 e.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));
\r
576 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
\r
578 throw new ServletModuleException("ServletModuleContent.clearparent: " + e.getMessage());
\r
581 redirect(aResponse, returnUrl);
\r
584 private EntityUsers _getUser(HttpServletRequest req)
\r
586 HttpSession session=req.getSession(false);
\r
588 return (EntityUsers)session.getAttribute("login.uid");
\r