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(this.getClass().getName());
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));
150 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
152 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
153 } catch (Exception e) {
154 theLog.printError("articletype could not be fetched.");
157 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
158 } catch (Exception e) {
159 theLog.printError("language-popup could not be fetched.");
161 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
162 mergeData.put("login_user", user);
163 deliver(req, res, mergeData, templateObjektString);
167 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
169 //theLog.printDebugInfo(":: content :: trying to insert");
171 EntityUsers user = _getUser(req);
172 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
173 //theLog.printDebugInfo(":: content :: got intersecting values");
174 String now = StringUtil.date2webdbDate(new GregorianCalendar());
175 withValues.put("date", now);
176 withValues.put("publish_path", StringUtil.webdbDate2path(now));
177 withValues.put("to_publisher", user.getId());
178 withValues.put("is_produced", "0");
179 if (!withValues.containsKey("is_published"))
180 withValues.put("is_published","0");
181 if (!withValues.containsKey("is_html"))
182 withValues.put("is_html","0");
183 if (withValues.get("creator").toString().equals(""))
184 withValues.put("creator","Anonym");
185 String id = mainModule.add(withValues);
186 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
187 //theLog.printDebugInfo(":: content :: inserted");
188 _showObject(id, req, res);
190 catch (StorageObjectException e) {
191 throw new ServletModuleException(e.toString());
193 catch (ModuleException e) {
194 throw new ServletModuleException(e.toString());
198 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
201 EntityUsers user = _getUser(req);
202 // hier pruefen ob dem akt. user loeschen erlaubt ist...
203 String idParam = req.getParameter("id");
204 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
206 String confirmParam = req.getParameter("confirm");
207 String cancelParam = req.getParameter("cancel");
209 if (confirmParam == null && cancelParam == null) {
210 // HTML Ausgabe zum Confirmen!
211 SimpleHash mergeData = new SimpleHash();
212 mergeData.put("module", "Content");
213 mergeData.put("infoString", "Content: " + idParam);
214 mergeData.put("id", idParam);
215 mergeData.put("where", req.getParameter("where"));
216 mergeData.put("order", req.getParameter("order"));
217 mergeData.put("offset", req.getParameter("offset"));
218 deliver(req, res, mergeData, templateConfirmString);
221 if (confirmParam!= null && !confirmParam.equals("")) {
223 mainModule.deleteById(idParam);
225 /** @todo the following two should be imlied in
228 //delete rows in the content_x_topic-table
229 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
230 //delete rows in the comment-table
231 DatabaseComment.getInstance().deleteByContentId(idParam);
232 } catch (ModuleException e) {
233 throw new ServletModuleException(e.toString());
234 } catch (StorageObjectException e) {
235 throw new ServletModuleException(e.toString());
240 // Datensatz anzeigen
241 _showObject(idParam, req, res);
246 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
248 String idParam = req.getParameter("id");
249 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
250 _showObject(idParam, req, res);
253 // methods for attaching media file
254 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
256 String mediaIdParam = req.getParameter("mid");
257 String idParam = req.getParameter("cid");
258 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
261 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
262 entContent.attach(mediaIdParam);
264 catch(ModuleException e) {
265 theLog.printError("smod content :: attach :: could not get entityContent");
267 _showObject(idParam, req, res);
270 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
272 String cidParam = req.getParameter("cid");
273 String midParam = req.getParameter("mid");
274 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
275 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
278 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
279 entContent.dettach(cidParam,midParam);
281 catch(ModuleException e) {
282 theLog.printError("smod content :: dettach :: could not get entityContent");
284 _showObject(cidParam, req, res);
287 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
289 String idParam = req.getParameter("id");
290 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
292 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
293 entContent.newswire();
295 catch(ModuleException e) {
296 theLog.printError("smod content :: newswire :: could not get entityContent");
302 public void update(HttpServletRequest req, HttpServletResponse res)
303 throws ServletModuleException
307 EntityUsers user = _getUser(req);
308 if (user==null) theLog.printDebugInfo("user null!");
309 String idParam = req.getParameter("id");
310 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
312 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
313 String[] topic_id = req.getParameterValues("to_topic");
314 String content_id = req.getParameter("id");
315 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
316 if(user != null) withValues.put("user_id", user.getId());
317 withValues.put("is_produced", "0");
318 if (!withValues.containsKey("is_published"))
319 withValues.put("is_published","0");
320 if (!withValues.containsKey("is_html"))
321 withValues.put("is_html","0");
322 if (withValues.get("creator").toString().equals(""))
323 withValues.put("creator","Anonym");
324 //theLog.printDebugInfo("updating. ");
325 String id = mainModule.set(withValues);
326 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
327 //theLog.printDebugInfo("update done. ");
328 String whereParam = req.getParameter("where");
329 String orderParam = req.getParameter("order");
330 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
331 //theLog.printDebugInfo("update to list");
335 _showObject(idParam, req, res);
337 catch (StorageObjectException e) {
338 throw new ServletModuleException(e.toString());
340 catch (ModuleException e) {
341 throw new ServletModuleException(e.toString());
348 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
349 throws ServletModuleException {
352 EntityContent entContent=(EntityContent)mainModule.getById(id);
353 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
354 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
355 if (topicToContent!=null && topicToContent.size()>0){
356 theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
357 Entity topics = null;
358 SimpleList topicList = new SimpleList();
359 for(int i=0;i<topicToContent.size();i++){
360 topics = (EntityTopics)topicToContent.elementAt(i);
361 topicList.add(topics.getId());
363 mergeData.put("to_topic",topicList);
365 //obsolete, because of psqgl 7.1.x
366 //mergeData.put("content_data", entContent.getContentData());
367 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
369 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
370 } catch (Exception e) {
371 theLog.printError("articletype could not be fetched.");
374 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
375 } catch (Exception e) {
376 theLog.printError("language-popup could not be fetched.");
379 EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(entContent);
380 if (currentMedia!=null && currentMedia.getCount()>=1) {
381 SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
382 mergeData.put("to_media", mediaList);
386 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
387 // hier code um zur liste zurueckzukommen
388 String offsetParam, whereParam, orderParam;
389 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
390 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
391 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
392 mergeData.put("login_user", _getUser(req));
393 deliver(req, res, mergeData, templateObjektString);
394 } catch (Exception e) {
395 throw new ServletModuleException(e.toString());
400 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
401 throws ServletModuleException {
404 // hier dann htmlcode rausschreiben
405 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
406 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
407 modelRoot.put("themenHashData", themenModule.getHashData());
408 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
409 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
410 deliver(req, res, modelRoot, templateListString);
411 } else { // count = 1
412 _showObject(theList.elementAt(0).getId(),req,res);
414 } catch (StorageObjectException e) {
415 throw new ServletModuleException(e.toString());
419 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
420 throws ServletModuleException {
424 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
425 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
426 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
428 EntityContent currentContent;
429 EntityList upMediaEntityList;
430 EntityList imageEntityList;
431 EntityList currentMediaList;
433 EntityMedia uploadedMedia;
435 String imageRoot = MirConfig.getProp("Producer.ImageRoot");
437 SimpleHash contentHash;
438 Class mediaHandlerClass=null;
439 MirMedia mediaHandler=null;
440 String mediaHandlerName=null;
441 Database mediaStorage=null;
445 opList = HTMLTemplateProcessor.makeSimpleList(theList);
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 * grrr. why doesn't getId return an int! if It
465 * did I could just compare the value of getId and
466 * pick the biggest one. or is there
467 * another way around this that I am missing?
468 * can we make getIdasInt() or can we just have
469 * another getId() that returns an Int and the VM
470 * will handle it transparantly? -mh
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)opList.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");