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.getParameterValues("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[] topic_id = req.getParameterValues("to_topic");
300 String content_id = req.getParameter("id");
301 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
302 if(user != null) withValues.put("user_id", user.getId());
303 withValues.put("is_produced", "0");
304 if (!withValues.containsKey("is_published"))
305 withValues.put("is_published","0");
306 if (!withValues.containsKey("is_html"))
307 withValues.put("is_html","0");
308 if (withValues.get("creator").toString().equals(""))
309 withValues.put("creator","Anonym");
310 //theLog.printDebugInfo("updating. ");
311 String id = mainModule.set(withValues);
312 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
313 //theLog.printDebugInfo("update done. ");
314 String whereParam = req.getParameter("where");
315 String orderParam = req.getParameter("order");
316 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
317 //theLog.printDebugInfo("update to list");
321 _showObject(idParam, req, res);
323 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
324 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
330 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
331 throws ServletModuleException {
334 EntityContent entContent=(EntityContent)mainModule.getById(id);
335 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
336 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
337 if (topicToContent!=null && topicToContent.size()>0){
338 theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
339 Entity topics = null;
340 SimpleList topicList = new SimpleList();
341 for(int i=0;i<topicToContent.size();i++){
342 topics = (EntityTopics)topicToContent.elementAt(i);
343 topicList.add(topics.getId());
345 mergeData.put("to_topic",topicList);
347 //obsolete, because of psqgl 7.1.x
348 //mergeData.put("content_data", entContent.getContentData());
349 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
350 mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
352 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
353 } catch (Exception e) {theLog.printError("articletype could not be fetched.");}
356 String currentMediaId = entContent.getValue("to_media");
357 SimpleHash imageHash = new SimpleHash();
358 if (currentMediaId!=null && !currentMediaId.equals("")) {
359 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
360 mergeData.put("images", imageHash);
364 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
365 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
366 // hier code um zur liste zurueckzukommen
367 String offsetParam, whereParam, orderParam;
368 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
369 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
370 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
371 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
372 deliver(req, res, mergeData, templateObjektString);
374 catch (Exception e) { throw new ServletModuleException(e.toString());}
378 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
379 throws ServletModuleException {
382 // hier dann htmlcode rausschreiben
383 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
384 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
385 modelRoot.put("themenHashData", themenModule.getHashData());
386 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
387 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
388 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
389 deliver(req, res, modelRoot, templateListString);
390 } else { // count = 1
391 _showObject(theList.elementAt(0).getId(),req,res);
393 } catch (StorageObjectException e) {
394 throw new ServletModuleException(e.toString());
398 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
399 throws ServletModuleException {
402 // hier dann htmlcode rausschreiben
403 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
404 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
405 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
406 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
407 deliver(req, res, modelRoot, templateListString);
408 } else { // count = 1
409 _showObject(theList.elementAt(0).getId(), req, res);
411 } catch (StorageObjectException e) {
412 throw new ServletModuleException(e.toString());
416 private EntityUsers _getUser(HttpServletRequest req)
418 HttpSession session=req.getSession(false);
419 return (EntityUsers)session.getAttribute("login.uid");