9871d949d56d0262016d12d70ddb26b7d2360296
[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  *  deliver html for the article admin form.
28  *
29  * @version $Revision: 1.19 $
30  * @author $Author: mh $
31  *
32  * $Log: ServletModuleContent.java,v $
33  * Revision 1.19  2002/07/20 22:24:25  mh
34  * made the add() method use _showObject. Fixes a bug that cause the popUps not to show up when adding an article in the admin
35  *
36  *
37  */
38
39 public class ServletModuleContent extends ServletModule
40 {
41
42   static ModuleTopics         themenModule;
43   static ModuleSchwerpunkt    schwerpunktModule;
44   static ModuleImages         imageModule;
45
46   static String templateOpString;
47
48   // Singelton / Kontruktor
49
50   private static ServletModuleContent instance = new ServletModuleContent();
51   public static ServletModule getInstance() { return instance; }
52
53   private ServletModuleContent() {
54     try {
55                 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Content.Logfile"));
56       templateListString = MirConfig.getProp("ServletModule.Content.ListTemplate");
57       //templateOpString = MirConfig.getProp("ServletModule.Content.OpTemplate");
58       templateObjektString = MirConfig.getProp("ServletModule.Content.ObjektTemplate");
59       templateConfirmString = MirConfig.getProp("ServletModule.Content.ConfirmTemplate");
60       mainModule = new ModuleContent(DatabaseContent.getInstance());
61       themenModule = new ModuleTopics(DatabaseTopics.getInstance());
62       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
63       imageModule = new ModuleImages(DatabaseImages.getInstance());
64     } catch (StorageObjectException e) {
65       theLog.printDebugInfo("servletmodulecontent konnte nicht initialisiert werden");
66     }
67   }
68
69   // Methoden
70
71   public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
72   {
73     try {
74       EntityUsers user = _getUser(req);
75       EntityList   theList;
76       String       offsetParam = req.getParameter("offset");
77       int          offset =0;
78
79       // hier offsetcode bearbeiteb
80       if (offsetParam != null && !offsetParam.equals(""))
81           offset = Integer.parseInt(offsetParam);
82
83       if (req.getParameter("next") != null)
84           offset=Integer.parseInt(req.getParameter("nextoffset"));
85       else
86           if (req.getParameter("prev") != null)
87             offset = Integer.parseInt(req.getParameter("prevoffset"));
88
89       String        whereParam = req.getParameter("where");
90       String        orderParam = req.getParameter("order");
91
92       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
93       _list(theList, req, res);
94     } catch (ModuleException e) {
95       throw new ServletModuleException(e.toString());
96     }
97   }
98
99   public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
100   {
101     try {
102       EntityUsers user = _getUser(req);
103       EntityList   theList;
104       String       offsetParam = req.getParameter("offset");
105       int          offset =0;
106
107       String whereParam = req.getParameter("where");
108
109       if (whereParam==null) whereParam = "to_article_type='0'";
110
111       // hier offsetcode bearbeiteb
112       if (offsetParam != null && !offsetParam.equals(""))
113           offset = Integer.parseInt(offsetParam);
114
115       if (req.getParameter("next") != null)
116           offset=Integer.parseInt(req.getParameter("nextoffset"));
117       else
118           if (req.getParameter("prev") != null)
119             offset = Integer.parseInt(req.getParameter("prevoffset"));
120
121       String orderParam = req.getParameter("order");
122
123       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
124       _list(theList, req, res);
125     } catch (ModuleException e) {
126       throw new ServletModuleException(e.toString());
127     }
128   }
129
130
131   public void search(HttpServletRequest req, HttpServletResponse res)
132     throws ServletModuleException {
133     try {
134       EntityUsers   user = _getUser(req);
135       EntityList    theList;
136       String        fieldParam = req.getParameter("field");
137       String        fieldValueParam = req.getParameter("fieldvalue");
138       String        orderParam = req.getParameter("order");
139
140       theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
141       _list(theList, req, res);
142     } catch (ModuleException e) {
143       throw new ServletModuleException(e.toString());
144     }
145   }
146
147   public void add(HttpServletRequest req, HttpServletResponse res)
148     throws ServletModuleException {
149     _showObject(null, req, res);
150   }
151
152
153   public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
154   {
155     //theLog.printDebugInfo(":: content :: trying to insert");
156     try {
157       EntityUsers   user = _getUser(req);
158       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
159       //theLog.printDebugInfo(":: content :: got intersecting values");
160       String now = StringUtil.date2webdbDate(new GregorianCalendar());
161       withValues.put("date", now);
162       withValues.put("publish_path", StringUtil.webdbDate2path(now));
163       withValues.put("to_publisher", user.getId());
164       withValues.put("is_produced", "0");
165       if (!withValues.containsKey("is_published"))
166         withValues.put("is_published","0");
167       if (!withValues.containsKey("is_html"))
168         withValues.put("is_html","0");
169       if (withValues.get("creator").toString().equals(""))
170         withValues.put("creator","Anonym");
171       String id = mainModule.add(withValues);
172       DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
173       //theLog.printDebugInfo(":: content :: inserted");
174       _showObject(id, req, res);
175     }
176     catch (StorageObjectException e) {
177       throw new ServletModuleException(e.toString());
178     }
179     catch (ModuleException e) {
180       throw new ServletModuleException(e.toString());
181     }
182   }
183
184   public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
185   {
186
187     EntityUsers   user = _getUser(req);
188     // hier pruefen ob dem akt. user loeschen erlaubt ist...
189     String idParam = req.getParameter("id");
190     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
191
192     String confirmParam = req.getParameter("confirm");
193     String cancelParam = req.getParameter("cancel");
194
195     if (confirmParam == null && cancelParam == null) {
196       // HTML Ausgabe zum Confirmen!
197       SimpleHash mergeData = new SimpleHash();
198       mergeData.put("module", "Content");
199       mergeData.put("infoString", "Content: " + idParam);
200       mergeData.put("id", idParam);
201       mergeData.put("where", req.getParameter("where"));
202       mergeData.put("order", req.getParameter("order"));
203       mergeData.put("offset", req.getParameter("offset"));
204       deliver(req, res, mergeData, templateConfirmString);
205     }
206     else {
207       if (confirmParam!= null && !confirmParam.equals("")) {
208         try {
209           mainModule.deleteById(idParam);
210
211           /** @todo the following two should be imlied in
212            *  DatabaseContent */
213
214           //delete rows in the content_x_topic-table
215           DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
216           //delete rows in the comment-table
217           DatabaseComment.getInstance().deleteByContentId(idParam);
218         } catch (ModuleException e) {
219           throw new ServletModuleException(e.toString());
220         } catch (StorageObjectException e) {
221           throw new ServletModuleException(e.toString());
222         }
223         list(req,res);
224       }
225       else {
226         // Datensatz anzeigen
227         _showObject(idParam, req, res);
228       }
229     }
230   }
231
232   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
233   {
234     String        idParam = req.getParameter("id");
235     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
236     _showObject(idParam, req, res);
237   }
238
239   // methods for attaching media file
240   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
241   {
242     String  mediaIdParam = req.getParameter("mid");
243     String  idParam = req.getParameter("cid");
244     if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
245
246     try {
247       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
248       entContent.attach(mediaIdParam);
249     }
250     catch(ModuleException e) {
251       theLog.printError("smod content :: attach :: could not get entityContent");
252     }
253     catch(StorageObjectException e) {
254       theLog.printError("smod content :: attach :: could not get entityContent");
255     }
256
257     _showObject(idParam, req, res);
258   }
259
260   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
261   {
262     String  cidParam = req.getParameter("cid");
263                 String  midParam = req.getParameter("mid");
264     if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
265     if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
266
267     try {
268       EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
269       entContent.dettach(cidParam,midParam);
270     }
271     catch(ModuleException e) {
272       theLog.printError("smod content :: dettach :: could not get entityContent");
273     }
274     catch(StorageObjectException e) {
275       theLog.printError("smod content :: dettach :: could not get entityContent");
276     }
277
278     _showObject(cidParam, req, res);
279   }
280
281   public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
282   {
283     String  idParam = req.getParameter("id");
284     if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
285     try {
286       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
287       entContent.newswire();
288     }
289     catch(ModuleException e) {
290       theLog.printError("smod content :: newswire :: could not get entityContent");
291     }
292     catch(StorageObjectException e) {
293       theLog.printError("smod content :: dettach :: could not get entityContent");
294     }
295
296     list(req, res);
297   }
298
299
300   public void update(HttpServletRequest req, HttpServletResponse res)
301     throws ServletModuleException
302   {
303     try {
304
305       EntityUsers   user = _getUser(req);
306       if (user==null) theLog.printDebugInfo("user null!");
307       String idParam = req.getParameter("id");
308       if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
309
310       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
311       String[] topic_id = req.getParameterValues("to_topic");
312       String content_id = req.getParameter("id");
313       // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
314       if(user != null) withValues.put("user_id", user.getId());
315       withValues.put("is_produced", "0");
316       if (!withValues.containsKey("is_published"))
317         withValues.put("is_published","0");
318       if (!withValues.containsKey("is_html"))
319         withValues.put("is_html","0");
320       if (withValues.get("creator").toString().equals(""))
321         withValues.put("creator","Anonym");
322       //theLog.printDebugInfo("updating. ");
323       String id = mainModule.set(withValues);
324       DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
325       //theLog.printDebugInfo("update done. ");
326       String whereParam = req.getParameter("where");
327       String orderParam = req.getParameter("order");
328       if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
329         //theLog.printDebugInfo("update to list");
330         list(req,res);
331       }
332       else
333         _showObject(idParam, req, res);
334     }
335     catch (StorageObjectException e) {
336       throw new ServletModuleException(e.toString());
337     }
338     catch (ModuleException e) {
339       throw new ServletModuleException(e.toString());
340     }
341   }
342
343   /* 
344    * HelperMethod shows the basic article editing form.
345    *
346    * if the "id" parameter is null, it means show an empty form to add a new
347    * article.
348    */ 
349   private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
350     throws ServletModuleException {
351
352     SimpleHash extraInfo = new SimpleHash();
353     try {
354       TemplateModelRoot entContent;
355       if (id != null) {
356         entContent = (TemplateModelRoot)mainModule.getById(id);
357       } else {
358         SimpleHash withValues = new SimpleHash();
359         withValues.put("new", "1");
360         withValues.put("is_published", "0");
361         String now = StringUtil.date2webdbDate(new GregorianCalendar());
362         withValues.put("date", new SimpleScalar(now));
363         EntityUsers   user = _getUser(req);
364         withValues.put("login_user", user);
365         entContent = withValues;
366       }
367         
368
369       extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
370       try {
371         extraInfo.put("articletypePopupData",
372                         DatabaseArticleType.getInstance().getPopupData());
373       } catch (Exception e) {
374         theLog.printError("articletype could not be fetched.");
375       }
376       try {
377         extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
378       } catch (Exception e) {
379         theLog.printError("language-popup could not be fetched.");
380       }
381
382       extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
383       // hier code um zur liste zurueckzukommen
384       String offsetParam, whereParam, orderParam;
385       if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
386       if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
387       if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
388       extraInfo.put("login_user", _getUser(req));
389       deliver(req, res, entContent, extraInfo, templateObjektString);
390     } catch (Exception e) {
391       throw new ServletModuleException(e.toString());
392     }
393   }
394
395
396   public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
397     throws ServletModuleException {
398
399     try {
400       // hier dann htmlcode rausschreiben
401       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
402         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
403         modelRoot.put("themenHashData", themenModule.getHashData());
404         modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
405         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
406         deliver(req, res, modelRoot, templateListString);
407       } else  { // count = 1
408         _showObject(theList.elementAt(0).getId(),req,res);
409       }
410     } catch (StorageObjectException e) {
411       throw new ServletModuleException(e.toString());
412     }
413   }
414
415   public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
416     throws ServletModuleException {
417
418     try {
419       // delivering html
420       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
421         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
422         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
423
424     EntityContent       currentContent;
425     EntityList          upMediaEntityList;
426     EntityList          imageEntityList;
427     EntityList          currentMediaList;
428     Entity              mediaType;
429     EntityMedia         uploadedMedia;
430     SimpleList          opList;
431       String imageRoot = MirConfig.getProp("Producer.ImageRoot");
432
433     SimpleHash          contentHash;
434     Class               mediaHandlerClass=null;
435     MirMedia            mediaHandler=null;
436     String              mediaHandlerName=null;
437     Database            mediaStorage=null;
438     String              tinyIcon;
439     String              iconAlt;
440
441       for (int i=0; i < theList.size();i++) {
442         currentContent = (EntityContent)theList.elementAt(i);
443         //fetching/setting the images
444         upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
445         if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
446           tinyIcon = null;
447           iconAlt = null;
448           mediaHandler = null;
449           mediaHandlerName = null;
450           for (int n=0; n < upMediaEntityList.size();n++) {
451             uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
452             mediaType = uploadedMedia.getMediaType();
453
454             //must of had a non-existant to_media_type entry..
455             //let's save our ass.
456             if (mediaType != null) {
457                 try {
458                   mediaHandlerName = mediaType.getValue("classname");
459                   mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
460                   mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
461                 } catch (Exception e) {
462                   theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
463                 }
464
465                 //the "best" media type to show
466                 if (mediaHandler.isVideo()) {
467                   tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
468                   iconAlt = "Video";
469                   break;
470                 } else if (mediaHandler.isAudio()) {
471                   tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
472                   iconAlt = "Audio";
473                 } else if (tinyIcon == null && !mediaHandler.isImage()) {
474                   tinyIcon = mediaHandler.getTinyIcon();
475                   iconAlt = mediaHandler.getIconAlt();
476                 }
477             }
478           }
479           //it only has image(s)
480           if (tinyIcon == null) {
481             tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
482             iconAlt = "Image";
483           }
484
485         // uploadedMedia Entity list is empty.
486         // we only have text
487         } else {
488           tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
489           iconAlt = "Text";
490         }
491
492         try{
493           //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
494           contentHash = (SimpleHash)theList.get(i);
495           contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
496           contentHash.put("icon_alt", iconAlt);
497         } catch (Exception e){}
498       }
499
500
501         deliver(req, res, modelRoot, templateListString);
502       } else  { // count = 1
503         _showObject(theList.elementAt(0).getId(), req, res);
504       }
505     } catch (StorageObjectException e) {
506       throw new ServletModuleException(e.toString());
507     }
508   }
509
510   private EntityUsers _getUser(HttpServletRequest req)
511   {
512     HttpSession session=req.getSession(false);
513     return (EntityUsers)session.getAttribute("login.uid");
514   }
515 }
516