1 package mircoders.servlet;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
11 import freemarker.template.*;
20 import mircoders.storage.*;
21 import mircoders.module.*;
22 import mircoders.entity.*;
26 * ServletModuleContent -
27 * deliver html for the article admin form.
29 * @version $Revision: 1.19 $
30 * @author $Author: mh $
32 * $Log: ServletModuleContent.java,v $
33 * Revision 1.19 2002/07/20 22:24:25 mh
34 * made the add() method use _showObject. Fixes a bug that cause the popUps not to show up when adding an article in the admin
39 public class ServletModuleContent extends ServletModule
42 static ModuleTopics themenModule;
43 static ModuleSchwerpunkt schwerpunktModule;
44 static ModuleImages imageModule;
46 static String templateOpString;
48 // Singelton / Kontruktor
50 private static ServletModuleContent instance = new ServletModuleContent();
51 public static ServletModule getInstance() { return instance; }
53 private ServletModuleContent() {
55 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Content.Logfile"));
56 templateListString = MirConfig.getProp("ServletModule.Content.ListTemplate");
57 //templateOpString = MirConfig.getProp("ServletModule.Content.OpTemplate");
58 templateObjektString = MirConfig.getProp("ServletModule.Content.ObjektTemplate");
59 templateConfirmString = MirConfig.getProp("ServletModule.Content.ConfirmTemplate");
60 mainModule = new ModuleContent(DatabaseContent.getInstance());
61 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
62 schwerpunktModule = new ModuleSchwerpunkt(DatabaseFeature.getInstance());
63 imageModule = new ModuleImages(DatabaseImages.getInstance());
64 } catch (StorageObjectException e) {
65 theLog.printDebugInfo("servletmodulecontent konnte nicht initialisiert werden");
71 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
74 EntityUsers user = _getUser(req);
76 String offsetParam = req.getParameter("offset");
79 // hier offsetcode bearbeiteb
80 if (offsetParam != null && !offsetParam.equals(""))
81 offset = Integer.parseInt(offsetParam);
83 if (req.getParameter("next") != null)
84 offset=Integer.parseInt(req.getParameter("nextoffset"));
86 if (req.getParameter("prev") != null)
87 offset = Integer.parseInt(req.getParameter("prevoffset"));
89 String whereParam = req.getParameter("where");
90 String orderParam = req.getParameter("order");
92 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
93 _list(theList, req, res);
94 } catch (ModuleException e) {
95 throw new ServletModuleException(e.toString());
99 public void listop(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
102 EntityUsers user = _getUser(req);
104 String offsetParam = req.getParameter("offset");
107 String whereParam = req.getParameter("where");
109 if (whereParam==null) whereParam = "to_article_type='0'";
111 // hier offsetcode bearbeiteb
112 if (offsetParam != null && !offsetParam.equals(""))
113 offset = Integer.parseInt(offsetParam);
115 if (req.getParameter("next") != null)
116 offset=Integer.parseInt(req.getParameter("nextoffset"));
118 if (req.getParameter("prev") != null)
119 offset = Integer.parseInt(req.getParameter("prevoffset"));
121 String orderParam = req.getParameter("order");
123 theList = ((ModuleContent)mainModule).getContent(whereParam, orderParam, offset, user);
124 _list(theList, req, res);
125 } catch (ModuleException e) {
126 throw new ServletModuleException(e.toString());
131 public void search(HttpServletRequest req, HttpServletResponse res)
132 throws ServletModuleException {
134 EntityUsers user = _getUser(req);
136 String fieldParam = req.getParameter("field");
137 String fieldValueParam = req.getParameter("fieldvalue");
138 String orderParam = req.getParameter("order");
140 theList = ((ModuleContent)mainModule).getContentByField(fieldParam, fieldValueParam, orderParam, 0, user);
141 _list(theList, req, res);
142 } catch (ModuleException e) {
143 throw new ServletModuleException(e.toString());
147 public void add(HttpServletRequest req, HttpServletResponse res)
148 throws ServletModuleException {
149 _showObject(null, req, res);
153 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
155 //theLog.printDebugInfo(":: content :: trying to insert");
157 EntityUsers user = _getUser(req);
158 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
159 //theLog.printDebugInfo(":: content :: got intersecting values");
160 String now = StringUtil.date2webdbDate(new GregorianCalendar());
161 withValues.put("date", now);
162 withValues.put("publish_path", StringUtil.webdbDate2path(now));
163 withValues.put("to_publisher", user.getId());
164 withValues.put("is_produced", "0");
165 if (!withValues.containsKey("is_published"))
166 withValues.put("is_published","0");
167 if (!withValues.containsKey("is_html"))
168 withValues.put("is_html","0");
169 if (withValues.get("creator").toString().equals(""))
170 withValues.put("creator","Anonym");
171 String id = mainModule.add(withValues);
172 DatabaseContentToTopics.getInstance().setTopics(id,req.getParameterValues("to_topic"));
173 //theLog.printDebugInfo(":: content :: inserted");
174 _showObject(id, req, res);
176 catch (StorageObjectException e) {
177 throw new ServletModuleException(e.toString());
179 catch (ModuleException e) {
180 throw new ServletModuleException(e.toString());
184 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
187 EntityUsers user = _getUser(req);
188 // hier pruefen ob dem akt. user loeschen erlaubt ist...
189 String idParam = req.getParameter("id");
190 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
192 String confirmParam = req.getParameter("confirm");
193 String cancelParam = req.getParameter("cancel");
195 if (confirmParam == null && cancelParam == null) {
196 // HTML Ausgabe zum Confirmen!
197 SimpleHash mergeData = new SimpleHash();
198 mergeData.put("module", "Content");
199 mergeData.put("infoString", "Content: " + idParam);
200 mergeData.put("id", idParam);
201 mergeData.put("where", req.getParameter("where"));
202 mergeData.put("order", req.getParameter("order"));
203 mergeData.put("offset", req.getParameter("offset"));
204 deliver(req, res, mergeData, templateConfirmString);
207 if (confirmParam!= null && !confirmParam.equals("")) {
209 mainModule.deleteById(idParam);
211 /** @todo the following two should be imlied in
214 //delete rows in the content_x_topic-table
215 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
216 //delete rows in the comment-table
217 DatabaseComment.getInstance().deleteByContentId(idParam);
218 } catch (ModuleException e) {
219 throw new ServletModuleException(e.toString());
220 } catch (StorageObjectException e) {
221 throw new ServletModuleException(e.toString());
226 // Datensatz anzeigen
227 _showObject(idParam, req, res);
232 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
234 String idParam = req.getParameter("id");
235 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
236 _showObject(idParam, req, res);
239 // methods for attaching media file
240 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
242 String mediaIdParam = req.getParameter("mid");
243 String idParam = req.getParameter("cid");
244 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
247 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
248 entContent.attach(mediaIdParam);
250 catch(ModuleException e) {
251 theLog.printError("smod content :: attach :: could not get entityContent");
253 catch(StorageObjectException e) {
254 theLog.printError("smod content :: attach :: could not get entityContent");
257 _showObject(idParam, req, res);
260 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
262 String cidParam = req.getParameter("cid");
263 String midParam = req.getParameter("mid");
264 if (cidParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
265 if (midParam == null) throw new ServletModuleException("smod content :: dettach :: mid missing");
268 EntityContent entContent = (EntityContent)mainModule.getById(cidParam);
269 entContent.dettach(cidParam,midParam);
271 catch(ModuleException e) {
272 theLog.printError("smod content :: dettach :: could not get entityContent");
274 catch(StorageObjectException e) {
275 theLog.printError("smod content :: dettach :: could not get entityContent");
278 _showObject(cidParam, req, res);
281 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
283 String idParam = req.getParameter("id");
284 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
286 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
287 entContent.newswire();
289 catch(ModuleException e) {
290 theLog.printError("smod content :: newswire :: could not get entityContent");
292 catch(StorageObjectException e) {
293 theLog.printError("smod content :: dettach :: 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("Wrong call: (id) is missing");
310 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
311 String[] topic_id = req.getParameterValues("to_topic");
312 String content_id = req.getParameter("id");
313 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
314 if(user != null) withValues.put("user_id", user.getId());
315 withValues.put("is_produced", "0");
316 if (!withValues.containsKey("is_published"))
317 withValues.put("is_published","0");
318 if (!withValues.containsKey("is_html"))
319 withValues.put("is_html","0");
320 if (withValues.get("creator").toString().equals(""))
321 withValues.put("creator","Anonym");
322 //theLog.printDebugInfo("updating. ");
323 String id = mainModule.set(withValues);
324 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
325 //theLog.printDebugInfo("update done. ");
326 String whereParam = req.getParameter("where");
327 String orderParam = req.getParameter("order");
328 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
329 //theLog.printDebugInfo("update to list");
333 _showObject(idParam, req, res);
335 catch (StorageObjectException e) {
336 throw new ServletModuleException(e.toString());
338 catch (ModuleException e) {
339 throw new ServletModuleException(e.toString());
344 * HelperMethod shows the basic article editing form.
346 * if the "id" parameter is null, it means show an empty form to add a new
349 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
350 throws ServletModuleException {
352 SimpleHash extraInfo = new SimpleHash();
354 TemplateModelRoot entContent;
356 entContent = (TemplateModelRoot)mainModule.getById(id);
358 SimpleHash withValues = new SimpleHash();
359 withValues.put("new", "1");
360 withValues.put("is_published", "0");
361 String now = StringUtil.date2webdbDate(new GregorianCalendar());
362 withValues.put("date", new SimpleScalar(now));
363 EntityUsers user = _getUser(req);
364 withValues.put("login_user", user);
365 entContent = withValues;
369 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
371 extraInfo.put("articletypePopupData",
372 DatabaseArticleType.getInstance().getPopupData());
373 } catch (Exception e) {
374 theLog.printError("articletype could not be fetched.");
377 extraInfo.put("languagePopupData", DatabaseLanguage.getInstance().getPopupData());
378 } catch (Exception e) {
379 theLog.printError("language-popup could not be fetched.");
382 extraInfo.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
383 // hier code um zur liste zurueckzukommen
384 String offsetParam, whereParam, orderParam;
385 if ((offsetParam = req.getParameter("offset"))!=null) extraInfo.put("offset", offsetParam);
386 if ((whereParam = req.getParameter("where"))!=null) extraInfo.put("where", whereParam);
387 if ((orderParam = req.getParameter("order"))!=null) extraInfo.put("order", orderParam);
388 extraInfo.put("login_user", _getUser(req));
389 deliver(req, res, entContent, extraInfo, templateObjektString);
390 } catch (Exception e) {
391 throw new ServletModuleException(e.toString());
396 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
397 throws ServletModuleException {
400 // hier dann htmlcode rausschreiben
401 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
402 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
403 modelRoot.put("themenHashData", themenModule.getHashData());
404 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
405 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
406 deliver(req, res, modelRoot, templateListString);
407 } else { // count = 1
408 _showObject(theList.elementAt(0).getId(),req,res);
410 } catch (StorageObjectException e) {
411 throw new ServletModuleException(e.toString());
415 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
416 throws ServletModuleException {
420 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
421 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
422 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
424 EntityContent currentContent;
425 EntityList upMediaEntityList;
426 EntityList imageEntityList;
427 EntityList currentMediaList;
429 EntityMedia uploadedMedia;
431 String imageRoot = MirConfig.getProp("Producer.ImageRoot");
433 SimpleHash contentHash;
434 Class mediaHandlerClass=null;
435 MirMedia mediaHandler=null;
436 String mediaHandlerName=null;
437 Database mediaStorage=null;
441 for (int i=0; i < theList.size();i++) {
442 currentContent = (EntityContent)theList.elementAt(i);
443 //fetching/setting the images
444 upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(currentContent);
445 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
449 mediaHandlerName = null;
450 for (int n=0; n < upMediaEntityList.size();n++) {
451 uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
452 mediaType = uploadedMedia.getMediaType();
454 //must of had a non-existant to_media_type entry..
455 //let's save our ass.
456 if (mediaType != null) {
458 mediaHandlerName = mediaType.getValue("classname");
459 mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
460 mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
461 } catch (Exception e) {
462 theLog.printError("ProducerStartpage:problem in reflection: "+mediaHandlerName);
465 //the "best" media type to show
466 if (mediaHandler.isVideo()) {
467 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
470 } else if (mediaHandler.isAudio()) {
471 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
473 } else if (tinyIcon == null && !mediaHandler.isImage()) {
474 tinyIcon = mediaHandler.getTinyIcon();
475 iconAlt = mediaHandler.getIconAlt();
479 //it only has image(s)
480 if (tinyIcon == null) {
481 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
485 // uploadedMedia Entity list is empty.
488 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
493 //mediaList = HTMLTemplateProcessor.makeSimpleList(upMediaEntityList);
494 contentHash = (SimpleHash)theList.get(i);
495 contentHash.put("tiny_icon", imageRoot+"/"+tinyIcon);
496 contentHash.put("icon_alt", iconAlt);
497 } catch (Exception e){}
501 deliver(req, res, modelRoot, templateListString);
502 } else { // count = 1
503 _showObject(theList.elementAt(0).getId(), req, res);
505 } catch (StorageObjectException e) {
506 throw new ServletModuleException(e.toString());
510 private EntityUsers _getUser(HttpServletRequest req)
512 HttpSession session=req.getSession(false);
513 return (EntityUsers)session.getAttribute("login.uid");