b70c29c14cdb6504eb4ea67261884e44ac1ec1fe
[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 webdb.servlet.*;
14 import webdb.module.*;
15 import webdb.misc.*;
16 import webdb.entity.*;
17 import webdb.storage.*;
18
19 import mir.entity.*;
20 import mir.storage.*;
21 import mir.module.*;
22
23 /*
24  *  ServletModuleContent -
25  *  liefert HTML fuer Content
26  *
27  *
28  * @author RK
29  */
30
31 public class ServletModuleContent extends ServletModule
32 {
33
34         static ModuleTopics         themenModule;
35         static ModuleSchwerpunkt    schwerpunktModule;
36         static ModuleGruppen        gruppenModule;
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(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.Content.Logfile"));
49                         templateListString = Configuration.getProperty("ServletModule.Content.ListTemplate");
50                         templateOpString = Configuration.getProperty("ServletModule.Content.OpTemplate");
51                         templateObjektString = Configuration.getProperty("ServletModule.Content.ObjektTemplate");
52                         templateConfirmString = Configuration.getProperty("ServletModule.Content.ConfirmTemplate");
53                         mainModule = new ModuleContent(DatabaseContent.getInstance());
54                         themenModule = new ModuleTopics(DatabaseTopics.getInstance());
55                         schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
56                         gruppenModule = new ModuleGruppen(DatabaseGroups.getInstance());
57                         imageModule = new ModuleImages(DatabaseImages.getInstance());
58                 }
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.getParameter("to_topic"));
183                         //theLog.printDebugInfo(":: content :: inserted");
184                         _showObject(id, req, res);
185                 }
186                 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
187                 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
188         }
189
190         public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
191         {
192
193                 EntityUsers   user = _getUser(req);
194                 // hier pruefen ob dem akt. user loeschen erlaubt ist...
195                 String idParam = req.getParameter("id");
196                 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
197
198                 String confirmParam = req.getParameter("confirm");
199                 String cancelParam = req.getParameter("cancel");
200
201                 if (confirmParam == null && cancelParam == null) {
202                         // HTML Ausgabe zum Confirmen!
203                         SimpleHash mergeData = new SimpleHash();
204                         mergeData.put("module", "Content");
205                         mergeData.put("infoString", "Content: " + idParam);
206                         mergeData.put("id", idParam);
207                         mergeData.put("where", req.getParameter("where"));
208                         mergeData.put("order", req.getParameter("order"));
209                         mergeData.put("offset", req.getParameter("offset"));
210                         deliver(req, res, mergeData, templateConfirmString);
211                 }
212                 else {
213                         if (confirmParam!= null && !confirmParam.equals("")) {
214                                 try {
215                                         mainModule.deleteById(idParam);
216                                         //delete rows in the content_x_topic-table
217                                         DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
218                                         //delete rows in the comment-table
219                                         DatabaseComment.getInstance().deleteByContentId(idParam);
220                                 } catch (ModuleException e) {
221                                         throw new ServletModuleException(e.toString());
222                                 } catch (StorageObjectException e) {
223                                         throw new ServletModuleException(e.toString());
224                                 }
225                                 list(req,res);
226                         }
227                         else {
228                                 // Datensatz anzeigen
229                                 _showObject(idParam, req, res);
230                         }
231                 }
232         }
233
234         public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
235         {
236                 String        idParam = req.getParameter("id");
237                 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
238                 _showObject(idParam, req, res);
239         }
240
241         // methods for attaching media file
242         public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
243         {
244                 String  mediaIdParam = req.getParameter("mid");
245                 String  idParam = req.getParameter("cid");
246                 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
247                 try {
248                         EntityContent entContent = (EntityContent)mainModule.getById(idParam);
249                         entContent.attach(mediaIdParam);
250                 }
251                 catch(ModuleException e) {
252                         theLog.printError("smod content :: attach :: could not get entityContent");
253                 }
254                 _showObject(idParam, req, res);
255         }
256
257         public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
258         {
259                 String  idParam = req.getParameter("cid");
260                 if (idParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
261                 try {
262                         EntityContent entContent = (EntityContent)mainModule.getById(idParam);
263                         entContent.dettach();
264                 }
265                 catch(ModuleException e) {
266                         theLog.printError("smod content :: dettach :: could not get entityContent");
267                 }
268                 _showObject(idParam, req, res);
269         }
270
271         public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
272         {
273                 String  idParam = req.getParameter("id");
274                 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
275                 try {
276                         EntityContent entContent = (EntityContent)mainModule.getById(idParam);
277                         entContent.newswire();
278                 }
279                 catch(ModuleException e) {
280                         theLog.printError("smod content :: newswire :: could not get entityContent");
281                 }
282                 list(req, res);
283         }
284
285
286         public void update(HttpServletRequest req, HttpServletResponse res)
287                 throws ServletModuleException
288         {
289                 try {
290
291                         EntityUsers   user = _getUser(req);
292                         if (user==null) theLog.printDebugInfo("user null!");
293                         String idParam = req.getParameter("id");
294                         if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
295
296                         HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
297                         String topic_id = req.getParameter("to_topic");
298                         String content_id = req.getParameter("id");
299                         // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
300                         if(user != null) withValues.put("user_id", user.getId());
301                         withValues.put("is_produced", "0");
302                         if (!withValues.containsKey("is_published"))
303                                 withValues.put("is_published","0");
304                         if (!withValues.containsKey("is_html"))
305                                 withValues.put("is_html","0");
306                         if (withValues.get("creator").toString().equals(""))
307                                 withValues.put("creator","Anonym");
308                         //theLog.printDebugInfo("updating. ");
309                         String id = mainModule.set(withValues);
310                         DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),req.getParameter("to_topic"));
311                         //theLog.printDebugInfo("update done. ");
312                         String whereParam = req.getParameter("where");
313                         String orderParam = req.getParameter("order");
314                         if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
315                                 //theLog.printDebugInfo("update to list");
316                                 list(req,res);
317                         }
318                         else
319                                 _showObject(idParam, req, res);
320                 }
321                 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
322                 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
323         }
324
325         //
326         // Hilfsmethoden
327
328         private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
329                 throws ServletModuleException {
330
331                 try {
332                         EntityContent entContent=(EntityContent)mainModule.getById(id);
333                         SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(entContent);
334                         EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
335                         if (topicToContent!=null && topicToContent.size()>0){
336                                 Entity topics = (EntityTopics)topicToContent.elementAt(0);
337                                 mergeData.put("to_topic",topics.getId());
338                         }
339                         //obsolete, because of psqgl 7.1.x
340       //mergeData.put("content_data", entContent.getContentData());
341                         mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
342                         mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
343                         try {
344                                 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
345                         } catch (Exception e) {theLog.printError("articletype could not be fetched.");}
346
347                         // get the images
348                         String currentMediaId = entContent.getValue("to_media");
349                         SimpleHash imageHash = new SimpleHash();
350                         if (currentMediaId!=null && !currentMediaId.equals("")) {
351                                 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
352                                 mergeData.put("images", imageHash);
353                         }
354
355
356                         //mergeData.put("gruppenHashData", gruppenModule.getHashData());
357                         mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
358                         // hier code um zur liste zurueckzukommen
359                         String offsetParam, whereParam, orderParam;
360                         if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
361                         if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
362                         if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
363                         mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
364                         deliver(req, res, mergeData, templateObjektString);
365                 }
366                 catch (Exception e) { throw new ServletModuleException(e.toString());}
367         }
368
369
370         public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
371                 throws ServletModuleException {
372
373                 try {
374                         // hier dann htmlcode rausschreiben
375                         if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
376                                 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
377                                 modelRoot.put("themenHashData", themenModule.getHashData());
378                                 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
379                                 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
380                                 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
381                                 deliver(req, res, modelRoot, templateListString);
382                         } else  { // count = 1
383                                 _showObject(theList.elementAt(0).getId(),req,res);
384                         }
385                 } catch (StorageObjectException e) {
386                         throw new ServletModuleException(e.toString());
387                 }
388         }
389
390         public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
391                 throws ServletModuleException {
392
393                 try {
394                         // hier dann htmlcode rausschreiben
395                         if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
396                                 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
397                                 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
398                                 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
399                                 deliver(req, res, modelRoot, templateListString);
400                         } else  { // count = 1
401                                 _showObject(theList.elementAt(0).getId(), req, res);
402                         }
403                 } catch (StorageObjectException e) {
404                         throw new ServletModuleException(e.toString());
405                 }
406         }
407
408         private EntityUsers _getUser(HttpServletRequest req)
409         {
410                 HttpSession session=req.getSession(false);
411                 return (EntityUsers)session.getAttribute("login.uid");
412         }
413
414
415 }
416