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;
43 import freemarker.template.*;
52 import mir.entity.adapter.*;
55 import mircoders.global.*;
56 import mircoders.storage.*;
57 import mircoders.module.*;
58 import mircoders.entity.*;
59 import mircoders.localizer.*;
62 * ServletModuleContent -
63 * deliver html for the article admin form.
65 * @version $Id: ServletModuleContent.java,v 1.25 2002/12/01 22:13:37 zapata Exp $
66 * @author rk, mir-coders
70 public class ServletModuleContent extends ServletModule
72 static ModuleTopics themenModule;
73 static ModuleSchwerpunkt schwerpunktModule;
74 static ModuleImages imageModule;
76 static String templateOpString;
78 // Singelton / Kontruktor
80 private static ServletModuleContent instance = new ServletModuleContent();
81 public static ServletModule getInstance() { return instance; }
83 private ServletModuleContent() {
84 logger = new LoggerWrapper("ServletModule.Content");
86 templateListString = MirConfig.getProp("ServletModule.Content.ListTemplate");
87 //templateOpString = MirConfig.getProp("ServletModule.Content.OpTemplate");
88 templateObjektString = MirConfig.getProp("ServletModule.Content.ObjektTemplate");
89 templateConfirmString = MirConfig.getProp("ServletModule.Content.ConfirmTemplate");
90 mainModule = new ModuleContent(DatabaseContent.getInstance());
91 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
92 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
93 imageModule = new ModuleImages(DatabaseImages.getInstance());
95 catch (StorageObjectException e) {
96 logger.error("servletmodulecontent konnte nicht initialisiert werden");
102 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
104 EntityUsers user = _getUser(req);
105 String offsetParam = req.getParameter("offset");
106 String whereParam = req.getParameter("where");
107 String orderParam = req.getParameter("order");
111 if (offsetParam != null && !offsetParam.equals(""))
112 offset = Integer.parseInt(offsetParam);
114 if (req.getParameter("next") != null)
115 offset=Integer.parseInt(req.getParameter("nextoffset"));
117 if (req.getParameter("prev") != null)
118 offset = Integer.parseInt(req.getParameter("prevoffset"));
120 returnArticleList(req, res, whereParam, orderParam, offset);
123 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
125 EntityUsers user = _getUser(req);
126 String offsetParam = req.getParameter("offset");
129 String whereParam = req.getParameter("where");
131 if (whereParam==null) whereParam = "to_article_type='0'";
133 // hier offsetcode bearbeiteb
134 if (offsetParam != null && !offsetParam.equals(""))
135 offset = Integer.parseInt(offsetParam);
137 if (req.getParameter("next") != null)
138 offset=Integer.parseInt(req.getParameter("nextoffset"));
140 if (req.getParameter("prev") != null)
141 offset = Integer.parseInt(req.getParameter("prevoffset"));
143 String orderParam = req.getParameter("order");
145 returnArticleList(req, res, whereParam, orderParam, offset);
149 public void search(HttpServletRequest req, HttpServletResponse res)
150 throws ServletModuleException {
152 EntityUsers user = _getUser(req);
154 String fieldParam = req.getParameter("field");
155 String fieldValueParam = req.getParameter("fieldvalue");
156 String orderParam = req.getParameter("order");
158 theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
159 returnArticleList(req, res, "lower("+ fieldParam + ") like lower('%" + StringUtil.quote(fieldValueParam) + "%')", orderParam, 0);
160 } catch (ModuleException e) {
161 throw new ServletModuleException(e.toString());
165 public void add(HttpServletRequest req, HttpServletResponse res)
166 throws ServletModuleException {
167 _showObject(null, req, res);
171 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
173 //theLog.printDebugInfo(":: content :: trying to insert");
175 EntityUsers user = _getUser(req);
176 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
178 String now = StringUtil.date2webdbDate(new GregorianCalendar());
179 withValues.put("date", now);
180 withValues.put("publish_path", StringUtil.webdbDate2path(now));
181 withValues.put("to_publisher", user.getId());
182 withValues.put("is_produced", "0");
183 if (!withValues.containsKey("is_published"))
184 withValues.put("is_published","0");
185 if (!withValues.containsKey("is_html"))
186 withValues.put("is_html","0");
188 String id = mainModule.add(withValues);
189 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
191 _showObject(id, req, res);
193 catch (StorageObjectException e) {
194 throw new ServletModuleException(e.toString());
196 catch (ModuleException e) {
197 throw new ServletModuleException(e.toString());
201 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
203 EntityUsers user = _getUser(req);
205 String idParam = req.getParameter("id");
206 if (idParam == null) throw new ServletModuleException("Invalid call: id missing");
208 String confirmParam = req.getParameter("confirm");
209 String cancelParam = req.getParameter("cancel");
211 logger.info("where = " + req.getParameter("where"));
213 if (confirmParam == null && cancelParam == null) {
215 SimpleHash mergeData = new SimpleHash();
216 mergeData.put("module", "Content");
217 mergeData.put("infoString", "Content: " + idParam);
218 mergeData.put("id", idParam);
219 mergeData.put("where", req.getParameter("where"));
220 mergeData.put("order", req.getParameter("order"));
221 mergeData.put("offset", req.getParameter("offset"));
222 deliver(req, res, mergeData, templateConfirmString);
225 if (confirmParam!= null && !confirmParam.equals("")) {
227 mainModule.deleteById(idParam);
229 /** @todo the following two should be implied in
232 //delete rows in the content_x_topic-table
233 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
234 //delete rows in the comment-table
235 DatabaseComment.getInstance().deleteByContentId(idParam);
236 } catch (ModuleException e) {
237 throw new ServletModuleException(e.toString());
238 } catch (StorageObjectException e) {
239 throw new ServletModuleException(e.toString());
244 // Datensatz anzeigen
245 _showObject(idParam, req, res);
250 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
252 String idParam = req.getParameter("id");
253 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
254 _showObject(idParam, req, res);
257 // methods for attaching media file
258 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
260 String mediaIdParam = req.getParameter("mid");
261 String idParam = req.getParameter("cid");
262 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
265 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
266 entContent.attach(mediaIdParam);
268 catch(ModuleException e) {
269 logger.error("smod content :: attach :: could not get entityContent");
271 catch(StorageObjectException e) {
272 logger.error("smod content :: attach :: could not get entityContent");
275 _showObject(idParam, req, res);
278 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
280 String cidParam = req.getParameter("cid");
281 String midParam = req.getParameter("mid");
282 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
283 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
286 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
287 entContent.dettach(cidParam,midParam);
289 catch(ModuleException e) {
290 logger.error("smod content :: dettach :: could not get entityContent");
292 catch(StorageObjectException e) {
293 logger.error("smod content :: dettach :: could not get entityContent");
296 _showObject(cidParam, req, res);
299 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
301 String idParam = req.getParameter("id");
302 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
304 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
305 entContent.newswire();
307 catch(ModuleException e) {
308 logger.error("smod content :: newswire :: could not get entityContent");
310 catch(StorageObjectException e) {
311 logger.error("smod content :: dettach :: could not get entityContent");
318 public void update(HttpServletRequest req, HttpServletResponse res)
319 throws ServletModuleException
323 EntityUsers user = _getUser(req);
324 if (user==null) logger.debug("user null!");
325 String idParam = req.getParameter("id");
326 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
328 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
329 String[] topic_id = req.getParameterValues("to_topic");
330 String content_id = req.getParameter("id");
331 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
332 if(user != null) withValues.put("user_id", user.getId());
333 withValues.put("is_produced", "0");
334 if (!withValues.containsKey("is_published"))
335 withValues.put("is_published","0");
336 if (!withValues.containsKey("is_html"))
337 withValues.put("is_html","0");
339 // ML: this is not multi-language friendly and this can be done in a template
340 // if (withValues.get("creator").toString().equals(""))
341 // withValues.put("creator","Anonym");
343 //theLog.printDebugInfo("updating. ");
344 String id = mainModule.set(withValues);
345 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
346 //theLog.printDebugInfo("update done. ");
347 String whereParam = req.getParameter("where");
348 String orderParam = req.getParameter("order");
349 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
350 //theLog.printDebugInfo("update to list");
354 _showObject(idParam, req, res);
356 catch (StorageObjectException e) {
357 throw new ServletModuleException(e.toString());
359 catch (ModuleException e) {
360 throw new ServletModuleException(e.toString());
365 * HelperMethod shows the basic article editing form.
367 * if the "id" parameter is null, it means show an empty form to add a new
370 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
371 throws ServletModuleException {
373 SimpleHash extraInfo = new SimpleHash();
375 TemplateModelRoot entContent;
377 entContent = (TemplateModelRoot)mainModule.getById(id);
379 SimpleHash withValues = new SimpleHash();
380 withValues.put("new", "1");
381 withValues.put("is_published", "0");
382 String now = StringUtil.date2webdbDate(new GregorianCalendar());
383 withValues.put("date", new SimpleScalar(now));
384 EntityUsers user = _getUser(req);
385 withValues.put("login_user", user);
386 entContent = withValues;
389 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
391 extraInfo.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
393 catch (Exception e) {
394 logger.error("articletype could not be fetched.");
397 extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
399 catch (Exception e) {
400 logger.error("language-popup could not be fetched.");
403 extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
405 // code to be able to return to the list:
406 String offsetParam, whereParam, orderParam;
407 if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
408 if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
409 if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
411 extraInfo.put("login_user", _getUser(req));
412 deliver(req, res, entContent, extraInfo, templateObjektString);
414 catch (Exception e) {
415 throw new ServletModuleException(e.toString());
419 public void returnArticleList(HttpServletRequest aRequest, HttpServletResponse aResponse,
420 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleException {
421 // ML: experiment in using the producer's generation system instead of the
424 EntityAdapterModel model;
425 int nrArticlesPerPage = 20;
429 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
430 model = MirGlobal.localizer().dataModel().adapterModel();
433 new CachingRewindableIterator(
434 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrArticlesPerPage,
435 MirGlobal.localizer().dataModel().adapterModel(), "content", nrArticlesPerPage, anOffset)
438 responseData.put("nexturl", null);
439 responseData.put("prevurl", null);
441 count=mainModule.getSize(aWhereClause);
443 if (count>=anOffset+nrArticlesPerPage) {
444 responseData.put("nexturl" ,
445 "module=Content&do=list&where=" + HTMLRoutines.encodeURL(aWhereClause) +
446 "&order=" + HTMLRoutines.encodeURL(anOrderByClause) +
447 "&offset=" + anOffset + nrArticlesPerPage);
450 responseData.put("prevurl" ,
451 "module=Content&do=list&where=" + HTMLRoutines.encodeURL(aWhereClause) +
452 "&order=" + HTMLRoutines.encodeURL(anOrderByClause) +
453 "&offset=" + Math.max(anOffset - nrArticlesPerPage, 0));
456 responseData.put("articles", contentList);
458 responseData.put("thisurl" ,
459 "module=Content&do=list&where=" + HTMLRoutines.encodeURL(aWhereClause) +
460 "&order=" + HTMLRoutines.encodeURL(anOrderByClause) +
461 "&offset=" + anOffset);
463 responseData.put("from" , Integer.toString(anOffset+1));
464 responseData.put("count", Integer.toString(count));
465 responseData.put("to", Integer.toString(Math.min(anOffset+nrArticlesPerPage, count)));
466 responseData.put("offset" , Integer.toString(anOffset));
467 responseData.put("order", anOrderByClause);
468 responseData.put("where" , aWhereClause);
470 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "contentlist.template");
472 catch (Throwable e) {
473 throw new ServletModuleException(e.toString());
477 private EntityUsers _getUser(HttpServletRequest req)
479 HttpSession session=req.getSession(false);
480 return (EntityUsers)session.getAttribute("login.uid");