package org.diagbot.graph.util;

import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Config;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * 连接neo4j,关闭neo4j工具类
 * Created by DELL on 2018/11/15.
 */
public class ConnectionNeo4j {
    public Driver connectionNeo4j(){
        Driver driver=null;
        InputStream inputstream;
        try {
            Properties prop = new Properties();

            inputstream = this.getClass().getClassLoader().getResourceAsStream("bolt.properties");

            if (inputstream != null) {
                prop.load(inputstream);
                String uri = prop.getProperty("bolt.uri");
                String user = prop.getProperty("bolt.user");
                String passwd = prop.getProperty("bolt.passwd");

                driver = GraphDatabase.driver(uri, AuthTokens.basic(user, passwd),
                        Config.build().withMaxConnectionLifetime(30, TimeUnit.MINUTES)
                                .withMaxTransactionRetryTime(5, TimeUnit.SECONDS)
                                .withMaxConnectionPoolSize(50)
                                .withConnectionAcquisitionTimeout(2, TimeUnit.MINUTES)
                                .toConfig()
                );
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return driver;
    }
    public Driver connection232Neo4j(){
        Driver driver=null;
        InputStream inputstream;
        try {
            Properties prop = new Properties();

            inputstream = this.getClass().getClassLoader().getResourceAsStream("bolt.properties");

            if (inputstream != null) {
                prop.load(inputstream);
                String uri = prop.getProperty("bolt232.uri");
                String user = prop.getProperty("bolt232.user");
                String passwd = prop.getProperty("bolt232.passwd");

                driver = GraphDatabase.driver(uri, AuthTokens.basic(user, passwd),
                        Config.build().withMaxConnectionLifetime(30, TimeUnit.MINUTES)
                                .withMaxTransactionRetryTime(5, TimeUnit.SECONDS)
                                .withMaxConnectionPoolSize(50)
                                .withConnectionAcquisitionTimeout(2, TimeUnit.MINUTES)
                                .toConfig()
                );
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return driver;
    }
}
