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.IOException;
\r
35 import java.util.GregorianCalendar;
\r
36 import java.util.HashMap;
\r
37 import java.util.Iterator;
\r
38 import java.util.List;
\r
39 import java.util.Map;
\r
41 import javax.servlet.http.HttpServletRequest;
\r
42 import javax.servlet.http.HttpServletResponse;
\r
43 import javax.servlet.http.HttpSession;
\r
45 import mir.entity.adapter.EntityAdapterModel;
\r
46 import mir.entity.adapter.EntityIteratorAdapter;
\r
47 import mir.log.LoggerWrapper;
\r
48 import mir.misc.StringUtil;
\r
49 import mir.module.ModuleException;
\r
50 import mir.servlet.ServletModule;
\r
51 import mir.servlet.ServletModuleException;
\r
52 import mir.storage.StorageObjectFailure;
\r
53 import mir.util.CachingRewindableIterator;
\r
54 import mir.util.HTTPRequestParser;
\r
55 import mir.util.JDBCStringRoutines;
\r
56 import mir.util.SQLQueryBuilder;
\r
57 import mir.util.URLBuilder;
\r
58 import mircoders.entity.EntityContent;
\r
59 import mircoders.entity.EntityUsers;
\r
60 import mircoders.global.MirGlobal;
\r
61 import mircoders.module.ModuleContent;
\r
62 import mircoders.search.IndexUtil;
\r
63 import mircoders.storage.DatabaseComment;
\r
64 import mircoders.storage.DatabaseContent;
\r
65 import mircoders.storage.DatabaseContentToTopics;
\r
67 import org.apache.lucene.index.IndexReader;
\r
69 import freemarker.template.SimpleHash;
\r
72 * ServletModuleContent -
\r
73 * deliver html for the article admin form.
\r
75 * @version $Id: ServletModuleContent.java,v 1.39 2003/03/05 19:23:16 idfx Exp $
\r
76 * @author rk, mir-coders
\r
80 public class ServletModuleContent extends ServletModule
\r
82 private String editTemplate = configuration.getString("ServletModule.Content.ObjektTemplate");;
\r
83 private String listTemplate = configuration.getString("ServletModule.Content.ListTemplate");
\r
85 private static ServletModuleContent instance = new ServletModuleContent();
\r
86 public static ServletModule getInstance() { return instance; }
\r
88 private ServletModuleContent() {
\r
90 logger = new LoggerWrapper("ServletModule.Content");
\r
93 templateListString = configuration.getString("ServletModule.Content.ListTemplate");
\r
94 templateObjektString = configuration.getString("ServletModule.Content.ObjektTemplate");
\r
95 templateConfirmString = configuration.getString("ServletModule.Content.ConfirmTemplate");
\r
97 mainModule = new ModuleContent(DatabaseContent.getInstance());
\r
99 catch (StorageObjectFailure e) {
\r
100 logger.error("servletmodulecontent konnte nicht initialisiert werden");
\r
104 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
106 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
108 String where = requestParser.getParameter("where");
\r
109 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
\r
110 int offset = requestParser.getIntegerWithDefault("offset", 0);
\r
111 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
\r
113 returnArticleList(aRequest, aResponse, where, order, offset, selectArticleUrl);
\r
116 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
\r
118 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
119 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
\r
120 String searchField = requestParser.getParameterWithDefault("searchfield", "");
\r
121 String searchValue = requestParser.getParameterWithDefault("searchvalue", "").trim();
\r
122 String searchOrder = requestParser.getParameterWithDefault("searchorder", "");
\r
123 String searchispublished = requestParser.getParameterWithDefault("searchispublished", "");
\r
124 String searchArticleType = requestParser.getParameterWithDefault("searcharticletype", "");
\r
125 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
\r
127 if (searchValue.length()>0) {
\r
128 if (searchField.equals("contents"))
\r
129 queryBuilder.appendAndCondition(
\r
130 "(lower(content_data) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')"+
\r
131 " or (lower(description) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')");
\r
133 queryBuilder.appendAndCondition(
\r
134 "lower("+ searchField + ") like " +
\r
135 "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%'");
\r
138 if (searchispublished.length()>0) {
\r
139 if (searchispublished.equals("0"))
\r
140 queryBuilder.appendAndCondition("is_published='f'");
\r
142 queryBuilder.appendAndCondition("is_published='t'");
\r
145 if (searchArticleType.length()>0) {
\r
146 queryBuilder.appendAndCondition("to_article_type="+Integer.parseInt(searchArticleType));
\r
149 if (searchOrder.length()>0) {
\r
150 if (searchOrder.equals("datedesc"))
\r
151 queryBuilder.appendAscendingOrder("webdb_create");
\r
152 else if (searchOrder.equals("dateasc"))
\r
153 queryBuilder.appendDescendingOrder("webdb_create");
\r
154 else if (searchOrder.equals("title"))
\r
155 queryBuilder.appendDescendingOrder("title");
\r
156 else if (searchOrder.equals("creator"))
\r
157 queryBuilder.appendDescendingOrder("creator");
\r
160 returnArticleList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0, selectArticleUrl);
\r
162 catch (Throwable e) {
\r
163 throw new ServletModuleException(e.toString());
\r
167 public void add(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
\r
168 _showObject(null, req, res);
\r
172 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
174 //theLog.printDebugInfo(":: content :: trying to insert");
\r
176 EntityUsers user = _getUser(req);
\r
177 Map withValues = getIntersectingValues(req, DatabaseContent.getInstance());
\r
179 String now = StringUtil.date2webdbDate(new GregorianCalendar());
\r
180 withValues.put("date", now);
\r
181 withValues.put("publish_path", StringUtil.webdbDate2path(now));
\r
182 withValues.put("to_publisher", user.getId());
\r
183 withValues.put("is_produced", "0");
\r
184 if (!withValues.containsKey("is_published"))
\r
185 withValues.put("is_published","0");
\r
186 if (!withValues.containsKey("is_html"))
\r
187 withValues.put("is_html","0");
\r
189 String id = mainModule.add(withValues);
\r
190 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
\r
192 _showObject(id, req, res);
\r
194 catch (StorageObjectFailure e) {
\r
195 throw new ServletModuleException(e.toString());
\r
197 catch (ModuleException e) {
\r
198 throw new ServletModuleException(e.toString());
\r
202 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
204 EntityUsers user = _getUser(req);
\r
206 String idParam = req.getParameter("id");
\r
207 if (idParam == null) throw new ServletModuleException("Invalid call: id missing");
\r
209 String confirmParam = req.getParameter("confirm");
\r
210 String cancelParam = req.getParameter("cancel");
\r
212 logger.info("where = " + req.getParameter("where"));
\r
214 if (confirmParam == null && cancelParam == null) {
\r
216 SimpleHash mergeData = new SimpleHash();
\r
217 mergeData.put("module", "Content");
\r
218 mergeData.put("infoString", "Content: " + idParam);
\r
219 mergeData.put("id", idParam);
\r
220 mergeData.put("where", req.getParameter("where"));
\r
221 mergeData.put("order", req.getParameter("order"));
\r
222 mergeData.put("offset", req.getParameter("offset"));
\r
223 deliver(req, res, mergeData, templateConfirmString);
\r
226 if (confirmParam!= null && !confirmParam.equals("")) {
\r
228 mainModule.deleteById(idParam);
\r
230 /** @todo the following two should be implied in
\r
231 * DatabaseContent */
\r
233 //delete rows in the content_x_topic-table
\r
234 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
\r
235 //delete rows in the comment-table
\r
236 DatabaseComment.getInstance().deleteByContentId(idParam);
\r
237 //delete from lucene index, if any
\r
238 String index = configuration.getString("IndexPath");
\r
239 if (IndexReader.indexExists(index)){
\r
240 IndexUtil.unindexID(idParam,index);
\r
244 catch (ModuleException e) {
\r
245 throw new ServletModuleException(e.toString());
\r
247 catch (StorageObjectFailure e) {
\r
248 throw new ServletModuleException(e.toString());
\r
250 catch (IOException e) {
\r
251 throw new ServletModuleException(e.toString());
\r
256 // Datensatz anzeigen
\r
257 _showObject(idParam, req, res);
\r
262 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
264 String idParam = req.getParameter("id");
\r
265 if (idParam == null)
\r
266 throw new ServletModuleException("Invalid call: id not supplied ");
\r
267 _showObject(idParam, req, res);
\r
270 // methods for attaching media file
\r
271 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
273 String mediaIdParam = req.getParameter("mid");
\r
274 String idParam = req.getParameter("cid");
\r
275 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
\r
278 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
\r
279 entContent.attach(mediaIdParam);
\r
281 catch(ModuleException e) {
\r
282 logger.error("smod content :: attach :: could not get entityContent");
\r
284 catch(StorageObjectFailure e) {
\r
285 logger.error("smod content :: attach :: could not get entityContent");
\r
288 _showObject(idParam, req, res);
\r
291 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
\r
293 String cidParam = req.getParameter("cid");
\r
294 String midParam = req.getParameter("mid");
\r
295 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
\r
296 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
\r
299 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
\r
300 entContent.dettach(cidParam,midParam);
\r
302 catch(ModuleException e) {
\r
303 logger.error("smod content :: dettach :: could not get entityContent");
\r
305 catch(StorageObjectFailure e) {
\r
306 logger.error("smod content :: dettach :: could not get entityContent");
\r
309 _showObject(cidParam, req, res);
\r
312 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse)
\r
313 throws ServletModuleException
\r
316 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
318 String returnUrl = requestParser.getParameter("returnurl");
\r
320 String idParam = aRequest.getParameter("id");
\r
321 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
\r
323 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
\r
324 String[] topic_id = aRequest.getParameterValues("to_topic");
\r
325 String content_id = aRequest.getParameter("id");
\r
327 withValues.put("is_produced", "0");
\r
328 if (!withValues.containsKey("is_published"))
\r
329 withValues.put("is_published","0");
\r
330 if (!withValues.containsKey("is_html"))
\r
331 withValues.put("is_html","0");
\r
333 String id = mainModule.set(withValues);
\r
334 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"),topic_id);
\r
336 String whereParam = aRequest.getParameter("where");
\r
337 String orderParam = aRequest.getParameter("order");
\r
339 if (returnUrl!=null){
\r
340 redirect(aResponse, returnUrl);
\r
343 _showObject(idParam, aRequest, aResponse);
\r
345 catch (Throwable e) {
\r
346 throw new ServletModuleException(e.toString());
\r
351 * HelperMethod shows the basic article editing form.
\r
353 * if the "id" parameter is null, it means show an empty form to add a new
\r
356 public void _showObject(String id, HttpServletRequest aRequest, HttpServletResponse aResponse)
\r
357 throws ServletModuleException {
\r
360 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
361 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
362 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
\r
364 URLBuilder urlBuilder = new URLBuilder();
\r
366 urlBuilder.setValue("module", "Content");
\r
367 urlBuilder.setValue("do", "edit");
\r
368 urlBuilder.setValue("id", id);
\r
369 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
372 responseData.put("new", Boolean.FALSE);
\r
373 article = model.makeEntityAdapter("content", mainModule.getById(id));
\r
376 List fields = DatabaseContent.getInstance().getFields();
\r
377 responseData.put("new", Boolean.TRUE);
\r
378 article = new HashMap();
\r
379 Iterator i = fields.iterator();
\r
380 while (i.hasNext()) {
\r
381 article.put(i.next(), null);
\r
384 article.put("to_topics", null);
\r
386 MirGlobal.localizer().adminInterface().initializeArticle(article);
\r
388 responseData.put("article", article);
\r
390 responseData.put("returnurl", requestParser.getParameter("returnurl"));
\r
391 responseData.put("thisurl", urlBuilder.getQuery());
\r
393 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editTemplate);
\r
395 catch (Exception e) {
\r
396 throw new ServletModuleException(e.toString());
\r
400 public void returnArticleList(
\r
401 HttpServletRequest aRequest,
\r
402 HttpServletResponse aResponse,
\r
403 String aWhereClause,
\r
404 String anOrderByClause,
\r
406 String aSelectArticleUrl) throws ServletModuleException {
\r
408 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
409 URLBuilder urlBuilder = new URLBuilder();
\r
410 EntityAdapterModel model;
\r
411 int nrArticlesPerPage = 20;
\r
415 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
416 model = MirGlobal.localizer().dataModel().adapterModel();
\r
418 Object articleList =
\r
419 new CachingRewindableIterator(
\r
420 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
\r
421 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
\r
424 responseData.put("nexturl", null);
\r
425 responseData.put("prevurl", null);
\r
427 count=mainModule.getSize(aWhereClause);
\r
429 urlBuilder.setValue("module", "Content");
\r
430 urlBuilder.setValue("do", "list");
\r
431 urlBuilder.setValue("where", aWhereClause);
\r
432 urlBuilder.setValue("order", anOrderByClause);
\r
435 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
\r
436 urlBuilder.setValue("searchvalue", requestParser.getParameter("searchvalue"));
\r
437 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
\r
438 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
\r
439 urlBuilder.setValue("searcharticletype", requestParser.getParameter("searcharticletype"));
\r
440 urlBuilder.setValue("selectarticleurl", aSelectArticleUrl);
\r
442 responseData.put("searchfield", requestParser.getParameter("searchfield"));
\r
443 responseData.put("searchvalue", requestParser.getParameter("searchvalue"));
\r
444 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
\r
445 responseData.put("searchorder", requestParser.getParameter("searchorder"));
\r
446 responseData.put("searcharticletype", requestParser.getParameter("searcharticletype"));
\r
447 responseData.put("selectarticleurl", aSelectArticleUrl);
\r
449 urlBuilder.setValue("offset", anOffset);
\r
450 responseData.put("offset" , new Integer(anOffset).toString());
\r
451 responseData.put("thisurl" , urlBuilder.getQuery());
\r
453 if (count>=anOffset+nrArticlesPerPage) {
\r
454 urlBuilder.setValue("offset", (anOffset + nrArticlesPerPage));
\r
455 responseData.put("nexturl" , urlBuilder.getQuery());
\r
459 urlBuilder.setValue("offset", Math.max(anOffset - nrArticlesPerPage, 0));
\r
460 responseData.put("prevurl" , urlBuilder.getQuery());
\r
463 responseData.put("articles", articleList);
\r
465 responseData.put("from" , Integer.toString(anOffset+1));
\r
466 responseData.put("count", Integer.toString(count));
\r
467 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
\r
468 responseData.put("offset" , Integer.toString(anOffset));
\r
469 responseData.put("order", anOrderByClause);
\r
470 responseData.put("where" , aWhereClause);
\r
472 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listTemplate);
\r
474 catch (Throwable e) {
\r
475 throw new ServletModuleException(e.toString());
\r
479 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
482 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
483 URLBuilder urlBuilder = new URLBuilder();
\r
485 urlBuilder.setValue("module", "Content");
\r
486 urlBuilder.setValue("do", "setparent");
\r
487 urlBuilder.setValue("childid", requestParser.getParameter("id"));
\r
488 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
490 returnArticleList(aRequest, aResponse, "", "", 0, urlBuilder.getQuery());
\r
492 catch (Throwable e) {
\r
493 throw new ServletModuleException(e.getMessage());
\r
497 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
500 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
501 String articleId = requestParser.getParameter("article_id");
\r
503 if (articleId == null)
\r
504 throw new ServletModuleException("ServletModuleContent.listchildren: article_id not set!");
\r
506 returnArticleList(aRequest, aResponse, "to_content = " + articleId, "", 0, null);
\r
508 catch (Throwable e) {
\r
509 throw new ServletModuleException(e.getMessage());
\r
513 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
515 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
516 String articleId = aRequest.getParameter("childid");
\r
517 String parentId = aRequest.getParameter("id");
\r
518 String returnUrl = aRequest.getParameter("returnurl");
\r
521 EntityContent article = (EntityContent) mainModule.getById(articleId);
\r
522 article.setValueForProperty("to_content", parentId);
\r
523 article.setProduced(false);
\r
526 catch(Throwable e) {
\r
527 logger.error("ServletModuleContent.setparent: " + e.getMessage());
\r
528 throw new ServletModuleException("ServletModuleContent.setparent: " + e.getMessage());
\r
531 redirect(aResponse, returnUrl);
\r
534 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
\r
536 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
537 String articleId = requestParser.getParameter("id");
\r
538 String returnUrl = requestParser.getParameter("returnurl");
\r
541 EntityContent article = (EntityContent) mainModule.getById(articleId);
\r
542 article.setValueForProperty("to_content", "");
\r
543 article.setProduced(false);
\r
546 catch(Throwable e) {
\r
547 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
548 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
\r
550 throw new ServletModuleException("ServletModuleContent.clearparent: " + e.getMessage());
\r
553 redirect(aResponse, returnUrl);
\r
556 private EntityUsers _getUser(HttpServletRequest req)
\r
558 HttpSession session=req.getSession(false);
\r
560 return (EntityUsers)session.getAttribute("login.uid");
\r