Subversion Repositories SmartDukaan

Rev

Rev 32482 | Rev 35390 | 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;
22097 kshitij.so 12
import com.spice.profitmandi.dao.model.FofoForm;
22496 amit.gupta 13
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
32482 amit.gupta 14
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
16
import org.bson.Document;
17
import org.json.JSONObject;
21545 ashik.ali 18
 
32482 amit.gupta 19
import java.lang.reflect.Type;
35236 amit 20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.List;
23
import java.util.Map;
32482 amit.gupta 24
 
21545 ashik.ali 25
public class Mongo {
22384 amit.gupta 26
 
32482 amit.gupta 27
    private static final Logger LOGGER = LogManager.getLogger(Mongo.class);
21545 ashik.ali 28
 
32482 amit.gupta 29
    private static final String CONTENT = "CONTENT";
30
    private static final String SITE_CONTENT = "siteContent";
31
    private static final String CATALOG_DB = "Catalog";
32
    private static final String MASTER_DATA = "MasterData";
33
    private static final String FOFO_DB = "Fofo";
34
    private static final String FOFO_BRANDS = "brands";
35
    private static final String PROFITMANDI_BANNERS = "banners";
36
    private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
37
    private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
38
    private static final String NOTIFICATION_CAMPAIGNS = "notificationcampaigns";
39
    private static final String USER_DB = "User";
40
    private static final int MONGO_PORT = 27017;
25380 amit.gupta 41
 
32482 amit.gupta 42
    private static final Gson gson = new Gson();
22165 amit.gupta 43
 
32482 amit.gupta 44
    private MongoClient mongoClient;
45
    private MongoClient contentMongoClient;
24995 amit.gupta 46
 
32482 amit.gupta 47
    public Mongo(String mongoHost, String contentMongoHost) {
48
        try {
49
            LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
50
            mongoClient = new MongoClient(mongoHost, MONGO_PORT);
51
            contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
52
        } catch (Exception e) {
53
            e.printStackTrace();
54
        }
55
    }
22162 amit.gupta 56
 
32482 amit.gupta 57
    public ContentPojo getEntityById(long id) throws Exception {
58
        DB db = contentMongoClient.getDB(CONTENT);
59
        DBCollection collection = db.getCollection(SITE_CONTENT);
60
        BasicDBObject obj = new BasicDBObject();
61
        obj.append("_id", id);
62
        DBObject result = collection.findOne(obj);
63
        if (result == null) {
64
            throw new Exception();
65
        }
66
        //LOGGER.info(result.toMap());
67
        ContentPojo cp = gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
68
        if (cp.getDefaultImageUrl() != null) {
69
            cp.setDefaultImageUrl(cp.getDefaultImageUrl().replaceAll("saholic", "smartdukaan"));
70
        }
24031 amit.gupta 71
 
32482 amit.gupta 72
        //LOGGER.info("cp" + cp);
29351 tejbeer 73
 
32482 amit.gupta 74
        return cp;
75
    }
29351 tejbeer 76
 
32482 amit.gupta 77
    public ContentPojo getEntityByName(String name) throws Exception {
78
        //LOGGER.info("Name --- {}", name);
79
        DB db = contentMongoClient.getDB(CONTENT);
80
        DBCollection collection = db.getCollection(SITE_CONTENT);
81
        BasicDBObject obj = new BasicDBObject();
82
        obj.append("title", name);
83
        DBObject result = collection.findOne();
84
        if (result == null) {
85
            throw new Exception();
86
        }
87
        return gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
88
    }
22165 amit.gupta 89
 
32482 amit.gupta 90
    public void persistEntity(ContentPojo contentPojo) {
91
        DB db = contentMongoClient.getDB(CONTENT);
92
        DBCollection collection = db.getCollection(SITE_CONTENT);
93
        insertOrUpdateById(collection, contentPojo.getId(), contentPojo);
22384 amit.gupta 94
 
32482 amit.gupta 95
    }
25380 amit.gupta 96
 
32482 amit.gupta 97
    private static <T> void insertOrUpdateById(DBCollection collection, long id, T obj) {
98
        DBObject dbo = BasicDBObject.parse(gson.toJson(obj));
99
        dbo.put("_id", id);
100
        collection.update(new BasicDBObject("_id", id), dbo, true, false);
101
    }
25380 amit.gupta 102
 
32482 amit.gupta 103
    public JSONObject getItemsByBundleId(long bundleId) throws Exception {
104
        DB db = mongoClient.getDB(CATALOG_DB);
105
        DBCollection collection = db.getCollection(MASTER_DATA);
106
        BasicDBObject obj = new BasicDBObject();
107
        BasicDBObject in_query = new BasicDBObject();
108
        obj.append("skuBundleId", bundleId);
109
        in_query.append("$in", new int[]{1, 2, 3, 4, 5, 6, 7});
110
        obj.append("source_id", in_query);
111
        DBObject result = collection.findOne(obj);
112
        if (result == null) {
113
            throw new Exception();
114
        }
115
        return new JSONObject(JSON.serialize(result));
116
    }
25380 amit.gupta 117
 
32482 amit.gupta 118
    public JSONObject getItemByID(long id) throws Exception {
119
        DB db = mongoClient.getDB(CATALOG_DB);
120
        DBCollection collection = db.getCollection(MASTER_DATA);
121
        BasicDBObject obj = new BasicDBObject();
122
        obj.append("_id", id);
123
        DBObject result = collection.findOne(obj);
124
        if (result == null) {
125
            throw new Exception();
126
        }
127
        return new JSONObject(JSON.serialize(result));
128
    }
22165 amit.gupta 129
 
32482 amit.gupta 130
    public void persistFofoRegInfo(FofoForm ff) {
131
        DB db = mongoClient.getDB(FOFO_DB);
132
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
133
        if (ff.get_id() == 0) {
134
            BasicDBObject orderBy = new BasicDBObject();
135
            orderBy.put("_id", -1);
136
            DBCursor cursor = collection.find().sort(orderBy).limit(1);
137
            long id = 1l;
138
            while (cursor.hasNext()) {
139
                FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
140
                id = existingFofo.get_id() + 1;
141
            }
142
            ff.set_id(id);
143
        }
144
        DBObject dbObject = (DBObject) JSON.parse(gson.toJson(ff));
145
        collection.save(dbObject);
146
    }
22165 amit.gupta 147
 
32482 amit.gupta 148
    public List<FofoForm> getFofoForms(int offset, int limit) {
149
        List<FofoForm> ffList = new ArrayList<FofoForm>();
150
        DB db = mongoClient.getDB(FOFO_DB);
151
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
152
        BasicDBObject orderBy = new BasicDBObject();
153
        orderBy.put("_id", -1);
154
        DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
155
        while (dbc.hasNext()) {
156
            ffList.add(convertJSONToPojo(dbc.next().toString()));
157
        }
158
        return ffList;
159
    }
22165 amit.gupta 160
 
32482 amit.gupta 161
    public String getFofoFormJsonStringByFofoId(int fofoId) {
162
        DB db = mongoClient.getDB(FOFO_DB);
163
        BasicDBObject filter = new BasicDBObject();
164
        filter.append("_id", fofoId);
165
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
166
        DBObject fofoDbOject = collection.findOne(filter);
167
        if (fofoDbOject != null) {
168
            return fofoDbOject.toString();
169
        } else {
170
            return null;
171
        }
172
    }
22165 amit.gupta 173
 
32482 amit.gupta 174
    public String getFofoFormJsonStringByEmail(String email) {
175
        DB db = mongoClient.getDB(FOFO_DB);
176
        BasicDBObject filter = new BasicDBObject();
177
        filter.append("registeredEmail1", email);
178
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
179
        DBObject fofoDbOject = collection.findOne(filter);
180
        if (fofoDbOject != null) {
181
            return fofoDbOject.toString();
182
        } else {
183
            return null;
184
        }
185
    }
22165 amit.gupta 186
 
32482 amit.gupta 187
    public String getFofoFormsJsonString() {
188
        DB db = mongoClient.getDB(FOFO_DB);
189
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
190
        DBCursor cursor = collection.find();
191
        StringBuilder fofoFormsJsonString = new StringBuilder();
192
        fofoFormsJsonString.append("[");
193
        while (cursor.hasNext()) {
194
            fofoFormsJsonString.append(cursor.next().toString());
195
            if (cursor.hasNext()) {
196
                fofoFormsJsonString.append(",");
197
            }
198
        }
199
        fofoFormsJsonString.append("]");
200
        return fofoFormsJsonString.toString();
201
    }
22165 amit.gupta 202
 
32482 amit.gupta 203
    public FofoForm getFofoForm(int fofoId) {
204
        String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
205
        System.out.println(fofoFormJsonString);
206
        return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
207
        // return convertJSONToPojo(fofoDbOject.toString());
208
    }
22165 amit.gupta 209
 
32482 amit.gupta 210
    public FofoForm getFofoForm(String email) {
211
        String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
212
        System.out.println(fofoFormJsonString);
213
        return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
214
        // return convertJSONToPojo(fofoDbOject.toString());
215
    }
22165 amit.gupta 216
 
32482 amit.gupta 217
    private static FofoForm convertJSONToPojo(String json) {
21545 ashik.ali 218
 
32482 amit.gupta 219
        Type type = new TypeToken<FofoForm>() {
220
        }.getType();
22097 kshitij.so 221
 
32482 amit.gupta 222
        return new Gson().fromJson(json, type);
22097 kshitij.so 223
 
32482 amit.gupta 224
    }
22165 amit.gupta 225
 
32482 amit.gupta 226
    public void updateColumnsById(Map<String, Integer> map, int fofoId) {
227
        DB db = mongoClient.getDB(FOFO_DB);
228
        BasicDBObject filter = new BasicDBObject();
229
        filter.append("_id", fofoId);
230
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
231
        BasicDBObject updateFields = new BasicDBObject();
232
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
233
            updateFields.append(entry.getKey(), entry.getValue());
234
        }
235
        BasicDBObject newDocument = new BasicDBObject();
236
        newDocument.append("$set", updateFields);
237
        collection.update(filter, newDocument);
238
    }
22165 amit.gupta 239
 
32482 amit.gupta 240
    public List<DBObject> getBrandsToDisplay(int categoryId) {
241
        DB db = mongoClient.getDB(FOFO_DB);
242
        BasicDBObject filter = new BasicDBObject();
243
        filter.append("active", true);
244
        if (categoryId != 0) {
245
            filter.append("categoryId", categoryId);
246
        }
247
        return db.getCollection(FOFO_BRANDS).find(filter).toArray();
248
    }
24031 amit.gupta 249
 
32482 amit.gupta 250
    public List<DBObject> getAllBrandsToDisplay(int categoryId) {
251
        DB db = mongoClient.getDB(FOFO_DB);
252
        BasicDBObject filter = new BasicDBObject();
253
        if (categoryId != 0) {
254
            filter.append("categoryId", categoryId);
255
        }
256
        return db.getCollection(FOFO_BRANDS).find(filter).toArray();
257
    }
26343 tejbeer 258
 
32482 amit.gupta 259
    public List<DBObject> getBannersByType(String bannerType) {
260
        DB db = mongoClient.getDB(FOFO_DB);
261
        BasicDBObject filter = new BasicDBObject();
262
        filter.append("type", bannerType);
263
        BasicDBObject orderBy = new BasicDBObject();
264
        orderBy.put("rank", 1);
265
        return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
266
    }
24031 amit.gupta 267
 
32482 amit.gupta 268
    @SuppressWarnings("unchecked")
269
    public List<Document> getSubcategoriesToDisplay() {
270
        MongoDatabase db = mongoClient.getDatabase("Catalog");
271
        System.out.println("Connection to MongoDB database successfully");
272
        MongoCollection<Document> collection = db.getCollection("Deals");
23793 tejbeer 273
 
32482 amit.gupta 274
        Document object = new Document().append("$match", new Document().append("category_id", 6).append("showDeal", 1)
275
                .append("dealRankPoints", new Document("$gt", 0)));
276
        Document ob = new Document("$group",
277
                new Document().append("_id",
278
                                new Document().append("subCategoryId", "$subCategoryId").append("subCategory", "$subCategory"))
279
                        .append("count", new Document("$sum", 1)));
280
        List<Document> pipeline = Arrays.asList(object, ob);
281
        AggregateIterable<Document> cursor = collection.aggregate(pipeline);
24031 amit.gupta 282
 
32482 amit.gupta 283
        List<Document> resultDocuments = new ArrayList<>();
284
        for (Document dbo : cursor) {
285
            System.out.println(dbo.toString());
286
            LOGGER.info("categories" + dbo.toString());
287
            resultDocuments.add(dbo);
288
        }
289
        return resultDocuments;
290
    }
23793 tejbeer 291
 
32482 amit.gupta 292
    public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
293
        DB db = mongoClient.getDB(FOFO_DB);
294
        Gson gs = new Gson();
295
        DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
296
        DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
297
        fofoInterestCollection.save(dbObject);
298
        return true;
299
    }
23793 tejbeer 300
 
32482 amit.gupta 301
    public boolean hasRetailerShownInterest(int userId) {
302
        DB db = mongoClient.getDB(FOFO_DB);
303
        BasicDBObject filter = new BasicDBObject();
304
        filter.append("userId", userId);
305
        DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
306
        return fofoInterestCollection.findOne(filter) != null;
307
    }
23793 tejbeer 308
 
32482 amit.gupta 309
    public void persistNotificationCmpInfo(NotificationCampaigns ff) {
310
        DB db = mongoClient.getDB(USER_DB);
311
        DBCollection collection = db.getCollection(NOTIFICATION_CAMPAIGNS);
312
        if (ff.get_id() == 0) {
313
            BasicDBObject orderBy = new BasicDBObject();
314
            orderBy.put("_id", -1);
315
            DBCursor cursor = collection.find().sort(orderBy).limit(1);
316
            long id = 1l;
317
            while (cursor.hasNext()) {
318
                Gson gson = new Gson();
319
                NotificationCampaigns existingFofo = gson.fromJson(cursor.next().toString(),
320
                        NotificationCampaigns.class);
321
                id = existingFofo.get_id() + 1;
322
            }
323
            ff.set_id(id);
324
        }
325
        Gson gs = new Gson();
326
        DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
327
        collection.save(dbObject);
328
    }
24031 amit.gupta 329
 
21545 ashik.ali 330
}