some indy.nl related updates
[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
192 //      ML: this is not multi-language friendly and this can be done in a template
193 //      if (withValues.get("creator").toString().equals(""))
194 //        withValues.put("creator","Anonym");
195
196
197       String id = mainModule.add(withValues);
198       DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
199       //theLog.printDebugInfo(":: content :: inserted");
200       _showObject(id, req, res);
201     }
202     catch (StorageObjectException e) {
203       throw new ServletModuleException(e.toString());
204     }
205     catch (ModuleException e) {
206       throw new ServletModuleException(e.toString());
207     }
208   }
209
210   public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
211   {
212
213     EntityUsers   user = _getUser(req);
214     // hier pruefen ob dem akt. user loeschen erlaubt ist...
215     String idParam = req.getParameter("id");
216     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
217
218     String confirmParam = req.getParameter("confirm");
219     String cancelParam = req.getParameter("cancel");
220
221     if (confirmParam == null && cancelParam == null) {
222       // HTML Ausgabe zum Confirmen!
223       SimpleHash mergeData = new SimpleHash();
224       mergeData.put("module", "Content");
225       mergeData.put("infoString", "Content: " + idParam);
226       mergeData.put("id", idParam);
227       mergeData.put("where", req.getParameter("where"));
228       mergeData.put("order", req.getParameter("order"));
229       mergeData.put("offset", req.getParameter("offset"));
230       deliver(req, res, mergeData, templateConfirmString);
231     }
232     else {
233       if (confirmParam!= null && !confirmParam.equals("")) {
234         try {
235           mainModule.deleteById(idParam);
236
237           /** @todo the following two should be imlied in
238            *  DatabaseContent */
239
240           //delete rows in the content_x_topic-table
241           DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
242           //delete rows in the comment-table
243           DatabaseComment.getInstance().deleteByContentId(idParam);
244         } catch (ModuleException e) {
245           throw new ServletModuleException(e.toString());
246         } catch (StorageObjectException e) {
247           throw new ServletModuleException(e.toString());
248         }
249         list(req,res);
250       }
251       else {
252         // Datensatz anzeigen
253         _showObject(idParam, req, res);
254       }
255     }
256   }
257
258   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
259   {
260     String        idParam = req.getParameter("id");
261     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
262     _showObject(idParam, req, res);
263   }
264
265   // methods for attaching media file
266   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
267   {
268     String  mediaIdParam = req.getParameter("mid");
269     String  idParam = req.getParameter("cid");
270     if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
271
272     try {
273       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
274       entContent.attach(mediaIdParam);
275     }
276     catch(ModuleException e) {
277       theLog.printError("smod content :: attach :: could not get entityContent");
278     }
279     catch(StorageObjectException e) {
280       theLog.printError("smod content :: attach :: could not get entityContent");
281     }
282
283     _showObject(idParam, req, res);
284   }
285
286   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
287   {
288     String  cidParam = req.getParameter("cid");
289                 String  midParam = req.getParameter("mid");
290     if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
291     if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
292
293     try {
294       EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
295       entContent.dettach(cidParam,midParam);
296     }
297     catch(ModuleException e) {
298       theLog.printError("smod content :: dettach :: could not get entityContent");
299     }
300     catch(StorageObjectException e) {
301       theLog.printError("smod content :: dettach :: could not get entityContent");
302     }
303
304     _showObject(cidParam, req, res);
305   }
306
307   public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
308   {
309     String  idParam = req.getParameter("id");
310     if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
311     try {
312       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
313       entContent.newswire();
314     }
315     catch(ModuleException e) {
316       theLog.printError("smod content :: newswire :: could not get entityContent");
317     }
318     catch(StorageObjectException e) {
319       theLog.printError("smod content :: dettach :: could not get entityContent");
320     }
321
322     list(req, res);
323   }
324
325
326   public void update(HttpServletRequest req, HttpServletResponse res)
327     throws ServletModuleException
328   {
329     try {
330
331       EntityUsers   user = _getUser(req);
332       if (user==null) theLog.printDebugInfo("user null!");
333       String idParam = req.getParameter("id");
334       if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
335
336       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
337       String[] topic_id = req.getParameterValues("to_topic");
338       String content_id = req.getParameter("id");
339       // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
340       if(user != null) withValues.put("user_id", user.getId());
341       withValues.put("is_produced", "0");
342       if (!withValues.containsKey("is_published"))
343         withValues.put("is_published","0");
344       if (!withValues.containsKey("is_html"))
345         withValues.put("is_html","0");
346
347 //      ML: this is not multi-language friendly and this can be done in a template
348 //      if (withValues.get("creator").toString().equals(""))
349 //        withValues.put("creator","Anonym");
350
351       //theLog.printDebugInfo("updating. ");
352       String id = mainModule.set(withValues);
353       DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
354       //theLog.printDebugInfo("update done. ");
355       String whereParam = req.getParameter("where");
356       String orderParam = req.getParameter("order");
357       if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
358         //theLog.printDebugInfo("update to list");
359         list(req,res);
360       }
361       else
362         _showObject(idParam, req, res);
363     }
364     catch (StorageObjectException e) {
365       throw new ServletModuleException(e.toString());
366     }
367     catch (ModuleException e) {
368       throw new ServletModuleException(e.toString());
369     }
370   }
371
372   //
373   // Hilfsmethoden
374
375   private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
376     throws ServletModuleException {
377
378     SimpleHash extraInfo = new SimpleHash();
379     try {
380       EntityContent entContent=(EntityContent)mainModule.getById(id);
381
382       extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
383       try {
384         extraInfo.put("articletypePopupData",
385                         DatabaseArticleType.getInstance().getPopupData());
386       } catch (Exception e) {
387         theLog.printError("articletype could not be fetched.");
388       }
389       try {
390         extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
391       } catch (Exception e) {
392         theLog.printError("language-popup could not be fetched.");
393       }
394
395       extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
396       // hier code um zur liste zurueckzukommen
397       String offsetParam, whereParam, orderParam;
398       if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
399       if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
400       if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
401       extraInfo.put("login_user", _getUser(req));
402       deliver(req, res, entContent, extraInfo, templateObjektString);
403     } catch (Exception e) {
404       throw new ServletModuleException(e.toString());
405     }
406   }
407
408
409   public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
410     throws ServletModuleException {
411
412     try {
413       // hier dann htmlcode rausschreiben
414       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
415         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
416         modelRoot.put("themenHashData", themenModule.getHashData());
417         modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
418         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
419         deliver(req, res, modelRoot, templateListString);
420       } else  { // count = 1
421         _showObject(theList.elementAt(0).getId(),req,res);
422       }
423     } catch (StorageObjectException e) {
424       throw new ServletModuleException(e.toString());
425     }
426   }
427
428   public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
429     throws ServletModuleException {
430
431     try {
432       // delivering html
433       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
434         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
435         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
436
437     EntityContent       currentContent;
438     EntityList          upMediaEntityList;
439     EntityList          imageEntityList;
440     EntityList          currentMediaList;
441     Entity              mediaType;
442     EntityMedia         uploadedMedia;
443     SimpleList          opList;
444       String imageRoot = MirConfig.getProp("Producer.ImageRoot");
445
446     SimpleHash          contentHash;
447     Class               mediaHandlerClass=null;
448     MirMedia            mediaHandler=null;
449     String              mediaHandlerName=null;
450     Database            mediaStorage=null;
451     String              tinyIcon;
452     String              iconAlt;
453
454       for (int i=0; i < theList.size();i++) {
455         currentContent = (EntityContent)theList.elementAt(i);
456         //fetching/setting the images
457         upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
458         if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
459           tinyIcon = null;
460           iconAlt = null;
461           mediaHandler = null;
462           mediaHandlerName = null;
463           for (int n=0; n < upMediaEntityList.size();n++) {
464             uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
465             mediaType = uploadedMedia.getMediaType();
466
467             //must of had a non-existant to_media_type entry..
468             //let's save our ass.
469             if (mediaType != null) {
470                 try {
471                   mediaHandlerName = mediaType.getValue("classname");
472                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
473                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
474                 } catch (Exception e) {
475                   theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
476                 }
477
478                 //the "best" media type to show
479                 if (mediaHandler.isVideo()) {
480                   tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
481                   iconAlt = "Video";
482                   break;
483                 } else if (mediaHandler.isAudio()) {
484                   tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
485                   iconAlt = "Audio";
486                 } else if (tinyIcon == null && !mediaHandler.isImage()) {
487                   tinyIcon = mediaHandler.getTinyIcon();
488                   iconAlt = mediaHandler.getIconAlt();
489                 }
490             }
491           }
492           //it only has image(s)
493           if (tinyIcon == null) {
494             tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
495             iconAlt = "Image";
496           }
497
498         // uploadedMedia Entity list is empty.
499         // we only have text
500         } else {
501           tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
502           iconAlt = "Text";
503         }
504
505         try{
506           //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
507           contentHash = (SimpleHash)theList.get(i);
508           contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
509           contentHash.put("icon_alt", iconAlt);
510         } catch (Exception e){}
511       }
512
513
514         deliver(req, res, modelRoot, templateListString);
515       } else  { // count = 1
516         _showObject(theList.elementAt(0).getId(), req, res);
517       }
518     } catch (StorageObjectException e) {
519       throw new ServletModuleException(e.toString());
520     }
521   }
522
523   private EntityUsers _getUser(HttpServletRequest req)
524   {
525     HttpSession session=req.getSession(false);
526     return (EntityUsers)session.getAttribute("login.uid");
527   }
528 }
529