?? generaladmintaskswebhandler.java
字號:
/*
* $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/GeneralAdminTasksWebHandler.java,v 1.17 2004/06/05 04:58:43 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.17 $
* $Date: 2004/06/05 04:58:43 $
*
* ====================================================================
*
* Copyright (C) 2002-2004 by MyVietnam.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* All copyright notices regarding mvnForum MUST remain intact
* in the scripts and in the outputted HTML.
* The "powered by" text/logo with a link back to
* http://www.mvnForum.com and http://www.MyVietnam.net in the
* footer of the pages MUST remain visible when the pages
* are viewed on the internet or intranet.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Support can be obtained from support forums at:
* http://www.mvnForum.com/mvnforum/index
*
* Correspondence and Marketing Questions can be sent to:
* info@MyVietnam.net
*
* @author: Minh Nguyen minhnn@MyVietnam.net
* @author: Mai Nguyen mai.nh@MyVietnam.net
* @author: Igor Manic imanic@users.sourceforge.net
*/
package com.mvnforum.admin;
import java.io.*;
import java.util.*;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mvnforum.MVNForumConfig;
import com.mvnforum.auth.*;
import com.mvnforum.common.SendMailUtil;
import com.mvnforum.db.DAOFactory;
import com.mvnforum.db.MemberBean;
import com.mvnforum.search.PostIndexer;
import freemarker.template.*;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
class GeneralAdminTasksWebHandler {
private static Log log = LogFactory.getLog(GeneralAdminTasksWebHandler.class);
private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
GeneralAdminTasksWebHandler() {
}
void prepareShowIndex(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureIsAuthenticated();
}
void prepareTestSystem(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
}
void prepareImportExport(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
request.setAttribute("BackupFilesOnServer", ImportWebHandler.getBackupFilesOnServer());
}
void importXmlZip(HttpServletRequest request, HttpServletResponse response)
throws DatabaseException, AssertionException, AuthenticationException, ImportException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
ImportWebHandler.importXmlZip(request, response);
}
void exportXmlZip(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException, ExportException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
ExportWebHandler.exportXmlZip(request);
}
void getExportXmlZip(HttpServletRequest request, HttpServletResponse response)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
ExportWebHandler.getExportXmlZip(request, response);
}
void deleteExportXmlZip(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
ExportWebHandler.deleteExportXmlZip(request);
//now prepare all for redirection to "/importexport"
request.setAttribute("BackupFilesOnServer", ImportWebHandler.getBackupFilesOnServer());
}
void rebuildIndex(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanAdminSystem();
PostIndexer.scheduleRebuildIndexTask();
}
void prepareSendMail(HttpServletRequest request)
throws DatabaseException, AssertionException, AuthenticationException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanSendMail();
}
void sendMail(HttpServletRequest request)
throws BadInputException, MessagingException, DatabaseException,
AuthenticationException, AssertionException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanSendMail();
String from = ParamUtil.getParameterEmail(request, "From");
String to = ParamUtil.getParameter(request, "To");
String cc = ParamUtil.getParameter(request, "Cc");
String bcc = ParamUtil.getParameter(request, "Bcc");
String subject = ParamUtil.getParameter(request, "Subject", true);
String message = ParamUtil.getParameter(request, "Message", true);
int mailToSelector = ParamUtil.getParameterInt(request, "MailToSelector");
Collection mailMessageStructs = new ArrayList();
if (mailToSelector == 0) {// that is, send to specific users
if ((to.length() == 0) && (cc.length() == 0) && (bcc.length() == 0)) {
throw new BadInputException("Please enter at least To, CC or BCC.");
}
MailMessageStruct mailMessageStruct = new MailMessageStruct();
mailMessageStruct.setFrom(from);
mailMessageStruct.setTo(to);
mailMessageStruct.setCc(cc);
mailMessageStruct.setBcc(bcc);
mailMessageStruct.setSubject(subject);
mailMessageStruct.setMessage(message);
mailMessageStructs.add(mailMessageStruct);
} else {// send to group of users
// now add emails in the To, CC, BCC
String[] emailArray = MailUtil.getEmails(to, cc, bcc);
for (int i = 0; i < emailArray.length; i++) {
MailMessageStruct mailMessage = new MailMessageStruct();
mailMessage.setFrom(from);
mailMessage.setTo(emailArray[i]);
mailMessage.setSubject(subject);
mailMessage.setMessage(message);
mailMessageStructs.add(mailMessage);
}
// Then add members coresponding to the mailToSelector
String kind;
switch (mailToSelector) {
case 1:
kind = "all";
break;
case 2:
kind = "activated";
break;
case 3:
kind = "nonactivated";
break;
default:
throw new BadInputException("Cannot process MailToSelector = " + mailToSelector);
}
Collection memberBeans = DAOFactory.getMemberDAO().getMembers_inActivationStatus(kind);
for (Iterator countIterator = memberBeans.iterator(); countIterator.hasNext(); ) {
MemberBean memberBean = (MemberBean) countIterator.next();
MailMessageStruct mailMessage = new MailMessageStruct();
mailMessage.setFrom(from);
mailMessage.setTo(memberBean.getMemberEmail());
mailMessage.setSubject(processMailTemplate(memberBean, subject));
mailMessage.setMessage(processMailTemplate(memberBean, message));
mailMessageStructs.add(mailMessage);
}//for
}
// finally, send the collection of emails
try {
MailUtil.sendMail(mailMessageStructs);
} catch (UnsupportedEncodingException e) {
log.error("Cannot support encoding", e);
}
request.setAttribute("MailMessageStructs", mailMessageStructs);
}
void sendActivateMailToAll(HttpServletRequest request)
throws BadInputException, MessagingException, DatabaseException,
AuthenticationException, AssertionException, ObjectNotFoundException, IOException, TemplateException {
OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
MVNForumPermission permission = onlineUser.getPermission();
permission.ensureCanSendMail();//@todo: check if this is the correct permission
//@todo: review this constant parameter "nonactivated"
Collection memberBeans = DAOFactory.getMemberDAO().getMembers_inActivationStatus("nonactivated");
Collection mailMessageStructs = new ArrayList();
String serverName = ParamUtil.getServer2(request);
for (Iterator countIterator = memberBeans.iterator(); countIterator.hasNext(); ) {
MemberBean memberBean = (MemberBean) countIterator.next();
MailMessageStruct mailMessage = SendMailUtil.getActivationCodeEmail(memberBean.getMemberID(), serverName);
mailMessageStructs.add(mailMessage);
}//for
try {
log.debug("About to send activate mail to all non activated members, total = " + mailMessageStructs.size());
MailUtil.sendMail(mailMessageStructs);
} catch (UnsupportedEncodingException e) {
log.error("Cannot support encoding", e);
}
request.setAttribute("MailMessageStructs", mailMessageStructs);
}
/**
*Process mail with a template from User
*/
private String processMailTemplate(MemberBean memberBean, String message) {
try {
Map root = new HashMap();
StringWriter messageWriter = new StringWriter(256);
//Just assume some variables are needed to be replaced
root.put("memberID", new Integer(memberBean.getMemberID()));
root.put("memberName", memberBean.getMemberName());
root.put("memberFirstname", memberBean.getMemberFirstname());
root.put("memberLastname", memberBean.getMemberLastname());
root.put("memberEmail", memberBean.getMemberEmail());
StringReader stringReader = new StringReader(message);
Configuration cfg = MVNForumConfig.getFreeMarkerConfiguration();
Template messageTemplate = new Template("", stringReader, cfg, "");
messageTemplate.process(root, messageWriter);
message = messageWriter.toString();
} catch (IOException ioe) {
log.error("Cannot process mail template", ioe);
//if we have problem while processing template, we will return orginal message
} catch (TemplateException te) {
log.error("Cannot process mail template", te);
//if we have problem while processing template, we will return orginal message
}
//log.debug("processMailTemplate return = " + message);
return message;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -