Subversion Repositories SmartDukaan

Rev

Rev 22125 | Rev 22161 | 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;
22125 ashik.ali 7
import java.util.Map;
21545 ashik.ali 8
 
9
import org.json.JSONObject;
10
 
22097 kshitij.so 11
import com.google.gson.Gson;
12
import com.google.gson.reflect.TypeToken;
21545 ashik.ali 13
import com.mongodb.BasicDBObject;
14
import com.mongodb.DB;
15
import com.mongodb.DBCollection;
22097 kshitij.so 16
import com.mongodb.DBCursor;
21545 ashik.ali 17
import com.mongodb.DBObject;
18
import com.mongodb.MongoClient;
19
import com.mongodb.util.JSON;
22097 kshitij.so 20
import com.spice.profitmandi.dao.model.FofoForm;
21545 ashik.ali 21
 
22
 
23
public class Mongo {
24
 
25
	private static MongoClient mongo_local;
26
	private static MongoClient mongo_dtr;
27
	private static final String CONTENT ="CONTENT";
28
	private static final String SITE_CONTENT = "siteContent";
29
	private static final String CATALOG_DB = "Catalog";
30
	private static final String MASTER_DATA = "MasterData";
22097 kshitij.so 31
	private static final String FOFO_DB = "Fofo";
32
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
21545 ashik.ali 33
	private static final int MONGO_PORT = 27017;
34
 
35
	static {
36
		synchronized(Mongo.class){
37
			try {
38
				mongo_local = new MongoClient( "localhost" , MONGO_PORT );
39
				mongo_dtr = new MongoClient("localhost", MONGO_PORT);
40
			} catch (UnknownHostException e) {
41
				e.printStackTrace();
42
			}
43
		}
44
	}
45
 
46
	public static JSONObject getEntityById(long id) throws Exception{
47
		DB db = mongo_local.getDB(CONTENT);
48
		DBCollection collection = db.getCollection(SITE_CONTENT);
49
		BasicDBObject obj = new BasicDBObject();
50
		obj.append("_id", id);
51
		DBObject result = collection.findOne(obj);
52
		if (result==null){
53
			throw new Exception();
54
		}
55
		return new JSONObject(JSON.serialize(result));
56
	}
57
 
58
	public static JSONObject getItemsByBundleId(long bundleId) throws Exception{
59
		DB db = mongo_dtr.getDB(CATALOG_DB);
60
		DBCollection collection = db.getCollection(MASTER_DATA);
61
		BasicDBObject obj = new BasicDBObject();
62
		BasicDBObject in_query = new BasicDBObject();
63
		obj.append("skuBundleId", bundleId);
64
		in_query.append("$in",new int[] {1,2,3,4,5,6,7});
65
		obj.append("source_id", in_query);
66
		DBObject result = collection.findOne(obj);
67
		if (result==null){
68
			throw new Exception();
69
		}
70
		return new JSONObject(JSON.serialize(result));
71
	}
72
 
73
	public static JSONObject getItemByID(long id) throws Exception{
74
		DB db = mongo_dtr.getDB(CATALOG_DB);
75
		DBCollection collection = db.getCollection(MASTER_DATA);
76
		BasicDBObject obj = new BasicDBObject();
77
		obj.append("_id", id);
78
		DBObject result = collection.findOne(obj);
79
		if (result==null){
80
			throw new Exception();
81
		}
82
		return new JSONObject(JSON.serialize(result));
83
	}
22097 kshitij.so 84
 
85
	public static void persistFofoRegInfo(FofoForm ff){
86
	    DB db = mongo_dtr.getDB(FOFO_DB);
87
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
88
		if(ff.get_id()==0) {
89
			BasicDBObject orderBy = new BasicDBObject();
90
			orderBy.put("_id", -1);
91
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
92
			long id = 1l;
93
			while (cursor.hasNext()) {
94
				Gson gson = new Gson();
95
				FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
96
				id = existingFofo.get_id() + 1;
97
			}
98
			ff.set_id(id);
99
		}
100
		Gson gs = new Gson();
101
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
102
		collection.save(dbObject);
103
	}
104
 
105
	public static List<FofoForm> getFofoForms(int offset, int limit) {
106
		List<FofoForm> ffList = new ArrayList<FofoForm>(); 
107
		DB db = mongo_dtr.getDB(FOFO_DB);
108
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
109
		BasicDBObject orderBy = new BasicDBObject();
110
		orderBy.put("_id", -1);
111
		DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
112
		while (dbc.hasNext()) {
113
			ffList.add(convertJSONToPojo(dbc.next().toString()));
114
		}
115
		return ffList;
116
	}
117
 
118
	public static String getFofoFormJsonStringByFofoId(int fofoId){
119
		DB db = mongo_dtr.getDB(FOFO_DB);
120
		BasicDBObject filter = new BasicDBObject();
121
		filter.append("_id", fofoId);
122
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
22105 ashik.ali 123
		DBObject fofoDbOject = collection.findOne(filter);
22097 kshitij.so 124
		return fofoDbOject.toString();
125
	}
126
 
22155 amit.gupta 127
	public static String getFofoFormJsonStringByEmail(String email){
128
		DB db = mongo_dtr.getDB(FOFO_DB);
129
		BasicDBObject filter = new BasicDBObject();
130
		filter.append("registeredEmail1", email);
131
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
132
		DBObject fofoDbOject = collection.findOne(filter);
133
		return fofoDbOject.toString();
134
	}
135
 
22125 ashik.ali 136
	public static String getFofoFormsJsonString(){
137
		DB db = mongo_dtr.getDB(FOFO_DB);
138
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
139
		DBCursor cursor = collection.find();
140
		StringBuilder fofoFormsJsonString = new StringBuilder();
141
		fofoFormsJsonString.append("[");
142
		while(cursor.hasNext()){
143
			fofoFormsJsonString.append(cursor.next().toString());
144
			if(cursor.hasNext()){
145
				fofoFormsJsonString.append(",");
146
			}
147
		}
148
		fofoFormsJsonString.append("]");
149
		return fofoFormsJsonString.toString();
150
	}
151
 
22097 kshitij.so 152
	public static FofoForm getFofoForm(int fofoId) {
153
		String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
154
		System.out.println(fofoFormJsonString);
155
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
156
		//return convertJSONToPojo(fofoDbOject.toString());
157
	}
158
 
22155 amit.gupta 159
	public static FofoForm getFofoForm(String email) {
160
		String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
161
		System.out.println(fofoFormJsonString);
162
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
163
		//return convertJSONToPojo(fofoDbOject.toString());
164
	}
165
 
22097 kshitij.so 166
	private static FofoForm convertJSONToPojo(String json){
21545 ashik.ali 167
 
22097 kshitij.so 168
	    Type type = new TypeToken< FofoForm >(){}.getType();
169
 
170
	    return new Gson().fromJson(json, type);
171
 
172
	}
22125 ashik.ali 173
 
174
	public static void updateColumnsById(Map<String, Integer> map, int fofoId){
175
		DB db = mongo_dtr.getDB(FOFO_DB);
176
		BasicDBObject filter = new BasicDBObject();
177
		filter.append("_id", fofoId);
178
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
179
		BasicDBObject updateFields = new BasicDBObject();
180
		for(Map.Entry<String, Integer> entry : map.entrySet()){
181
			updateFields.append(entry.getKey(), entry.getValue());
182
		}
183
		BasicDBObject newDocument = new BasicDBObject();
184
		newDocument.append("$set", updateFields);
185
		collection.update(filter, newDocument);
186
	}
22097 kshitij.so 187
 
21545 ashik.ali 188
}