lots of comments on restructuring
[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 ModuleImages         imageModule;
38
39   static String templateOpString;
40
41   // Singelton / Kontruktor
42
43   private static ServletModuleContent instance = new ServletModuleContent();
44   public static ServletModule getInstance() { return instance; }
45
46   private ServletModuleContent() {
47     try {
48       theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Content.Logfile"));
49       templateListString = MirConfig.getProp("ServletModule.Content.ListTemplate");
50       templateOpString = MirConfig.getProp("ServletModule.Content.OpTemplate");
51       templateObjektString = MirConfig.getProp("ServletModule.Content.ObjektTemplate");
52       templateConfirmString = MirConfig.getProp("ServletModule.Content.ConfirmTemplate");
53       mainModule = new ModuleContent(DatabaseContent.getInstance());
54       themenModule = new ModuleTopics(DatabaseTopics.getInstance());
55       schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
56       imageModule = new ModuleImages(DatabaseImages.getInstance());
57     } catch (StorageObjectException e) {
58       theLog.printDebugInfo("servletmodulecontent konnte nicht initialisiert werden");
59     }
60   }
61
62   // Methoden
63
64   public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
65   {
66     try {
67       EntityUsers user = _getUser(req);
68       EntityList   theList;
69       String       offsetParam = req.getParameter("offset");
70       int          offset =0;
71
72       // hier offsetcode bearbeiteb
73       if (offsetParam != null && !offsetParam.equals(""))
74           offset = Integer.parseInt(offsetParam);
75
76       if (req.getParameter("next") != null)
77           offset=Integer.parseInt(req.getParameter("nextoffset"));
78       else
79           if (req.getParameter("prev") != null)
80             offset = Integer.parseInt(req.getParameter("prevoffset"));
81
82       String        whereParam = req.getParameter("where");
83       String        orderParam = req.getParameter("order");
84
85       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
86       _list(theList, req, res);
87     } catch (ModuleException e) {
88       throw new ServletModuleException(e.toString());
89     }
90   }
91
92   public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
93   {
94     try {
95       EntityUsers user = _getUser(req);
96       EntityList   theList;
97       String       offsetParam = req.getParameter("offset");
98       int          offset =0;
99
100       String whereParam = req.getParameter("where");
101
102       if (whereParam==null) whereParam = "to_article_type='0'";
103
104       // hier offsetcode bearbeiteb
105       if (offsetParam != null && !offsetParam.equals(""))
106           offset = Integer.parseInt(offsetParam);
107
108       if (req.getParameter("next") != null)
109           offset=Integer.parseInt(req.getParameter("nextoffset"));
110       else
111           if (req.getParameter("prev") != null)
112             offset = Integer.parseInt(req.getParameter("prevoffset"));
113
114       String orderParam = req.getParameter("order");
115
116       theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
117       _list(theList, req, res);
118     } catch (ModuleException e) {
119       throw new ServletModuleException(e.toString());
120     }
121   }
122
123
124   public void search(HttpServletRequest req, HttpServletResponse res)
125     throws ServletModuleException {
126     try {
127       EntityUsers   user = _getUser(req);
128       EntityList    theList;
129       String        fieldParam = req.getParameter("field");
130       String        fieldValueParam = req.getParameter("fieldvalue");
131       String        orderParam = req.getParameter("order");
132
133       theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
134       _list(theList, req, res);
135     } catch (ModuleException e) {
136       throw new ServletModuleException(e.toString());
137     }
138   }
139
140   public void add(HttpServletRequest req, HttpServletResponse res)
141     throws ServletModuleException {
142
143     EntityUsers   user = _getUser(req);
144     SimpleHash mergeData = new SimpleHash();
145     mergeData.put("new", "1");
146     mergeData.put("is_published", "1");
147     String now = StringUtil.date2webdbDate(new GregorianCalendar());
148     mergeData.put("date", new SimpleScalar(now));
149     try {
150       mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
151     } catch (ModuleException e) {
152       theLog.printError("cannot get topics as simple list.");
153       throw new ServletModuleException(e.toString());
154     }
155     try {
156       mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
157     } catch (Exception e) {
158       theLog.printError("articletype could not be fetched.");
159       throw new ServletModuleException("articletype could not be fetched: "
160       +e.toString());
161     }
162     try {
163       mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
164       mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
165     } catch (Exception e) {
166       theLog.printError("language-popup or scwerpunk list could not be fetched.");
167       throw new ServletModuleException("language/schwerpunkt list could not be fetched.: "
168         +e.toString());
169     }
170     mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(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           //delete rows in the content_x_topic-table
233           DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
234           //delete rows in the comment-table
235           DatabaseComment.getInstance().deleteByContentId(idParam);
236         } catch (ModuleException e) {
237           throw new ServletModuleException(e.toString());
238         } catch (StorageObjectException e) {
239           throw new ServletModuleException(e.toString());
240         }
241         list(req,res);
242       }
243       else {
244         // Datensatz anzeigen
245         _showObject(idParam, req, res);
246       }
247     }
248   }
249
250   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
251   {
252     String        idParam = req.getParameter("id");
253     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
254     _showObject(idParam, req, res);
255   }
256
257   // methods for attaching media file
258   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
259   {
260     String  mediaIdParam = req.getParameter("mid");
261     String  idParam = req.getParameter("cid");
262     if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
263
264     try {
265       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
266       entContent.attach(mediaIdParam);
267     }
268     catch(Exception e) {
269       theLog.printError("smod content :: attach :: could not get entityContent");
270     }
271     _showObject(idParam, req, res);
272   }
273
274   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
275   {
276     String  cidParam = req.getParameter("cid");
277                 String  midParam = req.getParameter("mid");
278     if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
279     if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
280
281     try {
282       EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
283       entContent.dettach(cidParam,midParam);
284     }
285     catch(Exception e) {
286       theLog.printError("smod content :: dettach :: could not get entityContent");
287     }
288     _showObject(cidParam, req, res);
289   }
290
291   public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
292   {
293     String  idParam = req.getParameter("id");
294     if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
295     try {
296       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
297       entContent.newswire();
298     }
299     catch(Exception e) {
300       theLog.printError("smod content :: newswire :: could not get entityContent");
301       throw new ServletModuleException("smod content :: newswire :: could not get entityContent"+e.toString());
302     }
303     list(req, res);
304   }
305
306
307   public void update(HttpServletRequest req, HttpServletResponse res)
308     throws ServletModuleException
309   {
310     try {
311
312       EntityUsers   user = _getUser(req);
313       if (user==null) theLog.printDebugInfo("user null!");
314       String idParam = req.getParameter("id");
315       if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
316
317       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
318       //String topic_id = req.getParameter("to_topic");
319       String[] topic_id = req.getParameterValues("to_topic");
320       String content_id = req.getParameter("id");
321       // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
322       if(user != null) withValues.put("user_id", user.getId());
323       withValues.put("is_produced", "0");
324       if (!withValues.containsKey("is_published"))
325         withValues.put("is_published","0");
326       if (!withValues.containsKey("is_html"))
327         withValues.put("is_html","0");
328       if (withValues.get("creator").toString().equals(""))
329         withValues.put("creator","Anonym");
330       //theLog.printDebugInfo("updating. ");
331       String id = mainModule.set(withValues);
332       DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
333       //theLog.printDebugInfo("update done. ");
334       String whereParam = req.getParameter("where");
335       String orderParam = req.getParameter("order");
336       if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
337         //theLog.printDebugInfo("update to list");
338         list(req,res);
339       }
340       else
341         _showObject(idParam, req, res);
342     }
343     catch (StorageObjectException e) {
344       throw new ServletModuleException(e.toString());
345     }
346     catch (ModuleException e) {
347       throw new ServletModuleException(e.toString());
348     }
349   }
350
351   //
352   // Hilfsmethoden
353
354   private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
355     throws ServletModuleException {
356
357     try {
358       EntityContent entContent=(EntityContent)mainModule.getById(id);
359       SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(entContent);
360       EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
361       if (topicToContent!=null && topicToContent.size()>0){
362         theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
363         Entity topics = null;
364         SimpleList topicList = new SimpleList();
365         for(int i=0;i<topicToContent.size();i++){
366           topics = (EntityTopics)topicToContent.elementAt(i);
367           topicList.add(topics.getId());
368         }
369         mergeData.put("to_topic",topicList);
370       }
371       //obsolete, because of psqgl 7.1.x
372       //mergeData.put("content_data", entContent.getContentData());
373       mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
374       try {
375         mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
376       } catch (Exception e) {
377         theLog.printError("articletype could not be fetched.");
378       }
379       try {
380         mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
381       } catch (Exception e) {
382         theLog.printError("language-popup could not be fetched.");
383       }
384       // get the images
385       EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(entContent);
386       if (currentMedia!=null && currentMedia.getCount()>=1) {
387         SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
388                                 mergeData.put("to_media", mediaList);
389       }
390
391
392       mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
393       // hier code um zur liste zurueckzukommen
394       String offsetParam, whereParam, orderParam;
395       if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
396       if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
397       if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
398       mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
399       deliver(req, res, mergeData, templateObjektString);
400     } catch (Exception e) {
401       throw new ServletModuleException(e.toString());
402     }
403   }
404
405
406   public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
407     throws ServletModuleException {
408
409     try {
410       // hier dann htmlcode rausschreiben
411       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
412         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
413         modelRoot.put("themenHashData", themenModule.getHashData());
414         modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
415         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
416         deliver(req, res, modelRoot, templateListString);
417       } else  { // count = 1
418         _showObject(theList.elementAt(0).getId(),req,res);
419       }
420     } catch (StorageObjectException e) {
421       throw new ServletModuleException(e.toString());
422     }
423   }
424
425   public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
426     throws ServletModuleException {
427
428     try {
429       // delivering html
430       if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
431         SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
432         modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
433         deliver(req, res, modelRoot, templateListString);
434       } else  { // count = 1
435         _showObject(theList.elementAt(0).getId(), req, res);
436       }
437     } catch (StorageObjectException e) {
438       throw new ServletModuleException(e.toString());
439     }
440   }
441
442   private EntityUsers _getUser(HttpServletRequest req)
443   {
444     HttpSession session=req.getSession(false);
445     return (EntityUsers)session.getAttribute("login.uid");
446   }
447 }
448