Subversion Repositories SmartDukaan

Rev

Rev 22097 | Rev 22125 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21723 ashik.ali 1
package com.spice.profitmandi.dao.repository.dtr;
21545 ashik.ali 2
 
22097 kshitij.so 3
import java.lang.reflect.Type;
21545 ashik.ali 4
import java.net.UnknownHostException;
22097 kshitij.so 5
import java.util.ArrayList;
6
import java.util.List;
21545 ashik.ali 7
 
8
import org.json.JSONObject;
9
 
22097 kshitij.so 10
import com.google.gson.Gson;
11
import com.google.gson.reflect.TypeToken;
21545 ashik.ali 12
import com.mongodb.BasicDBObject;
13
import com.mongodb.DB;
14
import com.mongodb.DBCollection;
22097 kshitij.so 15
import com.mongodb.DBCursor;
21545 ashik.ali 16
import com.mongodb.DBObject;
17
import com.mongodb.MongoClient;
18
import com.mongodb.util.JSON;
22097 kshitij.so 19
import com.spice.profitmandi.dao.model.FofoForm;
21545 ashik.ali 20
 
21
 
22
public class Mongo {
23
 
24
	private static MongoClient mongo_local;
25
	private static MongoClient mongo_dtr;
26
	private static final String CONTENT ="CONTENT";
27
	private static final String SITE_CONTENT = "siteContent";
28
	private static final String CATALOG_DB = "Catalog";
29
	private static final String MASTER_DATA = "MasterData";
22097 kshitij.so 30
	private static final String FOFO_DB = "Fofo";
31
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
21545 ashik.ali 32
	private static final int MONGO_PORT = 27017;
33
 
34
	static {
35
		synchronized(Mongo.class){
36
			try {
37
				mongo_local = new MongoClient( "localhost" , MONGO_PORT );
38
				mongo_dtr = new MongoClient("localhost", MONGO_PORT);
39
			} catch (UnknownHostException e) {
40
				e.printStackTrace();
41
			}
42
		}
43
	}
44
 
45
	public static JSONObject getEntityById(long id) throws Exception{
46
		DB db = mongo_local.getDB(CONTENT);
47
		DBCollection collection = db.getCollection(SITE_CONTENT);
48
		BasicDBObject obj = new BasicDBObject();
49
		obj.append("_id", id);
50
		DBObject result = collection.findOne(obj);
51
		if (result==null){
52
			throw new Exception();
53
		}
54
		return new JSONObject(JSON.serialize(result));
55
	}
56
 
57
	public static JSONObject getItemsByBundleId(long bundleId) throws Exception{
58
		DB db = mongo_dtr.getDB(CATALOG_DB);
59
		DBCollection collection = db.getCollection(MASTER_DATA);
60
		BasicDBObject obj = new BasicDBObject();
61
		BasicDBObject in_query = new BasicDBObject();
62
		obj.append("skuBundleId", bundleId);
63
		in_query.append("$in",new int[] {1,2,3,4,5,6,7});
64
		obj.append("source_id", in_query);
65
		DBObject result = collection.findOne(obj);
66
		if (result==null){
67
			throw new Exception();
68
		}
69
		return new JSONObject(JSON.serialize(result));
70
	}
71
 
72
	public static JSONObject getItemByID(long id) throws Exception{
73
		DB db = mongo_dtr.getDB(CATALOG_DB);
74
		DBCollection collection = db.getCollection(MASTER_DATA);
75
		BasicDBObject obj = new BasicDBObject();
76
		obj.append("_id", id);
77
		DBObject result = collection.findOne(obj);
78
		if (result==null){
79
			throw new Exception();
80
		}
81
		return new JSONObject(JSON.serialize(result));
82
	}
22097 kshitij.so 83
 
84
	public static void persistFofoRegInfo(FofoForm ff){
85
	    DB db = mongo_dtr.getDB(FOFO_DB);
86
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
87
		if(ff.get_id()==0) {
88
			BasicDBObject orderBy = new BasicDBObject();
89
			orderBy.put("_id", -1);
90
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
91
			long id = 1l;
92
			while (cursor.hasNext()) {
93
				Gson gson = new Gson();
94
				FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
95
				id = existingFofo.get_id() + 1;
96
			}
97
			ff.set_id(id);
98
		}
99
		Gson gs = new Gson();
100
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
101
		collection.save(dbObject);
102
	}
103
 
104
	public static List<FofoForm> getFofoForms(int offset, int limit) {
105
		List<FofoForm> ffList = new ArrayList<FofoForm>(); 
106
		DB db = mongo_dtr.getDB(FOFO_DB);
107
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
108
		BasicDBObject orderBy = new BasicDBObject();
109
		orderBy.put("_id", -1);
110
		DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
111
		while (dbc.hasNext()) {
112
			ffList.add(convertJSONToPojo(dbc.next().toString()));
113
		}
114
		return ffList;
115
	}
116
 
117
	public static String getFofoFormJsonStringByFofoId(int fofoId){
118
		DB db = mongo_dtr.getDB(FOFO_DB);
119
		BasicDBObject filter = new BasicDBObject();
120
		filter.append("_id", fofoId);
121
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
22105 ashik.ali 122
		DBObject fofoDbOject = collection.findOne(filter);
22097 kshitij.so 123
		return fofoDbOject.toString();
124
	}
125
 
126
	public static FofoForm getFofoForm(int fofoId) {
127
		String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
128
		System.out.println(fofoFormJsonString);
129
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
130
		//return convertJSONToPojo(fofoDbOject.toString());
131
	}
132
 
133
	private static FofoForm convertJSONToPojo(String json){
21545 ashik.ali 134
 
22097 kshitij.so 135
	    Type type = new TypeToken< FofoForm >(){}.getType();
136
 
137
	    return new Gson().fromJson(json, type);
138
 
139
	}
140
 
21545 ashik.ali 141
}