Subversion Repositories SmartDukaan

Rev

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