www.pudn.com > sendmail.rar > ReportController.java


/* 
 * Created on 2006-1-23 
 * 
 * TODO To change the template for this generated file go to 
 * Window - Preferences - Java - Code Style - Code Templates 
 */ 
package com.sendmail; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.SQLException; 
import java.sql.Statement; 
 
import java.util.*; 
import java.sql.ResultSet; 
import java.util.zip.*; 
 
/** 
 * @author Administrator 
 * 这个类要完成的功能: 
 * 1.查询数据库,将查询结果生成文件(代号A),文件名取当前时间戳, 
 * 2.查看SendMail.FILE_PATH+"filename.txt" (代号L)文件(如果没有则创建之saveNewFileNameToStaticFile()), 
 * 		如果L中没有内容,则将A文件名直接保存到L中去,并跳到Step 4; 
 * 		如果L文件中有且只有一条记录(假设文件名为B),则将比较A文件与B文件, 
 * 		将存在于A文件但不存在于B文件中的记录(调用CompareUtil.compareFileAndExportResult方法) 
 * 		导出到全新的SendMail.FILE_PATH+"expfile.txt" (文件C)中。 
 * 3.删除文件A,将文件C重命名为A 
 * 4.将A文件打包成zip包 
 */ 
public class ReportController { 
    static public String STATIC_FILE_NAME = "filename.txt"; 
    static public String COUNTER_FILE_NAME = "counter.txt"; 
    static public String BASE_FILE_NAME = "base.txt"; 
    static public int INT_MAX = 19; 
    private static String propsFile = "sm.properties"; 
    private String oraDriver; 
    private String oraURL; 
    private String oraUserName; 
    private String oraPassword; 
    private String localPath; 
 
    public static void main(String[] args) throws Exception { 
        ReportController rc = new ReportController(); 
//        File expFile = new File(SendMail.FILE_PATH + CompareUtil.EXP_FILE_NAME); 
//        File txtFile = new File(SendMail.FILE_PATH + "20070810004102.txt"); 
//        expFile.renameTo(txtFile); 
        DateUtil du = new DateUtil(); 
        String today = du.getToday("yyyyMMddHHmmss"); 
        System.out.println(today.substring(0,8)); 
         
        //System.out.println(rc.getReportFileName()); 
        //rc.saveNewFileNameToStaticFile("test.txt"); 
        //rc.loadProperties(propsFile); 
        //rc.connDB(); 
        //rc.createZipFile(SendMail.FILE_PATH+getReportFileName("txt")); 
    } 
     
    public boolean doJob (String txtFileName){ 
        try{ 
	        loadProperties(propsFile); 
	        connDBAndGenListFile(txtFileName); 
	        //String compareFileName = saveNewFileNameToStaticFile(txtFileName); 
	        DateUtil du = new DateUtil(); 
	        String today = du.getToday("yyyyMMdd"); 
	        int intNowCounter = Integer.parseInt(getNowCounter()); 
	        if (intNowCounter>=INT_MAX){  //新的一天 
	            File baseFile = new File(SendMail.FILE_PATH + BASE_FILE_NAME); 
	            baseFile.delete(); 
	            baseFile.createNewFile(); 
	        } 
            CompareUtil cu = new CompareUtil(); 
            File oldFile = new File(SendMail.FILE_PATH + BASE_FILE_NAME); 
            System.out.println("oldFile:"+SendMail.FILE_PATH + BASE_FILE_NAME); 
            if (oldFile.exists()){ 
                System.out.println("oldFile:存在"); 
	            cu.compareFileAndExportResult(txtFileName,BASE_FILE_NAME); 
	            File expFile = new File(SendMail.FILE_PATH + CompareUtil.EXP_FILE_NAME); 
	            //System.out.println("比较后的文件expFile:"+SendMail.FILE_PATH + CompareUtil.EXP_FILE_NAME); 
	            File txtFile = new File(SendMail.FILE_PATH + txtFileName); 
	            //System.out.println("数据库导出txtFile:"+SendMail.FILE_PATH + txtFileName); 
	            txtFile.delete(); 
	            //System.out.println("delete 数据库导出txtFile,rename expfile to txtFile"); 
	            expFile.renameTo(txtFile); 
            }else{ 
                oldFile.createNewFile(); 
            } 
            createZipFile(SendMail.FILE_PATH+txtFileName); 
        }catch (Exception e){ 
            e.printStackTrace(); 
            return false; 
        } 
        return true; 
    } 
    public void connDBAndGenListFile(String txtFileName) { 
        Connection con2 = null; 
        try { 
            con2 = new ConnectionDB(new ConnParameter(this.oraDriver, 
                    this.oraURL, this.oraUserName, this.oraPassword)) 
                    .getConnection(); 
            con2.setAutoCommit(false); 
        } catch (SQLException se) { 
            se.printStackTrace(); 
            System.out.println("连接数据库操作失败."); 
            return; 
        } 
        try { 
            //生成文件 
            genListFile(con2,txtFileName); 
        } catch (Exception e) { 
            System.out.println("throws a Exception " + e.getMessage()); 
        } 
        try { 
            if (con2 != null) { 
                con2.close(); 
            } 
        } catch (SQLException se) { 
            System.out.println("关闭数据库连接时出错."); 
        } 
    } 
    public static synchronized void commitExecute(Connection con) 
            throws SQLException { 
        try { 
            con.commit(); 
        } catch (SQLException se) { 
            throw se; 
        } 
    } 
 
    public static void genListFile(Connection conn,String txtFileName) throws Exception { 
//        String sql1 = "lock table DWPDATA.ts_s_othergsmdetail for access " 
//                + " select distinct msisdn from DWPDATA.ts_s_othergsmdetail" 
//                + " where load_date = '"; 
//        String sql2 = "' " + " and cell_id in ('10381','10382','10383') " 
//                + " and substr(msisdn,1,1) = '1'"; 
        String sql = "lock table dwsdata.ti_s_othergsmdetail for access " 
            + " select distinct msisdn from dwsdata.ti_s_othergsmdetail" 
            + " where cell_id in ('10381','10382','10383') " 
            + " and substr(msisdn,1,1) = '1'"; 
//        DateUtil du = new DateUtil(); 
//        String today = du.getToday("yyyyMMdd"); 
//        today = du.getYesterday(today, "yyyyMMdd"); 
        Statement stmt = null; 
        try { 
//            conn.setAutoCommit(false); 
            stmt = conn.createStatement(); 
            ResultSet rs = stmt.executeQuery(sql); 
            String tempStr= ""; 
            while(rs.next()){ 
                tempStr += rs.getString("msisdn")+System.getProperty("line.separator"); 
            } 
            rs.close(); 
            conn.close(); 
            FileOutputStream fileWriter=new FileOutputStream(SendMail.FILE_PATH+txtFileName); 
            fileWriter.write(tempStr.getBytes());		    
            fileWriter.close(); 
        } catch (Exception e) { 
            System.out.println(e.getMessage()); 
            conn.close(); 
            e.printStackTrace(); 
            try { 
                stmt.close(); 
            } catch (Exception ex) { 
                throw ex; 
            } 
            throw e; 
        } 
    } 
     
    public String saveNewFileNameToStaticFile (String newFileName){ 
        try { 
            File staticFileEx = new File(SendMail.PROGRAM_FILE_PATH + STATIC_FILE_NAME); 
            if (staticFileEx.exists()){ 
                BufferedReader staticFile = new BufferedReader(new FileReader(SendMail.PROGRAM_FILE_PATH + STATIC_FILE_NAME)); 
                String lineValue = ""; 
                //String newline = System.getProperty("line.separator"); 
                while ((lineValue = staticFile.readLine()) != null) { 
                    //System.out.println(lineValue); 
                    if (!lineValue.equals("") ) { 
                        break; 
                    } 
                } 
                staticFile.close(); 
                if (!lineValue.equals(newFileName)) { 
                    staticFileEx.delete(); 
                    staticFileEx.createNewFile(); 
                    //将比较结果输出到一上全新的文件中 
                    PrintWriter out1 = new PrintWriter(new FileWriter(SendMail.PROGRAM_FILE_PATH + STATIC_FILE_NAME)); 
                    out1.println(newFileName); 
                    out1.close(); 
                    return lineValue;  //返回原来文件中保存的上一个文件名 
                } 
            }else{ //文件不存在 
                staticFileEx.createNewFile(); 
                //将比较结果输出到一上全新的文件中 
                PrintWriter out1 = new PrintWriter(new FileWriter(SendMail.PROGRAM_FILE_PATH + STATIC_FILE_NAME)); 
                out1.println(newFileName); 
                out1.close(); 
                return newFileName;  //返回传入的文件名,说明没有上一个文件。 
            } 
 
        } catch (IOException e) { 
            System.out.println("There's an IO Exception@ReportController:"); 
            e.printStackTrace(); 
            return newFileName; 
        } catch (Exception e) { 
            System.out.println("There's an Exception@ReportController:"); 
            e.printStackTrace(); 
            return newFileName; 
        }         
        return newFileName; 
    } 
     
    /** 
     * 记数器,> 20次清零 
     */ 
    public String getNowCounter (){ 
        try { 
            File counterFileEx = new File(SendMail.PROGRAM_FILE_PATH + COUNTER_FILE_NAME); 
            if (counterFileEx.exists()){ 
                BufferedReader staticFile = new BufferedReader(new FileReader(SendMail.PROGRAM_FILE_PATH + COUNTER_FILE_NAME)); 
                String lineValue = ""; 
                while ((lineValue = staticFile.readLine()) != null) { 
                    System.out.println(lineValue); 
                    if (!lineValue.equals("") ) { 
                        break; 
                    } 
                } 
                staticFile.close(); 
                counterFileEx.delete(); 
                counterFileEx.createNewFile(); 
                //将比较结果输出到一上全新的文件中 
                PrintWriter out1 = new PrintWriter(new FileWriter(SendMail.PROGRAM_FILE_PATH + COUNTER_FILE_NAME)); 
                int cplus = Integer.parseInt(lineValue.trim()); 
                int returnValue = 0; 
                if (cplus >=INT_MAX){ 
                    System.out.println("-------------写0"); 
                    cplus=0; 
                    returnValue =INT_MAX; 
                }else{ 
                    cplus++; 
                    System.out.println("-------------写"+cplus); 
                    returnValue = cplus; 
                } 
                out1.println(cplus); 
                out1.close(); 
                return returnValue+"";  //返回原来文件中保存的上一个文件名 
            }else{ //文件不存在 
                counterFileEx.createNewFile(); 
                //将比较结果输出到一上全新的文件中 
                PrintWriter out1 = new PrintWriter(new FileWriter(SendMail.PROGRAM_FILE_PATH + COUNTER_FILE_NAME)); 
                out1.println("0"); 
                out1.close(); 
                return "0";  //返回传入的文件名,说明没有上一个文件。 
            } 
 
        } catch (IOException e) { 
            System.out.println("There's an IO Exception@ReportController:"); 
            e.printStackTrace(); 
            return "0"; 
        } catch (Exception e) { 
            System.out.println("There's an Exception@ReportController:"); 
            e.printStackTrace(); 
            return "0"; 
        }         
    }     
//    public static String getReportFileName(String flag) { 
//        String fileName = ""; 
//        DateUtil du = new DateUtil(); 
//        String today = du.getToday("yyyyMMdd"); 
//        today= du.getYesterday(today,"yyyyMMdd"); 
//        if (flag.equals("txt")){ 
//            fileName= today+".txt"; 
//        }else if (flag.equals("zip")){ 
//            fileName= today+".zip"; 
//        }else{ 
//            fileName= today+".txt"; 
//        } 
//        return fileName; 
//    } 
     
    public static String getReportFileName(String flag) { 
        String fileName = ""; 
        DateUtil du = new DateUtil(); 
        String today = du.getToday("yyyyMMddHHmmss"); 
        //today= du.getYesterday(today,"yyyyMMdd"); 
        if (flag.equals("txt")){ 
            fileName= today+".txt"; 
        }else if (flag.equals("zip")){ 
            fileName= today+".zip"; 
        }else{ 
            fileName= today+".txt"; 
        } 
        return fileName; 
    } 
 
    public static String getReportFileName() { 
        DateUtil du = new DateUtil(); 
        String today = du.getToday("yyyyMMddHHmmss"); 
        return today+".txt"; 
    } 
     
 
    public static String getZipFileName(String txtFileName){ 
        try{ 
            return txtFileName.substring(0,txtFileName.length()-4)+".zip"; 
        }catch (Exception e){ 
            System.out.println("exception @ getZipFileName"+e.getMessage()); 
            return "tmp.zip"; 
        } 
    } 
 
    /** 
     * @return Returns the localPath. 
     */ 
    public String getLocalPath() { 
        return localPath; 
    } 
    /** 
     * @param localPath The localPath to set. 
     */ 
    public void setLocalPath(String localPath) { 
        this.localPath = localPath; 
    } 
     
    public boolean createZipFile (String fileName){ 
        boolean ifSuccess = false; 
//      These are the files to include in the ZIP file 
        String[] filenames = new String[]{fileName}; 
         
        // Create a buffer for reading the files 
        byte[] buf = new byte[1024]; 
         
        try { 
            // Create the ZIP file 
            //String outFilename = SendMail.FILE_PATH+getZipFileName(fileName); 
            String outFilename = getZipFileName(fileName); 
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); 
            // Compress the files 
            for (int i=0; i 0) { 
                    out.write(buf, 0, len); 
                } 
         
                // Complete the entry 
                out.closeEntry(); 
                in.close(); 
            } 
            // Complete the ZIP file 
            out.close(); 
            ifSuccess = true; 
        } catch (IOException e) { 
            System.out.println(e.getMessage()); 
        }         
        return ifSuccess; 
    } 
    private void loadProperties(String fileName) { 
        Properties properties = new Properties(); 
        try { 
            //InputStream is = getClass().getResourceAsStream(fileName); 
            FileInputStream is = new FileInputStream(fileName); 
            properties.load(is); 
            is.close(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
            return; 
        } 
        oraDriver = properties.getProperty("teradata.drivers"); 
        oraURL = properties.getProperty("teradata.url"); 
        oraUserName = properties.getProperty("teradata.user"); 
        oraPassword = properties.getProperty("teradata.password"); 
    } 
 
     
}