Subversion Repositories SmartDukaan

Rev

Rev 36437 | 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 com.google.gson.Gson;
4
import com.google.gson.reflect.TypeToken;
32482 amit.gupta 5
import com.mongodb.*;
23793 tejbeer 6
import com.mongodb.client.AggregateIterable;
7
import com.mongodb.client.MongoCollection;
8
import com.mongodb.client.MongoDatabase;
21545 ashik.ali 9
import com.mongodb.util.JSON;
24011 tejbeer 10
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaigns;
25380 amit.gupta 11
import com.spice.profitmandi.dao.model.ContentPojo;
37149 amit 12
import com.spice.profitmandi.dao.model.external.CatalogContentModel;
22097 kshitij.so 13
import com.spice.profitmandi.dao.model.FofoForm;
22496 amit.gupta 14
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
32482 amit.gupta 15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17
import org.bson.Document;
18
import org.json.JSONObject;
21545 ashik.ali 19
 
32482 amit.gupta 20
import java.lang.reflect.Type;
35236 amit 21
import java.util.ArrayList;
22
import java.util.Arrays;
36437 aman 23
import java.util.HashMap;
35236 amit 24
import java.util.List;
25
import java.util.Map;
32482 amit.gupta 26
 
21545 ashik.ali 27
public class Mongo {
22384 amit.gupta 28
 
32482 amit.gupta 29
    private static final Logger LOGGER = LogManager.getLogger(Mongo.class);
21545 ashik.ali 30
 
32482 amit.gupta 31
    private static final String CONTENT = "CONTENT";
35390 amit 32
    private volatile boolean closed = false;
32482 amit.gupta 33
    private static final String SITE_CONTENT = "siteContent";
34
    private static final String CATALOG_DB = "Catalog";
35
    private static final String MASTER_DATA = "MasterData";
36
    private static final String FOFO_DB = "Fofo";
37
    private static final String FOFO_BRANDS = "brands";
38
    private static final String PROFITMANDI_BANNERS = "banners";
39
    private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
40
    private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
41
    private static final String NOTIFICATION_CAMPAIGNS = "notificationcampaigns";
42
    private static final String USER_DB = "User";
43
    private static final int MONGO_PORT = 27017;
25380 amit.gupta 44
 
32482 amit.gupta 45
    private static final Gson gson = new Gson();
22165 amit.gupta 46
 
32482 amit.gupta 47
    private MongoClient mongoClient;
48
    private MongoClient contentMongoClient;
24995 amit.gupta 49
 
32482 amit.gupta 50
    public Mongo(String mongoHost, String contentMongoHost) {
51
        try {
52
            LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
53
            mongoClient = new MongoClient(mongoHost, MONGO_PORT);
54
            contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
55
        } catch (Exception e) {
56
            e.printStackTrace();
57
        }
58
    }
22162 amit.gupta 59
 
32482 amit.gupta 60
    public ContentPojo getEntityById(long id) throws Exception {
61
        DB db = contentMongoClient.getDB(CONTENT);
62
        DBCollection collection = db.getCollection(SITE_CONTENT);
63
        BasicDBObject obj = new BasicDBObject();
64
        obj.append("_id", id);
65
        DBObject result = collection.findOne(obj);
66
        if (result == null) {
67
            throw new Exception();
68
        }
69
        //LOGGER.info(result.toMap());
70
        ContentPojo cp = gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
71
        if (cp.getDefaultImageUrl() != null) {
72
            cp.setDefaultImageUrl(cp.getDefaultImageUrl().replaceAll("saholic", "smartdukaan"));
73
        }
24031 amit.gupta 74
 
32482 amit.gupta 75
        //LOGGER.info("cp" + cp);
29351 tejbeer 76
 
32482 amit.gupta 77
        return cp;
78
    }
29351 tejbeer 79
 
36437 aman 80
    public Map<Long, ContentPojo> getEntitiesByIds(List<Long> ids) {
81
        Map<Long, ContentPojo> result = new HashMap<>();
82
        if (ids == null || ids.isEmpty()) return result;
83
        DB db = contentMongoClient.getDB(CONTENT);
84
        DBCollection collection = db.getCollection(SITE_CONTENT);
85
        BasicDBObject query = new BasicDBObject("_id", new BasicDBObject("$in", ids));
86
        DBCursor cursor = collection.find(query);
87
        while (cursor.hasNext()) {
88
            DBObject doc = cursor.next();
89
            try {
90
                long docId = ((Number) doc.get("_id")).longValue();
91
                ContentPojo cp = gson.fromJson(new BasicDBObject(doc.toMap()).toJson(), ContentPojo.class);
92
                if (cp.getDefaultImageUrl() != null) {
93
                    cp.setDefaultImageUrl(cp.getDefaultImageUrl().replaceAll("saholic", "smartdukaan"));
94
                }
95
                result.put(docId, cp);
96
            } catch (Exception e) {
97
                // skip malformed entries
98
            }
99
        }
100
        cursor.close();
101
        return result;
102
    }
103
 
37149 amit 104
    /**
105
     * siteContent docs for the /external catalogMaster feed, ordered by _id.
106
     * sinceMillis == null: all docs. Otherwise docs whose lastModified >= since
107
     * OR that have no lastModified yet (pre-backfill docs stay visible).
108
     */
109
    public List<CatalogContentModel> getSiteContentPage(Long sinceMillis, int offset, int limit) {
110
        List<CatalogContentModel> result = new ArrayList<>();
111
        DB db = contentMongoClient.getDB(CONTENT);
112
        DBCollection collection = db.getCollection(SITE_CONTENT);
113
        DBCursor cursor = collection.find(siteContentSinceFilter(sinceMillis))
114
                .sort(new BasicDBObject("_id", 1)).skip(offset).limit(limit);
115
        while (cursor.hasNext()) {
116
            DBObject doc = cursor.next();
117
            try {
118
                long docId = ((Number) doc.get("_id")).longValue();
119
                // read directly off the doc: NumberLong renders as {"$numberLong":...}
120
                // in toJson, which gson cannot map onto a Long field
121
                Object lastModified = doc.get("lastModified");
122
                ContentPojo cp = gson.fromJson(new BasicDBObject(doc.toMap()).toJson(), ContentPojo.class);
123
                if (cp.getDefaultImageUrl() != null) {
124
                    cp.setDefaultImageUrl(cp.getDefaultImageUrl().replaceAll("saholic", "smartdukaan"));
125
                }
126
                result.add(new CatalogContentModel(docId,
127
                        lastModified == null ? null : ((Number) lastModified).longValue(), cp));
128
            } catch (Exception e) {
129
                // skip malformed entries
130
            }
131
        }
132
        cursor.close();
133
        return result;
134
    }
135
 
136
    public long countSiteContent(Long sinceMillis) {
137
        DB db = contentMongoClient.getDB(CONTENT);
138
        DBCollection collection = db.getCollection(SITE_CONTENT);
139
        return collection.count(siteContentSinceFilter(sinceMillis));
140
    }
141
 
142
    private static DBObject siteContentSinceFilter(Long sinceMillis) {
143
        if (sinceMillis == null) {
144
            return new BasicDBObject();
145
        }
146
        List<DBObject> or = new ArrayList<>();
147
        or.add(new BasicDBObject("lastModified", new BasicDBObject("$gte", sinceMillis)));
148
        or.add(new BasicDBObject("lastModified", new BasicDBObject("$exists", false)));
149
        return new BasicDBObject("$or", or);
150
    }
151
 
32482 amit.gupta 152
    public ContentPojo getEntityByName(String name) throws Exception {
153
        //LOGGER.info("Name --- {}", name);
154
        DB db = contentMongoClient.getDB(CONTENT);
155
        DBCollection collection = db.getCollection(SITE_CONTENT);
156
        BasicDBObject obj = new BasicDBObject();
157
        obj.append("title", name);
158
        DBObject result = collection.findOne();
159
        if (result == null) {
160
            throw new Exception();
161
        }
162
        return gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
163
    }
22165 amit.gupta 164
 
32482 amit.gupta 165
    public void persistEntity(ContentPojo contentPojo) {
166
        DB db = contentMongoClient.getDB(CONTENT);
167
        DBCollection collection = db.getCollection(SITE_CONTENT);
168
        insertOrUpdateById(collection, contentPojo.getId(), contentPojo);
22384 amit.gupta 169
 
32482 amit.gupta 170
    }
25380 amit.gupta 171
 
32482 amit.gupta 172
    private static <T> void insertOrUpdateById(DBCollection collection, long id, T obj) {
173
        DBObject dbo = BasicDBObject.parse(gson.toJson(obj));
35527 ranu 174
        LOGGER.info("dbo {}", dbo);
32482 amit.gupta 175
        dbo.put("_id", id);
37149 amit 176
        // full-document replace below, so the delta-sync marker must be (re)stamped here
177
        dbo.put("lastModified", System.currentTimeMillis());
35527 ranu 178
        Object result = collection.update(new BasicDBObject("_id", id), dbo, true, false);
179
        LOGGER.info("result mongo {}", result);
32482 amit.gupta 180
    }
25380 amit.gupta 181
 
32482 amit.gupta 182
    public JSONObject getItemsByBundleId(long bundleId) throws Exception {
183
        DB db = mongoClient.getDB(CATALOG_DB);
184
        DBCollection collection = db.getCollection(MASTER_DATA);
185
        BasicDBObject obj = new BasicDBObject();
186
        BasicDBObject in_query = new BasicDBObject();
187
        obj.append("skuBundleId", bundleId);
188
        in_query.append("$in", new int[]{1, 2, 3, 4, 5, 6, 7});
189
        obj.append("source_id", in_query);
190
        DBObject result = collection.findOne(obj);
191
        if (result == null) {
192
            throw new Exception();
193
        }
194
        return new JSONObject(JSON.serialize(result));
195
    }
25380 amit.gupta 196
 
32482 amit.gupta 197
    public JSONObject getItemByID(long id) throws Exception {
198
        DB db = mongoClient.getDB(CATALOG_DB);
199
        DBCollection collection = db.getCollection(MASTER_DATA);
200
        BasicDBObject obj = new BasicDBObject();
201
        obj.append("_id", id);
202
        DBObject result = collection.findOne(obj);
203
        if (result == null) {
204
            throw new Exception();
205
        }
206
        return new JSONObject(JSON.serialize(result));
207
    }
22165 amit.gupta 208
 
32482 amit.gupta 209
    public void persistFofoRegInfo(FofoForm ff) {
210
        DB db = mongoClient.getDB(FOFO_DB);
211
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
212
        if (ff.get_id() == 0) {
213
            BasicDBObject orderBy = new BasicDBObject();
214
            orderBy.put("_id", -1);
215
            DBCursor cursor = collection.find().sort(orderBy).limit(1);
216
            long id = 1l;
217
            while (cursor.hasNext()) {
218
                FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
219
                id = existingFofo.get_id() + 1;
220
            }
221
            ff.set_id(id);
222
        }
223
        DBObject dbObject = (DBObject) JSON.parse(gson.toJson(ff));
224
        collection.save(dbObject);
225
    }
22165 amit.gupta 226
 
32482 amit.gupta 227
    public List<FofoForm> getFofoForms(int offset, int limit) {
228
        List<FofoForm> ffList = new ArrayList<FofoForm>();
229
        DB db = mongoClient.getDB(FOFO_DB);
230
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
231
        BasicDBObject orderBy = new BasicDBObject();
232
        orderBy.put("_id", -1);
233
        DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
234
        while (dbc.hasNext()) {
235
            ffList.add(convertJSONToPojo(dbc.next().toString()));
236
        }
237
        return ffList;
238
    }
22165 amit.gupta 239
 
32482 amit.gupta 240
    public String getFofoFormJsonStringByFofoId(int fofoId) {
241
        DB db = mongoClient.getDB(FOFO_DB);
242
        BasicDBObject filter = new BasicDBObject();
243
        filter.append("_id", fofoId);
244
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
245
        DBObject fofoDbOject = collection.findOne(filter);
246
        if (fofoDbOject != null) {
247
            return fofoDbOject.toString();
248
        } else {
249
            return null;
250
        }
251
    }
22165 amit.gupta 252
 
32482 amit.gupta 253
    public String getFofoFormJsonStringByEmail(String email) {
254
        DB db = mongoClient.getDB(FOFO_DB);
255
        BasicDBObject filter = new BasicDBObject();
256
        filter.append("registeredEmail1", email);
257
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
258
        DBObject fofoDbOject = collection.findOne(filter);
259
        if (fofoDbOject != null) {
260
            return fofoDbOject.toString();
261
        } else {
262
            return null;
263
        }
264
    }
22165 amit.gupta 265
 
32482 amit.gupta 266
    public String getFofoFormsJsonString() {
267
        DB db = mongoClient.getDB(FOFO_DB);
268
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
269
        DBCursor cursor = collection.find();
270
        StringBuilder fofoFormsJsonString = new StringBuilder();
271
        fofoFormsJsonString.append("[");
272
        while (cursor.hasNext()) {
273
            fofoFormsJsonString.append(cursor.next().toString());
274
            if (cursor.hasNext()) {
275
                fofoFormsJsonString.append(",");
276
            }
277
        }
278
        fofoFormsJsonString.append("]");
279
        return fofoFormsJsonString.toString();
280
    }
22165 amit.gupta 281
 
32482 amit.gupta 282
    public FofoForm getFofoForm(int fofoId) {
283
        String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
284
        System.out.println(fofoFormJsonString);
285
        return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
286
        // return convertJSONToPojo(fofoDbOject.toString());
287
    }
22165 amit.gupta 288
 
32482 amit.gupta 289
    public FofoForm getFofoForm(String email) {
290
        String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
291
        System.out.println(fofoFormJsonString);
292
        return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
293
        // return convertJSONToPojo(fofoDbOject.toString());
294
    }
22165 amit.gupta 295
 
32482 amit.gupta 296
    private static FofoForm convertJSONToPojo(String json) {
21545 ashik.ali 297
 
32482 amit.gupta 298
        Type type = new TypeToken<FofoForm>() {
299
        }.getType();
22097 kshitij.so 300
 
32482 amit.gupta 301
        return new Gson().fromJson(json, type);
22097 kshitij.so 302
 
32482 amit.gupta 303
    }
22165 amit.gupta 304
 
32482 amit.gupta 305
    public void updateColumnsById(Map<String, Integer> map, int fofoId) {
306
        DB db = mongoClient.getDB(FOFO_DB);
307
        BasicDBObject filter = new BasicDBObject();
308
        filter.append("_id", fofoId);
309
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
310
        BasicDBObject updateFields = new BasicDBObject();
311
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
312
            updateFields.append(entry.getKey(), entry.getValue());
313
        }
314
        BasicDBObject newDocument = new BasicDBObject();
315
        newDocument.append("$set", updateFields);
316
        collection.update(filter, newDocument);
317
    }
22165 amit.gupta 318
 
32482 amit.gupta 319
    public List<DBObject> getBrandsToDisplay(int categoryId) {
320
        DB db = mongoClient.getDB(FOFO_DB);
321
        BasicDBObject filter = new BasicDBObject();
322
        filter.append("active", true);
323
        if (categoryId != 0) {
324
            filter.append("categoryId", categoryId);
325
        }
326
        return db.getCollection(FOFO_BRANDS).find(filter).toArray();
327
    }
24031 amit.gupta 328
 
32482 amit.gupta 329
    public List<DBObject> getAllBrandsToDisplay(int categoryId) {
330
        DB db = mongoClient.getDB(FOFO_DB);
331
        BasicDBObject filter = new BasicDBObject();
332
        if (categoryId != 0) {
333
            filter.append("categoryId", categoryId);
334
        }
335
        return db.getCollection(FOFO_BRANDS).find(filter).toArray();
336
    }
26343 tejbeer 337
 
32482 amit.gupta 338
    public List<DBObject> getBannersByType(String bannerType) {
339
        DB db = mongoClient.getDB(FOFO_DB);
340
        BasicDBObject filter = new BasicDBObject();
341
        filter.append("type", bannerType);
342
        BasicDBObject orderBy = new BasicDBObject();
343
        orderBy.put("rank", 1);
344
        return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
345
    }
24031 amit.gupta 346
 
32482 amit.gupta 347
    @SuppressWarnings("unchecked")
348
    public List<Document> getSubcategoriesToDisplay() {
349
        MongoDatabase db = mongoClient.getDatabase("Catalog");
350
        System.out.println("Connection to MongoDB database successfully");
351
        MongoCollection<Document> collection = db.getCollection("Deals");
23793 tejbeer 352
 
32482 amit.gupta 353
        Document object = new Document().append("$match", new Document().append("category_id", 6).append("showDeal", 1)
354
                .append("dealRankPoints", new Document("$gt", 0)));
355
        Document ob = new Document("$group",
356
                new Document().append("_id",
357
                                new Document().append("subCategoryId", "$subCategoryId").append("subCategory", "$subCategory"))
358
                        .append("count", new Document("$sum", 1)));
359
        List<Document> pipeline = Arrays.asList(object, ob);
360
        AggregateIterable<Document> cursor = collection.aggregate(pipeline);
24031 amit.gupta 361
 
32482 amit.gupta 362
        List<Document> resultDocuments = new ArrayList<>();
363
        for (Document dbo : cursor) {
364
            System.out.println(dbo.toString());
365
            LOGGER.info("categories" + dbo.toString());
366
            resultDocuments.add(dbo);
367
        }
368
        return resultDocuments;
369
    }
23793 tejbeer 370
 
32482 amit.gupta 371
    public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
372
        DB db = mongoClient.getDB(FOFO_DB);
373
        Gson gs = new Gson();
374
        DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
375
        DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
376
        fofoInterestCollection.save(dbObject);
377
        return true;
378
    }
23793 tejbeer 379
 
32482 amit.gupta 380
    public boolean hasRetailerShownInterest(int userId) {
381
        DB db = mongoClient.getDB(FOFO_DB);
382
        BasicDBObject filter = new BasicDBObject();
383
        filter.append("userId", userId);
384
        DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
385
        return fofoInterestCollection.findOne(filter) != null;
386
    }
23793 tejbeer 387
 
32482 amit.gupta 388
    public void persistNotificationCmpInfo(NotificationCampaigns ff) {
389
        DB db = mongoClient.getDB(USER_DB);
390
        DBCollection collection = db.getCollection(NOTIFICATION_CAMPAIGNS);
391
        if (ff.get_id() == 0) {
392
            BasicDBObject orderBy = new BasicDBObject();
393
            orderBy.put("_id", -1);
394
            DBCursor cursor = collection.find().sort(orderBy).limit(1);
395
            long id = 1l;
396
            while (cursor.hasNext()) {
397
                Gson gson = new Gson();
398
                NotificationCampaigns existingFofo = gson.fromJson(cursor.next().toString(),
399
                        NotificationCampaigns.class);
400
                id = existingFofo.get_id() + 1;
401
            }
402
            ff.set_id(id);
403
        }
404
        Gson gs = new Gson();
405
        DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
406
        collection.save(dbObject);
407
    }
24031 amit.gupta 408
 
35390 amit 409
    /**
410
     * Close MongoDB connections to prevent memory leaks on shutdown
411
     */
412
    public void close() {
413
        if (closed) {
414
            return;
415
        }
416
        closed = true;
417
        LOGGER.info("Closing MongoDB connections...");
418
        try {
419
            if (mongoClient != null) {
420
                mongoClient.close();
421
                LOGGER.info("Main MongoClient closed");
422
            }
423
        } catch (Exception e) {
424
            LOGGER.error("Error closing mongoClient", e);
425
        }
426
        try {
427
            if (contentMongoClient != null) {
428
                contentMongoClient.close();
429
                LOGGER.info("Content MongoClient closed");
430
            }
431
        } catch (Exception e) {
432
            LOGGER.error("Error closing contentMongoClient", e);
433
        }
434
    }
435
 
21545 ashik.ali 436
}