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