001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/categorytree/impl/CategorySelector.java,v 1.24 2007/12/19 10:08:10 phuongpdd Exp $
003: * $Author: phuongpdd $
004: * $Revision: 1.24 $
005: * $Date: 2007/12/19 10:08:10 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.categorytree.impl;
042:
043: import java.io.IOException;
044: import java.util.*;
045:
046: import com.mvnforum.MVNForumResourceBundle;
047: import com.mvnforum.MyUtil;
048: import com.mvnforum.auth.*;
049: import com.mvnforum.categorytree.CategoryTreeEvent;
050:
051: import com.mvnforum.db.CategoryBean;
052: import com.mvnforum.db.ForumBean;
053:
054: import freemarker.cache.ClassTemplateLoader;
055: import freemarker.cache.TemplateLoader;
056: import freemarker.template.*;
057:
058: import net.myvietnam.mvncore.exception.DatabaseException;
059:
060: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
061: import net.myvietnam.mvncore.service.URLResolverService;
062: import net.myvietnam.mvncore.util.I18nUtil;
063: import net.myvietnam.mvncore.util.ParamUtil;
064: import net.myvietnam.mvncore.web.*;
065:
066: import org.apache.commons.logging.Log;
067: import org.apache.commons.logging.LogFactory;
068:
069: public class CategorySelector extends FtlCategoryTreeListener {
070:
071: private static Log log = LogFactory.getLog(CategorySelector.class);
072:
073: private GenericRequest request;
074: private GenericResponse response;
075: /*
076: * page:in case performing only category (not include forum child)
077: * such as addcategory page --> page == null
078: */
079: private String page = null;
080: private OnlineUser onlineUser;
081: private MVNForumPermission permission;
082: private static URLResolverService urlResolver = MvnCoreServiceFactory
083: .getMvnCoreService().getURLResolverService();
084:
085: private static Template template;
086: private boolean page_Condition;
087: private int forumID;
088: private boolean addForum;
089: private int categoryID;
090: private boolean checkAddCategory = false;
091: private Locale locale;
092:
093: private int forum;
094: private int category;
095:
096: private String selectName;
097:
098: static {
099: Configuration conf = new Configuration();
100: TemplateLoader loader = new ClassTemplateLoader(
101: CategorySelector.class);
102: conf.setTemplateLoader(loader);
103: try {
104: template = conf.getTemplate("categoryselector.ftl");
105: } catch (IOException e) {
106: log.error("Cannot load template for CategorySelector", e);
107: }
108: }
109:
110: public CategorySelector() {
111: //default
112: }
113:
114: public CategorySelector(GenericRequest request,
115: GenericResponse response, String page)
116: throws AuthenticationException, DatabaseException {
117:
118: this .request = request;
119: this .response = response;
120: onlineUser = OnlineUserManager.getInstance().getOnlineUser(
121: request);
122: permission = onlineUser.getPermission();
123: this .page = page;
124: locale = I18nUtil.getLocaleInRequest(request);
125:
126: init(template);
127: }
128:
129: public CategorySelector(GenericRequest request,
130: GenericResponse response, String page, int forum,
131: int category) throws AuthenticationException,
132: DatabaseException {
133:
134: this .request = request;
135: this .response = response;
136: onlineUser = OnlineUserManager.getInstance().getOnlineUser(
137: request);
138: permission = onlineUser.getPermission();
139: this .page = page;
140: locale = I18nUtil.getLocaleInRequest(request);
141:
142: this .forum = forum;
143: this .category = category;
144:
145: init(template);
146: }
147:
148: public CategorySelector(GenericRequest request,
149: GenericResponse response) throws AuthenticationException,
150: DatabaseException {
151:
152: this .request = request;
153: this .response = response;
154: onlineUser = OnlineUserManager.getInstance().getOnlineUser(
155: request);
156: permission = onlineUser.getPermission();
157: locale = I18nUtil.getLocaleInRequest(request);
158:
159: init(template);
160: }
161:
162: public CategorySelector(GenericRequest request,
163: GenericResponse response, boolean addForum,
164: String selectName) throws AuthenticationException,
165: DatabaseException {
166:
167: this .request = request;
168: this .response = response;
169: onlineUser = OnlineUserManager.getInstance().getOnlineUser(
170: request);
171: permission = onlineUser.getPermission();
172: this .addForum = addForum;
173: locale = I18nUtil.getLocaleInRequest(request);
174: this .selectName = selectName;
175:
176: init(template);
177: }
178:
179: public CategorySelector(GenericRequest request,
180: GenericResponse response, String page, int forumID)
181: throws AuthenticationException, DatabaseException {
182:
183: this .request = request;
184: this .response = response;
185: this .page = page;
186: onlineUser = OnlineUserManager.getInstance().getOnlineUser(
187: request);
188: permission = onlineUser.getPermission();
189: this .forumID = forumID;
190: locale = I18nUtil.getLocaleInRequest(request);
191:
192: init(template);
193: }
194:
195: public CategorySelector(GenericRequest request,
196: GenericResponse response, ForumBean forumBean)
197: throws AuthenticationException, DatabaseException {
198:
199: this .request = request;
200: this .response = response;
201: onlineUser = OnlineUserManager.getInstance().getOnlineUser(
202: request);
203: permission = onlineUser.getPermission();
204: this .categoryID = forumBean.getCategoryID();
205: this .addForum = true;
206: locale = I18nUtil.getLocaleInRequest(request);
207:
208: init(template);
209: }
210:
211: public String drawHeader(CategoryTreeEvent event) {
212: SimpleHash row = new SimpleHash();
213:
214: row.put("name", "header");
215:
216: String choose_forum = MVNForumResourceBundle.getString(locale,
217: "mvnforum.common.prompt.choose_forum");
218: row.put("choose_forum", choose_forum);
219: String search_forum = MVNForumResourceBundle.getString(locale,
220: "mvnforum.user.search.all_forums");
221: row.put("search_forum", search_forum);
222:
223: boolean checkPageNotNull = (page != null);
224: row.put("checkPageNotNull", checkPageNotNull);
225:
226: boolean isAddForum = (addForum == true);
227: row.put("isAddForum", isAddForum);
228:
229: if (checkPageNotNull) {
230: boolean isSearchPage = page.equals("search")
231: || page.equals("searchattachments");
232: row.put("isSearchPage", isSearchPage);
233: //search
234: if (isSearchPage == false) {
235: //assignforumtogroup and movethread has the same style
236: boolean isPageContentAssignForumToGroup = (page
237: .indexOf("assignforumtogroup") != -1);
238: row.put("isPageContentAssignForumToGroup",
239: isPageContentAssignForumToGroup);
240:
241: if (isPageContentAssignForumToGroup) {
242: boolean isPageContentMoveThread = (page
243: .indexOf("movethread") != -1);
244: row.put("isPageContentMoveThread",
245: isPageContentMoveThread);
246:
247: } else {
248: row.put("isPageListUnansweredThreads",
249: ("listunansweredthreads").equals(page));
250: }
251: //listrecentthreads
252: //consist of Category (has forum child) and Forum
253: }
254: } else if (isAddForum) {
255: row.put("addwatch", new Boolean((selectName != null)
256: && (selectName.equals("category"))));
257: row.put("choose_category", MVNForumResourceBundle
258: .getString(locale,
259: "mvnforum.common.prompt.choose_category"));
260: //addForum
261: } else {
262: checkAddCategory = true;
263: //addcategory
264: //only need Category
265: //Mvnforum, not Mvnforum Enterprise
266: }
267:
268: rows.add(row);
269:
270: return "";
271:
272: }
273:
274: public String drawFooter(CategoryTreeEvent event) {
275:
276: SimpleHash row = new SimpleHash();
277:
278: row.put("name", "footer");
279:
280: rows.add(row);
281:
282: return "";
283: }
284:
285: public String drawCategory(CategoryTreeEvent event) {
286:
287: if (!checkAddCategory) {
288: SimpleHash subRoot = new SimpleHash();
289: CategoryBean bean = (CategoryBean) event.getSource();
290: int cateId = -1;
291: subRoot.put("name", "category");
292:
293: try {
294: cateId = ParamUtil.getParameterInt(request
295: .getServletRequest(), "category");
296: } catch (Exception ex) {
297: }
298:
299: if (bean == null) {
300: return "";
301: }
302:
303: boolean noForum_Condition = ((page != null) && (MyUtil
304: .canViewAnyForumInCategory(bean.getCategoryID(),
305: permission) == false));
306: subRoot.put("noForum_Condition", noForum_Condition);
307:
308: if (noForum_Condition) {
309: //In case Category has no Forum
310: return "";
311: }
312:
313: //edit forum, need categoryid contain this forum
314: boolean forumId_Condition = false;
315: if (bean.getCategoryID() == this .categoryID) {
316: forumId_Condition = true;
317: }
318: subRoot.put("forumId_Condition", forumId_Condition);
319:
320: page_Condition = (page == null);
321: subRoot.put("page_Condition", page_Condition);
322:
323: StringBuffer url_CategoryId = new StringBuffer();
324: url_CategoryId.append(bean.getCategoryID());
325: subRoot.put("url_CategoryId", url_CategoryId.toString());
326:
327: if (page_Condition) {
328: if (forumId_Condition == false) {
329: subRoot.put("addForum", addForum);
330: if (addForum) {
331: subRoot.put("categoryId_Condition", (bean
332: .getCategoryID() == cateId));
333: }
334: }
335: } else {
336: //page != null
337: boolean search_Page = ("search").equals(page)
338: || ("searchattachments").equals(page);
339: subRoot.put("search_Page", search_Page);
340:
341: if (search_Page) {
342: StringBuffer forum_CategoryId = new StringBuffer();
343: forum_CategoryId.append("forum-").append(
344: bean.getCategoryID());
345: subRoot.put("forum_CategoryId", forum_CategoryId
346: .toString());
347: } else {
348: boolean assignforumtogroup_Page = (page
349: .indexOf("assignforumtogroup") != -1);
350: subRoot.put("assignforumtogroup_Page",
351: assignforumtogroup_Page);
352:
353: if (assignforumtogroup_Page == false) {
354: boolean listunansweredthreads_Page = ("listunansweredthreads")
355: .equals(page);
356: boolean listattachments_Page = ("listattachments")
357: .equals(page);
358:
359: String destURL = "";
360: subRoot.put("isPageListUnansweredThreads",
361: listunansweredthreads_Page);
362: if (listunansweredthreads_Page) {
363: subRoot.put("select_this_category", (bean
364: .getCategoryID() == this .category));
365: destURL = "c" + bean.getCategoryID();
366: } else if (listattachments_Page) {
367: destURL = urlResolver.encodeURL(request,
368: response,
369: "listattachments?category="
370: + bean.getCategoryID());
371: } else {
372: destURL = urlResolver.encodeURL(request,
373: response, "listforums");
374: }
375: subRoot.put("destURL", destURL);
376: }
377: }
378: }
379:
380: boolean page_ElseCondition = (page != null);
381: subRoot.put("page_ElseCondition", page_ElseCondition);
382:
383: String catName = bean.getCategoryName();
384: subRoot.put("catName", catName);
385:
386: rows.add(subRoot);
387: }
388: return "";
389: }
390:
391: public String drawForum(CategoryTreeEvent event) {
392:
393: SimpleHash subRoot = new SimpleHash();
394:
395: subRoot.put("name", "forum");
396:
397: subRoot.put("hasForums", (checkAddCategory == false));
398: if (checkAddCategory == false) {
399: ForumBean forum = (ForumBean) event.getSource();
400:
401: page_Condition = (page != null);
402: subRoot.put("page_Condition", page_Condition);
403: if (page_Condition) {
404: boolean permiss = permission.canReadPost(forum
405: .getForumID())
406: && (forum.getForumStatus() != ForumBean.FORUM_STATUS_DISABLED);
407: subRoot.put("permiss", permiss);
408:
409: if (permiss) {
410: boolean search_Page = ((("search").equals(page))
411: || ("searchattachments").equals(page) || (page
412: .indexOf("assignforumtogroup") != -1));
413: subRoot.put("search_Page", search_Page);
414: if (search_Page) {
415: boolean isMovethreadPage = (page
416: .indexOf("movethread") != -1);
417: subRoot.put("isMovethreadPage",
418: isMovethreadPage);
419:
420: // foID:forumID file in search page
421: StringBuffer foID = new StringBuffer();
422: foID.append(forum.getForumID());
423: subRoot.put("foID", foID.toString());
424:
425: if (isMovethreadPage) {
426: boolean checkID = (forum.getForumID() != this .forumID);
427: subRoot.put("checkID", checkID);
428: }
429: } else {
430:
431: boolean listunansweredthreads_Page = ("listunansweredthreads")
432: .equals(page);
433: boolean listattachments_Page = ("listattachments")
434: .equals(page);
435:
436: String destURL = "";
437: subRoot.put("isPageListUnansweredThreads",
438: listunansweredthreads_Page);
439: if (listunansweredthreads_Page) {
440: subRoot.put("select_this_forum", (forum
441: .getForumID() == this .forum));
442: destURL = "f" + forum.getForumID();
443: } else if (listattachments_Page) {
444: destURL = urlResolver.encodeURL(request,
445: response, "listattachments?forum="
446: + forum.getForumID());
447: } else {
448: destURL = urlResolver.encodeURL(request,
449: response, "listthreads?forum="
450: + forum.getForumID());
451: }
452: subRoot.put("destURL", destURL);
453:
454: boolean forumId_Condition = forum.getForumID() == forumID;
455: subRoot.put("forumId_Condition",
456: forumId_Condition);
457:
458: }
459:
460: String forumName = forum.getForumName();
461: subRoot.put("forumName", forumName);
462: }
463: }
464: }
465: rows.add(subRoot);
466:
467: return "";
468: }
469:
470: public String drawSeparator(CategoryTreeEvent event) {
471: //no need
472: return "";
473: }
474:
475: public void setDepthTree(int depth) {
476: //noneed
477: }
478:
479: }
|