Subversion Repositories SmartDukaan

Rev

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