package biz.plexers.sparrow.db;

import java.net.MalformedURLException;

import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.DbAccessException;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbInstance;

import biz.plexers.sparrow.db.exceptions.SignInException;
import biz.plexers.sparrow.db.exceptions.SignUpException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class DbManager {
	private static HttpClient client = null;
	private static CouchDbConnector db = null;
	private static String dbURL = "http://db.plexers.biz:96";
	private static String dbName = "sol";

	public static boolean signIn(String username, String password)
			throws SignInException {
		if (client == null) {
			try {
				client = new StdHttpClient.Builder().url(dbURL)
						.username(username).password(password).build();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
			CouchDbInstance dbInstance = new StdCouchDbInstance(client);
			try {
				db = dbInstance.createConnector(dbName, true);
			} catch (DbAccessException e) {
				throw new SignInException("Invalid Username or Password!");
			}
			return true;
		}
		throw new SignInException("You are already signed in!");
	}

	public static boolean signUp(String username, String password, String email)
			throws SignUpException {
		HttpClient client = null;
		try {
			client = new StdHttpClient.Builder().url(dbURL).build();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		CouchDbInstance dbInstance = new StdCouchDbInstance(client);
		CouchDbConnector usersDb = dbInstance.createConnector("_users", false);
		try {
			usersDb.create(getUser(username, password, email));
		} catch (Exception e) {
			throw new SignUpException("Username not Available");
		}
		return false;
	}

	private static JsonNode getUser(String username, String password,
			String email) {
		ObjectNode user = JsonNodeFactory.instance.objectNode();
		user.put("_id", "org.couchdb.user:" + username);
		user.put("name", username);
		user.put("type", "user");
		user.put("roles", JsonNodeFactory.instance.arrayNode());
		user.put("password", password);
		user.put("email", email);
		return user;
	}

}

@@ -1551,374 +1551,674 @@
 %0D%0A%09%09
-HttpClient client = null;%0D%0A%09%09try %7B%0D%0A%09%09%09client = new StdHttpClient.Builder().url(dbURL).build();%0D%0A%09%09%7D catch (MalformedURLException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%09%09CouchDbInstance dbInstance = new StdCouchDbInstance(client);%0D%0A%09%09CouchDbConnector usersDb = dbInstance.createConnector(%22_users%22, false);%0D%0A%09%09try %7B%0D%0A%09%09%09usersDb.create(getUser(username, password, email)
+CouchDbConnector usersDb = getUsersDb();%0D%0A%09%09try %7B%0D%0A%09%09%09usersDb.create(getUser(username, password, email));%0D%0A%09%09%7D catch (Exception e) %7B%0D%0A%09%09%09throw new SignUpException(%22Username not Available%22);%0D%0A%09%09%7D%0D%0A%09%09return false;%0D%0A%09%7D%0D%0A%0D%0A%09private static CouchDbConnector getUsersDb() %7B%0D%0A%09%09HttpClient client = getClient();%0D%0A%09%09CouchDbInstance dbInstance = new StdCouchDbInstance(client);%0D%0A%09%09CouchDbConnector usersDb = dbInstance.createConnector(%22_users%22, false);%0D%0A%09%09return usersDb;%0D%0A%09%7D%0D%0A%0D%0A%09private static HttpClient getClient() %7B%0D%0A%09%09HttpClient client = null;%0D%0A%09%09if(DbManager.client != null)%0D%0A%09%09%09return DbManager.client;%0D%0A%09%09try %7B%0D%0A%09%09%09client = new StdHttpClient.Builder().url(dbURL).build(
 );%0D%0A
@@ -2220,32 +2220,44 @@
 ();%0D%0A%09%09%7D catch (
+MalformedURL
 Exception e) %7B%0D%0A
@@ -2263,58 +2263,26 @@
 %0A%09%09%09
-throw new SignUpException(%22Username not Available%22
+e.printStackTrace(
 );%0D%0A
@@ -2287,37 +2287,38 @@
 %0D%0A%09%09%7D%0D%0A%09%09return 
-false
+client
 ;%0D%0A%09%7D%0D%0A%0D%0A%09privat

Refactor DbManager