package kr.ac.kaist.resl.lilliput.util;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bson.BasicBSONObject;
import org.bson.types.BasicBSONList;
import org.bson.types.ObjectId;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteResult;
public class MongoHelper {
private Mongo m;
private DB db;
public MongoHelper()
{
try {
m = new Mongo("143.248.53.35", 27017);
db = m.getDB("Lilliput");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void close()
{
m.close();
}
public String deleteDocumentInDB(String docName, String epc)
{
DBCollection collection = db.getCollection(docName);
DBObject queryObject = new BasicDBObject();
queryObject.put("epc", epc );
WriteResult result = collection.remove(queryObject);
return result.toString();
}
@SuppressWarnings("deprecation")
public String updateInformationToDB(String docName, String _id, JSONObject updateJson)
{
try {
DBCollection collection = db.getCollection(docName);
DBObject queryObject = new BasicDBObject();
queryObject.put("_id",new ObjectId(_id));
BasicDBObject queriedObject = (BasicDBObject) collection.findOne(queryObject);
@SuppressWarnings("rawtypes")
Iterator iter = updateJson.keys();
while( iter.hasNext())
{
String key = (String)iter.next();
String value;
value = updateJson.getString(key);
if ( queriedObject.containsKey(key) == true ) queriedObject.remove(key);
queriedObject.put(key, value);
}
collection.update(collection.findOne(queryObject), queriedObject);
return "success";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String saveToDB(String docName, JSONObject json, String queryType, String queryValue)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection(docName);
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(json.toString(), Map.class);
BasicDBObject input = new BasicDBObject(inputMap);
collection.insert(input);
DBObject query = new BasicDBObject();
query.put(queryType, queryValue);
DBObject queriedObject = collection.findOne(query);
return queriedObject.get("_id").toString();
} catch (MongoException e) {
e.printStackTrace();
return e.toString();
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return e.toString();
}
}
public String saveToDB(String docName, JSONObject json, List<String> queryTypes)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection(docName);
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(json.toString(), Map.class);
BasicDBObject input = new BasicDBObject(inputMap);
collection.insert(input);
DBObject query = new BasicDBObject();
for( int i = 0 ; i < queryTypes.size() ; i ++ )
{
String queryType = queryTypes.get(i);
query.put(queryType, json.get(queryType));
}
DBObject queriedObject = collection.findOne(query);
return queriedObject.get("_id").toString();
} catch (MongoException e) {
e.printStackTrace();
return e.toString();
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return e.toString();
} catch (JSONException e) {
// TODO Auto-generated catch block
return e.toString();
}
}
public String saveToDBInArray(String docName, String queryString, JSONObject json, String arrayName)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection(docName);
System.out.println("getting collection");
Gson gson = new Gson();
System.out.println("init Gson");
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(json.toString(), Map.class);
System.out.println("json to map");
BasicDBObject input = new BasicDBObject(inputMap);
DBObject queryObject = new BasicDBObject();
queryObject.put("_id",new ObjectId(queryString));
BasicDBObject queriedObject = (BasicDBObject) collection.findOne(queryObject);
BasicBSONList events = (BasicBSONList)queriedObject.get(arrayName);
events.add(input);
queriedObject.removeField(arrayName);
queriedObject.put(arrayName, events);
collection.update(collection.findOne(queryObject), queriedObject);
return queriedObject.get("_id").toString();
} catch (MongoException e) {
e.printStackTrace();
return e.toString();
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return e.toString();
}
}
public String saveHumanEPCToDBLate(String epc, JSONObject human)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
System.out.println("getting collection");
DBObject query = new BasicDBObject();
query.put("fid", human.getString("fid"));
DBObject queriedObject = collection.findOne(query);
Gson gson = new Gson();
System.out.println("init Gson");
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(human.toString(), Map.class);
System.out.println("json to map");
BasicDBObject input = new BasicDBObject(inputMap);
input.put("generalManager", "1");
input.put("objectClass", "1");
input.put("serialNumber", epc);
input.put("epc", "urn:epc:id:gid:1.1."+epc);
collection.update(queriedObject, input);
return "urn:epc:id:gid:1.1."+epc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public boolean saveHumanAsFriendToDB(String fid)
{
try{
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
query.put("generalManager", "1");
query.put("objectClass", "1");
query.put("fid", fid);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null ) //friend is not saved
{
collection.insert(query);
return true;
}
else //friend is already saved
{
return true;
}
} catch (MongoException e) {
e.printStackTrace();
return false;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return false;
}
}
public String saveHumanEPCToDB(String epc, JSONObject human)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(human.toString(), Map.class);
BasicDBObject input = new BasicDBObject(inputMap);
input.put("epc", epc);
collection.insert(input);
return epc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public String saveObjectEPCToDB(String epc, JSONObject object)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(object.toString(), Map.class);
BasicDBObject input = new BasicDBObject(inputMap);
input.put("epc", epc);
collection.insert(input);
return epc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public String savePlaceEPCToDB(String epc, JSONObject object)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(object.toString(), Map.class);
BasicDBObject input = new BasicDBObject(inputMap);
input.put("epc", epc);
collection.insert(input);
return epc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public Boolean isLateBindedInDB(String fid)
{
try{
DBCollection collection = db.getCollection("EPCStorage");
System.out.println("getting collection for unique check");
DBObject query = new BasicDBObject();
query.put("fid", fid);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
return false;
}
else
{
return true;
}
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public List<String> isHumansSavedInDB(List<String> fids)
{
//Check whether object is saved or not
//return : null ? --> none
//return : any string --> Object ID
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
System.out.println("getting collection for unique check");
for(int i = 0 ; i < fids.size() ; i++ )
{
String fid = fids.get(i);
DBObject query = new BasicDBObject();
query.put("fid", fid);
DBObject queriedObject = collection.findOne(query);
if( queriedObject != null )
{
fids.set(i, queriedObject.get("epc").toString());
}
}
return fids;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public String isHumanSavedInDB(String fid)
{
//Check whether object is saved or not
//return : null ? --> none
//return : any string --> Object ID
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
query.put("fid", fid);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
return null;
}
else
{
return queriedObject.get("epc").toString();
}
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public String isSavedInDB(String thingType, String uniqueType, String uniqueID)
{
//Check whether object is saved or not
//return : null ? --> none
//return : any string --> Object ID
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection(thingType);
DBObject query = new BasicDBObject();
query.put(uniqueType, uniqueID);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
return null;
}
else
{
return queriedObject.get("epc").toString();
}
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unused")
public int getVacantEPCinDB(String thingType)
{
//Check whether object is saved or not
//return : null ? --> none
//return : any string --> Object ID
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
String baseEPC = "urn:epc:id:gid:1.";
if(thingType.equals("Human"))
{
baseEPC+="1.";
}
long i = 1L;
String returnEpc = null;
for( ; i < 68719476735L ; i++ )
{
String epc = baseEPC + Long.toString(i);
query.put("epc", epc);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
returnEpc = epc;
break;
}
}
return (int)i;
} catch (MongoException e) {
e.printStackTrace();
return -1;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return -1;
}
}
public String getVacantObjectEPCinDB()
{
try{
//String epc = "urn:epc:id:sgtin:0000001.000002.2";
//String epc = "urn:epc:id:sgln:00000000001.1.2";
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
String baseEPC = "urn:epc:id:sgtin:0000001.000001.";
long i = 1L;
String returnEpc = null;
for( ; i < 68719476735L ; i++ )
{
String epc = baseEPC + Long.toString(i);
query.put("epc", epc);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
returnEpc = epc;
break;
}
}
return returnEpc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public String getVacantPlaceEPCinDB()
{
try{
//String epc = "urn:epc:id:sgtin:0000001.000002.2";
//String epc = "urn:epc:id:sgln:00000000001.1.2";
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
String baseEPC = "urn:epc:id:sgln:00000000001.1.";
long i = 1L;
String returnEpc = null;
for( ; i < 68719476735L ; i++ )
{
String epc = baseEPC + Long.toString(i);
query.put("epc", epc);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
returnEpc = epc;
break;
}
}
return returnEpc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
public String getVacantHumanEPCinDB(String gender)
{
try{
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
String baseEPC = "urn:epc:id:gid:1.";
if(gender.equals("male"))
{
baseEPC+="1.";
}
else if(gender.equals("female"))
{
baseEPC+="2.";
}
else
{
return null;
}
long i = 1L;
String returnEpc = null;
for( ; i < 68719476735L ; i++ )
{
String epc = baseEPC + Long.toString(i);
query.put("epc", epc);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
returnEpc = epc;
break;
}
}
return returnEpc;
} catch (MongoException e) {
e.printStackTrace();
return null;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unused")
public boolean isLastEventInDB(String docName, String queryString, JSONObject json, String arrayName)
{
//Save the data to MongoDB and Return objectID
try{
DBCollection collection = db.getCollection(docName);
Gson gson = new Gson();
@SuppressWarnings("rawtypes")
Map inputMap = gson.fromJson(json.toString(), Map.class);
BasicDBObject input = new BasicDBObject(inputMap);
//�̰� Ȯ���� jsonobject
DBObject queryObject = new BasicDBObject();
queryObject.put("_id",new ObjectId(queryString));
BasicDBObject queriedObject = (BasicDBObject) collection.findOne(queryObject);
BasicBSONList events = (BasicBSONList)queriedObject.get(arrayName);
//�̺�Ʈ ��̸� ���� �Դ�.
//���� Ȯ���Ѵ�
for(int i = 0 ; i < events.size() ; i ++ )
{
BasicBSONObject event = (BasicBSONObject) events.get(i);
String eventTime = event.getString("eventTime");
if( eventTime.equals(json.get("eventTime")))
{
//ã�Ҵ�. �̰� �ֱ� �̺�Ʈ��
return true;
}
}
return false;
} catch (MongoException e) {
e.printStackTrace();
return true;
} catch( JsonSyntaxException e)
{
e.printStackTrace();
return true;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return true;
}
}
public JSONObject getInformationUsingFID(String document, String fid)
{
try
{
JSONObject returnJson = new JSONObject();
Mongo m = new Mongo("143.248.106.26", 27017);
System.out.println("connect db");
DB db = m.getDB("Lilliput");
System.out.println("getting db");
DBCollection collection = db.getCollection(document);
System.out.println("getting collection");
DBObject query = new BasicDBObject();
query.put("fid", fid);
DBObject queriedObject = collection.findOne(query);
if( queriedObject == null )
{
return null;
}
else
{
@SuppressWarnings("rawtypes")
Map queriedMap = queriedObject.toMap();
returnJson = new JSONObject(queriedMap);
return returnJson;
}
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public String getFacebookID(String epc)
{
try
{
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
query.put("epc", epc);
DBObject queriedObject = collection.findOne(query);
String fid = null;
if( queriedObject != null )
{
Object ret = queriedObject.get("fid");
fid = ret.toString();
}
return fid;
}catch( MongoException e ){
return null;
}
}
public String getAccessToken(String epc)
{
try
{
DBCollection collection = db.getCollection("EPCStorage");
DBObject query = new BasicDBObject();
query.put("epc", epc);
DBObject queriedObject = collection.findOne(query);
String ac = null;
if( queriedObject != null )
{
Object ret = queriedObject.get("access_token");
ac = ret.toString();
}
return ac;
}catch( MongoException e ){
return null;
}
}
}