package com.zhiwei.DBTemplate;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.zhiwei.conf.Conf;

/**
 * @deprecated: mongoDB模板类
 * @author LihuaTang
 * 
 */
public class MongoDBTemplate {
	protected static Mongo reader;
	protected static Mongo writer;
	protected String collName;
	protected String dbName;

	public MongoDBTemplate() {
		

		try {
			ServerAddress serverAddress = new ServerAddress(Conf.mongoReadIp,27017);
			List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
			MongoCredential credential = MongoCredential.createCredential(Conf.mongoUserName,
					Conf.mongodefaultDB, Conf.mongoPwd.toCharArray());
			credentialList.add(credential);
			reader = new MongoClient(serverAddress, credentialList);
			// reader = new MongoClient(new MongoClientURI(DBConfig.mongoURI,options));
			// reader = new Mongo(DBConfig.mongoReadIpHZ_91,27017);
			 reader.addOption(com.mongodb.Bytes.QUERYOPTION_NOTIMEOUT);
			 reader.slaveOk();
		} catch (MongoException e) {
			e.printStackTrace();
		}
		if (writer == null) {
			try {
				List<ServerAddress> serverAddrList = new ArrayList<ServerAddress>();
				ServerAddress serverAddress = new ServerAddress(Conf.mongoWriteIp, 27017);
				serverAddrList.add(serverAddress);
				List<MongoCredential> credentialList = new ArrayList<MongoCredential>();
				MongoCredential credential = MongoCredential.createCredential(Conf.mongoUserName,
						Conf.mongodefaultDB, Conf.mongoPwd.toCharArray());
				credentialList.add(credential);
				writer = new MongoClient(serverAddrList, credentialList);
				// writer = new MongoClient(new MongoClientURI(DBConfig.mongoURI,options));
			} catch (MongoException e) {
				e.printStackTrace();
			}
		}
//		
//		if (reader == null) {
//			try {
//				reader = new Mongo(Conf.mongoReadIp, 27017);
//				reader.slaveOk();
//			} catch (MongoException e) {
//				e.printStackTrace();
//			}
//		}
//		if (writer == null) {
//			try {
//				writer = new Mongo(Conf.mongoWriteIp, 27017);
//			} catch (MongoException e) {
//				e.printStackTrace();
//			}
//		}
	}

	public DBCollection getReadColl() {
		final DB db = getReader().getDB(dbName);
		final DBCollection coll = db.getCollection(collName);
		return coll;
	}

	protected Mongo getReader() {
		return reader;
	}

	public DBCollection getWriteColl() {
		final DB db = getWriter().getDB(dbName);
		final DBCollection coll = db.getCollection(collName);
		return coll;

	}

	protected Mongo getWriter() {
		return writer;
	}

	public String getCollName() {
		return collName;
	}

	public String getDbName() {
		return dbName;
	}

	protected void setCollName(final String collName) {
		this.collName = collName;
	}

	protected void setDbName(final String dbName) {
		this.dbName = dbName;
	}

	@SuppressWarnings("static-access")
	protected void setReader(final Mongo reader) {
		this.reader = reader;
	}

	@SuppressWarnings("static-access")
	protected void setWriter(final Mongo writer) {
		this.writer = writer;
	}

	public static void main(String[] args) {
	}

}
