Subversion Repositories SmartDukaan

Rev

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