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 mergeData.put("new", "1");
147 mergeData.put("is_published", "1");
148 String now = StringUtil.date2webdbDate(new GregorianCalendar());
149 mergeData.put("date", new SimpleScalar(now));
151 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
152 } catch (ModuleException e) {
153 theLog.printError("themenPopupData could not be fetched.");
156 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
157 } catch (Exception e) {
158 theLog.printError("articletype could not be fetched.");
161 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
162 } catch (Exception e) {
163 theLog.printError("language-popup could not be fetched.");
166 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
167 } catch (ModuleException e) {
168 theLog.printError("schwerpunktPopupData could not be fetched.");
170 mergeData.put("login_user", user);
171 deliver(req, res, mergeData, templateObjektString);
175 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
177 //theLog.printDebugInfo(":: content :: trying to insert");
179 EntityUsers user = _getUser(req);
180 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
181 //theLog.printDebugInfo(":: content :: got intersecting values");
182 String now = StringUtil.date2webdbDate(new GregorianCalendar());
183 withValues.put("date", now);
184 withValues.put("publish_path", StringUtil.webdbDate2path(now));
185 withValues.put("to_publisher", user.getId());
186 withValues.put("is_produced", "0");
187 if (!withValues.containsKey("is_published"))
188 withValues.put("is_published","0");
189 if (!withValues.containsKey("is_html"))
190 withValues.put("is_html","0");
191 if (withValues.get("creator").toString().equals(""))
192 withValues.put("creator","Anonym");
193 String id = mainModule.add(withValues);
194 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
195 //theLog.printDebugInfo(":: content :: inserted");
196 _showObject(id, req, res);
198 catch (StorageObjectException e) {
199 throw new ServletModuleException(e.toString());
201 catch (ModuleException e) {
202 throw new ServletModuleException(e.toString());
206 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
209 EntityUsers user = _getUser(req);
210 // hier pruefen ob dem akt. user loeschen erlaubt ist...
211 String idParam = req.getParameter("id");
212 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
214 String confirmParam = req.getParameter("confirm");
215 String cancelParam = req.getParameter("cancel");
217 if (confirmParam == null && cancelParam == null) {
218 // HTML Ausgabe zum Confirmen!
219 SimpleHash mergeData = new SimpleHash();
220 mergeData.put("module", "Content");
221 mergeData.put("infoString", "Content: " + idParam);
222 mergeData.put("id", idParam);
223 mergeData.put("where", req.getParameter("where"));
224 mergeData.put("order", req.getParameter("order"));
225 mergeData.put("offset", req.getParameter("offset"));
226 deliver(req, res, mergeData, templateConfirmString);
229 if (confirmParam!= null && !confirmParam.equals("")) {
231 mainModule.deleteById(idParam);
233 /** @todo the following two should be imlied in
236 //delete rows in the content_x_topic-table
237 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
238 //delete rows in the comment-table
239 DatabaseComment.getInstance().deleteByContentId(idParam);
240 } catch (ModuleException e) {
241 throw new ServletModuleException(e.toString());
242 } catch (StorageObjectException e) {
243 throw new ServletModuleException(e.toString());
248 // Datensatz anzeigen
249 _showObject(idParam, req, res);
254 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
256 String idParam = req.getParameter("id");
257 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
258 _showObject(idParam, req, res);
261 // methods for attaching media file
262 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
264 String mediaIdParam = req.getParameter("mid");
265 String idParam = req.getParameter("cid");
266 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
269 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
270 entContent.attach(mediaIdParam);
272 catch(ModuleException e) {
273 theLog.printError("smod content :: attach :: could not get entityContent");
275 catch(StorageObjectException e) {
276 theLog.printError("smod content :: attach :: could not get entityContent");
279 _showObject(idParam, req, res);
282 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
284 String cidParam = req.getParameter("cid");
285 String midParam = req.getParameter("mid");
286 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
287 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
290 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
291 entContent.dettach(cidParam,midParam);
293 catch(ModuleException e) {
294 theLog.printError("smod content :: dettach :: could not get entityContent");
296 catch(StorageObjectException e) {
297 theLog.printError("smod content :: dettach :: could not get entityContent");
300 _showObject(cidParam, req, res);
303 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
305 String idParam = req.getParameter("id");
306 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
308 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
309 entContent.newswire();
311 catch(ModuleException e) {
312 theLog.printError("smod content :: newswire :: could not get entityContent");
314 catch(StorageObjectException e) {
315 theLog.printError("smod content :: dettach :: could not get entityContent");
322 public void update(HttpServletRequest req, HttpServletResponse res)
323 throws ServletModuleException
327 EntityUsers user = _getUser(req);
328 if (user==null) theLog.printDebugInfo("user null!");
329 String idParam = req.getParameter("id");
330 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
332 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
333 String[] topic_id = req.getParameterValues("to_topic");
334 String content_id = req.getParameter("id");
335 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
336 if(user != null) withValues.put("user_id", user.getId());
337 withValues.put("is_produced", "0");
338 if (!withValues.containsKey("is_published"))
339 withValues.put("is_published","0");
340 if (!withValues.containsKey("is_html"))
341 withValues.put("is_html","0");
342 if (withValues.get("creator").toString().equals(""))
343 withValues.put("creator","Anonym");
344 //theLog.printDebugInfo("updating. ");
345 String id = mainModule.set(withValues);
346 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
347 //theLog.printDebugInfo("update done. ");
348 String whereParam = req.getParameter("where");
349 String orderParam = req.getParameter("order");
350 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
351 //theLog.printDebugInfo("update to list");
355 _showObject(idParam, req, res);
357 catch (StorageObjectException e) {
358 throw new ServletModuleException(e.toString());
360 catch (ModuleException e) {
361 throw new ServletModuleException(e.toString());
368 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
369 throws ServletModuleException {
371 SimpleHash extraInfo = new SimpleHash();
373 EntityContent entContent=(EntityContent)mainModule.getById(id);
375 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
377 extraInfo.put("articletypePopupData",
378 DatabaseArticleType.getInstance().getPopupData());
379 } catch (Exception e) {
380 theLog.printError("articletype could not be fetched.");
383 extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
384 } catch (Exception e) {
385 theLog.printError("language-popup could not be fetched.");
388 extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
389 // hier code um zur liste zurueckzukommen
390 String offsetParam, whereParam, orderParam;
391 if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
392 if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
393 if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
394 extraInfo.put("login_user", _getUser(req));
395 deliver(req, res, entContent, extraInfo, templateObjektString);
396 } catch (Exception e) {
397 throw new ServletModuleException(e.toString());
402 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
403 throws ServletModuleException {
406 // hier dann htmlcode rausschreiben
407 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
408 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
409 modelRoot.put("themenHashData", themenModule.getHashData());
410 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
411 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
412 deliver(req, res, modelRoot, templateListString);
413 } else { // count = 1
414 _showObject(theList.elementAt(0).getId(),req,res);
416 } catch (StorageObjectException e) {
417 throw new ServletModuleException(e.toString());
421 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
422 throws ServletModuleException {
426 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
427 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
428 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
430 EntityContent currentContent;
431 EntityList upMediaEntityList;
432 EntityList imageEntityList;
433 EntityList currentMediaList;
435 EntityMedia uploadedMedia;
437 String imageRoot = MirConfig.getProp("Producer.ImageRoot");
439 SimpleHash contentHash;
440 Class mediaHandlerClass=null;
441 MirMedia mediaHandler=null;
442 String mediaHandlerName=null;
443 Database mediaStorage=null;
447 for (int i=0; i < theList.size();i++) {
448 currentContent = (EntityContent)theList.elementAt(i);
449 //fetching/setting the images
450 upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
451 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
455 mediaHandlerName = null;
456 for (int n=0; n < upMediaEntityList.size();n++) {
457 uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
458 mediaType = uploadedMedia.getMediaType();
460 //must of had a non-existant to_media_type entry..
461 //let's save our ass.
462 if (mediaType != null) {
464 mediaHandlerName = mediaType.getValue("classname");
465 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
466 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
467 } catch (Exception e) {
468 theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
471 //the "best" media type to show
472 if (mediaHandler.isVideo()) {
473 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
476 } else if (mediaHandler.isAudio()) {
477 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
479 } else if (tinyIcon == null && !mediaHandler.isImage()) {
480 tinyIcon = mediaHandler.getTinyIcon();
481 iconAlt = mediaHandler.getIconAlt();
485 //it only has image(s)
486 if (tinyIcon == null) {
487 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
491 // uploadedMedia Entity list is empty.
494 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
499 //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
500 contentHash = (SimpleHash)theList.get(i);
501 contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
502 contentHash.put("icon_alt", iconAlt);
503 } catch (Exception e){}
507 deliver(req, res, modelRoot, templateListString);
508 } else { // count = 1
509 _showObject(theList.elementAt(0).getId(), req, res);
511 } catch (StorageObjectException e) {
512 throw new ServletModuleException(e.toString());
516 private EntityUsers _getUser(HttpServletRequest req)
518 HttpSession session=req.getSession(false);
519 return (EntityUsers)session.getAttribute("login.uid");