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.util.GregorianCalendar;
\r
35 import java.util.HashMap;
\r
36 import java.util.Iterator;
\r
37 import java.util.List;
\r
38 import java.util.Map;
\r
39 import javax.servlet.http.HttpServletRequest;
\r
40 import javax.servlet.http.HttpServletResponse;
\r
41 import javax.servlet.http.HttpSession;
\r
43 import org.apache.lucene.index.IndexReader;
\r
44 import freemarker.template.SimpleHash;
\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.servlet.ServletModule;
\r
50 import mir.servlet.ServletModuleExc;
\r
51 import mir.servlet.ServletModuleFailure;
\r
52 import mir.util.CachingRewindableIterator;
\r
53 import mir.util.HTTPRequestParser;
\r
54 import mir.util.JDBCStringRoutines;
\r
55 import mir.util.SQLQueryBuilder;
\r
56 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
66 import mircoders.storage.DatabaseContentToMedia;
\r
69 * ServletModuleContent -
\r
70 * deliver html for the article admin form.
\r
72 * @version $Id: ServletModuleContent.java,v 1.46 2003/03/27 20:11:35 zapata Exp $
\r
73 * @author rk, mir-coders
\r
77 public class ServletModuleContent extends ServletModule
\r
79 private String editTemplate = configuration.getString("ServletModule.Content.ObjektTemplate");;
\r
80 private String listTemplate = configuration.getString("ServletModule.Content.ListTemplate");
\r
82 private static ServletModuleContent instance = new ServletModuleContent();
\r
83 public static ServletModule getInstance() { return instance; }
\r
85 private ServletModuleContent() {
\r
87 logger = new LoggerWrapper("ServletModule.Content");
\r
90 templateListString = configuration.getString("ServletModule.Content.ListTemplate");
\r
91 templateObjektString = configuration.getString("ServletModule.Content.ObjektTemplate");
\r
92 templateConfirmString = configuration.getString("ServletModule.Content.ConfirmTemplate");
\r
94 mainModule = new ModuleContent(DatabaseContent.getInstance());
\r
96 catch (Throwable e) {
\r
97 logger.fatal("ServletModuleContent could not be initialized: " + e.toString());
\r
101 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
103 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
105 String where = requestParser.getParameter("where");
\r
106 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
\r
107 int offset = requestParser.getIntegerWithDefault("offset", 0);
\r
108 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
\r
110 returnArticleList(aRequest, aResponse, where, order, offset, selectArticleUrl);
\r
113 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {
\r
115 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
116 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
\r
117 String searchField = requestParser.getParameterWithDefault("searchfield", "");
\r
118 String searchValue = requestParser.getParameterWithDefault("searchvalue", "").trim();
\r
119 String searchOrder = requestParser.getParameterWithDefault("searchorder", "");
\r
120 String searchispublished = requestParser.getParameterWithDefault("searchispublished", "");
\r
121 String searchArticleType = requestParser.getParameterWithDefault("searcharticletype", "");
\r
122 String selectArticleUrl = requestParser.getParameter("selectarticleurl");
\r
124 if (searchValue.length()>0) {
\r
125 if (searchField.equals("id"))
\r
126 queryBuilder.appendAndCondition(
\r
127 "id='"+JDBCStringRoutines.escapeStringLiteral(searchValue)+"'");
\r
128 else 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 ServletModuleFailure(e);
\r
167 public void add(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
\r
168 _showObject(null, req, res);
\r
172 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\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 (Throwable e) {
\r
195 throw new ServletModuleFailure(e);
\r
199 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
201 EntityUsers user = _getUser(req);
\r
203 String idParam = req.getParameter("id");
\r
204 if (idParam == null) throw new ServletModuleExc("Invalid call: id missing");
\r
206 String confirmParam = req.getParameter("confirm");
\r
207 String cancelParam = req.getParameter("cancel");
\r
209 logger.info("where = " + req.getParameter("where"));
\r
211 if (confirmParam == null && cancelParam == null) {
\r
213 SimpleHash mergeData = new SimpleHash();
\r
214 mergeData.put("module", "Content");
\r
215 mergeData.put("infoString", "Content: " + idParam);
\r
216 mergeData.put("id", idParam);
\r
217 mergeData.put("where", req.getParameter("where"));
\r
218 mergeData.put("order", req.getParameter("order"));
\r
219 mergeData.put("offset", req.getParameter("offset"));
\r
220 deliver(req, res, mergeData, templateConfirmString);
\r
223 if (confirmParam!= null && !confirmParam.equals("")) {
\r
225 mainModule.deleteById(idParam);
\r
227 /** @todo the following two should be implied in
\r
228 * DatabaseContent */
\r
229 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
\r
230 DatabaseComment.getInstance().deleteByContentId(idParam);
\r
231 DatabaseContentToMedia.getInstance().deleteByContentId(idParam);
\r
234 //delete from lucene index, if any
\r
235 String index = configuration.getString("IndexPath");
\r
236 if (IndexReader.indexExists(index)){
\r
237 IndexUtil.unindexID(idParam,index);
\r
241 catch (Throwable e) {
\r
242 throw new ServletModuleFailure(e);
\r
247 // Datensatz anzeigen
\r
248 _showObject(idParam, req, res);
\r
253 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
255 String idParam = req.getParameter("id");
\r
256 if (idParam == null)
\r
257 throw new ServletModuleExc("Invalid call: id not supplied ");
\r
258 _showObject(idParam, req, res);
\r
261 // methods for attaching media file
\r
262 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
264 String mediaIdParam = req.getParameter("mid");
\r
265 String idParam = req.getParameter("cid");
\r
266 if (idParam == null||mediaIdParam==null) throw new ServletModuleExc("smod content :: attach :: cid/mid missing");
\r
269 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
\r
270 entContent.attach(mediaIdParam);
\r
272 catch(Throwable e) {
\r
273 logger.error("smod content :: attach :: could not get entityContent");
\r
276 _showObject(idParam, req, res);
\r
279 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
281 String cidParam = req.getParameter("cid");
\r
282 String midParam = req.getParameter("mid");
\r
283 if (cidParam == null)
\r
284 throw new ServletModuleExc("smod content :: dettach :: cid missing");
\r
285 if (midParam == null)
\r
286 throw new ServletModuleExc("smod content :: dettach :: mid missing");
\r
289 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
\r
290 entContent.dettach(cidParam,midParam);
\r
292 catch(Throwable e) {
\r
293 logger.error("smod content :: dettach :: could not get entityContent");
\r
296 _showObject(cidParam, req, res);
\r
299 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
302 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
304 String returnUrl = requestParser.getParameter("returnurl");
\r
306 String idParam = aRequest.getParameter("id");
\r
307 if (idParam == null)
\r
308 throw new ServletModuleExc("Wrong call: (id) is missing");
\r
310 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
\r
311 String[] topic_id = aRequest.getParameterValues("to_topic");
\r
312 String content_id = aRequest.getParameter("id");
\r
314 withValues.put("is_produced", "0");
\r
315 if (!withValues.containsKey("is_published"))
\r
316 withValues.put("is_published","0");
\r
317 if (!withValues.containsKey("is_html"))
\r
318 withValues.put("is_html","0");
\r
320 String id = mainModule.set(withValues);
\r
321 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"),topic_id);
\r
323 String whereParam = aRequest.getParameter("where");
\r
324 String orderParam = aRequest.getParameter("order");
\r
326 if (returnUrl!=null){
\r
327 redirect(aResponse, returnUrl);
\r
330 _showObject(idParam, aRequest, aResponse);
\r
332 catch (Throwable e) {
\r
333 throw new ServletModuleFailure(e);
\r
338 * HelperMethod shows the basic article editing form.
\r
340 * if the "id" parameter is null, it means show an empty form to add a new
\r
343 public void _showObject(String id, HttpServletRequest aRequest, HttpServletResponse aResponse)
\r
344 throws ServletModuleExc {
\r
346 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
347 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
348 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
\r
350 URLBuilder urlBuilder = new URLBuilder();
\r
352 urlBuilder.setValue("module", "Content");
\r
353 urlBuilder.setValue("do", "edit");
\r
354 urlBuilder.setValue("id", id);
\r
355 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
358 responseData.put("new", Boolean.FALSE);
\r
359 article = model.makeEntityAdapter("content", mainModule.getById(id));
\r
362 List fields = DatabaseContent.getInstance().getFields();
\r
363 responseData.put("new", Boolean.TRUE);
\r
364 article = new HashMap();
\r
365 Iterator i = fields.iterator();
\r
366 while (i.hasNext()) {
\r
367 article.put(i.next(), null);
\r
370 article.put("to_topics", null);
\r
372 MirGlobal.localizer().adminInterface().initializeArticle(article);
\r
374 responseData.put("article", article);
\r
376 responseData.put("topics",
\r
377 new EntityIteratorAdapter("", configuration.getString("Mir.Localizer.Admin.TopicListOrder"),
\r
378 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
\r
382 responseData.put("returnurl", requestParser.getParameter("returnurl"));
\r
383 responseData.put("thisurl", urlBuilder.getQuery());
\r
385 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editTemplate);
\r
387 catch (Throwable e) {
\r
388 throw new ServletModuleFailure(e);
\r
392 public void returnArticleList(
\r
393 HttpServletRequest aRequest,
\r
394 HttpServletResponse aResponse,
\r
395 String aWhereClause,
\r
396 String anOrderByClause,
\r
398 String aSelectArticleUrl) throws ServletModuleExc {
\r
400 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
401 URLBuilder urlBuilder = new URLBuilder();
\r
402 EntityAdapterModel model;
\r
403 int nrArticlesPerPage = 20;
\r
407 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
\r
408 model = MirGlobal.localizer().dataModel().adapterModel();
\r
410 Object articleList =
\r
411 new CachingRewindableIterator(
\r
412 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
\r
413 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
\r
416 responseData.put("nexturl", null);
\r
417 responseData.put("prevurl", null);
\r
419 count=mainModule.getSize(aWhereClause);
\r
421 urlBuilder.setValue("module", "Content");
\r
422 urlBuilder.setValue("do", "list");
\r
423 urlBuilder.setValue("where", aWhereClause);
\r
424 urlBuilder.setValue("order", anOrderByClause);
\r
427 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
\r
428 urlBuilder.setValue("searchvalue", requestParser.getParameter("searchvalue"));
\r
429 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
\r
430 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
\r
431 urlBuilder.setValue("searcharticletype", requestParser.getParameter("searcharticletype"));
\r
432 urlBuilder.setValue("selectarticleurl", aSelectArticleUrl);
\r
434 responseData.put("searchfield", requestParser.getParameter("searchfield"));
\r
435 responseData.put("searchvalue", requestParser.getParameter("searchvalue"));
\r
436 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
\r
437 responseData.put("searchorder", requestParser.getParameter("searchorder"));
\r
438 responseData.put("searcharticletype", requestParser.getParameter("searcharticletype"));
\r
439 responseData.put("selectarticleurl", aSelectArticleUrl);
\r
441 urlBuilder.setValue("offset", anOffset);
\r
442 responseData.put("offset" , new Integer(anOffset).toString());
\r
443 responseData.put("thisurl" , urlBuilder.getQuery());
\r
445 if (count>=anOffset+nrArticlesPerPage) {
\r
446 urlBuilder.setValue("offset", (anOffset + nrArticlesPerPage));
\r
447 responseData.put("nexturl" , urlBuilder.getQuery());
\r
451 urlBuilder.setValue("offset", Math.max(anOffset - nrArticlesPerPage, 0));
\r
452 responseData.put("prevurl" , urlBuilder.getQuery());
\r
455 responseData.put("articles", articleList);
\r
457 responseData.put("from" , Integer.toString(anOffset+1));
\r
458 responseData.put("count", Integer.toString(count));
\r
459 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
\r
460 responseData.put("offset" , Integer.toString(anOffset));
\r
461 responseData.put("order", anOrderByClause);
\r
462 responseData.put("where" , aWhereClause);
\r
464 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listTemplate);
\r
466 catch (Throwable e) {
\r
467 throw new ServletModuleFailure(e);
\r
471 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
474 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
475 URLBuilder urlBuilder = new URLBuilder();
\r
477 urlBuilder.setValue("module", "Content");
\r
478 urlBuilder.setValue("do", "setparent");
\r
479 urlBuilder.setValue("childid", requestParser.getParameter("id"));
\r
480 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
482 returnArticleList(aRequest, aResponse, "", "", 0, urlBuilder.getQuery());
\r
484 catch (Throwable e) {
\r
485 throw new ServletModuleFailure(e);
\r
489 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
492 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
493 String articleId = requestParser.getParameter("article_id");
\r
495 if (articleId == null)
\r
496 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
\r
498 returnArticleList(aRequest, aResponse, "to_content = " + articleId, "", 0, null);
\r
500 catch (Throwable e) {
\r
501 throw new ServletModuleFailure(e);
\r
505 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
507 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
508 String articleId = aRequest.getParameter("childid");
\r
509 String parentId = aRequest.getParameter("id");
\r
510 String returnUrl = aRequest.getParameter("returnurl");
\r
513 EntityContent article = (EntityContent) mainModule.getById(articleId);
\r
514 article.setValueForProperty("to_content", parentId);
\r
515 article.setProduced(false);
\r
518 catch(Throwable e) {
\r
519 logger.error("ServletModuleContent.setparent: " + e.getMessage());
\r
520 throw new ServletModuleFailure(e);
\r
523 redirect(aResponse, returnUrl);
\r
526 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
528 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
529 String articleId = requestParser.getParameter("id");
\r
530 String returnUrl = requestParser.getParameter("returnurl");
\r
533 EntityContent article = (EntityContent) mainModule.getById(articleId);
\r
534 article.setValueForProperty("to_content", "");
\r
535 article.setProduced(false);
\r
538 catch(Throwable e) {
\r
539 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
540 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
\r
542 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
\r
545 redirect(aResponse, returnUrl);
\r
548 private EntityUsers _getUser(HttpServletRequest req)
\r
550 HttpSession session=req.getSession(false);
\r
552 return (EntityUsers)session.getAttribute("login.uid");
\r