Subversion Repositories SmartDukaan

Rev

Rev 23793 | Rev 24031 | 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;
23793 tejbeer 5
import java.util.Arrays;
22097 kshitij.so 6
import java.util.List;
22125 ashik.ali 7
import java.util.Map;
21545 ashik.ali 8
 
9
import org.json.JSONObject;
23568 govind 10
import org.apache.logging.log4j.Logger;
23793 tejbeer 11
import org.bson.Document;
23568 govind 12
import org.apache.logging.log4j.LogManager;
21545 ashik.ali 13
 
22097 kshitij.so 14
import com.google.gson.Gson;
15
import com.google.gson.reflect.TypeToken;
21545 ashik.ali 16
import com.mongodb.BasicDBObject;
17
import com.mongodb.DB;
18
import com.mongodb.DBCollection;
22097 kshitij.so 19
import com.mongodb.DBCursor;
21545 ashik.ali 20
import com.mongodb.DBObject;
21
import com.mongodb.MongoClient;
23793 tejbeer 22
import com.mongodb.client.AggregateIterable;
23
import com.mongodb.client.MongoCollection;
24
import com.mongodb.client.MongoDatabase;
21545 ashik.ali 25
import com.mongodb.util.JSON;
24011 tejbeer 26
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaigns;
22097 kshitij.so 27
import com.spice.profitmandi.dao.model.FofoForm;
22496 amit.gupta 28
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
23793 tejbeer 29
import com.spice.profitmandi.dao.repository.transaction.OrderRepositoryImpl;
21545 ashik.ali 30
 
31
public class Mongo {
22384 amit.gupta 32
 
23568 govind 33
	private static final Logger LOGGER = LogManager.getLogger(Mongo.class);
21545 ashik.ali 34
 
22165 amit.gupta 35
	private static final String CONTENT = "CONTENT";
21545 ashik.ali 36
	private static final String SITE_CONTENT = "siteContent";
37
	private static final String CATALOG_DB = "Catalog";
38
	private static final String MASTER_DATA = "MasterData";
22097 kshitij.so 39
	private static final String FOFO_DB = "Fofo";
22335 amit.gupta 40
	private static final String FOFO_BRANDS = "brands";
22446 amit.gupta 41
	private static final String PROFITMANDI_BANNERS = "banners";
22496 amit.gupta 42
	private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
22097 kshitij.so 43
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
24011 tejbeer 44
	private static final String NOTIFICATION_CAMPAIGNS = "notificationcampaigns";
45
	private static final String USER_DB = "User";
21545 ashik.ali 46
	private static final int MONGO_PORT = 27017;
22165 amit.gupta 47
 
22162 amit.gupta 48
	private MongoClient mongoClient;
49
	private MongoClient contentMongoClient;
24011 tejbeer 50
	private MongoClient dtrDataMongoClient;
22162 amit.gupta 51
 
22171 amit.gupta 52
	public Mongo(String mongoHost, String contentMongoHost) {
22165 amit.gupta 53
		try {
22170 amit.gupta 54
			LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
22165 amit.gupta 55
			mongoClient = new MongoClient(mongoHost, MONGO_PORT);
22169 amit.gupta 56
			contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
22165 amit.gupta 57
		} catch (Exception e) {
58
			e.printStackTrace();
59
		}
21545 ashik.ali 60
	}
24011 tejbeer 61
 
62
	public Mongo(String mongoHost, String contentMongoHost, String dtrDataMongoHost) {
63
		try {
64
			LOGGER.info("mongoHost => {}, contentMongoHost {},dtrDataMongoHost {} ", mongoHost, contentMongoHost,dtrDataMongoHost);
65
			mongoClient = new MongoClient(mongoHost, MONGO_PORT);
66
			contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
67
			dtrDataMongoClient=new MongoClient(dtrDataMongoHost, MONGO_PORT);
68
		} catch (Exception e) {
69
			e.printStackTrace();
70
		}
71
	}
21545 ashik.ali 72
 
22165 amit.gupta 73
	public JSONObject getEntityById(long id) throws Exception {
22162 amit.gupta 74
		DB db = contentMongoClient.getDB(CONTENT);
21545 ashik.ali 75
		DBCollection collection = db.getCollection(SITE_CONTENT);
76
		BasicDBObject obj = new BasicDBObject();
77
		obj.append("_id", id);
78
		DBObject result = collection.findOne(obj);
22165 amit.gupta 79
		if (result == null) {
21545 ashik.ali 80
			throw new Exception();
81
		}
82
		return new JSONObject(JSON.serialize(result));
83
	}
22165 amit.gupta 84
 
22384 amit.gupta 85
	public JSONObject getEntityByName(String name) throws Exception {
22385 amit.gupta 86
		LOGGER.info("Name --- {}", name);
22384 amit.gupta 87
		DB db = contentMongoClient.getDB(CONTENT);
88
		DBCollection collection = db.getCollection(SITE_CONTENT);
89
		BasicDBObject obj = new BasicDBObject();
90
		obj.append("title", name);
91
		DBObject result = collection.findOne(obj);
92
		if (result == null) {
93
			throw new Exception();
94
		}
95
		return new JSONObject(JSON.serialize(result));
96
	}
97
 
22165 amit.gupta 98
	public JSONObject getItemsByBundleId(long bundleId) throws Exception {
22162 amit.gupta 99
		DB db = mongoClient.getDB(CATALOG_DB);
21545 ashik.ali 100
		DBCollection collection = db.getCollection(MASTER_DATA);
101
		BasicDBObject obj = new BasicDBObject();
102
		BasicDBObject in_query = new BasicDBObject();
103
		obj.append("skuBundleId", bundleId);
22165 amit.gupta 104
		in_query.append("$in", new int[] { 1, 2, 3, 4, 5, 6, 7 });
21545 ashik.ali 105
		obj.append("source_id", in_query);
106
		DBObject result = collection.findOne(obj);
22165 amit.gupta 107
		if (result == null) {
21545 ashik.ali 108
			throw new Exception();
109
		}
110
		return new JSONObject(JSON.serialize(result));
111
	}
22165 amit.gupta 112
 
113
	public JSONObject getItemByID(long id) throws Exception {
22162 amit.gupta 114
		DB db = mongoClient.getDB(CATALOG_DB);
21545 ashik.ali 115
		DBCollection collection = db.getCollection(MASTER_DATA);
116
		BasicDBObject obj = new BasicDBObject();
117
		obj.append("_id", id);
118
		DBObject result = collection.findOne(obj);
22165 amit.gupta 119
		if (result == null) {
21545 ashik.ali 120
			throw new Exception();
121
		}
122
		return new JSONObject(JSON.serialize(result));
123
	}
22165 amit.gupta 124
 
125
	public void persistFofoRegInfo(FofoForm ff) {
126
		DB db = mongoClient.getDB(FOFO_DB);
22097 kshitij.so 127
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
22165 amit.gupta 128
		if (ff.get_id() == 0) {
22097 kshitij.so 129
			BasicDBObject orderBy = new BasicDBObject();
130
			orderBy.put("_id", -1);
131
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
132
			long id = 1l;
133
			while (cursor.hasNext()) {
134
				Gson gson = new Gson();
135
				FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
136
				id = existingFofo.get_id() + 1;
137
			}
138
			ff.set_id(id);
139
		}
140
		Gson gs = new Gson();
141
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
142
		collection.save(dbObject);
143
	}
22165 amit.gupta 144
 
22162 amit.gupta 145
	public List<FofoForm> getFofoForms(int offset, int limit) {
22165 amit.gupta 146
		List<FofoForm> ffList = new ArrayList<FofoForm>();
22162 amit.gupta 147
		DB db = mongoClient.getDB(FOFO_DB);
22097 kshitij.so 148
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
149
		BasicDBObject orderBy = new BasicDBObject();
150
		orderBy.put("_id", -1);
151
		DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
152
		while (dbc.hasNext()) {
153
			ffList.add(convertJSONToPojo(dbc.next().toString()));
154
		}
155
		return ffList;
156
	}
22165 amit.gupta 157
 
158
	public String getFofoFormJsonStringByFofoId(int fofoId) {
22162 amit.gupta 159
		DB db = mongoClient.getDB(FOFO_DB);
22097 kshitij.so 160
		BasicDBObject filter = new BasicDBObject();
161
		filter.append("_id", fofoId);
162
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
22105 ashik.ali 163
		DBObject fofoDbOject = collection.findOne(filter);
22165 amit.gupta 164
		if (fofoDbOject != null) {
22161 amit.gupta 165
			return fofoDbOject.toString();
166
		} else {
167
			return null;
168
		}
22097 kshitij.so 169
	}
22165 amit.gupta 170
 
171
	public String getFofoFormJsonStringByEmail(String email) {
22162 amit.gupta 172
		DB db = mongoClient.getDB(FOFO_DB);
22155 amit.gupta 173
		BasicDBObject filter = new BasicDBObject();
174
		filter.append("registeredEmail1", email);
175
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
176
		DBObject fofoDbOject = collection.findOne(filter);
22165 amit.gupta 177
		if (fofoDbOject != null) {
22161 amit.gupta 178
			return fofoDbOject.toString();
179
		} else {
180
			return null;
181
		}
22155 amit.gupta 182
	}
22165 amit.gupta 183
 
184
	public String getFofoFormsJsonString() {
22162 amit.gupta 185
		DB db = mongoClient.getDB(FOFO_DB);
22125 ashik.ali 186
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
187
		DBCursor cursor = collection.find();
188
		StringBuilder fofoFormsJsonString = new StringBuilder();
189
		fofoFormsJsonString.append("[");
22165 amit.gupta 190
		while (cursor.hasNext()) {
22125 ashik.ali 191
			fofoFormsJsonString.append(cursor.next().toString());
22165 amit.gupta 192
			if (cursor.hasNext()) {
22125 ashik.ali 193
				fofoFormsJsonString.append(",");
194
			}
195
		}
196
		fofoFormsJsonString.append("]");
197
		return fofoFormsJsonString.toString();
198
	}
22165 amit.gupta 199
 
22162 amit.gupta 200
	public FofoForm getFofoForm(int fofoId) {
22097 kshitij.so 201
		String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
202
		System.out.println(fofoFormJsonString);
203
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
22165 amit.gupta 204
		// return convertJSONToPojo(fofoDbOject.toString());
22097 kshitij.so 205
	}
22165 amit.gupta 206
 
22162 amit.gupta 207
	public FofoForm getFofoForm(String email) {
22155 amit.gupta 208
		String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
209
		System.out.println(fofoFormJsonString);
210
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
22165 amit.gupta 211
		// return convertJSONToPojo(fofoDbOject.toString());
22155 amit.gupta 212
	}
21545 ashik.ali 213
 
22165 amit.gupta 214
	private static FofoForm convertJSONToPojo(String json) {
22097 kshitij.so 215
 
22165 amit.gupta 216
		Type type = new TypeToken<FofoForm>() {
217
		}.getType();
22097 kshitij.so 218
 
22165 amit.gupta 219
		return new Gson().fromJson(json, type);
220
 
22097 kshitij.so 221
	}
22165 amit.gupta 222
 
223
	public void updateColumnsById(Map<String, Integer> map, int fofoId) {
22162 amit.gupta 224
		DB db = mongoClient.getDB(FOFO_DB);
22125 ashik.ali 225
		BasicDBObject filter = new BasicDBObject();
226
		filter.append("_id", fofoId);
227
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
228
		BasicDBObject updateFields = new BasicDBObject();
22165 amit.gupta 229
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
22125 ashik.ali 230
			updateFields.append(entry.getKey(), entry.getValue());
231
		}
232
		BasicDBObject newDocument = new BasicDBObject();
233
		newDocument.append("$set", updateFields);
234
		collection.update(filter, newDocument);
235
	}
22335 amit.gupta 236
 
237
	public List<DBObject> getBrandsToDisplay() {
238
		DB db = mongoClient.getDB(FOFO_DB);
239
		BasicDBObject filter = new BasicDBObject();
240
		filter.append("active", true);
241
		return db.getCollection(FOFO_BRANDS).find(filter).toArray();
242
	}
22446 amit.gupta 243
 
22447 amit.gupta 244
	public List<DBObject> getBannersByType(String bannerType) {
22446 amit.gupta 245
		DB db = mongoClient.getDB(CATALOG_DB);
246
		BasicDBObject filter = new BasicDBObject();
22449 amit.gupta 247
		filter.append("type", bannerType);
22992 amit.gupta 248
		BasicDBObject orderBy = new BasicDBObject();
23008 amit.gupta 249
		orderBy.put("rank", 1);
22992 amit.gupta 250
		return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
22446 amit.gupta 251
	}
23793 tejbeer 252
 
253
	@SuppressWarnings("unchecked")
254
	public List<Document> getSubcategoriesToDisplay() {
255
		MongoDatabase db = mongoClient.getDatabase("Catalog");
256
		System.out.println("Connection to MongoDB database successfully");
257
		MongoCollection<Document> collection = db.getCollection("Deals");
258
 
259
		Document object = new Document().append("$match", new Document()
260
											.append("category_id", 6)
261
											.append("showDeal", 1)
262
											.append("dealRankPoints", new Document("$gt", 0))
263
							);
264
		Document ob = new Document("$group",
265
							new Document()
266
								.append("_id",
267
									new Document()
268
										.append("subCategoryId", "$subCategoryId")
269
										.append("subCategory", "$subCategory"))
270
								.append("count", new Document("$sum", 1)));
271
		List<Document> pipeline = Arrays.asList(object, ob);
272
		AggregateIterable<Document> cursor = collection.aggregate(pipeline);
273
 
274
		List<Document> resultDocuments = new ArrayList<>();
275
		for (Document dbo : cursor) {
276
			System.out.println(dbo.toString());
277
			LOGGER.info("categories" + dbo.toString());
278
			resultDocuments.add(dbo);
279
		}
280
		return resultDocuments;
281
	}
282
 
22496 amit.gupta 283
	public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
284
		DB db = mongoClient.getDB(FOFO_DB);
285
		Gson gs = new Gson();
286
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
287
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
288
		fofoInterestCollection.save(dbObject);
289
		return true;
290
	}
23793 tejbeer 291
 
22496 amit.gupta 292
	public boolean hasRetailerShownInterest(int userId) {
293
		DB db = mongoClient.getDB(FOFO_DB);
294
		BasicDBObject filter = new BasicDBObject();
23793 tejbeer 295
		filter.append("userId", userId);
22496 amit.gupta 296
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
23793 tejbeer 297
		return fofoInterestCollection.findOne(filter) != null;
22496 amit.gupta 298
	}
24011 tejbeer 299
 
300
	public void persistNotificationCmpInfo(NotificationCampaigns ff) {
301
		DB db = dtrDataMongoClient.getDB(USER_DB);
302
		DBCollection collection = db.getCollection(NOTIFICATION_CAMPAIGNS);
303
		if (ff.get_id() == 0) {
304
			BasicDBObject orderBy = new BasicDBObject();
305
			orderBy.put("_id", -1);
306
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
307
			long id = 1l;
308
			while (cursor.hasNext()) {
309
				Gson gson = new Gson();
310
				NotificationCampaigns existingFofo = gson.fromJson(cursor.next().toString(), NotificationCampaigns.class);
311
				id = existingFofo.get_id() + 1;
312
			}
313
			ff.set_id(id);
314
		}
315
		Gson gs = new Gson();
316
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
317
		collection.save(dbObject);
318
	}
22097 kshitij.so 319
 
21545 ashik.ali 320
}