⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backupdao.java

📁 EasyJForum 是一个基于 Java 技术的免费社区论坛软件系统
💻 JAVA
字号:
package com.hongshee.ejforum.data;

/**
 * <p>Title: BackupDAO.java</p>
 * <p>Description: Data backup task access object</p>
 * <p>Copyright: Hongshee Software (c) 2008</p>
 * @author jackie du
 * @version 1.0
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.io.File;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import com.hongshee.ejforum.util.PageUtils;
import com.hongshee.ejforum.util.AppUtils;
import com.hongshee.ejforum.common.ForumSetting;
import com.hongshee.ejforum.common.AppContext;
import com.hongshee.common.util.FileZipper;
import com.hongshee.ejforum.common.AppException;

public class BackupDAO extends EntityDAO
{
    private static BackupDAO _dao = null;

    protected BackupDAO()
    {}

    public static BackupDAO getInstance()
    {
        if (_dao == null)
        {
            _dao = new BackupDAO();
        }
        return _dao;
    } 

    /**
     * Add a backup task
     * @param 
     *      request - HttpServletRequest
     * @return none
     * @throws SQLException
     * @since 1.0
     */
    public void addTask(HttpServletRequest request) throws Exception
    {
        String inputFile = PageUtils.getParam(request,"inputFile");
        String outputFile = PageUtils.getParam(request,"outputFile");
        String runat = PageUtils.getParam(request,"runat");
        String sendmail = PageUtils.getParam(request,"sendmail");
        String remark = PageUtils.getParam(request,"remark");

        int p = inputFile.lastIndexOf(File.separatorChar);
        if (p <= 0) 
            throw new AppException("源文件应包含完整路径");
        
        p = outputFile.lastIndexOf(File.separatorChar);
        if (p <= 0) 
            throw new AppException("目标文件应包含完整路径");
        
        String filename = outputFile.substring(p+1);
        
        if (runat.equals("N"))
        {
            String appPath = AppContext.getInstance().getRealPath();
            String fname = appPath;
            if (File.separatorChar == '\\')
                fname = fname + "WEB-INF\\data";
            else
                fname = fname + "WEB-INF/data";
            
            FileZipper zipper = new FileZipper();
            if (inputFile.equals(fname))
            {
                String[] exts = {".lck"};
                zipper.zip(inputFile, outputFile, exts);
            }
            else
                zipper.zip(inputFile, outputFile, null);
            
            if (sendmail.equals("T"))
            {
                String[] attachFiles = new String[1];
                attachFiles[0] = outputFile;
                String forumName = ForumSetting.getInstance().getForumName();
                AppUtils.sendMail2Admin(forumName + "备份文件: " + filename,"详见附件。",attachFiles);
            }
        }
        
        ArrayList<Object> paramValues = new ArrayList<Object>();
        paramValues.add(inputFile);
        paramValues.add(outputFile);
        paramValues.add(runat);
        paramValues.add(sendmail);
        paramValues.add(remark);
        this.execUpdateSql(adapter.BackupTask_Insert, paramValues);
    }
    
    /**
     * Update backup task
     * @param 
     *      request - HttpServletRequest
     * @return none
     * @throws SQLException
     * @since 1.0
     */
    public void updateTask(HttpServletRequest request) throws Exception
    {
        String inputFile = PageUtils.getParam(request,"inputFile");
        String outputFile = PageUtils.getParam(request,"outputFile");
        String runat = PageUtils.getParam(request,"runat");
        String sendmail = PageUtils.getParam(request,"sendmail");
        String remark = PageUtils.getParam(request,"remark");
        String taskID = PageUtils.getParam(request,"taskID");

        int p = inputFile.lastIndexOf(File.separatorChar);
        if (p <= 0) 
            throw new AppException("源文件应包含完整路径");
        
        p = outputFile.lastIndexOf(File.separatorChar);
        if (p <= 0) 
            throw new AppException("目标文件应包含完整路径");
        
        String filename = outputFile.substring(p+1);
        
        if (runat.equals("N"))
        {
            String appPath = AppContext.getInstance().getRealPath();
            String fname = appPath;
            if (File.separatorChar == '\\')
                fname = fname + "WEB-INF\\data";
            else
                fname = fname + "WEB-INF/data";
            
            FileZipper zipper = new FileZipper();
            if (inputFile.equals(fname))
            {
                String[] exts = {".lck"};
                zipper.zip(inputFile, outputFile, exts);
            }
            else
                zipper.zip(inputFile, outputFile, null);
            
            if (sendmail.equals("T"))
            {
                String[] attachFiles = new String[1];
                attachFiles[0] = outputFile;
                String forumName = ForumSetting.getInstance().getForumName();
                AppUtils.sendMail2Admin(forumName + "备份文件: " + filename,"详见附件。",attachFiles);
            }
        }
        
        ArrayList<Object> paramValues = new ArrayList<Object>();
        paramValues.add(inputFile);
        paramValues.add(outputFile);
        paramValues.add(runat);
        paramValues.add(sendmail);
        paramValues.add(remark);
        paramValues.add(taskID);
        this.execUpdateSql(adapter.BackupTask_Update, paramValues);
    }
    
    /**
     * Delete backup task from DB
     * @param 
     *      taskID - Backup task ID to be deleted
     * @return none
     * @throws SQLException
     * @since 1.0
     */
    public void deleteTask(String taskID) throws SQLException
    {
        ArrayList<Object> paramValues = new ArrayList<Object>();
        paramValues.add(taskID);
        this.execUpdateSql(adapter.BackupTask_Delete, paramValues);
    }

    /**
     * Get a backup task info
     * @param
     *      taskID - Backup task ID
     * @return Backup task object
     * @throws SQLException
     * @since 1.0
     */
    public HashMap getTask(String taskID) throws SQLException
    {
        ArrayList<Object> paramValues = new ArrayList<Object>();
        paramValues.add(taskID);
        ArrayList<HashMap> taskList = 
            this.execSelectSql(adapter.BackupTask_Select, paramValues);
        if (taskList != null && taskList.size() > 0)
            return taskList.get(0);
        else
            return null;
    }
    
    /**
     * Get all backup task info
     * @param none
     * @return Backup task list
     * @throws SQLException
     * @since 1.0
     */
    public ArrayList<HashMap> getAllTasks() throws SQLException
    {
        return this.execSelectSql(adapter.BackupTask_GetAllList, null);
    }

    /**
     * Get backup tasks by runAt property
     * @param none
     * @return Backup task list
     * @throws SQLException
     * @since 1.0
     */
    private ArrayList<HashMap> getTasksByRunAt(String runAt) throws SQLException
    {
        ArrayList<Object> paramValues = new ArrayList<Object>();
        paramValues.add(runAt);
        return this.execSelectSql(adapter.BackupTask_GetListByRunAt, paramValues);
    }

    /**
     * Update backup task
     * @param 
     *      request - HttpServletRequest
     * @return none
     * @throws SQLException
     * @since 1.0
     */
    public void execBackupTasks(String runAt) throws Exception
    {
        ArrayList<HashMap> taskList = getTasksByRunAt(runAt);
        if (taskList != null && taskList.size() > 0)
        {
            HashMap aTask = null;
            String inputFile = null;
            String outputFile = null;
            String sendmail = null;
            StringBuilder filenames = new StringBuilder();
            String[] attachFiles = new String[taskList.size()];
            FileZipper zipper = new FileZipper();
            
            String appPath = AppContext.getInstance().getRealPath();
            String fname = appPath;
            if (File.separatorChar == '\\')
                fname = fname + "WEB-INF\\data";
            else
                fname = fname + "WEB-INF/data";
            
            String[] exts = {".lck"};
            
            for (int i=0; i<taskList.size(); i++)
            {
                aTask = taskList.get(i);
                inputFile = (String)aTask.get("INPUTFILE");
                outputFile = (String)aTask.get("OUTPUTFILE");
                sendmail = (String)aTask.get("SENDMAIL");
                
                if (inputFile.equals(fname))
                {
                    zipper.zip(inputFile, outputFile, exts);
                }
                else
                    zipper.zip(inputFile, outputFile, null);

                if (sendmail.equals("T"))
                {                
                    int p = outputFile.lastIndexOf(File.separatorChar);
                    if (p <= 0) continue;
                    if (filenames.length() > 0)
                        filenames.append(",");
                    filenames = filenames.append(outputFile.substring(p+1));
                    attachFiles[i] = outputFile;
                }
            }
            String forumName = ForumSetting.getInstance().getForumName();
            AppUtils.sendMail2Admin(forumName + "备份文件: " + filenames.toString(),
                                    "详见附件。",attachFiles);
        }
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -