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 the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.servlet;
38 import javax.servlet.*;
39 import javax.servlet.http.*;
41 import org.apache.struts.util.MessageResources;
42 import org.apache.lucene.index.*;
44 import freemarker.template.*;
53 import mir.entity.adapter.*;
56 import mircoders.global.*;
57 import mircoders.storage.*;
58 import mircoders.module.*;
59 import mircoders.entity.*;
60 import mircoders.localizer.*;
61 import mircoders.search.IndexUtil;
64 * ServletModuleContent -
65 * deliver html for the article admin form.
67 * @version $Id: ServletModuleContent.java,v 1.31 2003/01/18 15:55:14 john Exp $
68 * @author rk, mir-coders
72 public class ServletModuleContent extends ServletModule
74 static ModuleTopics themenModule;
75 static ModuleSchwerpunkt schwerpunktModule;
76 static ModuleImages imageModule;
78 static String templateOpString;
80 // Singelton / Kontruktor
82 private static ServletModuleContent instance = new ServletModuleContent();
83 public static ServletModule getInstance() { return instance; }
85 private ServletModuleContent() {
86 logger = new LoggerWrapper("ServletModule.Content");
88 templateListString = MirConfig.getProp("ServletModule.Content.ListTemplate");
89 templateObjektString = MirConfig.getProp("ServletModule.Content.ObjektTemplate");
90 templateConfirmString = MirConfig.getProp("ServletModule.Content.ConfirmTemplate");
92 mainModule = new ModuleContent(DatabaseContent.getInstance());
93 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
94 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
95 imageModule = new ModuleImages(DatabaseImages.getInstance());
97 catch (StorageObjectException e) {
98 logger.error("servletmodulecontent konnte nicht initialisiert werden");
104 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
106 EntityUsers user = _getUser(req);
107 String offsetParam = req.getParameter("offset");
108 String whereParam = req.getParameter("where");
109 String orderParam = req.getParameter("order");
113 if (offsetParam != null && !offsetParam.equals(""))
114 offset = Integer.parseInt(offsetParam);
116 if (req.getParameter("next") != null)
117 offset=Integer.parseInt(req.getParameter("nextoffset"));
119 if (req.getParameter("prev") != null)
120 offset = Integer.parseInt(req.getParameter("prevoffset"));
122 returnArticleList(req, res, whereParam, orderParam, offset);
125 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
127 EntityUsers user = _getUser(req);
128 String offsetParam = req.getParameter("offset");
131 String whereParam = req.getParameter("where");
133 if (whereParam==null) whereParam = "to_article_type='0'";
135 // hier offsetcode bearbeiteb
136 if (offsetParam != null && !offsetParam.equals(""))
137 offset = Integer.parseInt(offsetParam);
139 if (req.getParameter("next") != null)
140 offset=Integer.parseInt(req.getParameter("nextoffset"));
142 if (req.getParameter("prev") != null)
143 offset = Integer.parseInt(req.getParameter("prevoffset"));
145 String orderParam = req.getParameter("order");
147 returnArticleList(req, res, whereParam, orderParam, offset);
151 public void search(HttpServletRequest req, HttpServletResponse res)
152 throws ServletModuleException {
154 EntityUsers user = _getUser(req);
156 String fieldParam = req.getParameter("field");
157 String fieldValueParam = req.getParameter("fieldvalue");
158 String orderParam = req.getParameter("order");
160 theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
161 returnArticleList(req, res, "lower("+ fieldParam + ") like lower('%" + JDBCStringRoutines.escapeStringLiteral(fieldValueParam) + "%')", orderParam, 0);
162 } catch (ModuleException e) {
163 throw new ServletModuleException(e.toString());
167 public void add(HttpServletRequest req, HttpServletResponse res)
168 throws ServletModuleException {
169 _showObject(null, req, res);
173 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
175 //theLog.printDebugInfo(":: content :: trying to insert");
177 EntityUsers user = _getUser(req);
178 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
180 String now = StringUtil.date2webdbDate(new GregorianCalendar());
181 withValues.put("date", now);
182 withValues.put("publish_path", StringUtil.webdbDate2path(now));
183 withValues.put("to_publisher", user.getId());
184 withValues.put("is_produced", "0");
185 if (!withValues.containsKey("is_published"))
186 withValues.put("is_published","0");
187 if (!withValues.containsKey("is_html"))
188 withValues.put("is_html","0");
190 String id = mainModule.add(withValues);
191 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
193 _showObject(id, req, res);
195 catch (StorageObjectException e) {
196 throw new ServletModuleException(e.toString());
198 catch (ModuleException e) {
199 throw new ServletModuleException(e.toString());
203 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
205 EntityUsers user = _getUser(req);
207 String idParam = req.getParameter("id");
208 if (idParam == null) throw new ServletModuleException("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
234 //delete rows in the content_x_topic-table
235 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
236 //delete rows in the comment-table
237 DatabaseComment.getInstance().deleteByContentId(idParam);
238 //delete from lucene index, if any
239 String index=MirConfig.getProp("IndexPath");
240 if (IndexReader.indexExists(index)){
241 IndexUtil.unindexID(idParam,index);
244 } catch (ModuleException e) {
245 throw new ServletModuleException(e.toString());
246 } catch (StorageObjectException e) {
247 throw new ServletModuleException(e.toString());
248 } catch (IOException e) {
249 throw new ServletModuleException(e.toString());
254 // Datensatz anzeigen
255 _showObject(idParam, req, res);
260 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
262 String idParam = req.getParameter("id");
263 if (idParam == null) throw new ServletModuleException("Invalid call: id not supplied ");
264 _showObject(idParam, req, res);
267 // methods for attaching media file
268 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
270 String mediaIdParam = req.getParameter("mid");
271 String idParam = req.getParameter("cid");
272 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
275 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
276 entContent.attach(mediaIdParam);
278 catch(ModuleException e) {
279 logger.error("smod content :: attach :: could not get entityContent");
281 catch(StorageObjectException e) {
282 logger.error("smod content :: attach :: could not get entityContent");
285 _showObject(idParam, req, res);
288 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
290 String cidParam = req.getParameter("cid");
291 String midParam = req.getParameter("mid");
292 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
293 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
296 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
297 entContent.dettach(cidParam,midParam);
299 catch(ModuleException e) {
300 logger.error("smod content :: dettach :: could not get entityContent");
302 catch(StorageObjectException e) {
303 logger.error("smod content :: dettach :: could not get entityContent");
306 _showObject(cidParam, req, res);
309 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
311 String idParam = req.getParameter("id");
312 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
314 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
315 entContent.newswire();
317 catch(ModuleException e) {
318 logger.error("smod content :: newswire :: could not get entityContent");
320 catch(StorageObjectException e) {
321 logger.error("smod content :: dettach :: could not get entityContent");
328 public void update(HttpServletRequest req, HttpServletResponse res)
329 throws ServletModuleException
333 EntityUsers user = _getUser(req);
334 if (user==null) logger.debug("user null!");
335 String idParam = req.getParameter("id");
336 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
338 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
339 String[] topic_id = req.getParameterValues("to_topic");
340 String content_id = req.getParameter("id");
342 if(user != null) withValues.put("user_id", user.getId());
343 withValues.put("is_produced", "0");
344 if (!withValues.containsKey("is_published"))
345 withValues.put("is_published","0");
346 if (!withValues.containsKey("is_html"))
347 withValues.put("is_html","0");
349 String id = mainModule.set(withValues);
350 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
352 String whereParam = req.getParameter("where");
353 String orderParam = req.getParameter("order");
355 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
359 _showObject(idParam, req, res);
361 catch (StorageObjectException e) {
362 throw new ServletModuleException(e.toString());
364 catch (ModuleException e) {
365 throw new ServletModuleException(e.toString());
370 * HelperMethod shows the basic article editing form.
372 * if the "id" parameter is null, it means show an empty form to add a new
375 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
376 throws ServletModuleException {
378 SimpleHash extraInfo = new SimpleHash();
380 TemplateModelRoot entContent;
383 entContent = (TemplateModelRoot)mainModule.getById(id);
386 SimpleHash withValues = new SimpleHash();
387 withValues.put("new", "1");
388 withValues.put("is_published", "0");
389 String now = StringUtil.date2webdbDate(new GregorianCalendar());
390 withValues.put("date", new SimpleScalar(now));
391 EntityUsers user = _getUser(req);
392 withValues.put("login_user", user);
393 entContent = withValues;
396 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
397 extraInfo.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
398 extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
400 // code to be able to return to the list:
401 String offsetParam, whereParam, orderParam;
402 if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
403 if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
404 if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
406 extraInfo.put("login_user", _getUser(req));
407 deliver(req, res, entContent, extraInfo, templateObjektString);
409 catch (Exception e) {
410 throw new ServletModuleException(e.toString());
414 public void returnArticleList(HttpServletRequest aRequest, HttpServletResponse aResponse,
415 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleException {
416 // ML: experiment in using the producer's generation system instead of the
419 EntityAdapterModel model;
420 int nrArticlesPerPage = 20;
424 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
425 model = MirGlobal.localizer().dataModel().adapterModel();
428 new CachingRewindableIterator(
429 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
430 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
433 responseData.put("nexturl", null);
434 responseData.put("prevurl", null);
436 count=mainModule.getSize(aWhereClause);
438 if (count>=anOffset+nrArticlesPerPage) {
439 responseData.put("nexturl" ,
440 "module=Content&do=list&where=" + HTMLRoutines.encodeURL(aWhereClause) +
441 "&order=" + HTMLRoutines.encodeURL(anOrderByClause) +
442 "&offset=" + (anOffset + nrArticlesPerPage));
445 responseData.put("prevurl" ,
446 "module=Content&do=list&where=" + HTMLRoutines.encodeURL(aWhereClause) +
447 "&order=" + HTMLRoutines.encodeURL(anOrderByClause) +
448 "&offset=" + Math.max(anOffset - nrArticlesPerPage, 0));
451 responseData.put("articles", contentList);
453 responseData.put("thisurl" ,
454 "module=Content&do=list&where=" + HTMLRoutines.encodeURL(aWhereClause) +
455 "&order=" + HTMLRoutines.encodeURL(anOrderByClause) +
456 "&offset=" + anOffset);
458 responseData.put("from" , Integer.toString(anOffset+1));
459 responseData.put("count", Integer.toString(count));
460 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
461 responseData.put("offset" , Integer.toString(anOffset));
462 responseData.put("order", anOrderByClause);
463 responseData.put("where" , aWhereClause);
465 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "contentlist.template");
467 catch (Throwable e) {
468 throw new ServletModuleException(e.toString());
472 private EntityUsers _getUser(HttpServletRequest req)
474 HttpSession session=req.getSession(false);
475 return (EntityUsers)session.getAttribute("login.uid");