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) {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.getParameterValues("to_topic"));
183 //theLog.printDebugInfo(":: content :: inserted");
184 _showObject(id, req, res);
186 catch (StorageObjectException e) {
187 throw new ServletModuleException(e.toString());
189 catch (ModuleException e) {
190 throw new ServletModuleException(e.toString());
194 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
197 EntityUsers user = _getUser(req);
198 // hier pruefen ob dem akt. user loeschen erlaubt ist...
199 String idParam = req.getParameter("id");
200 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
202 String confirmParam = req.getParameter("confirm");
203 String cancelParam = req.getParameter("cancel");
205 if (confirmParam == null && cancelParam == null) {
206 // HTML Ausgabe zum Confirmen!
207 SimpleHash mergeData = new SimpleHash();
208 mergeData.put("module", "Content");
209 mergeData.put("infoString", "Content: " + idParam);
210 mergeData.put("id", idParam);
211 mergeData.put("where", req.getParameter("where"));
212 mergeData.put("order", req.getParameter("order"));
213 mergeData.put("offset", req.getParameter("offset"));
214 deliver(req, res, mergeData, templateConfirmString);
217 if (confirmParam!= null && !confirmParam.equals("")) {
219 mainModule.deleteById(idParam);
220 //delete rows in the content_x_topic-table
221 DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
222 //delete rows in the comment-table
223 DatabaseComment.getInstance().deleteByContentId(idParam);
224 } catch (ModuleException e) {
225 throw new ServletModuleException(e.toString());
226 } catch (StorageObjectException e) {
227 throw new ServletModuleException(e.toString());
232 // Datensatz anzeigen
233 _showObject(idParam, req, res);
238 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
240 String idParam = req.getParameter("id");
241 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
242 _showObject(idParam, req, res);
245 // methods for attaching media file
246 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
248 String mediaIdParam = req.getParameter("mid");
249 String idParam = req.getParameter("cid");
250 if (idParam == null||mediaIdParam==null) throw new ServletModuleException("smod content :: attach :: cid/mid missing");
252 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
253 entContent.attach(mediaIdParam);
255 catch(ModuleException e) {
256 theLog.printError("smod content :: attach :: could not get entityContent");
258 _showObject(idParam, req, res);
261 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
263 String idParam = req.getParameter("cid");
264 if (idParam == null) throw new ServletModuleException("smod content :: dettach :: cid missing");
266 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
267 entContent.dettach();
269 catch(ModuleException e) {
270 theLog.printError("smod content :: dettach :: could not get entityContent");
272 _showObject(idParam, req, res);
275 public void newswire(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
277 String idParam = req.getParameter("id");
278 if (idParam == null) throw new ServletModuleException("smod content :: newswire :: id missing");
280 EntityContent entContent = (EntityContent)mainModule.getById(idParam);
281 entContent.newswire();
283 catch(ModuleException e) {
284 theLog.printError("smod content :: newswire :: could not get entityContent");
290 public void update(HttpServletRequest req, HttpServletResponse res)
291 throws ServletModuleException
295 EntityUsers user = _getUser(req);
296 if (user==null) theLog.printDebugInfo("user null!");
297 String idParam = req.getParameter("id");
298 if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
300 HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
301 //String topic_id = req.getParameter("to_topic");
302 String[] topic_id = req.getParameterValues("to_topic");
303 String content_id = req.getParameter("id");
304 // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
305 if(user != null) withValues.put("user_id", user.getId());
306 withValues.put("is_produced", "0");
307 if (!withValues.containsKey("is_published"))
308 withValues.put("is_published","0");
309 if (!withValues.containsKey("is_html"))
310 withValues.put("is_html","0");
311 if (withValues.get("creator").toString().equals(""))
312 withValues.put("creator","Anonym");
313 //theLog.printDebugInfo("updating. ");
314 String id = mainModule.set(withValues);
315 DatabaseContentToTopics.getInstance().setTopics(req.getParameter("id"),topic_id);
316 //theLog.printDebugInfo("update done. ");
317 String whereParam = req.getParameter("where");
318 String orderParam = req.getParameter("order");
319 if ((whereParam!=null && !whereParam.equals("")) || (orderParam!=null && !orderParam.equals(""))){
320 //theLog.printDebugInfo("update to list");
324 _showObject(idParam, req, res);
326 catch (StorageObjectException e) {
327 throw new ServletModuleException(e.toString());
329 catch (ModuleException e) {
330 throw new ServletModuleException(e.toString());
337 private void _showObject(String id, HttpServletRequest req, HttpServletResponse res)
338 throws ServletModuleException {
341 EntityContent entContent=(EntityContent)mainModule.getById(id);
342 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(entContent);
343 EntityList topicToContent = DatabaseContentToTopics.getInstance().getTopics(entContent);
344 if (topicToContent!=null && topicToContent.size()>0){
345 theLog.printDebugInfo("topicanzahl: "+topicToContent.size());
346 Entity topics = null;
347 SimpleList topicList = new SimpleList();
348 for(int i=0;i<topicToContent.size();i++){
349 topics = (EntityTopics)topicToContent.elementAt(i);
350 topicList.add(topics.getId());
352 mergeData.put("to_topic",topicList);
354 //obsolete, because of psqgl 7.1.x
355 //mergeData.put("content_data", entContent.getContentData());
356 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
357 mergeData.put("gruppenPopupData", gruppenModule.getGruppenAsSimpleList());
359 mergeData.put("articletypePopupData", DatabaseArticleType.getInstance().getPopupData());
360 } catch (Exception e) {theLog.printError("articletype could not be fetched.");}
363 String currentMediaId = entContent.getValue("to_media");
364 SimpleHash imageHash = new SimpleHash();
365 if (currentMediaId!=null && !currentMediaId.equals("")) {
366 imageHash.put(currentMediaId, HTMLTemplateProcessor.makeSimpleHash(imageModule.getById(currentMediaId)));
367 mergeData.put("images", imageHash);
371 //mergeData.put("gruppenHashData", gruppenModule.getHashData());
372 mergeData.put("schwerpunktPopupData", schwerpunktModule.getSchwerpunktAsSimpleList());
373 // hier code um zur liste zurueckzukommen
374 String offsetParam, whereParam, orderParam;
375 if ((offsetParam = req.getParameter("offset"))!=null) mergeData.put("offset", offsetParam);
376 if ((whereParam = req.getParameter("where"))!=null) mergeData.put("where", whereParam);
377 if ((orderParam = req.getParameter("order"))!=null) mergeData.put("order", orderParam);
378 mergeData.put("login_user", HTMLTemplateProcessor.makeSimpleHash(_getUser(req)));
379 deliver(req, res, mergeData, templateObjektString);
380 } catch (Exception e) {
381 throw new ServletModuleException(e.toString());
386 public void _list(EntityList theList, HttpServletRequest req, HttpServletResponse res)
387 throws ServletModuleException {
390 // hier dann htmlcode rausschreiben
391 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
392 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
393 modelRoot.put("themenHashData", themenModule.getHashData());
394 modelRoot.put("schwerpunktHashData", schwerpunktModule.getHashData());
395 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
396 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
397 deliver(req, res, modelRoot, templateListString);
398 } else { // count = 1
399 _showObject(theList.elementAt(0).getId(),req,res);
401 } catch (StorageObjectException e) {
402 throw new ServletModuleException(e.toString());
406 public void _listop(EntityList theList, HttpServletRequest req, HttpServletResponse res)
407 throws ServletModuleException {
410 // hier dann htmlcode rausschreiben
411 if (theList == null || theList.getCount() == 0 || theList.getCount()>1) {
412 SimpleHash modelRoot = HTMLTemplateProcessor.makeSimpleHashWithEntitylistInfos(theList);
413 modelRoot.put("articletypeHash", DatabaseArticleType.getInstance().getHashData());
414 modelRoot.put("gruppenHashData", gruppenModule.getHashData());
415 deliver(req, res, modelRoot, templateListString);
416 } else { // count = 1
417 _showObject(theList.elementAt(0).getId(), req, res);
419 } catch (StorageObjectException e) {
420 throw new ServletModuleException(e.toString());
424 private EntityUsers _getUser(HttpServletRequest req)
426 HttpSession session=req.getSession(false);
427 return (EntityUsers)session.getAttribute("login.uid");