Subversion Repositories SmartDukaan

Rev

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