Subversion Repositories SmartDukaan

Rev

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