1 package mircoders.servlet;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
11 import freemarker.template.*;
20 import mircoders.storage.*;
21 import mircoders.module.*;
22 import mircoders.entity.*;
26 * ServletModuleContent -
27 * liefert HTML fuer Content
33 public class ServletModuleContent extends ServletModule
36 static ModuleTopics themenModule;
37 static ModuleSchwerpunkt schwerpunktModule;
38 static ModuleImages imageModule;
40 static String templateOpString;
42 // Singelton / Kontruktor
44 private static ServletModuleContent instance = new ServletModuleContent();
45 public static ServletModule getInstance() { return instance; }
47 private ServletModuleContent() {
49 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Content.Logfile"));
50 templateListString = MirConfig.getProp("ServletModule.Content.ListTemplate");
51 //templateOpString = MirConfig.getProp("ServletModule.Content.OpTemplate");
52 templateObjektString = MirConfig.getProp("ServletModule.Content.ObjektTemplate");
53 templateConfirmString = MirConfig.getProp("ServletModule.Content.ConfirmTemplate");
54 mainModule = new ModuleContent(DatabaseContent.getInstance());
55 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
56 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
57 imageModule = new ModuleImages(DatabaseImages.getInstance());
58 } catch (StorageObjectException e) {
59 theLog.printDebugInfo("servletmodulecontent konnte nicht initialisiert werden");
65 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
68 EntityUsers user = _getUser(req);
70 String offsetParam = req.getParameter("offset");
73 // hier offsetcode bearbeiteb
74 if (offsetParam != null && !offsetParam.equals(""))
75 offset = Integer.parseInt(offsetParam);
77 if (req.getParameter("next") != null)
78 offset=Integer.parseInt(req.getParameter("nextoffset"));
80 if (req.getParameter("prev") != null)
81 offset = Integer.parseInt(req.getParameter("prevoffset"));
83 String whereParam = req.getParameter("where");
84 String orderParam = req.getParameter("order");
86 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
87 _list(theList, req, res);
88 } catch (ModuleException e) {
89 throw new ServletModuleException(e.toString());
93 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
96 EntityUsers user = _getUser(req);
98 String offsetParam = req.getParameter("offset");
101 String whereParam = req.getParameter("where");
103 if (whereParam==null) whereParam = "to_article_type='0'";
105 // hier offsetcode bearbeiteb
106 if (offsetParam != null && !offsetParam.equals(""))
107 offset = Integer.parseInt(offsetParam);
109 if (req.getParameter("next") != null)
110 offset=Integer.parseInt(req.getParameter("nextoffset"));
112 if (req.getParameter("prev") != null)
113 offset = Integer.parseInt(req.getParameter("prevoffset"));
115 String orderParam = req.getParameter("order");
117 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
118 _list(theList, req, res);
119 } catch (ModuleException e) {
120 throw new ServletModuleException(e.toString());
125 public void search(HttpServletRequest req, HttpServletResponse res)
126 throws ServletModuleException {
128 EntityUsers user = _getUser(req);
130 String fieldParam = req.getParameter("field");
131 String fieldValueParam = req.getParameter("fieldvalue");
132 String orderParam = req.getParameter("order");
134 theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
135 _list(theList, req, res);
136 } catch (ModuleException e) {
137 throw new ServletModuleException(e.toString());
141 public void add(HttpServletRequest req, HttpServletResponse res)
142 throws ServletModuleException {
144 EntityUsers user = _getUser(req);
145 SimpleHash mergeData = new SimpleHash();
146 SimpleHash extraInfo = new SimpleHash();
148 mergeData.put("new", "1");
149 mergeData.put("is_published", "1");
150 String now = StringUtil.date2webdbDate(new GregorianCalendar());
151 mergeData.put("date", new SimpleScalar(now));
153 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
154 } catch (ModuleException e) {
155 theLog.printError("themenPopupData could not be fetched.");
158 extraInfo.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
159 } catch (Exception e) {
160 theLog.printError("articletype could not be fetched.");
163 extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
164 } catch (Exception e) {
165 theLog.printError("language-popup could not be fetched.");
168 extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
169 } catch (ModuleException e) {
170 theLog.printError("schwerpunktPopupData could not be fetched.");
172 extraInfo.put("login_user", user);
173 deliver(req, res, mergeData, extraInfo, templateObjektString);
177 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
179 //theLog.printDebugInfo(":: content :: trying to insert");
181 EntityUsers user = _getUser(req);
182 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
183 //theLog.printDebugInfo(":: content :: got intersecting values");
184 String now = StringUtil.date2webdbDate(new GregorianCalendar());
185 withValues.put("date", now);
186 withValues.put("publish_path", StringUtil.webdbDate2path(now));
187 withValues.put("to_publisher", user.getId());
188 withValues.put("is_produced", "0");
189 if (!withValues.containsKey("is_published"))
190 withValues.put("is_published","0");
191 if (!withValues.containsKey("is_html"))
192 withValues.put("is_html","0");
194 // ML: this is not multi-language friendly and this can be done in a template
195 // if (withValues.get("creator").toString().equals(""))
196 // withValues.put("creator","Anonym");
199 String id = mainModule.add(withValues);
200 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
201 //theLog.printDebugInfo(":: content :: inserted");
202 _showObject(id, req, res);
204 catch (StorageObjectException e) {
205 throw new ServletModuleException(e.toString());
207 catch (ModuleException e) {
208 throw new ServletModuleException(e.toString());
212 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
215 EntityUsers user = _getUser(req);
216 // hier pruefen ob dem akt. user loeschen erlaubt ist...
217 String idParam = req.getParameter("id");
218 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
220 String confirmParam = req.getParameter("confirm");
221 String cancelParam = req.getParameter("cancel");
223 if (confirmParam == null && cancelParam == null) {
224 // HTML Ausgabe zum Confirmen!
225 SimpleHash mergeData = new SimpleHash();
226 mergeData.put("module", "Content");
227 mergeData.put("infoString", "Content: " + idParam);
228 mergeData.put("id", idParam);
229 mergeData.put("where", req.getParameter("where"));
230 mergeData.put("order", req.getParameter("order"));
231 mergeData.put("offset", req.getParameter("offset"));
232 deliver(req, res, mergeData, templateConfirmString);
235 if (confirmParam!= null && !confirmParam.equals("")) {
237 mainModule.deleteById(idParam);
239 /** @todo the following two should be imlied in
242 //delete rows in the content_x_topic-table
243 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
244 //delete rows in the comment-table
245 DatabaseComment.getInstance().deleteByContentId(idParam);
246 } catch (ModuleException e) {
247 throw new ServletModuleException(e.toString());
248 } catch (StorageObjectException 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("Falscher Aufruf: (id) nicht angegeben");
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 theLog.printError("smod content :: attach :: could not get entityContent");
281 catch(StorageObjectException e) {
282 theLog.printError("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 theLog.printError("smod content :: dettach :: could not get entityContent");
302 catch(StorageObjectException e) {
303 theLog.printError("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 theLog.printError("smod content :: newswire :: could not get entityContent");
320 catch(StorageObjectException e) {
321 theLog.printError("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) theLog.printDebugInfo("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");
341 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
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 // ML: this is not multi-language friendly and this can be done in a template
350 // if (withValues.get("creator").toString().equals(""))
351 // withValues.put("creator","Anonym");
353 //theLog.printDebugInfo("updating. ");
354 String id = mainModule.set(withValues);
355 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
356 //theLog.printDebugInfo("update done. ");
357 String whereParam = req.getParameter("where");
358 String orderParam = req.getParameter("order");
359 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
360 //theLog.printDebugInfo("update to list");
364 _showObject(idParam, req, res);
366 catch (StorageObjectException e) {
367 throw new ServletModuleException(e.toString());
369 catch (ModuleException e) {
370 throw new ServletModuleException(e.toString());
377 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
378 throws ServletModuleException {
380 SimpleHash extraInfo = new SimpleHash();
382 EntityContent entContent=(EntityContent)mainModule.getById(id);
384 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
386 extraInfo.put("articletypePopupData",
387 DatabaseArticleType.getInstance().getPopupData());
388 } catch (Exception e) {
389 theLog.printError("articletype could not be fetched.");
392 extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
393 } catch (Exception e) {
394 theLog.printError("language-popup could not be fetched.");
397 extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
398 // hier code um zur liste zurueckzukommen
399 String offsetParam, whereParam, orderParam;
400 if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
401 if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
402 if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
403 extraInfo.put("login_user", _getUser(req));
404 deliver(req, res, entContent, extraInfo, templateObjektString);
405 } catch (Exception e) {
406 throw new ServletModuleException(e.toString());
411 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
412 throws ServletModuleException {
415 // hier dann htmlcode rausschreiben
416 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
417 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
418 modelRoot.put("themenHashData", themenModule.getHashData());
419 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
420 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
421 deliver(req, res, modelRoot, templateListString);
422 } else { // count = 1
423 _showObject(theList.elementAt(0).getId(),req,res);
425 } catch (StorageObjectException e) {
426 throw new ServletModuleException(e.toString());
430 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
431 throws ServletModuleException {
435 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
436 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
437 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
439 EntityContent currentContent;
440 EntityList upMediaEntityList;
441 EntityList imageEntityList;
442 EntityList currentMediaList;
444 EntityMedia uploadedMedia;
446 String imageRoot = MirConfig.getProp("Producer.ImageRoot");
448 SimpleHash contentHash;
449 Class mediaHandlerClass=null;
450 MirMedia mediaHandler=null;
451 String mediaHandlerName=null;
452 Database mediaStorage=null;
456 for (int i=0; i < theList.size();i++) {
457 currentContent = (EntityContent)theList.elementAt(i);
458 //fetching/setting the images
459 upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
460 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
464 mediaHandlerName = null;
465 for (int n=0; n < upMediaEntityList.size();n++) {
466 uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
467 mediaType = uploadedMedia.getMediaType();
469 //must of had a non-existant to_media_type entry..
470 //let's save our ass.
471 if (mediaType != null) {
473 mediaHandlerName = mediaType.getValue("classname");
474 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
475 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
476 } catch (Exception e) {
477 theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
480 //the "best" media type to show
481 if (mediaHandler.isVideo()) {
482 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
485 } else if (mediaHandler.isAudio()) {
486 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
488 } else if (tinyIcon == null && !mediaHandler.isImage()) {
489 tinyIcon = mediaHandler.getTinyIcon();
490 iconAlt = mediaHandler.getIconAlt();
494 //it only has image(s)
495 if (tinyIcon == null) {
496 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
500 // uploadedMedia Entity list is empty.
503 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
508 //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
509 contentHash = (SimpleHash)theList.get(i);
510 contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
511 contentHash.put("icon_alt", iconAlt);
512 } catch (Exception e){}
516 deliver(req, res, modelRoot, templateListString);
517 } else { // count = 1
518 _showObject(theList.elementAt(0).getId(), req, res);
520 } catch (StorageObjectException e) {
521 throw new ServletModuleException(e.toString());
525 private EntityUsers _getUser(HttpServletRequest req)
527 HttpSession session=req.getSession(false);
528 return (EntityUsers)session.getAttribute("login.uid");