1 package mircoders.servlet;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
11 import freemarker.template.*;
14 import mircoders.module.*;
16 import mircoders.entity.*;
20 import mircoders.storage.*;
24 * ServletModuleContent -
25 * liefert HTML fuer Content
31 public class ServletModuleContent extends ServletModule
34 static ModuleTopics themenModule;
35 static ModuleSchwerpunkt schwerpunktModule;
36 static ModuleGruppen gruppenModule;
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(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.Content.Logfile"));
49 templateListString = Configuration.getProperty("ServletModule.Content.ListTemplate");
50 templateOpString = Configuration.getProperty("ServletModule.Content.OpTemplate");
51 templateObjektString = Configuration.getProperty("ServletModule.Content.ObjektTemplate");
52 templateConfirmString = Configuration.getProperty("ServletModule.Content.ConfirmTemplate");
53 mainModule = new ModuleContent(DatabaseContent.getInstance());
54 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
55 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
56 gruppenModule = new ModuleGruppen(DatabaseGroups.getInstance());
57 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) {theLog.printError("articletype could not be fetched.");}
156 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
157 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
158 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(user));
159 deliver(req, res, mergeData, templateObjektString);
163 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
165 //theLog.printDebugInfo(":: content :: trying to insert");
167 EntityUsers user = _getUser(req);
168 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
169 //theLog.printDebugInfo(":: content :: got intersecting values");
170 String now = StringUtil.date2webdbDate(new GregorianCalendar());
171 withValues.put("date", now);
172 withValues.put("publish_path", StringUtil.webdbDate2path(now));
173 withValues.put("to_publisher", user.getId());
174 withValues.put("is_produced", "0");
175 if (!withValues.containsKey("is_published"))
176 withValues.put("is_published","0");
177 if (!withValues.containsKey("is_html"))
178 withValues.put("is_html","0");
179 if (withValues.get("creator").toString().equals(""))
180 withValues.put("creator","Anonym");
181 String id = mainModule.add(withValues);
182 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameter("to_topic"));
183 //theLog.printDebugInfo(":: content :: inserted");
184 _showObject(id, req, res);
186 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
187 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
190 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
193 EntityUsers user = _getUser(req);
194 // hier pruefen ob dem akt. user loeschen erlaubt ist...
195 String idParam = req.getParameter("id");
196 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
198 String confirmParam = req.getParameter("confirm");
199 String cancelParam = req.getParameter("cancel");
201 if (confirmParam == null && cancelParam == null) {
202 // HTML Ausgabe zum Confirmen!
203 SimpleHash mergeData = new SimpleHash();
204 mergeData.put("module", "Content");
205 mergeData.put("infoString", "Content: " + idParam);
206 mergeData.put("id", idParam);
207 mergeData.put("where", req.getParameter("where"));
208 mergeData.put("order", req.getParameter("order"));
209 mergeData.put("offset", req.getParameter("offset"));
210 deliver(req, res, mergeData, templateConfirmString);
213 if (confirmParam!= null && !confirmParam.equals("")) {
215 mainModule.deleteById(idParam);
216 //delete rows in the content_x_topic-table
217 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
218 //delete rows in the comment-table
219 DatabaseComment.getInstance().deleteByContentId(idParam);
220 } catch (ModuleException e) {
221 throw new ServletModuleException(e.toString());
222 } catch (StorageObjectException e) {
223 throw new ServletModuleException(e.toString());
228 // Datensatz anzeigen
229 _showObject(idParam, req, res);
234 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
236 String idParam = req.getParameter("id");
237 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
238 _showObject(idParam, req, res);
241 // methods for attaching media file
242 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
244 String mediaIdParam = req.getParameter("mid");
245 String idParam = req.getParameter("cid");
246 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
248 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
249 entContent.attach(mediaIdParam);
251 catch(ModuleException e) {
252 theLog.printError("smod content :: attach :: could not get entityContent");
254 _showObject(idParam, req, res);
257 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
259 String idParam = req.getParameter("cid");
260 if (idParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
262 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
263 entContent.dettach();
265 catch(ModuleException e) {
266 theLog.printError("smod content :: dettach :: could not get entityContent");
268 _showObject(idParam, req, res);
271 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
273 String idParam = req.getParameter("id");
274 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
276 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
277 entContent.newswire();
279 catch(ModuleException e) {
280 theLog.printError("smod content :: newswire :: could not get entityContent");
286 public void update(HttpServletRequest req, HttpServletResponse res)
287 throws ServletModuleException
291 EntityUsers user = _getUser(req);
292 if (user==null) theLog.printDebugInfo("user null!");
293 String idParam = req.getParameter("id");
294 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
296 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
297 String topic_id = req.getParameter("to_topic");
298 String content_id = req.getParameter("id");
299 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
300 if(user != null) withValues.put("user_id", user.getId());
301 withValues.put("is_produced", "0");
302 if (!withValues.containsKey("is_published"))
303 withValues.put("is_published","0");
304 if (!withValues.containsKey("is_html"))
305 withValues.put("is_html","0");
306 if (withValues.get("creator").toString().equals(""))
307 withValues.put("creator","Anonym");
308 //theLog.printDebugInfo("updating. ");
309 String id = mainModule.set(withValues);
310 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),req.getParameter("to_topic"));
311 //theLog.printDebugInfo("update done. ");
312 String whereParam = req.getParameter("where");
313 String orderParam = req.getParameter("order");
314 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
315 //theLog.printDebugInfo("update to list");
319 _showObject(idParam, req, res);
321 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
322 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
328 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
329 throws ServletModuleException {
332 EntityContent entContent=(EntityContent)mainModule.getById(id);
333 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
334 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
335 if (topicToContent!=null && topicToContent.size()>0){
336 Entity topics = (EntityTopics)topicToContent.elementAt(0);
337 mergeData.put("to_topic",topics.getId());
339 //obsolete, because of psqgl 7.1.x
340 //mergeData.put("content_data", entContent.getContentData());
341 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
342 mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
344 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
345 } catch (Exception e) {theLog.printError("articletype could not be fetched.");}
348 String currentMediaId = entContent.getValue("to_media");
349 SimpleHash imageHash = new SimpleHash();
350 if (currentMediaId!=null && !currentMediaId.equals("")) {
351 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
352 mergeData.put("images", imageHash);
356 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
357 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
358 // hier code um zur liste zurueckzukommen
359 String offsetParam, whereParam, orderParam;
360 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
361 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
362 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
363 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
364 deliver(req, res, mergeData, templateObjektString);
366 catch (Exception e) { throw new ServletModuleException(e.toString());}
370 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
371 throws ServletModuleException {
374 // hier dann htmlcode rausschreiben
375 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
376 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
377 modelRoot.put("themenHashData", themenModule.getHashData());
378 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
379 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
380 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
381 deliver(req, res, modelRoot, templateListString);
382 } else { // count = 1
383 _showObject(theList.elementAt(0).getId(),req,res);
385 } catch (StorageObjectException e) {
386 throw new ServletModuleException(e.toString());
390 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
391 throws ServletModuleException {
394 // hier dann htmlcode rausschreiben
395 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
396 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
397 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
398 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
399 deliver(req, res, modelRoot, templateListString);
400 } else { // count = 1
401 _showObject(theList.elementAt(0).getId(), req, res);
403 } catch (StorageObjectException e) {
404 throw new ServletModuleException(e.toString());
408 private EntityUsers _getUser(HttpServletRequest req)
410 HttpSession session=req.getSession(false);
411 return (EntityUsers)session.getAttribute("login.uid");