1-n-relation content to topic
[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.getParameterValues("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[] topic_id = req.getParameterValues("to_topic");
300                         String content_id = req.getParameter("id");
301                         // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
302                         if(user != null) withValues.put("user_id", user.getId());
303                         withValues.put("is_produced", "0");
304                         if (!withValues.containsKey("is_published"))
305                                 withValues.put("is_published","0");
306                         if (!withValues.containsKey("is_html"))
307                                 withValues.put("is_html","0");
308                         if (withValues.get("creator").toString().equals(""))
309                                 withValues.put("creator","Anonym");
310                         //theLog.printDebugInfo("updating. ");
311                         String id = mainModule.set(withValues);
312                         DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
313                         //theLog.printDebugInfo("update done. ");
314                         String whereParam = req.getParameter("where");
315                         String orderParam = req.getParameter("order");
316                         if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
317                                 //theLog.printDebugInfo("update to list");
318                                 list(req,res);
319                         }
320                         else
321                                 _showObject(idParam, req, res);
322                 }
323                 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
324                 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
325         }
326
327         //
328         // Hilfsmethoden
329
330         private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
331                 throws ServletModuleException {
332
333                 try {
334                         EntityContent entContent=(EntityContent)mainModule.getById(id);
335                         SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(entContent);
336                         EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
337                         if (topicToContent!=null && topicToContent.size()>0){
338         theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
339         Entity topics = null;
340         SimpleList topicList = new SimpleList();
341         for(int i=0;i<topicToContent.size();i++){
342           topics = (EntityTopics)topicToContent.elementAt(i);
343           topicList.add(topics.getId());
344         }
345                                 mergeData.put("to_topic",topicList);
346                         }
347                         //obsolete, because of psqgl 7.1.x
348       //mergeData.put("content_data", entContent.getContentData());
349                         mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
350                         mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
351                         try {
352                                 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
353                         } catch (Exception e) {theLog.printError("articletype could not be fetched.");}
354
355                         // get the images
356                         String currentMediaId = entContent.getValue("to_media");
357                         SimpleHash imageHash = new SimpleHash();
358                         if (currentMediaId!=null && !currentMediaId.equals("")) {
359                                 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
360                                 mergeData.put("images", imageHash);
361                         }
362
363
364                         //mergeData.put("gruppenHashData", gruppenModule.getHashData());
365                         mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
366                         // hier code um zur liste zurueckzukommen
367                         String offsetParam, whereParam, orderParam;
368                         if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
369                         if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
370                         if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
371                         mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
372                         deliver(req, res, mergeData, templateObjektString);
373                 }
374                 catch (Exception e) { throw new ServletModuleException(e.toString());}
375         }
376
377
378         public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
379                 throws ServletModuleException {
380
381                 try {
382                         // hier dann htmlcode rausschreiben
383                         if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
384                                 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
385                                 modelRoot.put("themenHashData", themenModule.getHashData());
386                                 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
387                                 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
388                                 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
389                                 deliver(req, res, modelRoot, templateListString);
390                         } else  { // count = 1
391                                 _showObject(theList.elementAt(0).getId(),req,res);
392                         }
393                 } catch (StorageObjectException e) {
394                         throw new ServletModuleException(e.toString());
395                 }
396         }
397
398         public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
399                 throws ServletModuleException {
400
401                 try {
402                         // hier dann htmlcode rausschreiben
403                         if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
404                                 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
405                                 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
406                                 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
407                                 deliver(req, res, modelRoot, templateListString);
408                         } else  { // count = 1
409                                 _showObject(theList.elementAt(0).getId(), req, res);
410                         }
411                 } catch (StorageObjectException e) {
412                         throw new ServletModuleException(e.toString());
413                 }
414         }
415
416         private EntityUsers _getUser(HttpServletRequest req)
417         {
418                 HttpSession session=req.getSession(false);
419                 return (EntityUsers)session.getAttribute("login.uid");
420         }
421
422
423 }
424