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");
267 DatabaseContentToMedia.getInstance().setMedia(idParam,mediaIdParam);
268 } catch(Exception e){
269 theLog.printError("set media failed");
271 _showObject(idParam, req, res);
274 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
276 String idParam = req.getParameter("cid");
277 if (idParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
280 //schould be deleted soon
282 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
283 entContent.dettach();
285 catch(ModuleException e) {
286 theLog.printError("smod content :: dettach :: could not get entityContent");
291 DatabaseContentToMedia.getInstance().deleteByContentId(idParam);
292 } catch(Exception e){
293 theLog.printError("remove media failed");
295 _showObject(idParam, req, res);
298 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
300 String idParam = req.getParameter("id");
301 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
303 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
304 entContent.newswire();
306 catch(ModuleException e) {
307 theLog.printError("smod content :: newswire :: could not get entityContent");
313 public void update(HttpServletRequest req, HttpServletResponse res)
314 throws ServletModuleException
318 EntityUsers user = _getUser(req);
319 if (user==null) theLog.printDebugInfo("user null!");
320 String idParam = req.getParameter("id");
321 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
323 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
324 //String topic_id = req.getParameter("to_topic");
325 String[] topic_id = req.getParameterValues("to_topic");
326 String content_id = req.getParameter("id");
327 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
328 if(user != null) withValues.put("user_id", user.getId());
329 withValues.put("is_produced", "0");
330 if (!withValues.containsKey("is_published"))
331 withValues.put("is_published","0");
332 if (!withValues.containsKey("is_html"))
333 withValues.put("is_html","0");
334 if (withValues.get("creator").toString().equals(""))
335 withValues.put("creator","Anonym");
336 //theLog.printDebugInfo("updating. ");
337 String id = mainModule.set(withValues);
338 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
339 //theLog.printDebugInfo("update done. ");
340 String whereParam = req.getParameter("where");
341 String orderParam = req.getParameter("order");
342 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
343 //theLog.printDebugInfo("update to list");
347 _showObject(idParam, req, res);
349 catch (StorageObjectException e) {
350 throw new ServletModuleException(e.toString());
352 catch (ModuleException e) {
353 throw new ServletModuleException(e.toString());
360 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
361 throws ServletModuleException {
364 EntityContent entContent=(EntityContent)mainModule.getById(id);
365 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
366 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
367 if (topicToContent!=null && topicToContent.size()>0){
368 theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
369 Entity topics = null;
370 SimpleList topicList = new SimpleList();
371 for(int i=0;i<topicToContent.size();i++){
372 topics = (EntityTopics)topicToContent.elementAt(i);
373 topicList.add(topics.getId());
375 mergeData.put("to_topic",topicList);
377 //obsolete, because of psqgl 7.1.x
378 //mergeData.put("content_data", entContent.getContentData());
379 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
380 mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
382 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
383 } catch (Exception e) {
384 theLog.printError("articletype could not be fetched.");
387 mergeData.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
388 } catch (Exception e) {
389 theLog.printError("language-popup could not be fetched.");
392 String currentMediaId = entContent.getValue("to_media");
393 SimpleHash imageHash = new SimpleHash();
394 if (currentMediaId!=null && !currentMediaId.equals("")) {
395 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
396 mergeData.put("images", imageHash);
400 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
401 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
402 // hier code um zur liste zurueckzukommen
403 String offsetParam, whereParam, orderParam;
404 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
405 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
406 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
407 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
408 deliver(req, res, mergeData, templateObjektString);
409 } catch (Exception e) {
410 throw new ServletModuleException(e.toString());
415 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
416 throws ServletModuleException {
419 // hier dann htmlcode rausschreiben
420 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
421 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
422 modelRoot.put("themenHashData", themenModule.getHashData());
423 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
424 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
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 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
436 throws ServletModuleException {
439 // hier dann htmlcode rausschreiben
440 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
441 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
442 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
443 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
444 deliver(req, res, modelRoot, templateListString);
445 } else { // count = 1
446 _showObject(theList.elementAt(0).getId(), req, res);
448 } catch (StorageObjectException e) {
449 throw new ServletModuleException(e.toString());
453 private EntityUsers _getUser(HttpServletRequest req)
455 HttpSession session=req.getSession(false);
456 return (EntityUsers)session.getAttribute("login.uid");