package com.enseirb.telecom.s9.db;

import java.io.Serializable;
import java.net.UnknownHostException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class UserDB implements CrudRepository {

	@Override
	public Object save(Object entity) {
		// Maybe the content should be checked....
		try {
			/* Init MangoDB */
			MongoClient mongoClient = DbInit.Connect();
			DB db = mongoClient.getDB("mediahome");
			DBCollection dbUsers = db.getCollection("users");
			
			dbUsers.save(DbInit.createDBObject(entity));
		} catch (UnknownHostException | JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return entity;
	}

	@Override
	public Iterable save(Iterable entities) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object findOne(Serializable id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean exists(Serializable id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Iterable findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Iterable findAll(Iterable ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void delete(Serializable id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Object entity) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Iterable entities) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub

	}

}

@@ -29,16 +29,44 @@
 s9.db;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -125,16 +125,52 @@
 ption;%0A%0A
+import com.enseirb.telecom.s9.User;%0A
 import c
@@ -216,24 +216,110 @@
 gException;%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.mongodb.BasicDBObject;%0A
 import com.m
@@ -358,24 +358,53 @@
 Collection;%0A
+import com.mongodb.DBCursor;%0A
 import com.m
@@ -527,80 +527,13 @@
 %7B%0A%09%09
-// Maybe the content should be checked....%0A%09%09try %7B%0A%09%09%09/* Init MangoDB */
+try %7B
 %0A%09%09%09
@@ -724,16 +724,40 @@
 tity));%0A
+%09%09%09mongoClient.close();%0A
 %09%09%7D catc
@@ -1106,32 +1106,766 @@
 ted method stub%0A
+%09%09//The id is the email address%0A%09%09try %7B%0A%09%09%09MongoClient mongoClient = DbInit.Connect();%0A%09%09%09DB db = mongoClient.getDB(%22mediahome%22);%0A%09%09%09DBCollection dbUsers = db.getCollection(%22users%22);%0A%09%09%09%0A%09%09%09BasicDBObject query = new BasicDBObject(%22mail%22, id);%0A%09%09%09DBCursor cursor = dbUsers.find(query);%0A%09%09%09User user = null;%0A%09%09%09ObjectMapper mapper = new ObjectMapper();%0A%09%09%09if (cursor.hasNext()) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09user = mapper.readValue(cursor.next().toString(), User.class);%0A%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09System.err.println(%22User Mapping failed ! %22);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09return user;%0A%09%09%09%0A%09%09%7D catch (UnknownHostException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09
 %09%09return null;%0A%09
@@ -1855,32 +1855,36 @@
 %09%09%09return null;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Override%0A%09

add FindOne Feature for UserDB