Ok, big merge. here's the new xml-config stuff in action. There's a few
[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(this.getClass().getName());
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     mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
150     try {
151       mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
152     } catch (Exception e) {
153       theLog.printError("articletype could not be fetched.");
154     }
155     try {
156       mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
157     } catch (Exception e) {
158       theLog.printError("language-popup could not be fetched.");
159     }
160     mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
161     mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(user));
162     deliver(req, res, mergeData, templateObjektString);
163   }
164
165
166   public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
167   {
168     //theLog.printDebugInfo(":: content :: trying to insert");
169     try {
170       EntityUsers   user = _getUser(req);
171       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
172       //theLog.printDebugInfo(":: content :: got intersecting values");
173       String now = StringUtil.date2webdbDate(new GregorianCalendar());
174       withValues.put("date", now);
175       withValues.put("publish_path", StringUtil.webdbDate2path(now));
176       withValues.put("to_publisher", user.getId());
177       withValues.put("is_produced", "0");
178       if (!withValues.containsKey("is_published"))
179         withValues.put("is_published","0");
180       if (!withValues.containsKey("is_html"))
181         withValues.put("is_html","0");
182       if (withValues.get("creator").toString().equals(""))
183         withValues.put("creator","Anonym");
184       String id = mainModule.add(withValues);
185       DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
186       //theLog.printDebugInfo(":: content :: inserted");
187       _showObject(id, req, res);
188     }
189     catch (StorageObjectException e) {
190       throw new ServletModuleException(e.toString());
191     }
192     catch (ModuleException e) {
193       throw new ServletModuleException(e.toString());
194     }
195   }
196
197   public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
198   {
199
200     EntityUsers   user = _getUser(req);
201     // hier pruefen ob dem akt. user loeschen erlaubt ist...
202     String idParam = req.getParameter("id");
203     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
204
205     String confirmParam = req.getParameter("confirm");
206     String cancelParam = req.getParameter("cancel");
207
208     if (confirmParam == null && cancelParam == null) {
209       // HTML Ausgabe zum Confirmen!
210       SimpleHash mergeData = new SimpleHash();
211       mergeData.put("module", "Content");
212       mergeData.put("infoString", "Content: " + idParam);
213       mergeData.put("id", idParam);
214       mergeData.put("where", req.getParameter("where"));
215       mergeData.put("order", req.getParameter("order"));
216       mergeData.put("offset", req.getParameter("offset"));
217       deliver(req, res, mergeData, templateConfirmString);
218     }
219     else {
220       if (confirmParam!= null && !confirmParam.equals("")) {
221         try {
222           mainModule.deleteById(idParam);
223           //delete rows in the content_x_topic-table
224           DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
225           //delete rows in the comment-table
226           DatabaseComment.getInstance().deleteByContentId(idParam);
227         } catch (ModuleException e) {
228           throw new ServletModuleException(e.toString());
229         } catch (StorageObjectException e) {
230           throw new ServletModuleException(e.toString());
231         }
232         list(req,res);
233       }
234       else {
235         // Datensatz anzeigen
236         _showObject(idParam, req, res);
237       }
238     }
239   }
240
241   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
242   {
243     String        idParam = req.getParameter("id");
244     if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
245     _showObject(idParam, req, res);
246   }
247
248   // methods for attaching media file
249   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
250   {
251     String  mediaIdParam = req.getParameter("mid");
252     String  idParam = req.getParameter("cid");
253     if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
254
255     try {
256       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
257       entContent.attach(mediaIdParam);
258     }
259     catch(ModuleException e) {
260       theLog.printError("smod content :: attach :: could not get entityContent");
261     }
262     _showObject(idParam, req, res);
263   }
264
265   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
266   {
267     String  cidParam = req.getParameter("cid");
268                 String  midParam = req.getParameter("mid");
269     if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
270     if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
271
272     try {
273       EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
274       entContent.dettach(cidParam,midParam);
275     }
276     catch(ModuleException e) {
277       theLog.printError("smod content :: dettach :: could not get entityContent");
278     }
279     _showObject(cidParam, req, res);
280   }
281
282   public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
283   {
284     String  idParam = req.getParameter("id");
285     if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
286     try {
287       EntityContent entContent = (EntityContent)mainModule.getById(idParam);
288       entContent.newswire();
289     }
290     catch(ModuleException e) {
291       theLog.printError("smod content :: newswire :: could not get entityContent");
292     }
293     list(req, res);
294   }
295
296
297   public void update(HttpServletRequest req, HttpServletResponse res)
298     throws ServletModuleException
299   {
300     try {
301
302       EntityUsers   user = _getUser(req);
303       if (user==null) theLog.printDebugInfo("user null!");
304       String idParam = req.getParameter("id");
305       if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
306
307       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
308       //String topic_id = req.getParameter("to_topic");
309       String[] topic_id = req.getParameterValues("to_topic");
310       String content_id = req.getParameter("id");
311       // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
312       if(user != null) withValues.put("user_id", user.getId());
313       withValues.put("is_produced", "0");
314       if (!withValues.containsKey("is_published"))
315         withValues.put("is_published","0");
316       if (!withValues.containsKey("is_html"))
317         withValues.put("is_html","0");
318       if (withValues.get("creator").toString().equals(""))
319         withValues.put("creator","Anonym");
320       //theLog.printDebugInfo("updating. ");
321       String id = mainModule.set(withValues);
322       DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
323       //theLog.printDebugInfo("update done. ");
324       String whereParam = req.getParameter("where");
325       String orderParam = req.getParameter("order");
326       if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
327         //theLog.printDebugInfo("update to list");
328         list(req,res);
329       }
330       else
331         _showObject(idParam, req, res);
332     }
333     catch (StorageObjectException e) {
334       throw new ServletModuleException(e.toString());
335     }
336     catch (ModuleException e) {
337       throw new ServletModuleException(e.toString());
338     }
339   }
340
341   //
342   // Hilfsmethoden
343
344   private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
345     throws ServletModuleException {
346
347     try {
348       EntityContent entContent=(EntityContent)mainModule.getById(id);
349       SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(entContent);
350       EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
351       if (topicToContent!=null && topicToContent.size()>0){
352         theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
353         Entity topics = null;
354         SimpleList topicList = new SimpleList();
355         for(int i=0;i<topicToContent.size();i++){
356           topics = (EntityTopics)topicToContent.elementAt(i);
357           topicList.add(topics.getId());
358         }
359         mergeData.put("to_topic",topicList);
360       }
361       //obsolete, because of psqgl 7.1.x
362       //mergeData.put("content_data", entContent.getContentData());
363       mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
364       try {
365         mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
366       } catch (Exception e) {
367         theLog.printError("articletype could not be fetched.");
368       }
369       try {
370         mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
371       } catch (Exception e) {
372         theLog.printError("language-popup could not be fetched.");
373       }
374       // get the images
375       EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(entContent);
376       if (currentMedia!=null && currentMedia.getCount()>=1) {
377         SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
378                                 mergeData.put("to_media", mediaList);
379       }
380
381
382       mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
383       // hier code um zur liste zurueckzukommen
384       String offsetParam, whereParam, orderParam;
385       if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
386       if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
387       if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
388       mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
389       deliver(req, res, mergeData, 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         deliver(req, res, modelRoot, templateListString);
424       } else  { // count = 1
425         _showObject(theList.elementAt(0).getId(), req, res);
426       }
427     } catch (StorageObjectException e) {
428       throw new ServletModuleException(e.toString());
429     }
430   }
431
432   private EntityUsers _getUser(HttpServletRequest req)
433   {
434     HttpSession session=req.getSession(false);
435     return (EntityUsers)session.getAttribute("login.uid");
436   }
437 }
438