index.IncrementIndex.java Source code

Java tutorial

Introduction

Here is the source code for index.IncrementIndex.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package index;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

//?  
/* 
 * ?:?????idstoreId.txt 
 *         ????????? 
 *         ??storeId.txtid????? 
 * */
public class IncrementIndex {
    private static java.util.Properties config = new java.util.Properties();

    public static void buildIndex(String indexFile, String storeIdFile, String sqlPath, String defaultPath,
            String classPath, String keyName) {
        try {
            String path = indexFile;//  
            String storeIdPath = storeIdFile;//ID  
            String storeId = "";
            storeId = getStoreId(storeIdPath);
            ResultSet rs = getResult(storeId, sqlPath, defaultPath);
            indexBuilding(path, storeIdPath, rs, classPath, keyName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    * 
    * @param path
    * @param storeIdPath
    * @param rs
    * @return 
    */
    public static boolean indexBuilding(String path, String storeIdPath, ResultSet rs, String classPath,
            String keyName) throws SQLException {
        try {
            Analyzer luceneAnalyzer = new StandardAnalyzer();
            // ??ID??  
            boolean isEmpty = true;
            try {
                File file = new File(storeIdPath);
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileReader fr = new FileReader(storeIdPath);
                BufferedReader br = new BufferedReader(fr);
                if (br.readLine() != null) {
                    isEmpty = false;
                }
                br.close();
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //isEmpty=false?  
            IndexWriter writer = new IndexWriter(path, luceneAnalyzer, isEmpty);
            String storeId = "";
            boolean indexFlag = false;
            // ?
            Class c2 = Class.forName(classPath);
            // 
            java.lang.reflect.Field[] fields = c2.getDeclaredFields();
            while (rs.next()) {
                // list?    
                Map map = new HashMap();
                // ????
                for (java.lang.reflect.Field field : fields) {
                    //  
                    if (keyName.equals(field.getName())) {
                        storeId = rs.getString(field.getName().toUpperCase());
                    }
                    // ???
                    if (whetherExist(field.getName().toUpperCase(), rs)) {
                        map.put(field.getName(), rs.getString(field.getName().toUpperCase()));
                    }
                }
                writer.addDocument(Document(map));
                indexFlag = true;
            }
            writer.optimize();
            writer.close();
            if (indexFlag) {
                // ?ID?  
                writeStoreId(storeIdPath, storeId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("" + e.getClass() + "\n   ?:   " + e.getMessage());
            return false;
        } finally {
            if (null != rs) {
                rs.close();
            }
        }

    }

    // ??ID  
    public static String getStoreId(String path) {
        String storeId = "";
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileReader fr = new FileReader(path);
            BufferedReader br = new BufferedReader(fr);
            storeId = br.readLine();
            if (storeId == null || storeId == "")
                storeId = "0";
            br.close();
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storeId;
    }

    public static ResultSet getResult(String storeId, String sqlPath, String defaultPath) throws Exception {
        // ??
        config.load(new FileInputStream(sqlPath));
        // 
        String jdbcDriverClassName = config.getProperty("jdbcDriverClassName");
        String jdbcUrl = "jdbc:db2://";
        if (jdbcDriverClassName.contains("db2")) {
            jdbcUrl = "jdbc:db2://";
        } else if (jdbcDriverClassName.contains("mysql")) {
            jdbcUrl = "jdbc:mysql://";
        } else if (jdbcDriverClassName.contains("sqlserver")) {
            jdbcUrl = "jdbc:sqlserver://";
        } else if (jdbcDriverClassName.contains("sourceforge")) {
            jdbcUrl = "jdbc:jtds:sqlserver://";
        } else if (jdbcDriverClassName.contains("oracle")) {
            jdbcUrl = "jdbc:oracle:thin:@";
        }
        // url
        jdbcUrl = jdbcUrl + config.getProperty("iampDBIp") + ":" + config.getProperty("iampDBPort") + "/"
                + config.getProperty("iampDBName");
        String jdbcUsername = config.getProperty("iampDBUserName");
        String jdbcPassword = config.getProperty("iampDBPassword");
        // ?
        Class.forName(jdbcDriverClassName).newInstance();
        Connection conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
        // ???
        config.load(new FileInputStream(defaultPath));
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String sql;
        if ("0".equals(storeId)) {
            sql = config.getProperty("nullSql");
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
        } else {
            sql = config.getProperty("entitySql");

            System.out.print("sql=" + sql);
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, storeId);
            // 
            rs = stmt.executeQuery();
        }

        return rs;
    }

    /**
     * id
     * @param path
     * @param storeId
     * @return 
     */
    public static boolean writeStoreId(String path, String storeId) {
        boolean b = false;
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(path);
            PrintWriter out = new PrintWriter(fw);
            out.write(storeId);
            out.close();
            fw.close();
            b = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 
     * 
     */
    public static Document Document(Map map) {
        Document doc = new Document();
        // list
        if (map != null) {
            // ??map
            for (Object key : map.keySet()) {
                doc.add(new Field(key.toString(), map.get(key).toString(), Field.Store.YES, Field.Index.TOKENIZED));
            }
        }
        return doc;
    }

    /**
     * ?
     * @param key ??
     * @param res 
     * @return 
     */
    public static boolean whetherExist(String key, ResultSet res) {
        try {
            // 
            if (res.findColumn(key) > 0) {
                return true;
            }
        } catch (SQLException e) {
            return false;
        }
        return false;
    }
}