1-n-content-media, tomcat-session-tracking without cookies, and more
[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.module.*;
15 import mir.misc.*;
16 import mir.storage.*;
17 import mir.entity.*;
18
19 import mircoders.storage.*;
20 import mircoders.module.*;
21 import mircoders.entity.*;
22
23
24 /*
25  *  ServletModuleContent -
26  *  liefert HTML fuer Content
27  *
28  *
29  * @author RK
30  */
31
32 public class ServletModuleContent extends ServletModule
33 {
34
35   static ModuleTopics         themenModule;
36   static ModuleSchwerpunkt    schwerpunktModule;
37   static ModuleGruppen        gruppenModule;
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(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.Content.Logfile"));
50       templateListString = Configuration.getProperty("ServletModule.Content.ListTemplate");
51       templateOpString = Configuration.getProperty("ServletModule.Content.OpTemplate");
52       templateObjektString = Configuration.getProperty("ServletModule.Content.ObjektTemplate");
53       templateConfirmString = Configuration.getProperty("ServletModule.Content.ConfirmTemplate");
54       mainModule = new ModuleContent(DatabaseContent.getInstance());
55       themenModule = new ModuleTopics(DatabaseTopics.getInstance());
56       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
57       gruppenModule = new ModuleGruppen(DatabaseGroups.getInstance());
58       imageModule = new ModuleImages(DatabaseImages.getInstance());
59     } catch (StorageObjectException e) {
60       theLog.printDebugInfo("servletmodulecontent konnte nicht initialisiert werden");
61     }
62   }
63
64   // Methoden
65
66   public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
67   {
68     try {
69       EntityUsers user = _getUser(req);
70       EntityList   theList;
71       String       offsetParam = req.getParameter("offset");
72       int          offset =0;
73
74       // hier offsetcode bearbeiteb
75       if (offsetParam != null && !offsetParam.equals(""))
76           offset = Integer.parseInt(offsetParam);
77
78       if (req.getParameter("next") != null)
79           offset=Integer.parseInt(req.getParameter("nextoffset"));
80       else
81           if (req.getParameter("prev") != null)
82             offset = Integer.parseInt(req.getParameter("prevoffset"));
83
84       String        whereParam = req.getParameter("where");
85       String        orderParam = req.getParameter("order");
86
87       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
88       _list(theList, req, res);
89     } catch (ModuleException e) {
90       throw new ServletModuleException(e.toString());
91     }
92   }
93
94   public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
95   {
96     try {
97       EntityUsers user = _getUser(req);
98       EntityList   theList;
99       String       offsetParam = req.getParameter("offset");
100       int          offset =0;
101
102       String whereParam = req.getParameter("where");
103
104       if (whereParam==null) whereParam = "to_article_type='0'";
105
106       // hier offsetcode bearbeiteb
107       if (offsetParam != null && !offsetParam.equals(""))
108           offset = Integer.parseInt(offsetParam);
109
110       if (req.getParameter("next") != null)
111           offset=Integer.parseInt(req.getParameter("nextoffset"));
112       else
113           if (req.getParameter("prev") != null)
114             offset = Integer.parseInt(req.getParameter("prevoffset"));
115
116       String orderParam = req.getParameter("order");
117
118       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
119       _list(theList, req, res);
120     } catch (ModuleException e) {
121       throw new ServletModuleException(e.toString());
122     }
123   }
124
125
126   public void search(HttpServletRequest req, HttpServletResponse res)
127     throws ServletModuleException {
128     try {
129       EntityUsers   user = _getUser(req);
130       EntityList    theList;
131       String        fieldParam = req.getParameter("field");
132       String        fieldValueParam = req.getParameter("fieldvalue");
133       String        orderParam = req.getParameter("order");
134
135       theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
136       _list(theList, req, res);
137     } catch (ModuleException e) {
138       throw new ServletModuleException(e.toString());
139     }
140   }
141
142   public void add(HttpServletRequest req, HttpServletResponse res)
143     throws ServletModuleException {
144
145     EntityUsers   user = _getUser(req);
146     SimpleHash mergeData = new SimpleHash();
147     mergeData.put("new", "1");
148     mergeData.put("is_published", "1");
149     String now = StringUtil.date2webdbDate(new GregorianCalendar());
150     mergeData.put("date", new SimpleScalar(now));
151     mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
152     mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
153     try {
154       mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
155     } catch (Exception e) {
156       theLog.printError("articletype could not be fetched.");
157     }
158     try {
159       mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
160     } catch (Exception e) {
161       theLog.printError("language-popup could not be fetched.");
162     }
163     mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
164     mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(user));
165     deliver(req, res, mergeData, templateObjektString);
166   }
167
168
169   public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
170   {
171     //theLog.printDebugInfo(":: content :: trying to insert");
172     try {
173       EntityUsers   user = _getUser(req);
174       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
175       //theLog.printDebugInfo(":: content :: got intersecting values");
176       String now = StringUtil.date2webdbDate(new GregorianCalendar());
177       withValues.put("date", now);
178       withValues.put("publish_path", StringUtil.webdbDate2path(now));
179       withValues.put("to_publisher", user.getId());
180       withValues.put("is_produced", "0");
181       if (!withValues.containsKey("is_published"))
182         withValues.put("is_published","0");
183       if (!withValues.containsKey("is_html"))
184         withValues.put("is_html","0");
185       if (withValues.get("creator").toString().equals(""))
186         withValues.put("creator","Anonym");
187       String id = mainModule.add(withValues);
188       DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
189       //theLog.printDebugInfo(":: content :: inserted");
190       _showObject(id, req, res);
191     }
192     catch (StorageObjectException e) {
193       throw new ServletModuleException(e.toString());
194     }
195     catch (ModuleException e) {
196       throw new ServletModuleException(e.toString());
197     }
198   }
199
200   public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
201   {
202
203     EntityUsers   user = _getUser(req);
204     // hier pruefen ob dem akt. user loeschen erlaubt ist...
205     String idParam = req.getParameter("id");
206     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
207
208     String confirmParam = req.getParameter("confirm");
209     String cancelParam = req.getParameter("cancel");
210
211     if (confirmParam == null && cancelParam == null) {
212       // HTML Ausgabe zum Confirmen!
213       SimpleHash mergeData = new SimpleHash();
214       mergeData.put("module", "Content");
215       mergeData.put("infoString", "Content: " + idParam);
216       mergeData.put("id", idParam);
217       mergeData.put("where", req.getParameter("where"));
218       mergeData.put("order", req.getParameter("order"));
219       mergeData.put("offset", req.getParameter("offset"));
220       deliver(req, res, mergeData, templateConfirmString);
221     }
222     else {
223       if (confirmParam!= null && !confirmParam.equals("")) {
224         try {
225           mainModule.deleteById(idParam);
226           //delete rows in the content_x_topic-table
227           DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
228           //delete rows in the comment-table
229           DatabaseComment.getInstance().deleteByContentId(idParam);
230         } catch (ModuleException e) {
231           throw new ServletModuleException(e.toString());
232         } catch (StorageObjectException e) {
233           throw new ServletModuleException(e.toString());
234         }
235         list(req,res);
236       }
237       else {
238         // Datensatz anzeigen
239         _showObject(idParam, req, res);
240       }
241     }
242   }
243
244   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
245   {
246     String        idParam = req.getParameter("id");
247     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
248     _showObject(idParam, req, res);
249   }
250
251   // methods for attaching media file
252   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
253   {
254     String  mediaIdParam = req.getParameter("mid");
255     String  idParam = req.getParameter("cid");
256     if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
257     
258     try {
259       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
260       entContent.attach(mediaIdParam);
261     }
262     catch(ModuleException e) {
263       theLog.printError("smod content :: attach :: could not get entityContent");
264     }
265     _showObject(idParam, req, res);
266   }
267
268   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
269   {
270     String  cidParam = req.getParameter("cid");
271                 String  midParam = req.getParameter("mid");
272     if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
273     if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
274     
275     try {
276       EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
277       entContent.dettach(cidParam,midParam);
278     }
279     catch(ModuleException e) {
280       theLog.printError("smod content :: dettach :: could not get entityContent");
281     }
282     _showObject(cidParam, req, res);
283   }
284
285   public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
286   {
287     String  idParam = req.getParameter("id");
288     if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
289     try {
290       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
291       entContent.newswire();
292     }
293     catch(ModuleException e) {
294       theLog.printError("smod content :: newswire :: could not get entityContent");
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("Falscher Aufruf: (id) nicht angegeben");
309
310       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
311       //String topic_id = req.getParameter("to_topic");
312       String[] topic_id = req.getParameterValues("to_topic");
313       String content_id = req.getParameter("id");
314       // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
315       if(user != null) withValues.put("user_id", user.getId());
316       withValues.put("is_produced", "0");
317       if (!withValues.containsKey("is_published"))
318         withValues.put("is_published","0");
319       if (!withValues.containsKey("is_html"))
320         withValues.put("is_html","0");
321       if (withValues.get("creator").toString().equals(""))
322         withValues.put("creator","Anonym");
323       //theLog.printDebugInfo("updating. ");
324       String id = mainModule.set(withValues);
325       DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
326       //theLog.printDebugInfo("update done. ");
327       String whereParam = req.getParameter("where");
328       String orderParam = req.getParameter("order");
329       if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
330         //theLog.printDebugInfo("update to list");
331         list(req,res);
332       }
333       else
334         _showObject(idParam, req, res);
335     }
336     catch (StorageObjectException e) {
337       throw new ServletModuleException(e.toString());
338     }
339     catch (ModuleException e) {
340       throw new ServletModuleException(e.toString());
341     }
342   }
343
344   //
345   // Hilfsmethoden
346
347   private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
348     throws ServletModuleException {
349
350     try {
351       EntityContent entContent=(EntityContent)mainModule.getById(id);
352       SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(entContent);
353       EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
354       if (topicToContent!=null && topicToContent.size()>0){
355         theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
356         Entity topics = null;
357         SimpleList topicList = new SimpleList();
358         for(int i=0;i<topicToContent.size();i++){
359           topics = (EntityTopics)topicToContent.elementAt(i);
360           topicList.add(topics.getId());
361         }
362         mergeData.put("to_topic",topicList);
363       }
364       //obsolete, because of psqgl 7.1.x
365       //mergeData.put("content_data", entContent.getContentData());
366       mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
367       mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
368       try {
369         mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
370       } catch (Exception e) {
371         theLog.printError("articletype could not be fetched.");
372       }
373       try {
374         mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
375       } catch (Exception e) {
376         theLog.printError("language-popup could not be fetched.");
377       }
378       // get the images
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);
383       }
384
385
386       //mergeData.put("gruppenHashData", gruppenModule.getHashData());
387       mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
388       // hier code um zur liste zurueckzukommen
389       String offsetParam, whereParam, orderParam;
390       if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
391       if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
392       if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
393       mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
394       deliver(req, res, mergeData, templateObjektString);
395     } catch (Exception e) {
396       throw new ServletModuleException(e.toString());
397     }
398   }
399
400
401   public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
402     throws ServletModuleException {
403
404     try {
405       // hier dann htmlcode rausschreiben
406       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
407         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
408         modelRoot.put("themenHashData", themenModule.getHashData());
409         modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
410         modelRoot.put("gruppenHashData", gruppenModule.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);
415       }
416     } catch (StorageObjectException e) {
417       throw new ServletModuleException(e.toString());
418     }
419   }
420
421   public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
422     throws ServletModuleException {
423
424     try {
425       // hier dann htmlcode rausschreiben
426       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
427         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
428         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
429         modelRoot.put("gruppenHashData", gruppenModule.getHashData());
430         deliver(req, res, modelRoot, templateListString);
431       } else  { // count = 1
432         _showObject(theList.elementAt(0).getId(), req, res);
433       }
434     } catch (StorageObjectException e) {
435       throw new ServletModuleException(e.toString());
436     }
437   }
438
439   private EntityUsers _getUser(HttpServletRequest req)
440   {
441     HttpSession session=req.getSession(false);
442     return (EntityUsers)session.getAttribute("login.uid");
443   }
444 }
445