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());
60 catch (StorageObjectException e) {
61 theLog.printDebugInfo("servletmodulecontent konnte nicht initialisiert werden");
67 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
70 EntityUsers user = _getUser(req);
72 String offsetParam = req.getParameter("offset");
75 // hier offsetcode bearbeiteb
76 if (offsetParam != null && !offsetParam.equals(""))
77 offset = Integer.parseInt(offsetParam);
79 if (req.getParameter("next") != null)
80 offset=Integer.parseInt(req.getParameter("nextoffset"));
82 if (req.getParameter("prev") != null)
83 offset = Integer.parseInt(req.getParameter("prevoffset"));
85 String whereParam = req.getParameter("where");
86 String orderParam = req.getParameter("order");
88 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
89 _list(theList, req, res);
90 } catch (ModuleException e) {
91 throw new ServletModuleException(e.toString());
95 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
98 EntityUsers user = _getUser(req);
100 String offsetParam = req.getParameter("offset");
103 String whereParam = req.getParameter("where");
105 if (whereParam==null) whereParam = "to_article_type='0'";
107 // hier offsetcode bearbeiteb
108 if (offsetParam != null && !offsetParam.equals(""))
109 offset = Integer.parseInt(offsetParam);
111 if (req.getParameter("next") != null)
112 offset=Integer.parseInt(req.getParameter("nextoffset"));
114 if (req.getParameter("prev") != null)
115 offset = Integer.parseInt(req.getParameter("prevoffset"));
117 String orderParam = req.getParameter("order");
119 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
120 _list(theList, req, res);
121 } catch (ModuleException e) {
122 throw new ServletModuleException(e.toString());
127 public void search(HttpServletRequest req, HttpServletResponse res)
128 throws ServletModuleException {
130 EntityUsers user = _getUser(req);
132 String fieldParam = req.getParameter("field");
133 String fieldValueParam = req.getParameter("fieldvalue");
134 String orderParam = req.getParameter("order");
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());
143 public void add(HttpServletRequest req, HttpServletResponse res)
144 throws ServletModuleException {
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());
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);
164 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
166 //theLog.printDebugInfo(":: content :: trying to insert");
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.getParameter("to_topic"));
184 //theLog.printDebugInfo(":: content :: inserted");
185 _showObject(id, req, res);
187 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
188 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
191 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
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");
199 String confirmParam = req.getParameter("confirm");
200 String cancelParam = req.getParameter("cancel");
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);
214 if (confirmParam!= null && !confirmParam.equals("")) {
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());
229 // Datensatz anzeigen
230 _showObject(idParam, req, res);
235 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
237 String idParam = req.getParameter("id");
238 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
239 _showObject(idParam, req, res);
242 // methods for attaching media file
243 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
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");
249 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
250 entContent.attach(mediaIdParam);
252 catch(ModuleException e) {
253 theLog.printError("smod content :: attach :: could not get entityContent");
255 _showObject(idParam, req, res);
258 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
260 String idParam = req.getParameter("cid");
261 if (idParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
263 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
264 entContent.dettach();
266 catch(ModuleException e) {
267 theLog.printError("smod content :: dettach :: could not get entityContent");
269 _showObject(idParam, req, res);
272 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
274 String idParam = req.getParameter("id");
275 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
277 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
278 entContent.newswire();
280 catch(ModuleException e) {
281 theLog.printError("smod content :: newswire :: could not get entityContent");
287 public void update(HttpServletRequest req, HttpServletResponse res)
288 throws ServletModuleException
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");
297 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
298 String topic_id = req.getParameter("to_topic");
299 String content_id = req.getParameter("id");
300 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
301 if(user != null) withValues.put("user_id", user.getId());
302 withValues.put("is_produced", "0");
303 if (!withValues.containsKey("is_published"))
304 withValues.put("is_published","0");
305 if (!withValues.containsKey("is_html"))
306 withValues.put("is_html","0");
307 if (withValues.get("creator").toString().equals(""))
308 withValues.put("creator","Anonym");
309 //theLog.printDebugInfo("updating. ");
310 String id = mainModule.set(withValues);
311 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),req.getParameter("to_topic"));
312 //theLog.printDebugInfo("update done. ");
313 String whereParam = req.getParameter("where");
314 String orderParam = req.getParameter("order");
315 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
316 //theLog.printDebugInfo("update to list");
320 _showObject(idParam, req, res);
322 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
323 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
329 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
330 throws ServletModuleException {
333 EntityContent entContent=(EntityContent)mainModule.getById(id);
334 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
335 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
336 if (topicToContent!=null && topicToContent.size()>0){
337 Entity topics = (EntityTopics)topicToContent.elementAt(0);
338 mergeData.put("to_topic",topics.getId());
340 //obsolete, because of psqgl 7.1.x
341 //mergeData.put("content_data", entContent.getContentData());
342 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
343 mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
345 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
346 } catch (Exception e) {theLog.printError("articletype could not be fetched.");}
349 String currentMediaId = entContent.getValue("to_media");
350 SimpleHash imageHash = new SimpleHash();
351 if (currentMediaId!=null && !currentMediaId.equals("")) {
352 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
353 mergeData.put("images", imageHash);
357 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
358 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
359 // hier code um zur liste zurueckzukommen
360 String offsetParam, whereParam, orderParam;
361 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
362 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
363 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
364 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
365 deliver(req, res, mergeData, templateObjektString);
367 catch (Exception e) { throw new ServletModuleException(e.toString());}
371 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
372 throws ServletModuleException {
375 // hier dann htmlcode rausschreiben
376 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
377 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
378 modelRoot.put("themenHashData", themenModule.getHashData());
379 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
380 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
381 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
382 deliver(req, res, modelRoot, templateListString);
383 } else { // count = 1
384 _showObject(theList.elementAt(0).getId(),req,res);
386 } catch (StorageObjectException e) {
387 throw new ServletModuleException(e.toString());
391 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
392 throws ServletModuleException {
395 // hier dann htmlcode rausschreiben
396 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
397 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
398 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
399 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
400 deliver(req, res, modelRoot, templateListString);
401 } else { // count = 1
402 _showObject(theList.elementAt(0).getId(), req, res);
404 } catch (StorageObjectException e) {
405 throw new ServletModuleException(e.toString());
409 private EntityUsers _getUser(HttpServletRequest req)
411 HttpSession session=req.getSession(false);
412 return (EntityUsers)session.getAttribute("login.uid");