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