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 ModuleGruppen gruppenModule;
38 static ModuleImages imageModule;
40 static String templateOpString;
42 // Singelton / Kontruktor
44 private static ServletModuleContent instance = new ServletModuleContent();
45 public static ServletModule getInstance() { return instance; }
47 private ServletModuleContent() {
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");
66 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
69 EntityUsers user = _getUser(req);
71 String offsetParam = req.getParameter("offset");
74 // hier offsetcode bearbeiteb
75 if (offsetParam != null && !offsetParam.equals(""))
76 offset = Integer.parseInt(offsetParam);
78 if (req.getParameter("next") != null)
79 offset=Integer.parseInt(req.getParameter("nextoffset"));
81 if (req.getParameter("prev") != null)
82 offset = Integer.parseInt(req.getParameter("prevoffset"));
84 String whereParam = req.getParameter("where");
85 String orderParam = req.getParameter("order");
87 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
88 _list(theList, req, res);
89 } catch (ModuleException e) {
90 throw new ServletModuleException(e.toString());
94 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
97 EntityUsers user = _getUser(req);
99 String offsetParam = req.getParameter("offset");
102 String whereParam = req.getParameter("where");
104 if (whereParam==null) whereParam = "to_article_type='0'";
106 // hier offsetcode bearbeiteb
107 if (offsetParam != null && !offsetParam.equals(""))
108 offset = Integer.parseInt(offsetParam);
110 if (req.getParameter("next") != null)
111 offset=Integer.parseInt(req.getParameter("nextoffset"));
113 if (req.getParameter("prev") != null)
114 offset = Integer.parseInt(req.getParameter("prevoffset"));
116 String orderParam = req.getParameter("order");
118 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
119 _list(theList, req, res);
120 } catch (ModuleException e) {
121 throw new ServletModuleException(e.toString());
126 public void search(HttpServletRequest req, HttpServletResponse res)
127 throws ServletModuleException {
129 EntityUsers user = _getUser(req);
131 String fieldParam = req.getParameter("field");
132 String fieldValueParam = req.getParameter("fieldvalue");
133 String orderParam = req.getParameter("order");
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());
142 public void add(HttpServletRequest req, HttpServletResponse res)
143 throws ServletModuleException {
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());
154 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
155 } catch (Exception e) {
156 theLog.printError("articletype could not be fetched.");
159 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
160 } catch (Exception e) {
161 theLog.printError("language-popup could not be fetched.");
163 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
164 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(user));
165 deliver(req, res, mergeData, templateObjektString);
169 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
171 //theLog.printDebugInfo(":: content :: trying to insert");
173 EntityUsers user = _getUser(req);
174 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
175 //theLog.printDebugInfo(":: content :: got intersecting values");
176 String now = StringUtil.date2webdbDate(new GregorianCalendar());
177 withValues.put("date", now);
178 withValues.put("publish_path", StringUtil.webdbDate2path(now));
179 withValues.put("to_publisher", user.getId());
180 withValues.put("is_produced", "0");
181 if (!withValues.containsKey("is_published"))
182 withValues.put("is_published","0");
183 if (!withValues.containsKey("is_html"))
184 withValues.put("is_html","0");
185 if (withValues.get("creator").toString().equals(""))
186 withValues.put("creator","Anonym");
187 String id = mainModule.add(withValues);
188 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
189 //theLog.printDebugInfo(":: content :: inserted");
190 _showObject(id, req, res);
192 catch (StorageObjectException e) {
193 throw new ServletModuleException(e.toString());
195 catch (ModuleException e) {
196 throw new ServletModuleException(e.toString());
200 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
203 EntityUsers user = _getUser(req);
204 // hier pruefen ob dem akt. user loeschen erlaubt ist...
205 String idParam = req.getParameter("id");
206 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
208 String confirmParam = req.getParameter("confirm");
209 String cancelParam = req.getParameter("cancel");
211 if (confirmParam == null && cancelParam == null) {
212 // HTML Ausgabe zum Confirmen!
213 SimpleHash mergeData = new SimpleHash();
214 mergeData.put("module", "Content");
215 mergeData.put("infoString", "Content: " + idParam);
216 mergeData.put("id", idParam);
217 mergeData.put("where", req.getParameter("where"));
218 mergeData.put("order", req.getParameter("order"));
219 mergeData.put("offset", req.getParameter("offset"));
220 deliver(req, res, mergeData, templateConfirmString);
223 if (confirmParam!= null && !confirmParam.equals("")) {
225 mainModule.deleteById(idParam);
226 //delete rows in the content_x_topic-table
227 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
228 //delete rows in the comment-table
229 DatabaseComment.getInstance().deleteByContentId(idParam);
230 } catch (ModuleException e) {
231 throw new ServletModuleException(e.toString());
232 } catch (StorageObjectException e) {
233 throw new ServletModuleException(e.toString());
238 // Datensatz anzeigen
239 _showObject(idParam, req, res);
244 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
246 String idParam = req.getParameter("id");
247 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
248 _showObject(idParam, req, res);
251 // methods for attaching media file
252 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
254 String mediaIdParam = req.getParameter("mid");
255 String idParam = req.getParameter("cid");
256 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
259 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
260 entContent.attach(mediaIdParam);
262 catch(ModuleException e) {
263 theLog.printError("smod content :: attach :: could not get entityContent");
265 _showObject(idParam, req, res);
268 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
270 String cidParam = req.getParameter("cid");
271 String midParam = req.getParameter("mid");
272 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
273 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
276 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
277 entContent.dettach(cidParam,midParam);
279 catch(ModuleException e) {
280 theLog.printError("smod content :: dettach :: could not get entityContent");
282 _showObject(cidParam, req, res);
285 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
287 String idParam = req.getParameter("id");
288 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
290 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
291 entContent.newswire();
293 catch(ModuleException e) {
294 theLog.printError("smod content :: newswire :: could not get entityContent");
300 public void update(HttpServletRequest req, HttpServletResponse res)
301 throws ServletModuleException
305 EntityUsers user = _getUser(req);
306 if (user==null) theLog.printDebugInfo("user null!");
307 String idParam = req.getParameter("id");
308 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
310 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
311 //String topic_id = req.getParameter("to_topic");
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());
367 mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
369 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
370 } catch (Exception e) {
371 theLog.printError("articletype could not be fetched.");
374 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
375 } catch (Exception e) {
376 theLog.printError("language-popup could not be fetched.");
379 EntityList currentMedia = DatabaseContentToMedia.getInstance().getMedia(entContent);
380 if (currentMedia!=null && currentMedia.getCount()>=1) {
381 SimpleList mediaList = HTMLTemplateProcessor.makeSimpleList(currentMedia);
382 mergeData.put("to_media", mediaList);
386 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
387 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
388 // hier code um zur liste zurueckzukommen
389 String offsetParam, whereParam, orderParam;
390 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
391 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
392 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
393 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
394 deliver(req, res, mergeData, templateObjektString);
395 } catch (Exception e) {
396 throw new ServletModuleException(e.toString());
401 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
402 throws ServletModuleException {
405 // hier dann htmlcode rausschreiben
406 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
407 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
408 modelRoot.put("themenHashData", themenModule.getHashData());
409 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
410 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
411 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
412 deliver(req, res, modelRoot, templateListString);
413 } else { // count = 1
414 _showObject(theList.elementAt(0).getId(),req,res);
416 } catch (StorageObjectException e) {
417 throw new ServletModuleException(e.toString());
421 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
422 throws ServletModuleException {
425 // hier dann htmlcode rausschreiben
426 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
427 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
428 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
429 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
430 deliver(req, res, modelRoot, templateListString);
431 } else { // count = 1
432 _showObject(theList.elementAt(0).getId(), req, res);
434 } catch (StorageObjectException e) {
435 throw new ServletModuleException(e.toString());
439 private EntityUsers _getUser(HttpServletRequest req)
441 HttpSession session=req.getSession(false);
442 return (EntityUsers)session.getAttribute("login.uid");