Subversion Repositories SmartDukaan

Rev

Rev 24011 | Rev 24383 | 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
 
24031 amit.gupta 9
import org.apache.logging.log4j.LogManager;
23568 govind 10
import org.apache.logging.log4j.Logger;
23793 tejbeer 11
import org.bson.Document;
24031 amit.gupta 12
import org.json.JSONObject;
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;
21545 ashik.ali 29
 
30
public class Mongo {
22384 amit.gupta 31
 
23568 govind 32
	private static final Logger LOGGER = LogManager.getLogger(Mongo.class);
21545 ashik.ali 33
 
22165 amit.gupta 34
	private static final String CONTENT = "CONTENT";
21545 ashik.ali 35
	private static final String SITE_CONTENT = "siteContent";
36
	private static final String CATALOG_DB = "Catalog";
37
	private static final String MASTER_DATA = "MasterData";
22097 kshitij.so 38
	private static final String FOFO_DB = "Fofo";
22335 amit.gupta 39
	private static final String FOFO_BRANDS = "brands";
22446 amit.gupta 40
	private static final String PROFITMANDI_BANNERS = "banners";
22496 amit.gupta 41
	private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
22097 kshitij.so 42
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
24011 tejbeer 43
	private static final String NOTIFICATION_CAMPAIGNS = "notificationcampaigns";
44
	private static final String USER_DB = "User";
21545 ashik.ali 45
	private static final int MONGO_PORT = 27017;
22165 amit.gupta 46
 
22162 amit.gupta 47
	private MongoClient mongoClient;
48
	private MongoClient contentMongoClient;
24011 tejbeer 49
	private MongoClient dtrDataMongoClient;
22162 amit.gupta 50
 
22171 amit.gupta 51
	public Mongo(String mongoHost, String contentMongoHost) {
22165 amit.gupta 52
		try {
22170 amit.gupta 53
			LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
22165 amit.gupta 54
			mongoClient = new MongoClient(mongoHost, MONGO_PORT);
22169 amit.gupta 55
			contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
22165 amit.gupta 56
		} catch (Exception e) {
57
			e.printStackTrace();
58
		}
21545 ashik.ali 59
	}
24031 amit.gupta 60
 
24011 tejbeer 61
	public Mongo(String mongoHost, String contentMongoHost, String dtrDataMongoHost) {
62
		try {
24031 amit.gupta 63
			LOGGER.info("mongoHost => {}, contentMongoHost {},dtrDataMongoHost {} ", mongoHost, contentMongoHost,
64
					dtrDataMongoHost);
24011 tejbeer 65
			mongoClient = new MongoClient(mongoHost, MONGO_PORT);
66
			contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
24031 amit.gupta 67
			dtrDataMongoClient = new MongoClient(dtrDataMongoHost, MONGO_PORT);
24011 tejbeer 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
	}
24031 amit.gupta 236
 
237
	public List<DBObject> getBrandsToDisplay(int categoryId) {
22335 amit.gupta 238
		DB db = mongoClient.getDB(FOFO_DB);
239
		BasicDBObject filter = new BasicDBObject();
240
		filter.append("active", true);
24031 amit.gupta 241
		if (categoryId != 0) {
242
			filter.append("categoryId", categoryId);
243
		}
22335 amit.gupta 244
		return db.getCollection(FOFO_BRANDS).find(filter).toArray();
245
	}
24031 amit.gupta 246
 
22447 amit.gupta 247
	public List<DBObject> getBannersByType(String bannerType) {
22446 amit.gupta 248
		DB db = mongoClient.getDB(CATALOG_DB);
249
		BasicDBObject filter = new BasicDBObject();
22449 amit.gupta 250
		filter.append("type", bannerType);
22992 amit.gupta 251
		BasicDBObject orderBy = new BasicDBObject();
23008 amit.gupta 252
		orderBy.put("rank", 1);
22992 amit.gupta 253
		return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
22446 amit.gupta 254
	}
23793 tejbeer 255
 
256
	@SuppressWarnings("unchecked")
257
	public List<Document> getSubcategoriesToDisplay() {
258
		MongoDatabase db = mongoClient.getDatabase("Catalog");
259
		System.out.println("Connection to MongoDB database successfully");
260
		MongoCollection<Document> collection = db.getCollection("Deals");
24031 amit.gupta 261
 
262
		Document object = new Document().append("$match", new Document().append("category_id", 6).append("showDeal", 1)
263
				.append("dealRankPoints", new Document("$gt", 0)));
23793 tejbeer 264
		Document ob = new Document("$group",
24031 amit.gupta 265
				new Document().append("_id",
266
						new Document().append("subCategoryId", "$subCategoryId").append("subCategory", "$subCategory"))
267
						.append("count", new Document("$sum", 1)));
23793 tejbeer 268
		List<Document> pipeline = Arrays.asList(object, ob);
269
		AggregateIterable<Document> cursor = collection.aggregate(pipeline);
270
 
271
		List<Document> resultDocuments = new ArrayList<>();
272
		for (Document dbo : cursor) {
273
			System.out.println(dbo.toString());
274
			LOGGER.info("categories" + dbo.toString());
275
			resultDocuments.add(dbo);
276
		}
277
		return resultDocuments;
278
	}
279
 
22496 amit.gupta 280
	public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
281
		DB db = mongoClient.getDB(FOFO_DB);
282
		Gson gs = new Gson();
283
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
284
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
285
		fofoInterestCollection.save(dbObject);
286
		return true;
287
	}
23793 tejbeer 288
 
22496 amit.gupta 289
	public boolean hasRetailerShownInterest(int userId) {
290
		DB db = mongoClient.getDB(FOFO_DB);
291
		BasicDBObject filter = new BasicDBObject();
23793 tejbeer 292
		filter.append("userId", userId);
22496 amit.gupta 293
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
23793 tejbeer 294
		return fofoInterestCollection.findOne(filter) != null;
22496 amit.gupta 295
	}
24031 amit.gupta 296
 
24011 tejbeer 297
	public void persistNotificationCmpInfo(NotificationCampaigns ff) {
298
		DB db = dtrDataMongoClient.getDB(USER_DB);
299
		DBCollection collection = db.getCollection(NOTIFICATION_CAMPAIGNS);
300
		if (ff.get_id() == 0) {
301
			BasicDBObject orderBy = new BasicDBObject();
302
			orderBy.put("_id", -1);
303
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
304
			long id = 1l;
305
			while (cursor.hasNext()) {
306
				Gson gson = new Gson();
24031 amit.gupta 307
				NotificationCampaigns existingFofo = gson.fromJson(cursor.next().toString(),
308
						NotificationCampaigns.class);
24011 tejbeer 309
				id = existingFofo.get_id() + 1;
310
			}
311
			ff.set_id(id);
312
		}
313
		Gson gs = new Gson();
314
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
315
		collection.save(dbObject);
316
	}
22097 kshitij.so 317
 
21545 ashik.ali 318
}