2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.servlet;
33 import mir.entity.adapter.EntityAdapter;
34 import mir.entity.adapter.EntityAdapterModel;
35 import mir.entity.adapter.EntityIteratorAdapter;
36 import mir.generator.Generator;
37 import mir.log.LoggerWrapper;
38 import mir.misc.StringUtil;
39 import mir.servlet.ServletModule;
40 import mir.servlet.ServletModuleExc;
41 import mir.servlet.ServletModuleFailure;
43 import mircoders.entity.EntityContent;
44 import mircoders.entity.EntityUsers;
45 import mircoders.global.MirGlobal;
46 import mircoders.module.ModuleContent;
47 import mircoders.storage.DatabaseContent;
48 import mircoders.storage.DatabaseContentToTopics;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
55 * Article admin interface code
58 public class ServletModuleContent extends ServletModule
60 private static ServletModuleContent instance = new ServletModuleContent();
61 public static ServletModule getInstance() { return instance; }
62 private static ModuleContent contentModule;
64 private ServletModuleContent() {
66 propagatedParameters.add("selectarticleurl");
68 logger = new LoggerWrapper("ServletModule.Content");
71 definition = "content";
72 contentModule = new ModuleContent();
73 mainModule = contentModule;
76 logger.fatal("ServletModuleContent could not be initialized: " + e.toString());
80 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {
82 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
83 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
84 String searchField = requestParser.getParameterWithDefault("searchfield", "");
85 String searchValue = requestParser.getParameterWithDefault("searchvalue", "").trim();
86 String searchOrder = requestParser.getParameterWithDefault("searchorder", "");
87 String searchispublished = requestParser.getParameterWithDefault("searchispublished", "");
88 String searchArticleType = requestParser.getParameterWithDefault("searcharticletype", "");
90 if (searchValue.length()>0) {
91 if (searchField.equals("id"))
92 queryBuilder.appendAndCondition(
93 "id='"+JDBCStringRoutines.escapeStringLiteral(searchValue)+"'");
94 else if (searchField.equals("contents"))
95 queryBuilder.appendAndCondition(
96 "(lower(content_data) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')"+
97 " or (lower(description) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')");
99 queryBuilder.appendAndCondition(
100 "lower("+ searchField + ") like " +
101 "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%'");
104 if (searchispublished.length()>0) {
105 if (searchispublished.equals("0"))
106 queryBuilder.appendAndCondition("is_published='f'");
108 queryBuilder.appendAndCondition("is_published='t'");
111 if (searchArticleType.length()>0) {
112 queryBuilder.appendAndCondition("to_article_type="+Integer.parseInt(searchArticleType));
115 if (searchOrder.length()>0) {
116 if (searchOrder.equals("datedesc"))
117 queryBuilder.appendDescendingOrder("webdb_create");
118 else if (searchOrder.equals("dateasc"))
119 queryBuilder.appendAscendingOrder("webdb_create");
120 else if (searchOrder.equals("title"))
121 queryBuilder.appendAscendingOrder("title");
122 else if (searchOrder.equals("creator"))
123 queryBuilder.appendAscendingOrder("creator");
126 returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
128 catch (Throwable e) {
129 throw new ServletModuleFailure(e);
133 public void add(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
134 editObject(aRequest, aResponse, null);
137 public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
139 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
141 String now = StringUtil.date2webdbDate(new GregorianCalendar());
142 withValues.put("date", now);
143 withValues.put("publish_path", StringUtil.webdbDate2path(now));
144 withValues.put("to_publisher", ServletHelper.getUser(aRequest).getId());
145 withValues.put("is_produced", "0");
146 if (!withValues.containsKey("is_published"))
147 withValues.put("is_published","0");
148 if (!withValues.containsKey("is_html"))
149 withValues.put("is_html","0");
151 String webdbCreate = (String) withValues.get("webdb_create");
152 if (webdbCreate==null || webdbCreate.trim().length()==0)
153 withValues.remove("webdb_create");
155 String id = mainModule.add(withValues);
156 logAdminUsage(aRequest, id, "object added");
158 DatabaseContentToTopics.getInstance().setTopics(id, aRequest.getParameterValues("to_topic"));
160 editObject(aRequest, aResponse, id);
162 catch (Throwable e) {
163 throw new ServletModuleFailure(e);
167 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
168 String idParam = aRequest.getParameter("id");
170 throw new ServletModuleExc("Invalid call: id not supplied ");
171 editObject(aRequest, aResponse, idParam);
175 * Attaches media to an article
178 public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
179 String mediaIdParam = aRequest.getParameter("mid");
180 String articleId = aRequest.getParameter("articleid");
182 if (articleId == null || mediaIdParam==null)
183 throw new ServletModuleExc("smod content :: attach :: articleid/mid missing");
185 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
186 throw new ServletModuleExc("Article has been locked");
189 EntityContent entContent = (EntityContent) mainModule.getById(articleId);
190 entContent.attach(mediaIdParam);
193 throw new ServletModuleFailure(e);
196 logAdminUsage(aRequest, articleId, "media " + mediaIdParam + " attached");
198 editObject(aRequest, aResponse, articleId);
202 * Deattaches media from an article
204 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
206 String articleId = aRequest.getParameter("articleid");
207 String midParam = aRequest.getParameter("mid");
208 if (articleId == null)
209 throw new ServletModuleExc("smod content :: dettach :: articleid missing");
210 if (midParam == null)
211 throw new ServletModuleExc("smod content :: dettach :: mid missing");
213 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
214 throw new ServletModuleExc("Article has been locked");
217 EntityContent entContent = (EntityContent)mainModule.getById(articleId);
218 entContent.dettach(articleId, midParam);
221 throw new ServletModuleFailure(e);
224 logAdminUsage(aRequest, articleId, "media " + midParam + " deattached");
226 editObject(aRequest, aResponse, articleId);
232 public void lock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
234 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
236 String idParam = requestParser.getParameter("id");
238 throw new ServletModuleExc("Wrong call: (id) is missing");
240 EntityUsers user = ServletHelper.getUser(aRequest);
242 if (!MirGlobal.accessControl().article().mayLockArticle(user, idParam))
243 throw new ServletModuleExc("Unable to lock");
245 contentModule.lockArticle(idParam, user.getId(), false);
247 editObject(aRequest, aResponse, idParam);
253 public void unlock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
255 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
257 String idParam = requestParser.getParameter("id");
259 throw new ServletModuleExc("Wrong call: (id) is missing");
261 EntityUsers user = ServletHelper.getUser(aRequest);
263 if (!MirGlobal.accessControl().article().mayUnlockArticle(user, idParam))
264 throw new ServletModuleExc("Unable to unlock");
266 contentModule.unlockArticle(idParam, user.getId(), false);
268 editObject(aRequest, aResponse, idParam);
272 * Forcelocks an article
274 public void forcelock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
276 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
278 String idParam = requestParser.getParameter("id");
280 throw new ServletModuleExc("Wrong call: (id) is missing");
282 EntityUsers user = ServletHelper.getUser(aRequest);
284 if (!MirGlobal.accessControl().article().mayForceLockArticle(user, idParam))
285 throw new ServletModuleExc("Unable to force lock");
287 contentModule.lockArticle(idParam, user.getId(), true);
289 editObject(aRequest, aResponse, idParam);
295 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
298 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
300 String idParam = requestParser.getParameter("id");
302 throw new ServletModuleExc("Wrong call: (id) is missing");
304 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), idParam))
305 throw new ServletModuleExc("Article has been locked");
307 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
309 withValues.put("is_produced", "0");
310 if (!withValues.containsKey("is_published"))
311 withValues.put("is_published","0");
312 if (!withValues.containsKey("is_html"))
313 withValues.put("is_html","0");
315 String webdbCreate = (String) withValues.get("webdb_create");
316 if (webdbCreate==null || webdbCreate.trim().length()==0)
317 withValues.remove("webdb_create");
319 String id = mainModule.set(withValues);
321 logAdminUsage(aRequest, id, "object modified");
323 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"), aRequest.getParameterValues("to_topic"));
325 if (MirGlobal.accessControl().article().mayUnlockArticle(ServletHelper.getUser(aRequest), idParam) &&
326 (requestParser.getParameterWithDefault("unlock", "0").equals("1"))) {
327 contentModule.unlockArticle(id, ServletHelper.getUser(aRequest).getId(), false);
330 editObject(aRequest, aResponse, idParam);
332 catch (Throwable e) {
333 throw new ServletModuleFailure(e);
339 * Returns the basic article editing form.
341 * @param id identifier of the article. <code>null</code>, means show an
342 * empty form to add a new article.
344 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String id)
345 throws ServletModuleExc {
347 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
348 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
349 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
351 URLBuilder urlBuilder = new URLBuilder();
353 urlBuilder.setValue("module", "Content");
354 urlBuilder.setValue("do", "edit");
355 urlBuilder.setValue("id", id);
356 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
359 responseData.put("new", Boolean.FALSE);
360 article = model.makeEntityAdapter("content", mainModule.getById(id));
362 EntityUsers user = ServletHelper.getUser(aRequest);
364 responseData.put("mayEdit", new Boolean(MirGlobal.accessControl().article().mayEditArticle(user, id)));
365 responseData.put("mayLock", new Boolean(MirGlobal.accessControl().article().mayLockArticle(user, id)));
366 responseData.put("mayForceLock", new Boolean(MirGlobal.accessControl().article().mayForceLockArticle(user, id)));
367 responseData.put("mayUnlock", new Boolean(MirGlobal.accessControl().article().mayUnlockArticle(user, id)));
370 List fields = DatabaseContent.getInstance().getFieldNames();
371 responseData.put("new", Boolean.TRUE);
372 article = new HashMap();
373 Iterator i = fields.iterator();
374 while (i.hasNext()) {
375 article.put(i.next(), null);
378 article.put("to_topics", null);
380 MirGlobal.localizer().adminInterface().initializeArticle(article);
382 responseData.put("mayEdit", Boolean.TRUE);
383 responseData.put("mayLock", Boolean.FALSE);
384 responseData.put("mayForceLock", Boolean.FALSE);
385 responseData.put("mayUnlock", Boolean.FALSE);
387 responseData.put("article", article);
389 List topicsList = new ArrayList();
391 String[] topicCategories = configuration.getStringArray("Mir.Localizer.Admin.TopicLists");
393 if (topicCategories.length==0 ) {
394 Map categoryMap = new HashMap();
395 categoryMap.put("key", "topic");
396 categoryMap.put("listtype", "0");
397 categoryMap.put("listparameter", "3");
398 categoryMap.put("items",
399 new EntityIteratorAdapter("", "title",
400 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
401 topicsList.add(categoryMap);
404 for (int i = 0; i < topicCategories.length; i++) {
406 Map categoryMap = new HashMap();
407 List parts = StringRoutines.splitString(topicCategories[i], ":");
409 String listtype = "0";
410 String listparameter = "5";
414 if (parts.size() > 0)
415 key = (String) parts.get(0);
416 if (parts.size() > 1)
417 listtype = (String) parts.get(1);
418 if (parts.size() > 2)
419 listparameter = (String) parts.get(2);
420 if (parts.size() > 3)
421 where = (String) parts.get(3);
422 if (parts.size() > 4)
423 order = (String) parts.get(4);
426 categoryMap.put("key", key);
427 categoryMap.put("listtype", listtype);
428 categoryMap.put("listparameter", listparameter);
429 categoryMap.put("items",
430 new EntityIteratorAdapter(where, order,
431 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
432 topicsList.add(categoryMap);
435 catch (Throwable t) {
436 logger.error("error while preparing topics: " + t.toString());
441 responseData.put("topics", topicsList);
443 responseData.put("returnurl", requestParser.getParameter("returnurl"));
444 responseData.put("thisurl", urlBuilder.getQuery());
446 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
448 catch (Throwable e) {
449 throw new ServletModuleFailure(e);
453 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
455 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
456 URLBuilder urlBuilder = new URLBuilder();
458 urlBuilder.setValue("module", "Content");
459 urlBuilder.setValue("do", "setparent");
460 urlBuilder.setValue("childid", requestParser.getParameter("id"));
461 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
463 returnList(aRequest, aResponse, "", "", 0,
464 Collections.singletonMap("selectarticleurl", urlBuilder.getQuery()));
466 catch (Throwable e) {
467 throw new ServletModuleFailure(e);
471 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
473 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
474 String articleId = requestParser.getParameter("article_id");
476 if (articleId == null)
477 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
479 returnList(aRequest, aResponse, "to_content = " + articleId, "webdb_create desc", 0, null);
481 catch (Throwable e) {
482 throw new ServletModuleFailure(e);
486 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
488 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
489 String articleId = requestParser.getParameter("childid");
490 String parentId = requestParser.getParameter("id");
491 String returnUrl = requestParser.getParameter("returnurl");
493 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
494 throw new ServletModuleExc("Article has been locked");
497 EntityContent article = (EntityContent) mainModule.getById(articleId);
498 article.setFieldValue("to_content", parentId);
499 article.setProduced(false);
501 logAdminUsage(aRequest, articleId, "parent set to " + parentId);
504 logger.error("ServletModuleContent.setparent: " + e.getMessage());
505 throw new ServletModuleFailure(e);
508 ServletHelper.redirect(aResponse, returnUrl);
511 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
513 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
514 String articleId = requestParser.getParameter("id");
515 String returnUrl = requestParser.getParameter("returnurl");
518 EntityContent article = (EntityContent) mainModule.getById(articleId);
519 article.setFieldValue("to_content", "");
520 article.setProduced(false);
522 logAdminUsage(aRequest, articleId, "parent cleared");
525 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
526 logger.error("ServletModuleContent.clearparent: " + e.getMessage());
528 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
531 ServletHelper.redirect(aResponse, returnUrl);
534 public void showPreview(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
536 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
537 String articleId = requestParser.getParameter("id");
538 EntityAdapter article = model.makeEntityAdapter("content", mainModule.getById(articleId));
539 String preview = requestParser.getParameterWithDefault("preview", "default");
541 Map generationValues = new HashMap();
542 Generator generator =
543 MirGlobal.localizer().adminInterface().prepareArticlePreview(preview, article, generationValues);
545 generator.generate(aResponse.getWriter(), generationValues, logger);
547 catch (Exception e) {
548 throw new ServletModuleFailure(e);