Subversion Repositories SmartDukaan

Rev

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