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.EntityAdapterEngine;
35 import mir.entity.adapter.EntityAdapterModel;
36 import mir.generator.Generator;
37 import mir.misc.StringUtil;
38 import mir.servlet.AdminServletModule;
39 import mir.servlet.ServletModuleExc;
40 import mir.servlet.ServletModuleFailure;
41 import mir.util.HTTPRequestParser;
42 import mir.util.JDBCStringRoutines;
43 import mir.util.SQLQueryBuilder;
44 import mir.util.StringRoutines;
45 import mir.util.URLBuilder;
46 import mircoders.entity.EntityContent;
47 import mircoders.entity.EntityUsers;
48 import mircoders.global.MirGlobal;
49 import mircoders.module.ModuleContent;
50 import mircoders.storage.DatabaseContent;
51 import mircoders.storage.DatabaseContentToTopics;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
55 import java.util.ArrayList;
56 import java.util.Collections;
57 import java.util.GregorianCalendar;
58 import java.util.HashMap;
59 import java.util.Iterator;
60 import java.util.List;
61 import java.util.Locale;
65 * Article admin interface code
68 public class ServletModuleContent extends AdminServletModule {
69 private ModuleContent contentModule = new ModuleContent();
71 public ServletModuleContent() {
72 addPropagatedParameter("selectarticleurl");
73 addPropagatedParameter("searchtopic");
74 addPropagatedParameter("searchvalue");
75 addPropagatedParameter("searcharticletype");
77 definition = "content";
78 mainModule = contentModule;
81 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleFailure {
83 String extraTables = "";
84 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
85 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
86 String searchField = requestParser.getParameterWithDefault("searchfield", "");
87 String searchValue = requestParser.getParameterWithDefault("searchvalue", "").trim();
88 String searchOrder = requestParser.getParameterWithDefault("searchorder", "");
89 String searchispublished = requestParser.getParameterWithDefault("searchispublished", "");
90 String searchArticleType = requestParser.getParameterWithDefault("searcharticletype", "");
91 String searchTopic = requestParser.getParameterWithDefault("searchtopic", "");
93 if (searchValue.length()>0) {
94 if ("id".equals(searchField)) {
95 queryBuilder.appendAndCondition(
96 "id='"+JDBCStringRoutines.escapeStringLiteral(searchValue)+"'");
98 else if ("contents".equals(searchField)) {
99 queryBuilder.appendAndCondition(
100 "(lower(content_data) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')"+
101 " or (lower(description) like " + "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%')");
104 queryBuilder.appendAndCondition(
105 "lower("+ searchField + ") like " +
106 "'%" + JDBCStringRoutines.escapeStringLiteral(searchValue.toLowerCase()) + "%'");
110 if (searchispublished.length()>0) {
111 if ("0".equals(searchispublished)) {
112 queryBuilder.appendAndCondition("is_published='f'");
115 queryBuilder.appendAndCondition("is_published='t'");
119 if (searchArticleType.length()>0) {
120 queryBuilder.appendAndCondition("to_article_type="+Integer.parseInt(searchArticleType));
123 if (searchOrder.length()>0) {
124 if ("datedesc".equals(searchOrder)) {
125 queryBuilder.appendDescendingOrder("webdb_create");
127 else if ("dateasc".equals(searchOrder)) {
128 queryBuilder.appendAscendingOrder("webdb_create");
130 else if ("title".equals(searchOrder)) {
131 queryBuilder.appendAscendingOrder("title");
133 else if ("creator".equals(searchOrder)) {
134 queryBuilder.appendAscendingOrder("creator");
138 if (searchTopic.length() > 0) {
139 queryBuilder.appendAndCondition("cxt.content_id = id");
140 queryBuilder.appendAndCondition("cxt.topic_id = " + JDBCStringRoutines.escapeStringLiteral(searchTopic));
142 extraTables = "content_x_topic cxt";
145 returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0,
146 Collections.EMPTY_MAP, null, extraTables);
148 catch (Throwable e) {
149 throw new ServletModuleFailure(e);
153 public void add(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
154 editObject(aRequest, aResponse, null, aRequest.getParameter("template"));
157 public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
159 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
161 String now = StringUtil.date2webdbDate(new GregorianCalendar());
162 withValues.put("date", now);
163 withValues.put("publish_path", StringUtil.webdbDate2path(now));
164 withValues.put("to_publisher", ServletHelper.getUser(aRequest).getId());
165 withValues.put("is_produced", "0");
166 if (!withValues.containsKey("is_published")) {
167 withValues.put("is_published","0");
169 if (!withValues.containsKey("is_html")) {
170 withValues.put("is_html","0");
173 String webdbCreate = (String) withValues.get("webdb_create");
174 if (webdbCreate==null || webdbCreate.trim().length()==0) {
175 withValues.remove("webdb_create");
178 String id = mainModule.add(withValues);
179 logAdminUsage(aRequest, id, "object added");
181 DatabaseContentToTopics.getInstance().setTopics(id, aRequest.getParameterValues("to_topic"));
183 editObject(aRequest, aResponse, id, aRequest.getParameter("template"));
185 catch (Throwable e) {
186 throw new ServletModuleFailure(e);
190 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
191 String idParam = aRequest.getParameter("id");
192 if (idParam == null) {
193 throw new ServletModuleExc("Invalid call: id not supplied ");
196 editObject(aRequest, aResponse, idParam, aRequest.getParameter("template"));
200 * Attaches media to an article
203 * @throws mir.servlet.ServletModuleExc
205 public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
206 String mediaIdParam = aRequest.getParameter("mid");
207 String articleId = aRequest.getParameter("articleid");
208 if (articleId == null || mediaIdParam==null) {
209 throw new ServletModuleExc("smod content :: attach :: articleid/mid missing");
212 // check if mediaIdParam and articleid are correct integers
214 Integer.parseInt(mediaIdParam);
215 Integer.parseInt(articleId);
217 catch (NumberFormatException e) {
218 throw new ServletModuleExc("smod content :: attach :: invalid articleid/mid", e);
221 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId)) {
222 throw new ServletModuleExc("Article has been locked");
226 EntityContent entContent = (EntityContent) mainModule.getById(articleId);
227 entContent.attach(mediaIdParam);
230 throw new ServletModuleFailure(e);
233 logAdminUsage(aRequest, articleId, "media " + mediaIdParam + " attached");
235 editObject(aRequest, aResponse, articleId, aRequest.getParameter("template"));
239 * Deattaches media from an article
241 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
242 String articleId = aRequest.getParameter("articleid");
243 String midParam = aRequest.getParameter("mid");
244 if (articleId == null)
245 throw new ServletModuleExc("smod content :: dettach :: articleid missing");
246 if (midParam == null)
247 throw new ServletModuleExc("smod content :: dettach :: mid missing");
249 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
250 throw new ServletModuleExc("Article has been locked");
253 EntityContent entContent = (EntityContent)mainModule.getById(articleId);
254 entContent.dettach(articleId, midParam);
257 throw new ServletModuleFailure(e);
260 logAdminUsage(aRequest, articleId, "media " + midParam + " deattached");
262 editObject(aRequest, aResponse, articleId, aRequest.getParameter("request"));
268 public void lock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
270 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
272 String idParam = requestParser.getParameter("id");
273 if (idParam == null) {
274 throw new ServletModuleExc("Wrong call: (id) is missing");
277 EntityUsers user = ServletHelper.getUser(aRequest);
279 if (!MirGlobal.accessControl().article().mayLockArticle(user, idParam)) {
280 throw new ServletModuleExc("Unable to lock");
283 contentModule.lockArticle(idParam, user.getId(), false);
285 editObject(aRequest, aResponse, idParam, aRequest.getParameter("template"));
291 public void unlock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
293 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
295 String idParam = requestParser.getParameter("id");
296 if (idParam == null) {
297 throw new ServletModuleExc("Wrong call: (id) is missing");
300 EntityUsers user = ServletHelper.getUser(aRequest);
302 if (!MirGlobal.accessControl().article().mayUnlockArticle(user, idParam)) {
303 throw new ServletModuleExc("Unable to unlock");
306 contentModule.unlockArticle(idParam, user.getId(), false);
308 editObject(aRequest, aResponse, idParam, aRequest.getParameter("template"));
312 * Forcelocks an article
314 public void forcelock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
315 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
317 String idParam = requestParser.getParameter("id");
318 if (idParam == null) {
319 throw new ServletModuleExc("Wrong call: (id) is missing");
322 EntityUsers user = ServletHelper.getUser(aRequest);
324 if (!MirGlobal.accessControl().article().mayForceLockArticle(user, idParam)) {
325 throw new ServletModuleExc("Unable to force lock");
328 contentModule.lockArticle(idParam, user.getId(), true);
330 editObject(aRequest, aResponse, idParam, aRequest.getParameter("template"));
336 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
338 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
340 String idParam = requestParser.getParameter("id");
341 if (idParam == null) {
342 throw new ServletModuleExc("Wrong call: (id) is missing");
345 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), idParam)) {
346 throw new ServletModuleExc("Article has been locked");
349 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
351 withValues.put("is_produced", "0");
352 if (!withValues.containsKey("is_published")) {
353 withValues.put("is_published","0");
355 if (!withValues.containsKey("is_html")) {
356 withValues.put("is_html","0");
359 String webdbCreate = (String) withValues.get("webdb_create");
360 if (webdbCreate==null || webdbCreate.trim().length()==0) {
361 withValues.remove("webdb_create");
364 String id = mainModule.set(withValues);
366 logAdminUsage(aRequest, id, "object modified");
368 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"), aRequest.getParameterValues("to_topic"));
370 if (MirGlobal.accessControl().article().mayUnlockArticle(ServletHelper.getUser(aRequest), idParam) &&
371 requestParser.getParameterWithDefault("unlock", "0").equals("1")) {
372 contentModule.unlockArticle(id, ServletHelper.getUser(aRequest).getId(), false);
375 editObject(aRequest, aResponse, idParam, aRequest.getParameter("template"));
377 catch (Throwable e) {
378 throw new ServletModuleFailure(e);
384 * Returns the basic article editing form.
386 * @param anId identifier of the article. <code>null</code>, means show an
389 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse,
390 String anId, String aTemplate) throws ServletModuleExc {
392 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
393 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
394 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
396 URLBuilder urlBuilder = new URLBuilder();
398 urlBuilder.setValue("module", "Content");
399 urlBuilder.setValue("do", "edit");
400 urlBuilder.setValue("id", anId);
401 urlBuilder.setValue("template", aTemplate);
402 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
405 responseData.put("new", Boolean.FALSE);
406 article = model.makeEntityAdapter("content", mainModule.getById(anId));
408 EntityUsers user = ServletHelper.getUser(aRequest);
410 responseData.put("mayEdit",
411 Boolean.valueOf(MirGlobal.accessControl().article().mayEditArticle(user, anId)));
412 responseData.put("mayLock",
413 Boolean.valueOf(MirGlobal.accessControl().article().mayLockArticle(user, anId)));
414 responseData.put("mayForceLock",
415 Boolean.valueOf(MirGlobal.accessControl().article().mayForceLockArticle(user, anId)));
416 responseData.put("mayUnlock",
417 Boolean.valueOf(MirGlobal.accessControl().article().mayUnlockArticle(user, anId)));
420 List fields = DatabaseContent.getInstance().getFieldNames();
421 responseData.put("new", Boolean.TRUE);
423 Map emptyArticle = new HashMap();
425 Iterator i = fields.iterator();
426 while (i.hasNext()) {
427 emptyArticle.put(i.next(), null);
430 emptyArticle.put("to_topics", null);
432 MirGlobal.localizer().adminInterface().initializeArticle(emptyArticle);
433 article = emptyArticle;
435 responseData.put("mayEdit", Boolean.TRUE);
436 responseData.put("mayLock", Boolean.FALSE);
437 responseData.put("mayForceLock", Boolean.FALSE);
438 responseData.put("mayUnlock", Boolean.FALSE);
441 responseData.put("article", article);
443 List topicsList = new ArrayList();
445 String[] topicCategories = getConfiguration().getStringArray("Mir.Localizer.Admin.TopicLists");
447 if (topicCategories.length==0 ) {
448 Map categoryMap = new HashMap();
449 categoryMap.put("key", "topic");
450 categoryMap.put("listtype", "0");
451 categoryMap.put("listparameter", "3");
452 categoryMap.put("items", EntityAdapterEngine.retrieveAdapterList(model, "topic", "", "title", -1, 0));
453 topicsList.add(categoryMap);
456 for (int i = 0; i < topicCategories.length; i++) {
457 Map categoryMap = new HashMap();
458 List parts = StringRoutines.splitString(topicCategories[i], ":");
462 if (parts.size() > 0) {
463 key = (String) parts.get(0);
465 String listtype = "0";
466 if (parts.size() > 1) {
467 listtype = (String) parts.get(1);
469 String listparameter = "5";
470 if (parts.size() > 2) {
471 listparameter = (String) parts.get(2);
474 if (parts.size() > 3) {
475 where = (String) parts.get(3);
478 if (parts.size() > 4) {
479 order = (String) parts.get(4);
483 categoryMap.put("key", key);
484 categoryMap.put("listtype", listtype);
485 categoryMap.put("listparameter", listparameter);
486 categoryMap.put("items", EntityAdapterEngine.retrieveAdapterList(model, "topic", where, order, -1, 0));
487 topicsList.add(categoryMap);
492 responseData.put("topics", topicsList);
494 responseData.put("returnurl", requestParser.getParameter("returnurl"));
495 responseData.put("thisurl", urlBuilder.getQuery());
497 if (aTemplate == null) {
498 aTemplate = editGenerator;
501 ServletHelper.generateResponse(aResponse.getWriter(), responseData, aTemplate);
503 catch (Throwable e) {
504 throw new ServletModuleFailure(e);
508 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
510 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
511 URLBuilder urlBuilder = new URLBuilder();
513 urlBuilder.setValue("module", "Content");
514 urlBuilder.setValue("do", "setparent");
515 urlBuilder.setValue("childid", requestParser.getParameter("id"));
516 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
518 returnList(aRequest, aResponse, "", "", 0,
519 Collections.singletonMap("selectarticleurl", urlBuilder.getQuery()));
521 catch (Throwable e) {
522 throw new ServletModuleFailure(e);
526 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
527 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
528 String articleId = requestParser.getParameter("article_id");
530 if (articleId == null) {
531 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
535 returnList(aRequest, aResponse, "to_content = " + articleId, "webdb_create desc", 0);
537 catch (Throwable e) {
538 throw new ServletModuleFailure(e);
542 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
543 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
544 String articleId = requestParser.getParameter("childid");
545 String parentId = requestParser.getParameter("id");
546 String returnUrl = requestParser.getParameter("returnurl");
548 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
549 throw new ServletModuleExc("Article has been locked");
552 EntityContent article = (EntityContent) mainModule.getById(articleId);
553 article.setFieldValue("to_content", parentId);
554 article.setProduced(false);
556 logAdminUsage(aRequest, articleId, "parent set to " + parentId);
559 getLogger().error("ServletModuleContent.setparent: " + e.getMessage());
561 throw new ServletModuleFailure(e);
564 ServletHelper.redirect(aResponse, returnUrl);
567 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
568 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
569 String articleId = requestParser.getParameter("id");
570 String returnUrl = requestParser.getParameter("returnurl");
573 EntityContent article = (EntityContent) mainModule.getById(articleId);
574 article.setFieldValue("to_content", null);
575 article.setProduced(false);
577 logAdminUsage(aRequest, articleId, "parent cleared");
580 getLogger().error("ServletModuleContent.clearparent: " + e.getMessage(), e);
582 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
585 ServletHelper.redirect(aResponse, returnUrl);
588 public void showPreview(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
590 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
591 String articleId = requestParser.getParameter("id");
592 EntityAdapter article = getModel().makeEntityAdapter("content", mainModule.getById(articleId));
593 String preview = requestParser.getParameterWithDefault("preview", "default");
595 Map generationValues = new HashMap();
596 Generator generator =
597 MirGlobal.localizer().adminInterface().prepareArticlePreview(preview, article, generationValues);
599 generator.generate(aResponse.getWriter(), generationValues, getLogger());
601 catch (Exception e) {
602 throw new ServletModuleFailure(e);