get rid of deprecated makeSimpleList and makeSimpleHash usage. this should be it...
[mir.git] / source / mircoders / servlet / ServletModuleContent.java
1 package mircoders.servlet;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import java.net.*;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9
10
11 import freemarker.template.*;
12
13 import mir.servlet.*;
14 import mir.media.*;
15 import mir.module.*;
16 import mir.misc.*;
17 import mir.storage.*;
18 import mir.entity.*;
19
20 import mircoders.storage.*;
21 import mircoders.module.*;
22 import mircoders.entity.*;
23
24
25 /*
26  *  ServletModuleContent -
27  *  liefert HTML fuer Content
28  *
29  *
30  * @author RK
31  */
32
33 public class ServletModuleContent extends ServletModule
34 {
35
36   static ModuleTopics         themenModule;
37   static ModuleSchwerpunkt    schwerpunktModule;
38   static ModuleImages         imageModule;
39
40   static String templateOpString;
41
42   // Singelton / Kontruktor
43
44   private static ServletModuleContent instance = new ServletModuleContent();
45   public static ServletModule getInstance() { return instance; }
46
47   private ServletModuleContent() {
48     try {
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");
60     }
61   }
62
63   // Methoden
64
65   public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
66   {
67     try {
68       EntityUsers user = _getUser(req);
69       EntityList   theList;
70       String       offsetParam = req.getParameter("offset");
71       int          offset =0;
72
73       // hier offsetcode bearbeiteb
74       if (offsetParam != null && !offsetParam.equals(""))
75           offset = Integer.parseInt(offsetParam);
76
77       if (req.getParameter("next") != null)
78           offset=Integer.parseInt(req.getParameter("nextoffset"));
79       else
80           if (req.getParameter("prev") != null)
81             offset = Integer.parseInt(req.getParameter("prevoffset"));
82
83       String        whereParam = req.getParameter("where");
84       String        orderParam = req.getParameter("order");
85
86       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
87       _list(theList, req, res);
88     } catch (ModuleException e) {
89       throw new ServletModuleException(e.toString());
90     }
91   }
92
93   public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
94   {
95     try {
96       EntityUsers user = _getUser(req);
97       EntityList   theList;
98       String       offsetParam = req.getParameter("offset");
99       int          offset =0;
100
101       String whereParam = req.getParameter("where");
102
103       if (whereParam==null) whereParam = "to_article_type='0'";
104
105       // hier offsetcode bearbeiteb
106       if (offsetParam != null && !offsetParam.equals(""))
107           offset = Integer.parseInt(offsetParam);
108
109       if (req.getParameter("next") != null)
110           offset=Integer.parseInt(req.getParameter("nextoffset"));
111       else
112           if (req.getParameter("prev") != null)
113             offset = Integer.parseInt(req.getParameter("prevoffset"));
114
115       String orderParam = req.getParameter("order");
116
117       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
118       _list(theList, req, res);
119     } catch (ModuleException e) {
120       throw new ServletModuleException(e.toString());
121     }
122   }
123
124
125   public void search(HttpServletRequest req, HttpServletResponse res)
126     throws ServletModuleException {
127     try {
128       EntityUsers   user = _getUser(req);
129       EntityList    theList;
130       String        fieldParam = req.getParameter("field");
131       String        fieldValueParam = req.getParameter("fieldvalue");
132       String        orderParam = req.getParameter("order");
133
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());
138     }
139   }
140
141   public void add(HttpServletRequest req, HttpServletResponse res)
142     throws ServletModuleException {
143
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     try {
151     mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
152     } catch (ModuleException e) {
153       theLog.printError("themenPopupData could not be fetched.");
154     }
155     try {
156       mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
157     } catch (Exception e) {
158       theLog.printError("articletype could not be fetched.");
159     }
160     try {
161       mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
162     } catch (Exception e) {
163       theLog.printError("language-popup could not be fetched.");
164     }
165     try {
166     mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
167     } catch (ModuleException e) {
168       theLog.printError("schwerpunktPopupData could not be fetched.");
169     }
170     mergeData.put("login_user", user);
171     deliver(req, res, mergeData, templateObjektString);
172   }
173
174
175   public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
176   {
177     //theLog.printDebugInfo(":: content :: trying to insert");
178     try {
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);
197     }
198     catch (StorageObjectException e) {
199       throw new ServletModuleException(e.toString());
200     }
201     catch (ModuleException e) {
202       throw new ServletModuleException(e.toString());
203     }
204   }
205
206   public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
207   {
208
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");
213
214     String confirmParam = req.getParameter("confirm");
215     String cancelParam = req.getParameter("cancel");
216
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);
227     }
228     else {
229       if (confirmParam!= null && !confirmParam.equals("")) {
230         try {
231           mainModule.deleteById(idParam);
232
233           /** @todo the following two should be imlied in
234            *  DatabaseContent */
235
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());
244         }
245         list(req,res);
246       }
247       else {
248         // Datensatz anzeigen
249         _showObject(idParam, req, res);
250       }
251     }
252   }
253
254   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
255   {
256     String        idParam = req.getParameter("id");
257     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
258     _showObject(idParam, req, res);
259   }
260
261   // methods for attaching media file
262   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
263   {
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");
267
268     try {
269       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
270       entContent.attach(mediaIdParam);
271     }
272     catch(ModuleException e) {
273       theLog.printError("smod content :: attach :: could not get entityContent");
274     }
275     catch(StorageObjectException e) {
276       theLog.printError("smod content :: attach :: could not get entityContent");
277     }
278
279     _showObject(idParam, req, res);
280   }
281
282   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
283   {
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");
288
289     try {
290       EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
291       entContent.dettach(cidParam,midParam);
292     }
293     catch(ModuleException e) {
294       theLog.printError("smod content :: dettach :: could not get entityContent");
295     }
296     catch(StorageObjectException e) {
297       theLog.printError("smod content :: dettach :: could not get entityContent");
298     }
299
300     _showObject(cidParam, req, res);
301   }
302
303   public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
304   {
305     String  idParam = req.getParameter("id");
306     if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
307     try {
308       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
309       entContent.newswire();
310     }
311     catch(ModuleException e) {
312       theLog.printError("smod content :: newswire :: could not get entityContent");
313     }
314     catch(StorageObjectException e) {
315       theLog.printError("smod content :: dettach :: could not get entityContent");
316     }
317
318     list(req, res);
319   }
320
321
322   public void update(HttpServletRequest req, HttpServletResponse res)
323     throws ServletModuleException
324   {
325     try {
326
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");
331
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");
352         list(req,res);
353       }
354       else
355         _showObject(idParam, req, res);
356     }
357     catch (StorageObjectException e) {
358       throw new ServletModuleException(e.toString());
359     }
360     catch (ModuleException e) {
361       throw new ServletModuleException(e.toString());
362     }
363   }
364
365   //
366   // Hilfsmethoden
367
368   private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
369     throws ServletModuleException {
370
371     try {
372       EntityContent entContent=(EntityContent)mainModule.getById(id);
373       SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(entContent);
374       EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
375       if (topicToContent!=null && topicToContent.size()>0){
376         theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
377         Entity topics = null;
378         SimpleList topicList = new SimpleList();
379         for(int i=0;i<topicToContent.size();i++){
380           topics = (EntityTopics)topicToContent.elementAt(i);
381           topicList.add(topics.getId());
382         }
383         mergeData.put("to_topic",topicList);
384       }
385       //obsolete, because of psqgl 7.1.x
386       //mergeData.put("content_data", entContent.getContentData());
387       mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
388       try {
389         mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
390       } catch (Exception e) {
391         theLog.printError("articletype could not be fetched.");
392       }
393       try {
394         mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
395       } catch (Exception e) {
396         theLog.printError("language-popup could not be fetched.");
397       }
398       // get the images
399       EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(entContent);
400       if (currentMedia!=null && currentMedia.getCount()>=1) {
401                                 mergeData.put("to_media", currentMedia);
402       }
403
404
405       mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
406       // hier code um zur liste zurueckzukommen
407       String offsetParam, whereParam, orderParam;
408       if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
409       if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
410       if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
411       mergeData.put("login_user", _getUser(req));
412       deliver(req, res, mergeData, templateObjektString);
413     } catch (Exception e) {
414       throw new ServletModuleException(e.toString());
415     }
416   }
417
418
419   public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
420     throws ServletModuleException {
421
422     try {
423       // hier dann htmlcode rausschreiben
424       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
425         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
426         modelRoot.put("themenHashData", themenModule.getHashData());
427         modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
428         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
429         deliver(req, res, modelRoot, templateListString);
430       } else  { // count = 1
431         _showObject(theList.elementAt(0).getId(),req,res);
432       }
433     } catch (StorageObjectException e) {
434       throw new ServletModuleException(e.toString());
435     }
436   }
437
438   public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
439     throws ServletModuleException {
440
441     try {
442       // delivering html
443       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
444         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
445         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
446
447     EntityContent       currentContent;
448     EntityList          upMediaEntityList;
449     EntityList          imageEntityList;
450     EntityList          currentMediaList;
451     Entity              mediaType;
452     EntityMedia         uploadedMedia;
453     SimpleList          opList;
454       String imageRoot = MirConfig.getProp("Producer.ImageRoot");
455
456     SimpleHash          contentHash;
457     Class               mediaHandlerClass=null;
458     MirMedia            mediaHandler=null;
459     String              mediaHandlerName=null;
460     Database            mediaStorage=null;
461     String              tinyIcon;
462     String              iconAlt;
463
464       for (int i=0; i < theList.size();i++) {
465         currentContent = (EntityContent)theList.elementAt(i);
466         //fetching/setting the images
467         upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
468         if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
469           tinyIcon = null;
470           iconAlt = null;
471           mediaHandler = null;
472           mediaHandlerName = null;
473           for (int n=0; n < upMediaEntityList.size();n++) {
474             uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
475             mediaType = uploadedMedia.getMediaType();
476
477             //must of had a non-existant to_media_type entry..
478             //let's save our ass.
479             if (mediaType != null) {
480                 try {
481                   mediaHandlerName = mediaType.getValue("classname");
482                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
483                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
484                 } catch (Exception e) {
485                   theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
486                 }
487
488                 //the "best" media type to show
489                 if (mediaHandler.isVideo()) {
490                   tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
491                   iconAlt = "Video";
492                   break;
493                 } else if (mediaHandler.isAudio()) {
494                   tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
495                   iconAlt = "Audio";
496                 } else if (tinyIcon == null && !mediaHandler.isImage()) {
497                   tinyIcon = mediaHandler.getTinyIcon();
498                   iconAlt = mediaHandler.getIconAlt();
499                 }
500             }
501           }
502           //it only has image(s)
503           if (tinyIcon == null) {
504             tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
505             iconAlt = "Image";
506           }
507
508         // uploadedMedia Entity list is empty.
509         // we only have text
510         } else {
511           tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
512           iconAlt = "Text";
513         }
514
515         try{
516           //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
517           contentHash = (SimpleHash)theList.get(i);
518           contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
519           contentHash.put("icon_alt", iconAlt);
520         } catch (Exception e){}
521       }
522
523
524         deliver(req, res, modelRoot, templateListString);
525       } else  { // count = 1
526         _showObject(theList.elementAt(0).getId(), req, res);
527       }
528     } catch (StorageObjectException e) {
529       throw new ServletModuleException(e.toString());
530     }
531   }
532
533   private EntityUsers _getUser(HttpServletRequest req)
534   {
535     HttpSession session=req.getSession(false);
536     return (EntityUsers)session.getAttribute("login.uid");
537   }
538 }
539