001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/MessageFolderWebHandler.java,v 1.30 2007/10/09 11:09:15 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.30 $
005: * $Date: 2007/10/09 11:09:15 $
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.user;
042:
043: import java.sql.Timestamp;
044: import java.util.*;
045:
046: import com.mvnforum.*;
047: import com.mvnforum.auth.*;
048: import com.mvnforum.common.PrivateMessageUtil;
049: import com.mvnforum.db.DAOFactory;
050: import com.mvnforum.db.MessageFolderBean;
051: import net.myvietnam.mvncore.exception.*;
052: import net.myvietnam.mvncore.security.SecurityUtil;
053: import net.myvietnam.mvncore.util.*;
054: import net.myvietnam.mvncore.web.GenericRequest;
055: import org.apache.commons.logging.Log;
056: import org.apache.commons.logging.LogFactory;
057:
058: public class MessageFolderWebHandler {
059:
060: private static Log log = LogFactory
061: .getLog(MessageFolderWebHandler.class);
062:
063: private OnlineUserManager onlineUserManager = OnlineUserManager
064: .getInstance();
065:
066: public MessageFolderWebHandler() {
067: }
068:
069: public void prepareAdd(GenericRequest request)
070: throws DatabaseException, AuthenticationException {
071:
072: Locale locale = I18nUtil.getLocaleInRequest(request);
073:
074: if (MVNForumConfig.getEnablePrivateMessage() == false) {
075: String localizedMessage = MVNForumResourceBundle
076: .getString(locale,
077: "java.lang.IllegalStateException.private_message_disabled");
078: throw new IllegalStateException(localizedMessage);
079: //throw new IllegalStateException("Cannot operate with private messages. Because the private message feature is disabled");
080: }
081:
082: OnlineUser onlineUser = onlineUserManager
083: .getOnlineUser(request);
084: MVNForumPermission permission = onlineUser.getPermission();
085: permission.ensureIsAuthenticated();
086: permission.ensureCanUseMessage();
087: }
088:
089: public void processAdd(GenericRequest request)
090: throws CreateException, DuplicateKeyException,
091: DatabaseException, BadInputException,
092: AuthenticationException, ForeignKeyNotFoundException {
093:
094: SecurityUtil.checkHttpPostMethod(request);
095:
096: Locale locale = I18nUtil.getLocaleInRequest(request);
097:
098: if (MVNForumConfig.getEnablePrivateMessage() == false) {
099: String localizedMessage = MVNForumResourceBundle
100: .getString(locale,
101: "java.lang.IllegalStateException.private_message_disabled");
102: throw new IllegalStateException(localizedMessage);
103: //throw new IllegalStateException("Cannot operate with private messages. Because the private message feature is disabled");
104: }
105:
106: OnlineUser onlineUser = onlineUserManager
107: .getOnlineUser(request);
108: MVNForumPermission permission = onlineUser.getPermission();
109: permission.ensureIsAuthenticated();
110: permission.ensureCanUseMessage();
111:
112: int folderStatus = 0;
113: int folderOption = 0;
114: int folderType = 0;
115: Timestamp now = DateUtil.getCurrentGMTTimestamp();
116: String folderName = GenericParamUtil.getParameterSafe(request,
117: "FolderName", true);
118: StringUtil.checkGoodName(folderName);
119:
120: int folderOrder = DAOFactory.getMessageFolderDAO()
121: .getMaxFolderOrder(onlineUser.getMemberID());
122: folderOrder++;// One value more than the current max value
123: if (folderOrder < 10)
124: folderOrder = 10;
125:
126: DAOFactory.getMessageFolderDAO().create(folderName,
127: onlineUser.getMemberID(), folderOrder, folderStatus,
128: folderOption, folderType, now, now);
129: }
130:
131: public void prepareList(GenericRequest request)
132: throws DatabaseException, AuthenticationException {
133:
134: Locale locale = I18nUtil.getLocaleInRequest(request);
135:
136: if (MVNForumConfig.getEnablePrivateMessage() == false) {
137: String localizedMessage = MVNForumResourceBundle
138: .getString(locale,
139: "java.lang.IllegalStateException.private_message_disabled");
140: throw new IllegalStateException(localizedMessage);
141: //throw new IllegalStateException("Cannot operate with private messages. Because the private message feature is disabled");
142: }
143:
144: OnlineUser onlineUser = onlineUserManager
145: .getOnlineUser(request);
146: MVNForumPermission permission = onlineUser.getPermission();
147: permission.ensureIsAuthenticated();
148: permission.ensureCanUseMessage();
149:
150: int memberID = onlineUser.getMemberID();
151:
152: Collection messageFolderBeans = DAOFactory
153: .getMessageFolderDAO().getMessageFolders_inMember(
154: memberID);
155: for (Iterator iter = messageFolderBeans.iterator(); iter
156: .hasNext();) {
157: MessageFolderBean messageFolder = (MessageFolderBean) iter
158: .next();
159: int messageCount;
160: int unreadMessageCount;
161: if (messageFolder.getFolderName().equalsIgnoreCase(
162: MVNForumConstant.MESSAGE_FOLDER_DRAFT)) {
163: // also get the draft public messages
164: messageCount = DAOFactory
165: .getMessageDAO()
166: .getNumberOfAllMessages_inMember_inFolder(
167: memberID, messageFolder.getFolderName());
168: unreadMessageCount = DAOFactory
169: .getMessageDAO()
170: .getNumberOfUnreadAllMessages_inMember_inFolder(
171: memberID, messageFolder.getFolderName());
172: } else {
173: messageCount = DAOFactory
174: .getMessageDAO()
175: .getNumberOfNonPublicMessages_inMember_inFolder(
176: memberID, messageFolder.getFolderName());
177: unreadMessageCount = DAOFactory
178: .getMessageDAO()
179: .getNumberOfUnreadNonPublicMessages_inMember_inFolder(
180: memberID, messageFolder.getFolderName());
181: }
182: messageFolder.setMessageCount(messageCount);
183: messageFolder.setUnreadMessageCount(unreadMessageCount);
184: }
185: request.setAttribute("FolderMessageBeans", messageFolderBeans);
186: }
187:
188: public void prepareDelete(GenericRequest request)
189: throws DatabaseException, AuthenticationException,
190: BadInputException, ObjectNotFoundException {
191:
192: Locale locale = I18nUtil.getLocaleInRequest(request);
193:
194: if (MVNForumConfig.getEnablePrivateMessage() == false) {
195: String localizedMessage = MVNForumResourceBundle
196: .getString(locale,
197: "java.lang.IllegalStateException.private_message_disabled");
198: throw new IllegalStateException(localizedMessage);
199: //throw new IllegalStateException("Cannot operate with private messages. Because the private message feature is disabled");
200: }
201:
202: OnlineUser onlineUser = onlineUserManager
203: .getOnlineUser(request);
204: MVNForumPermission permission = onlineUser.getPermission();
205: permission.ensureIsAuthenticated();
206: permission.ensureCanUseMessage();
207:
208: String folderName = GenericParamUtil.getParameterSafe(request,
209: "folder", true);
210: StringUtil.checkGoodName(folderName);
211:
212: if ((folderName
213: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_INBOX))
214: || (folderName
215: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_SENT))
216: || (folderName
217: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT))
218: || (folderName
219: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_TRASH))) {
220: String localizedMessage = MVNForumResourceBundle
221: .getString(locale,
222: "mvncore.exception.BadInputException.cannot_delete_default_folder");
223: throw new BadInputException(localizedMessage);
224: }
225:
226: //Checking if this Folder belong to current Member
227: MessageFolderBean messageFolderBean = null;
228: try {
229: messageFolderBean = DAOFactory.getMessageFolderDAO()
230: .getMessageFolder(folderName,
231: onlineUser.getMemberID());
232: } catch (ObjectNotFoundException onf) {
233: String localizedMessage = MVNForumResourceBundle
234: .getString(
235: locale,
236: "mvncore.exception.ObjectNotFoundException.messagefolder_not_exists",
237: new Object[] { folderName });
238: throw new ObjectNotFoundException(localizedMessage);
239: }
240:
241: int numberOfMessages = DAOFactory.getMessageDAO()
242: .getNumberOfNonPublicMessages_inMember_inFolder(
243: onlineUser.getMemberID(), folderName);
244: int numberOfUnreadMessages = DAOFactory.getMessageDAO()
245: .getNumberOfUnreadNonPublicMessages_inMember_inFolder(
246: onlineUser.getMemberID(), folderName);
247:
248: request.setAttribute("MessageFolderBean", messageFolderBean);
249: request.setAttribute("NumberOfMessages", new Integer(
250: numberOfMessages));
251: request.setAttribute("NumberOfUnreadMessages", new Integer(
252: numberOfUnreadMessages));
253: }
254:
255: public void processDelete(GenericRequest request)
256: throws DatabaseException, AuthenticationException,
257: BadInputException, ObjectNotFoundException {
258:
259: SecurityUtil.checkHttpPostMethod(request);
260:
261: Locale locale = I18nUtil.getLocaleInRequest(request);
262:
263: if (MVNForumConfig.getEnablePrivateMessage() == false) {
264: String localizedMessage = MVNForumResourceBundle
265: .getString(locale,
266: "java.lang.IllegalStateException.private_message_disabled");
267: throw new IllegalStateException(localizedMessage);
268: //throw new IllegalStateException("Cannot operate with private messages. Because the private message feature is disabled");
269: }
270:
271: OnlineUser onlineUser = onlineUserManager
272: .getOnlineUser(request);
273: MVNForumPermission permission = onlineUser.getPermission();
274: permission.ensureIsAuthenticated();
275: permission.ensureCanUseMessage();
276:
277: // now check the password
278: MyUtil.ensureCorrectCurrentPassword(request);
279:
280: int memberID = onlineUser.getMemberID();
281: String folderName = GenericParamUtil.getParameterSafe(request,
282: "folder", true);
283: StringUtil.checkGoodName(folderName);
284:
285: if ((folderName
286: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_INBOX))
287: || (folderName
288: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_SENT))
289: || (folderName
290: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_DRAFT))
291: || (folderName
292: .equalsIgnoreCase(MVNForumConstant.MESSAGE_FOLDER_TRASH))) {
293: String localizedMessage = MVNForumResourceBundle
294: .getString(locale,
295: "mvncore.exception.BadInputException.cannot_delete_default_folder");
296: throw new BadInputException(localizedMessage);
297: }
298:
299: try {
300: DAOFactory.getMessageFolderDAO().findByPrimaryKey(
301: folderName, memberID);
302: } catch (ObjectNotFoundException onf) {
303: String localizedMessage = MVNForumResourceBundle
304: .getString(
305: locale,
306: "mvncore.exception.ObjectNotFoundException.messagefolder_not_exists",
307: new Object[] { folderName });
308: throw new ObjectNotFoundException(localizedMessage);
309: }
310:
311: PrivateMessageUtil.deleteMessageFolderInDatabase(folderName,
312: memberID);
313: }
314:
315: public void processUpdateOrder(GenericRequest request)
316: throws DatabaseException, AuthenticationException,
317: BadInputException, ObjectNotFoundException {
318:
319: Locale locale = I18nUtil.getLocaleInRequest(request);
320:
321: if (MVNForumConfig.getEnablePrivateMessage() == false) {
322: String localizedMessage = MVNForumResourceBundle
323: .getString(locale,
324: "java.lang.IllegalStateException.private_message_disabled");
325: throw new IllegalStateException(localizedMessage);
326: //throw new IllegalStateException("Cannot operate with private messages. Because the private message feature is disabled");
327: }
328:
329: OnlineUser onlineUser = onlineUserManager
330: .getOnlineUser(request);
331: MVNForumPermission permission = onlineUser.getPermission();
332: permission.ensureIsAuthenticated();
333: permission.ensureCanUseMessage();
334:
335: int memberID = onlineUser.getMemberID();
336: String action = GenericParamUtil.getParameterSafe(request,
337: "action", true);
338: String folderName = GenericParamUtil.getParameterSafe(request,
339: "folder", true);
340: StringUtil.checkGoodName(folderName);
341:
342: try {
343: DAOFactory.getMessageFolderDAO().findByPrimaryKey(
344: folderName, memberID);
345: } catch (ObjectNotFoundException onf) {
346: String localizedMessage = MVNForumResourceBundle
347: .getString(
348: locale,
349: "mvncore.exception.ObjectNotFoundException.messagefolder_not_exists",
350: new Object[] { folderName });
351: throw new ObjectNotFoundException(localizedMessage);
352: }
353:
354: Timestamp now = DateUtil.getCurrentGMTTimestamp();
355:
356: if (action.equals("up")) {
357: DAOFactory.getMessageFolderDAO().decreaseFolderOrder(
358: folderName, memberID, now);
359: } else if (action.equals("down")) {
360: DAOFactory.getMessageFolderDAO().increaseFolderOrder(
361: folderName, memberID, now);
362: } else {
363: String localizedMessage = MVNForumResourceBundle
364: .getString(
365: locale,
366: "mvncore.exception.BadInputException.cannot_update_order.unknown_action",
367: new Object[] { action });
368: throw new BadInputException(localizedMessage);
369: //throw new BadInputException("Cannot update Order: unknown action: " + action);
370: }
371: }
372:
373: }
|