1 package mircoders.servlet;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
11 import freemarker.template.*;
19 import mircoders.storage.*;
20 import mircoders.module.*;
21 import mircoders.entity.*;
25 * ServletModuleContent -
26 * liefert HTML fuer Content
32 public class ServletModuleContent extends ServletModule
35 static ModuleTopics themenModule;
36 static ModuleSchwerpunkt schwerpunktModule;
37 static ModuleImages imageModule;
39 static String templateOpString;
41 // Singelton / Kontruktor
43 private static ServletModuleContent instance = new ServletModuleContent();
44 public static ServletModule getInstance() { return instance; }
46 private ServletModuleContent() {
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");
64 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
67 EntityUsers user = _getUser(req);
69 String offsetParam = req.getParameter("offset");
72 // hier offsetcode bearbeiteb
73 if (offsetParam != null && !offsetParam.equals(""))
74 offset = Integer.parseInt(offsetParam);
76 if (req.getParameter("next") != null)
77 offset=Integer.parseInt(req.getParameter("nextoffset"));
79 if (req.getParameter("prev") != null)
80 offset = Integer.parseInt(req.getParameter("prevoffset"));
82 String whereParam = req.getParameter("where");
83 String orderParam = req.getParameter("order");
85 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
86 _list(theList, req, res);
87 } catch (ModuleException e) {
88 throw new ServletModuleException(e.toString());
92 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
95 EntityUsers user = _getUser(req);
97 String offsetParam = req.getParameter("offset");
100 String whereParam = req.getParameter("where");
102 if (whereParam==null) whereParam = "to_article_type='0'";
104 // hier offsetcode bearbeiteb
105 if (offsetParam != null && !offsetParam.equals(""))
106 offset = Integer.parseInt(offsetParam);
108 if (req.getParameter("next") != null)
109 offset=Integer.parseInt(req.getParameter("nextoffset"));
111 if (req.getParameter("prev") != null)
112 offset = Integer.parseInt(req.getParameter("prevoffset"));
114 String orderParam = req.getParameter("order");
116 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
117 _list(theList, req, res);
118 } catch (ModuleException e) {
119 throw new ServletModuleException(e.toString());
124 public void search(HttpServletRequest req, HttpServletResponse res)
125 throws ServletModuleException {
127 EntityUsers user = _getUser(req);
129 String fieldParam = req.getParameter("field");
130 String fieldValueParam = req.getParameter("fieldvalue");
131 String orderParam = req.getParameter("order");
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());
140 public void add(HttpServletRequest req, HttpServletResponse res)
141 throws ServletModuleException {
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());
151 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
152 } catch (Exception e) {
153 theLog.printError("articletype could not be fetched.");
156 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
157 } catch (Exception e) {
158 theLog.printError("language-popup could not be fetched.");
160 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
161 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(user));
162 deliver(req, res, mergeData, templateObjektString);
166 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
168 //theLog.printDebugInfo(":: content :: trying to insert");
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);
189 catch (StorageObjectException e) {
190 throw new ServletModuleException(e.toString());
192 catch (ModuleException e) {
193 throw new ServletModuleException(e.toString());
197 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
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");
205 String confirmParam = req.getParameter("confirm");
206 String cancelParam = req.getParameter("cancel");
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);
220 if (confirmParam!= null && !confirmParam.equals("")) {
222 mainModule.deleteById(idParam);
224 /** @todo the following two should be imlied in
227 //delete rows in the content_x_topic-table
228 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
229 //delete rows in the comment-table
230 DatabaseComment.getInstance().deleteByContentId(idParam);
231 } catch (ModuleException e) {
232 throw new ServletModuleException(e.toString());
233 } catch (StorageObjectException e) {
234 throw new ServletModuleException(e.toString());
239 // Datensatz anzeigen
240 _showObject(idParam, req, res);
245 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
247 String idParam = req.getParameter("id");
248 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
249 _showObject(idParam, req, res);
252 // methods for attaching media file
253 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
255 String mediaIdParam = req.getParameter("mid");
256 String idParam = req.getParameter("cid");
257 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
260 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
261 entContent.attach(mediaIdParam);
263 catch(ModuleException e) {
264 theLog.printError("smod content :: attach :: could not get entityContent");
266 _showObject(idParam, req, res);
269 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
271 String cidParam = req.getParameter("cid");
272 String midParam = req.getParameter("mid");
273 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
274 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
277 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
278 entContent.dettach(cidParam,midParam);
280 catch(ModuleException e) {
281 theLog.printError("smod content :: dettach :: could not get entityContent");
283 _showObject(cidParam, req, res);
286 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
288 String idParam = req.getParameter("id");
289 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
291 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
292 entContent.newswire();
294 catch(ModuleException e) {
295 theLog.printError("smod content :: newswire :: could not get entityContent");
301 public void update(HttpServletRequest req, HttpServletResponse res)
302 throws ServletModuleException
306 EntityUsers user = _getUser(req);
307 if (user==null) theLog.printDebugInfo("user null!");
308 String idParam = req.getParameter("id");
309 if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
311 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
312 String[] topic_id = req.getParameterValues("to_topic");
313 String content_id = req.getParameter("id");
314 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
315 if(user != null) withValues.put("user_id", user.getId());
316 withValues.put("is_produced", "0");
317 if (!withValues.containsKey("is_published"))
318 withValues.put("is_published","0");
319 if (!withValues.containsKey("is_html"))
320 withValues.put("is_html","0");
321 if (withValues.get("creator").toString().equals(""))
322 withValues.put("creator","Anonym");
323 //theLog.printDebugInfo("updating. ");
324 String id = mainModule.set(withValues);
325 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
326 //theLog.printDebugInfo("update done. ");
327 String whereParam = req.getParameter("where");
328 String orderParam = req.getParameter("order");
329 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
330 //theLog.printDebugInfo("update to list");
334 _showObject(idParam, req, res);
336 catch (StorageObjectException e) {
337 throw new ServletModuleException(e.toString());
339 catch (ModuleException e) {
340 throw new ServletModuleException(e.toString());
347 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
348 throws ServletModuleException {
351 EntityContent entContent=(EntityContent)mainModule.getById(id);
352 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
353 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
354 if (topicToContent!=null && topicToContent.size()>0){
355 theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
356 Entity topics = null;
357 SimpleList topicList = new SimpleList();
358 for(int i=0;i<topicToContent.size();i++){
359 topics = (EntityTopics)topicToContent.elementAt(i);
360 topicList.add(topics.getId());
362 mergeData.put("to_topic",topicList);
364 //obsolete, because of psqgl 7.1.x
365 //mergeData.put("content_data", entContent.getContentData());
366 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
368 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
369 } catch (Exception e) {
370 theLog.printError("articletype could not be fetched.");
373 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
374 } catch (Exception e) {
375 theLog.printError("language-popup could not be fetched.");
378 EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(entContent);
379 if (currentMedia!=null && currentMedia.getCount()>=1) {
380 SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
381 mergeData.put("to_media", mediaList);
385 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
386 // hier code um zur liste zurueckzukommen
387 String offsetParam, whereParam, orderParam;
388 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
389 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
390 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
391 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
392 deliver(req, res, mergeData, templateObjektString);
393 } catch (Exception e) {
394 throw new ServletModuleException(e.toString());
399 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
400 throws ServletModuleException {
403 // hier dann htmlcode rausschreiben
404 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
405 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
406 modelRoot.put("themenHashData", themenModule.getHashData());
407 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
408 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
409 deliver(req, res, modelRoot, templateListString);
410 } else { // count = 1
411 _showObject(theList.elementAt(0).getId(),req,res);
413 } catch (StorageObjectException e) {
414 throw new ServletModuleException(e.toString());
418 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
419 throws ServletModuleException {
423 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
424 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
425 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
426 deliver(req, res, modelRoot, templateListString);
427 } else { // count = 1
428 _showObject(theList.elementAt(0).getId(), req, res);
430 } catch (StorageObjectException e) {
431 throw new ServletModuleException(e.toString());
435 private EntityUsers _getUser(HttpServletRequest req)
437 HttpSession session=req.getSession(false);
438 return (EntityUsers)session.getAttribute("login.uid");