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);
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);
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");
193 throw new ServletModuleExc("Invalid call: id not supplied ");
194 editObject(aRequest, aResponse, idParam);
198 * Attaches media to an article
200 public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
201 String mediaIdParam = aRequest.getParameter("mid");
202 String articleId = aRequest.getParameter("articleid");
203 if (articleId == null || mediaIdParam==null) {
204 throw new ServletModuleExc("smod content :: attach :: articleid/mid missing");
207 // check if mediaIdParam and articleid are correct integers
209 Integer.parseInt(mediaIdParam);
210 Integer.parseInt(articleId);
212 catch (NumberFormatException e) {
213 throw new ServletModuleExc("smod content :: attach :: invalid articleid/mid", e);
216 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId)) {
217 throw new ServletModuleExc("Article has been locked");
221 EntityContent entContent = (EntityContent) mainModule.getById(articleId);
222 entContent.attach(mediaIdParam);
225 throw new ServletModuleFailure(e);
228 logAdminUsage(aRequest, articleId, "media " + mediaIdParam + " attached");
230 editObject(aRequest, aResponse, articleId);
234 * Deattaches media from an article
236 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
237 String articleId = aRequest.getParameter("articleid");
238 String midParam = aRequest.getParameter("mid");
239 if (articleId == null)
240 throw new ServletModuleExc("smod content :: dettach :: articleid missing");
241 if (midParam == null)
242 throw new ServletModuleExc("smod content :: dettach :: mid missing");
244 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
245 throw new ServletModuleExc("Article has been locked");
248 EntityContent entContent = (EntityContent)mainModule.getById(articleId);
249 entContent.dettach(articleId, midParam);
252 throw new ServletModuleFailure(e);
255 logAdminUsage(aRequest, articleId, "media " + midParam + " deattached");
257 editObject(aRequest, aResponse, articleId);
263 public void lock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
265 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
267 String idParam = requestParser.getParameter("id");
268 if (idParam == null) {
269 throw new ServletModuleExc("Wrong call: (id) is missing");
272 EntityUsers user = ServletHelper.getUser(aRequest);
274 if (!MirGlobal.accessControl().article().mayLockArticle(user, idParam)) {
275 throw new ServletModuleExc("Unable to lock");
278 contentModule.lockArticle(idParam, user.getId(), false);
280 editObject(aRequest, aResponse, idParam);
286 public void unlock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
288 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
290 String idParam = requestParser.getParameter("id");
291 if (idParam == null) {
292 throw new ServletModuleExc("Wrong call: (id) is missing");
295 EntityUsers user = ServletHelper.getUser(aRequest);
297 if (!MirGlobal.accessControl().article().mayUnlockArticle(user, idParam)) {
298 throw new ServletModuleExc("Unable to unlock");
301 contentModule.unlockArticle(idParam, user.getId(), false);
303 editObject(aRequest, aResponse, idParam);
307 * Forcelocks an article
309 public void forcelock(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
310 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
312 String idParam = requestParser.getParameter("id");
314 throw new ServletModuleExc("Wrong call: (id) is missing");
316 EntityUsers user = ServletHelper.getUser(aRequest);
318 if (!MirGlobal.accessControl().article().mayForceLockArticle(user, idParam))
319 throw new ServletModuleExc("Unable to force lock");
321 contentModule.lockArticle(idParam, user.getId(), true);
323 editObject(aRequest, aResponse, idParam);
329 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
331 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
333 String idParam = requestParser.getParameter("id");
334 if (idParam == null) {
335 throw new ServletModuleExc("Wrong call: (id) is missing");
338 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), idParam)) {
339 throw new ServletModuleExc("Article has been locked");
342 Map withValues = getIntersectingValues(aRequest, DatabaseContent.getInstance());
344 withValues.put("is_produced", "0");
345 if (!withValues.containsKey("is_published")) {
346 withValues.put("is_published","0");
348 if (!withValues.containsKey("is_html")) {
349 withValues.put("is_html","0");
352 String webdbCreate = (String) withValues.get("webdb_create");
353 if (webdbCreate==null || webdbCreate.trim().length()==0) {
354 withValues.remove("webdb_create");
357 String id = mainModule.set(withValues);
359 logAdminUsage(aRequest, id, "object modified");
361 DatabaseContentToTopics.getInstance().setTopics(aRequest.getParameter("id"), aRequest.getParameterValues("to_topic"));
363 if (MirGlobal.accessControl().article().mayUnlockArticle(ServletHelper.getUser(aRequest), idParam) &&
364 requestParser.getParameterWithDefault("unlock", "0").equals("1")) {
365 contentModule.unlockArticle(id, ServletHelper.getUser(aRequest).getId(), false);
368 editObject(aRequest, aResponse, idParam);
370 catch (Throwable e) {
371 throw new ServletModuleFailure(e);
377 * Returns the basic article editing form.
379 * @param anId identifier of the article. <code>null</code>, means show an
380 * empty form to add a new article.
382 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse,
383 String anId) throws ServletModuleExc {
385 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
386 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
387 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
389 URLBuilder urlBuilder = new URLBuilder();
391 urlBuilder.setValue("module", "Content");
392 urlBuilder.setValue("do", "edit");
393 urlBuilder.setValue("id", anId);
394 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
397 responseData.put("new", Boolean.FALSE);
398 article = model.makeEntityAdapter("content", mainModule.getById(anId));
400 EntityUsers user = ServletHelper.getUser(aRequest);
402 responseData.put("mayEdit",
403 new Boolean(MirGlobal.accessControl().article().mayEditArticle(user, anId)));
404 responseData.put("mayLock", new Boolean(MirGlobal.accessControl().article().mayLockArticle(user, anId)));
405 responseData.put("mayForceLock", new Boolean(MirGlobal.accessControl().article().mayForceLockArticle(user, anId)));
406 responseData.put("mayUnlock", new Boolean(MirGlobal.accessControl().article().mayUnlockArticle(user, anId)));
409 List fields = DatabaseContent.getInstance().getFieldNames();
410 responseData.put("new", Boolean.TRUE);
411 Map emptyArticle = new HashMap();
412 Iterator i = fields.iterator();
413 while (i.hasNext()) {
414 emptyArticle.put(i.next(), null);
417 emptyArticle.put("to_topics", null);
419 MirGlobal.localizer().adminInterface().initializeArticle(emptyArticle);
420 article = emptyArticle;
422 responseData.put("mayEdit", Boolean.TRUE);
423 responseData.put("mayLock", Boolean.FALSE);
424 responseData.put("mayForceLock", Boolean.FALSE);
425 responseData.put("mayUnlock", Boolean.FALSE);
428 responseData.put("article", article);
430 List topicsList = new ArrayList();
432 String[] topicCategories = getConfiguration().getStringArray("Mir.Localizer.Admin.TopicLists");
434 if (topicCategories.length==0 ) {
435 Map categoryMap = new HashMap();
436 categoryMap.put("key", "topic");
437 categoryMap.put("listtype", "0");
438 categoryMap.put("listparameter", "3");
439 categoryMap.put("items", EntityAdapterEngine.retrieveAdapterList(model, "topic", "", "title", -1, 0));
440 topicsList.add(categoryMap);
443 for (int i = 0; i < topicCategories.length; i++) {
444 Map categoryMap = new HashMap();
445 List parts = StringRoutines.splitString(topicCategories[i], ":");
449 if (parts.size() > 0) {
450 key = (String) parts.get(0);
452 String listtype = "0";
453 if (parts.size() > 1) {
454 listtype = (String) parts.get(1);
456 String listparameter = "5";
457 if (parts.size() > 2) {
458 listparameter = (String) parts.get(2);
461 if (parts.size() > 3) {
462 where = (String) parts.get(3);
465 if (parts.size() > 4) {
466 order = (String) parts.get(4);
470 categoryMap.put("key", key);
471 categoryMap.put("listtype", listtype);
472 categoryMap.put("listparameter", listparameter);
473 categoryMap.put("items", EntityAdapterEngine.retrieveAdapterList(model, "topic", where, order, -1, 0));
474 topicsList.add(categoryMap);
479 responseData.put("topics", topicsList);
481 responseData.put("returnurl", requestParser.getParameter("returnurl"));
482 responseData.put("thisurl", urlBuilder.getQuery());
484 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
486 catch (Throwable e) {
487 throw new ServletModuleFailure(e);
491 public void selectparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
493 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
494 URLBuilder urlBuilder = new URLBuilder();
496 urlBuilder.setValue("module", "Content");
497 urlBuilder.setValue("do", "setparent");
498 urlBuilder.setValue("childid", requestParser.getParameter("id"));
499 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
501 returnList(aRequest, aResponse, "", "", 0,
502 Collections.singletonMap("selectarticleurl", urlBuilder.getQuery()));
504 catch (Throwable e) {
505 throw new ServletModuleFailure(e);
509 public void listchildren(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
510 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
511 String articleId = requestParser.getParameter("article_id");
513 if (articleId == null) {
514 throw new ServletModuleExc("ServletModuleContent.listchildren: article_id not set!");
518 returnList(aRequest, aResponse, "to_content = " + articleId, "webdb_create desc", 0);
520 catch (Throwable e) {
521 throw new ServletModuleFailure(e);
525 public void setparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
526 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
527 String articleId = requestParser.getParameter("childid");
528 String parentId = requestParser.getParameter("id");
529 String returnUrl = requestParser.getParameter("returnurl");
531 if (!MirGlobal.accessControl().article().mayEditArticle(ServletHelper.getUser(aRequest), articleId))
532 throw new ServletModuleExc("Article has been locked");
535 EntityContent article = (EntityContent) mainModule.getById(articleId);
536 article.setFieldValue("to_content", parentId);
537 article.setProduced(false);
539 logAdminUsage(aRequest, articleId, "parent set to " + parentId);
542 getLogger().error("ServletModuleContent.setparent: " + e.getMessage());
544 throw new ServletModuleFailure(e);
547 ServletHelper.redirect(aResponse, returnUrl);
550 public void clearparent(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
551 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
552 String articleId = requestParser.getParameter("id");
553 String returnUrl = requestParser.getParameter("returnurl");
556 EntityContent article = (EntityContent) mainModule.getById(articleId);
557 article.setFieldValue("to_content", null);
558 article.setProduced(false);
560 logAdminUsage(aRequest, articleId, "parent cleared");
563 getLogger().error("ServletModuleContent.clearparent: " + e.getMessage(), e);
565 throw new ServletModuleFailure("ServletModuleContent.clearparent: " + e.getMessage(), e);
568 ServletHelper.redirect(aResponse, returnUrl);
571 public void showPreview(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
573 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
574 String articleId = requestParser.getParameter("id");
575 EntityAdapter article = getModel().makeEntityAdapter("content", mainModule.getById(articleId));
576 String preview = requestParser.getParameterWithDefault("preview", "default");
578 Map generationValues = new HashMap();
579 Generator generator =
580 MirGlobal.localizer().adminInterface().prepareArticlePreview(preview, article, generationValues);
582 generator.generate(aResponse.getWriter(), generationValues, getLogger());
584 catch (Exception e) {
585 throw new ServletModuleFailure(e);