Subversion Repositories SmartDukaan

Rev

Rev 31720 | Rev 35236 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31720 Rev 32482
Line 1... Line 1...
1
package com.spice.profitmandi.dao.repository.dtr;
1
package com.spice.profitmandi.dao.repository.dtr;
2
 
2
 
3
import java.lang.reflect.Type;
-
 
4
import java.util.ArrayList;
-
 
5
import java.util.Arrays;
-
 
6
import java.util.HashMap;
-
 
7
import java.util.Iterator;
-
 
8
import java.util.List;
-
 
9
import java.util.Map;
-
 
10
import java.util.stream.Collectors;
-
 
11
 
-
 
12
import org.apache.logging.log4j.LogManager;
-
 
13
import org.apache.logging.log4j.Logger;
-
 
14
import org.bson.Document;
-
 
15
import org.json.JSONObject;
-
 
16
import org.springframework.beans.factory.annotation.Autowired;
-
 
17
 
-
 
18
import com.google.gson.Gson;
3
import com.google.gson.Gson;
19
import com.google.gson.reflect.TypeToken;
4
import com.google.gson.reflect.TypeToken;
20
import com.mongodb.BasicDBObject;
-
 
21
import com.mongodb.DB;
5
import com.mongodb.*;
22
import com.mongodb.DBCollection;
-
 
23
import com.mongodb.DBCursor;
-
 
24
import com.mongodb.DBObject;
-
 
25
import com.mongodb.MongoClient;
-
 
26
import com.mongodb.client.AggregateIterable;
6
import com.mongodb.client.AggregateIterable;
27
import com.mongodb.client.MongoCollection;
7
import com.mongodb.client.MongoCollection;
28
import com.mongodb.client.MongoDatabase;
8
import com.mongodb.client.MongoDatabase;
29
import com.mongodb.util.JSON;
9
import com.mongodb.util.JSON;
30
import com.spice.profitmandi.common.util.Utils;
10
import com.spice.profitmandi.common.util.Utils;
31
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaigns;
11
import com.spice.profitmandi.dao.entity.dtr.NotificationCampaigns;
32
import com.spice.profitmandi.dao.model.ContentPojo;
12
import com.spice.profitmandi.dao.model.ContentPojo;
33
import com.spice.profitmandi.dao.model.FofoForm;
13
import com.spice.profitmandi.dao.model.FofoForm;
34
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
14
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
-
 
15
import org.apache.logging.log4j.LogManager;
-
 
16
import org.apache.logging.log4j.Logger;
-
 
17
import org.bson.Document;
-
 
18
import org.json.JSONObject;
-
 
19
import org.springframework.beans.factory.annotation.Autowired;
-
 
20
 
-
 
21
import java.lang.reflect.Type;
-
 
22
import java.util.*;
-
 
23
import java.util.stream.Collectors;
35
 
24
 
36
public class Mongo {
25
public class Mongo {
37
 
26
 
38
	private static final Logger LOGGER = LogManager.getLogger(Mongo.class);
27
    private static final Logger LOGGER = LogManager.getLogger(Mongo.class);
39
 
28
 
40
	private static final String CONTENT = "CONTENT";
29
    private static final String CONTENT = "CONTENT";
41
	private static final String SITE_CONTENT = "siteContent";
30
    private static final String SITE_CONTENT = "siteContent";
42
	private static final String CATALOG_DB = "Catalog";
31
    private static final String CATALOG_DB = "Catalog";
43
	private static final String MASTER_DATA = "MasterData";
32
    private static final String MASTER_DATA = "MasterData";
44
	private static final String FOFO_DB = "Fofo";
33
    private static final String FOFO_DB = "Fofo";
45
	private static final String FOFO_BRANDS = "brands";
34
    private static final String FOFO_BRANDS = "brands";
46
	private static final String PROFITMANDI_BANNERS = "banners";
35
    private static final String PROFITMANDI_BANNERS = "banners";
47
	private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
36
    private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
48
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
37
    private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
49
	private static final String NOTIFICATION_CAMPAIGNS = "notificationcampaigns";
38
    private static final String NOTIFICATION_CAMPAIGNS = "notificationcampaigns";
50
	private static final String USER_DB = "User";
39
    private static final String USER_DB = "User";
51
	private static final int MONGO_PORT = 27017;
40
    private static final int MONGO_PORT = 27017;
52
 
41
 
53
	private static final Gson gson = new Gson();
42
    private static final Gson gson = new Gson();
54
	private static final List<String> BLOCKED_BRANDS = Arrays.asList();
43
 
55
	public static final Map<Integer, List<String>> PARTNER_BLoCKED_BRANDS = new HashMap<>();
44
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
56
	public static final Map<Integer, List<String>> PARTNER_ALLOWED_BRANDS = new HashMap<>();
45
    @Autowired
57
 
46
    private RetailerBlockBrandsRepository retailerBlockBrandsRepository;
58
	static {
47
 
59
		PARTNER_BLoCKED_BRANDS.put(175135597, Arrays.asList("Vivo"));
48
    private MongoClient mongoClient;
60
		PARTNER_BLoCKED_BRANDS.put(175135183, Arrays.asList("Vivo"));
49
    private MongoClient contentMongoClient;
61
		PARTNER_BLoCKED_BRANDS.put(175137445, Arrays.asList("Vivo"));
50
 
62
		PARTNER_BLoCKED_BRANDS.put(175137557, Arrays.asList("Vivo"));
51
    public Mongo(String mongoHost, String contentMongoHost) {
63
		PARTNER_BLoCKED_BRANDS.put(175138281, Arrays.asList("Vivo"));
52
        try {
64
		PARTNER_BLoCKED_BRANDS.put(175138441, Arrays.asList("Vivo"));
53
            LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
65
		PARTNER_BLoCKED_BRANDS.put(175138447, Arrays.asList("Vivo"));
54
            mongoClient = new MongoClient(mongoHost, MONGO_PORT);
66
		PARTNER_BLoCKED_BRANDS.put(175138570, Arrays.asList("Vivo"));
55
            contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
67
		// TITU
56
        } catch (Exception e) {
68
		PARTNER_BLoCKED_BRANDS.put(175138610, Arrays.asList("Vivo"));
57
            e.printStackTrace();
69
		// HRPHK204 = MOBILE ZONE
58
        }
70
		// PARTNER_BLoCKED_BRANDS.put(175138917, Arrays.asList("Vivo"));
59
    }
71
		// HRKK205, SATIJA
60
 
72
		PARTNER_BLoCKED_BRANDS.put(175138918, Arrays.asList("Vivo"));
61
    public ContentPojo getEntityById(long id) throws Exception {
73
		PARTNER_BLoCKED_BRANDS.put(175138611, Arrays.asList("Vivo"));
62
        DB db = contentMongoClient.getDB(CONTENT);
74
		PARTNER_BLoCKED_BRANDS.put(175138639, Arrays.asList("Vivo"));
63
        DBCollection collection = db.getCollection(SITE_CONTENT);
75
		PARTNER_BLoCKED_BRANDS.put(175138716, Arrays.asList("Vivo"));
64
        BasicDBObject obj = new BasicDBObject();
76
		// Laxmi Communication - Shahbad
65
        obj.append("_id", id);
77
		// PARTNER_BLoCKED_BRANDS.put(175138911, Arrays.asList("Vivo"));
66
        DBObject result = collection.findOne(obj);
78
		// Chaudhary Electronics
67
        if (result == null) {
79
		// PARTNER_BLoCKED_BRANDS.put(175138906, Arrays.asList("Vivo"));
68
            throw new Exception();
80
		// DY Venturs
69
        }
81
		// PARTNER_BLoCKED_BRANDS.put(175138874, Arrays.asList("Vivo"));
70
        //LOGGER.info(result.toMap());
82
		// Pardeep Telecom
71
        ContentPojo cp = gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
83
		// PARTNER_BLoCKED_BRANDS.put(175138843, Arrays.asList("Vivo"));
72
        if (cp.getDefaultImageUrl() != null) {
84
		// PARTNER_BLoCKED_BRANDS.put(175138733,Arrays.asList("Vivo"));
73
            cp.setDefaultImageUrl(cp.getDefaultImageUrl().replaceAll("saholic", "smartdukaan"));
85
		// Venus Agencies
74
        }
86
		// PARTNER_BLoCKED_BRANDS.put(175138801, Arrays.asList("Vivo"));
75
 
87
		// PARTNER_BLoCKED_BRANDS.put(175138717, Arrays.asList("Vivo"));
76
        //LOGGER.info("cp" + cp);
88
		PARTNER_BLoCKED_BRANDS.put(175138803, Arrays.asList("Vivo"));
77
 
89
		// PARTNER_BLoCKED_BRANDS.put(175138821, Arrays.asList("Vivo"));
78
        return cp;
90
		// PARTNER_BLoCKED_BRANDS.put(175138820,Arrays.asList("Vivo"));
79
    }
91
		PARTNER_BLoCKED_BRANDS.put(175138718, Arrays.asList("Vivo"));
80
 
92
		// Gaurav Electronics
81
    public List<DBObject> getMongoBrands(int fofoId, String email, int categoryId) {
93
		PARTNER_BLoCKED_BRANDS.put(175138637, Arrays.asList("Vivo"));
82
        List<DBObject> brandsDisplay = this.getBrandsToDisplay(categoryId);
94
		// PARTNER_BLoCKED_BRANDS.put(175138814,Arrays.asList("Vivo"));
83
        if (fofoId == Utils.SYSTEM_PARTNER_ID) {
95
		// PARTNER_BLoCKED_BRANDS.put(175138833, Arrays.asList("Vivo"));
84
            return brandsDisplay;
96
		// Durga Enterprises
85
        }
97
		// PARTNER_BLoCKED_BRANDS.put(175138832, Arrays.asList("Vivo"));
86
        List<String> blockedBrands = retailerBlockBrandsRepository.selectAllByRetailer(fofoId).stream()
98
		PARTNER_ALLOWED_BRANDS.put(172157716, Arrays.asList("Realme"));
87
                .map(x -> x.getBlockBrands()).collect(Collectors.toList());
99
		// DY Ventures
88
        brandsDisplay = brandsDisplay.stream().filter(x -> !blockedBrands.contains(x.get("name")))
100
		PARTNER_ALLOWED_BRANDS.put(175138874, Arrays.asList("Realme"));
89
                .collect(Collectors.toList());
101
		PARTNER_ALLOWED_BRANDS.put(175135227, Arrays.asList("Realme"));
90
 
102
		PARTNER_ALLOWED_BRANDS.put(175135707, Arrays.asList("Realme"));
91
        //LOGGER.info("brandsDisplay" + brandsDisplay);
103
		PARTNER_ALLOWED_BRANDS.put(175135923, Arrays.asList("Realme"));
92
        return brandsDisplay;
104
		PARTNER_ALLOWED_BRANDS.put(175138372, Arrays.asList("Realme"));
93
    }
105
		PARTNER_ALLOWED_BRANDS.put(175138520, Arrays.asList("Realme"));
94
 
106
		PARTNER_ALLOWED_BRANDS.put(175138522, Arrays.asList("Realme"));
95
    public ContentPojo getEntityByName(String name) throws Exception {
107
		PARTNER_ALLOWED_BRANDS.put(175138541, Arrays.asList("Realme"));
96
        //LOGGER.info("Name --- {}", name);
108
		PARTNER_ALLOWED_BRANDS.put(175138674, Arrays.asList("Realme"));
97
        DB db = contentMongoClient.getDB(CONTENT);
109
		PARTNER_ALLOWED_BRANDS.put(175138716, Arrays.asList("Realme"));
98
        DBCollection collection = db.getCollection(SITE_CONTENT);
110
		PARTNER_ALLOWED_BRANDS.put(175138801, Arrays.asList("Realme"));
99
        BasicDBObject obj = new BasicDBObject();
111
		PARTNER_ALLOWED_BRANDS.put(175138812, Arrays.asList("Realme"));
100
        obj.append("title", name);
112
		PARTNER_ALLOWED_BRANDS.put(175138813, Arrays.asList("Realme"));
101
        DBObject result = collection.findOne();
113
		PARTNER_ALLOWED_BRANDS.put(175138814, Arrays.asList("Realme"));
102
        if (result == null) {
114
		PARTNER_ALLOWED_BRANDS.put(175138816, Arrays.asList("Realme"));
103
            throw new Exception();
115
		PARTNER_ALLOWED_BRANDS.put(175138820, Arrays.asList("Realme"));
104
        }
116
		PARTNER_ALLOWED_BRANDS.put(175138836, Arrays.asList("Realme"));
105
        return gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
117
		PARTNER_ALLOWED_BRANDS.put(175138838, Arrays.asList("Realme"));
106
    }
118
		PARTNER_ALLOWED_BRANDS.put(175138840, Arrays.asList("Realme"));
107
 
119
		PARTNER_ALLOWED_BRANDS.put(175138842, Arrays.asList("Realme"));
108
    public void persistEntity(ContentPojo contentPojo) {
120
		PARTNER_ALLOWED_BRANDS.put(175138843, Arrays.asList("Realme"));
109
        DB db = contentMongoClient.getDB(CONTENT);
121
		PARTNER_ALLOWED_BRANDS.put(175138847, Arrays.asList("Realme"));
110
        DBCollection collection = db.getCollection(SITE_CONTENT);
122
		PARTNER_ALLOWED_BRANDS.put(175138858, Arrays.asList("Realme"));
111
        insertOrUpdateById(collection, contentPojo.getId(), contentPojo);
123
		PARTNER_ALLOWED_BRANDS.put(175138859, Arrays.asList("Realme"));
112
 
124
		PARTNER_ALLOWED_BRANDS.put(175138860, Arrays.asList("Realme"));
113
    }
125
		PARTNER_ALLOWED_BRANDS.put(175138862, Arrays.asList("Realme"));
114
 
126
		PARTNER_ALLOWED_BRANDS.put(175138863, Arrays.asList("Realme"));
115
    private static <T> void insertOrUpdateById(DBCollection collection, long id, T obj) {
127
		PARTNER_ALLOWED_BRANDS.put(175138865, Arrays.asList("Realme"));
116
        DBObject dbo = BasicDBObject.parse(gson.toJson(obj));
128
		PARTNER_ALLOWED_BRANDS.put(175138868, Arrays.asList("Realme"));
117
        dbo.put("_id", id);
129
		PARTNER_ALLOWED_BRANDS.put(175138829, Arrays.asList("Realme"));
118
        collection.update(new BasicDBObject("_id", id), dbo, true, false);
130
		PARTNER_ALLOWED_BRANDS.put(175138864, Arrays.asList("Realme"));
119
    }
131
		PARTNER_ALLOWED_BRANDS.put(175138878, Arrays.asList("Realme"));
120
 
132
		PARTNER_ALLOWED_BRANDS.put(175138876, Arrays.asList("Realme"));
121
    public JSONObject getItemsByBundleId(long bundleId) throws Exception {
133
		PARTNER_ALLOWED_BRANDS.put(175138883, Arrays.asList("Realme"));
122
        DB db = mongoClient.getDB(CATALOG_DB);
134
		PARTNER_ALLOWED_BRANDS.put(175138862, Arrays.asList("Realme"));
123
        DBCollection collection = db.getCollection(MASTER_DATA);
135
		PARTNER_ALLOWED_BRANDS.put(175138847, Arrays.asList("Realme"));
124
        BasicDBObject obj = new BasicDBObject();
136
		PARTNER_ALLOWED_BRANDS.put(175138870, Arrays.asList("Realme"));
125
        BasicDBObject in_query = new BasicDBObject();
137
		PARTNER_ALLOWED_BRANDS.put(175138873, Arrays.asList("Realme"));
126
        obj.append("skuBundleId", bundleId);
138
		PARTNER_ALLOWED_BRANDS.put(175138879, Arrays.asList("Realme"));
127
        in_query.append("$in", new int[]{1, 2, 3, 4, 5, 6, 7});
139
		PARTNER_ALLOWED_BRANDS.put(175138884, Arrays.asList("Realme"));
128
        obj.append("source_id", in_query);
140
		PARTNER_ALLOWED_BRANDS.put(175138872, Arrays.asList("Realme"));
129
        DBObject result = collection.findOne(obj);
141
		PARTNER_ALLOWED_BRANDS.put(175138866, Arrays.asList("Realme"));
130
        if (result == null) {
142
 
131
            throw new Exception();
143
	}
132
        }
144
	@Autowired
133
        return new JSONObject(JSON.serialize(result));
145
	private RetailerBlockBrandsRepository retailerBlockBrandsRepository;
134
    }
146
 
135
 
147
	private MongoClient mongoClient;
136
    public JSONObject getItemByID(long id) throws Exception {
148
	private MongoClient contentMongoClient;
137
        DB db = mongoClient.getDB(CATALOG_DB);
149
 
138
        DBCollection collection = db.getCollection(MASTER_DATA);
150
	public Mongo(String mongoHost, String contentMongoHost) {
139
        BasicDBObject obj = new BasicDBObject();
151
		try {
140
        obj.append("_id", id);
152
			LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
141
        DBObject result = collection.findOne(obj);
153
			mongoClient = new MongoClient(mongoHost, MONGO_PORT);
142
        if (result == null) {
154
			contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
143
            throw new Exception();
155
		} catch (Exception e) {
144
        }
156
			e.printStackTrace();
145
        return new JSONObject(JSON.serialize(result));
157
		}
146
    }
158
	}
147
 
159
 
148
    public void persistFofoRegInfo(FofoForm ff) {
160
	public ContentPojo getEntityById(long id) throws Exception {
149
        DB db = mongoClient.getDB(FOFO_DB);
161
		DB db = contentMongoClient.getDB(CONTENT);
150
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
162
		DBCollection collection = db.getCollection(SITE_CONTENT);
151
        if (ff.get_id() == 0) {
163
		BasicDBObject obj = new BasicDBObject();
152
            BasicDBObject orderBy = new BasicDBObject();
164
		obj.append("_id", id);
153
            orderBy.put("_id", -1);
165
		DBObject result = collection.findOne(obj);
154
            DBCursor cursor = collection.find().sort(orderBy).limit(1);
166
		if (result == null) {
155
            long id = 1l;
167
			throw new Exception();
156
            while (cursor.hasNext()) {
168
		}
157
                FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
169
		//LOGGER.info(result.toMap());
158
                id = existingFofo.get_id() + 1;
170
		ContentPojo cp = gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
159
            }
171
		if (cp.getDefaultImageUrl() != null) {
160
            ff.set_id(id);
172
			cp.setDefaultImageUrl(cp.getDefaultImageUrl().replaceAll("saholic", "smartdukaan"));
161
        }
173
		}
162
        DBObject dbObject = (DBObject) JSON.parse(gson.toJson(ff));
174
 
163
        collection.save(dbObject);
175
		//LOGGER.info("cp" + cp);
164
    }
176
 
165
 
177
		return cp;
166
    public List<FofoForm> getFofoForms(int offset, int limit) {
178
	}
167
        List<FofoForm> ffList = new ArrayList<FofoForm>();
179
 
168
        DB db = mongoClient.getDB(FOFO_DB);
180
	public List<DBObject> getMongoBrands(int fofoId, String email, int categoryId) {
169
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
181
		List<DBObject> brandsDisplay = this.getBrandsToDisplay(categoryId);
170
        BasicDBObject orderBy = new BasicDBObject();
182
		if (fofoId == Utils.SYSTEM_PARTNER_ID) {
171
        orderBy.put("_id", -1);
183
			return brandsDisplay;
172
        DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
184
		}
173
        while (dbc.hasNext()) {
185
		List<String> blockedBrands = retailerBlockBrandsRepository.selectAllByRetailer(fofoId).stream()
174
            ffList.add(convertJSONToPojo(dbc.next().toString()));
186
				.map(x -> x.getBlockBrands()).collect(Collectors.toList());
175
        }
187
		//LOGGER.info("blockedBrands" + blockedBrands);
176
        return ffList;
188
		brandsDisplay = brandsDisplay.stream().filter(x -> !blockedBrands.contains(x.get("name")))
177
    }
189
				.collect(Collectors.toList());
178
 
190
 
179
    public String getFofoFormJsonStringByFofoId(int fofoId) {
191
		//LOGGER.info("brandsDisplay" + brandsDisplay);
180
        DB db = mongoClient.getDB(FOFO_DB);
192
		return brandsDisplay;
181
        BasicDBObject filter = new BasicDBObject();
193
	}
182
        filter.append("_id", fofoId);
194
 
183
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
195
	public ContentPojo getEntityByName(String name) throws Exception {
184
        DBObject fofoDbOject = collection.findOne(filter);
196
		//LOGGER.info("Name --- {}", name);
185
        if (fofoDbOject != null) {
197
		DB db = contentMongoClient.getDB(CONTENT);
186
            return fofoDbOject.toString();
198
		DBCollection collection = db.getCollection(SITE_CONTENT);
187
        } else {
199
		BasicDBObject obj = new BasicDBObject();
188
            return null;
200
		obj.append("title", name);
189
        }
201
		DBObject result = collection.findOne();
190
    }
202
		if (result == null) {
191
 
203
			throw new Exception();
192
    public String getFofoFormJsonStringByEmail(String email) {
204
		}
193
        DB db = mongoClient.getDB(FOFO_DB);
205
		return gson.fromJson(new BasicDBObject(result.toMap()).toJson(), ContentPojo.class);
194
        BasicDBObject filter = new BasicDBObject();
206
	}
195
        filter.append("registeredEmail1", email);
207
 
196
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
208
	public void persistEntity(ContentPojo contentPojo) {
197
        DBObject fofoDbOject = collection.findOne(filter);
209
		DB db = contentMongoClient.getDB(CONTENT);
198
        if (fofoDbOject != null) {
210
		DBCollection collection = db.getCollection(SITE_CONTENT);
199
            return fofoDbOject.toString();
211
		insertOrUpdateById(collection, contentPojo.getId(), contentPojo);
200
        } else {
212
 
201
            return null;
213
	}
202
        }
214
 
203
    }
215
	private static <T> void insertOrUpdateById(DBCollection collection, long id, T obj) {
204
 
216
		DBObject dbo = BasicDBObject.parse(gson.toJson(obj));
205
    public String getFofoFormsJsonString() {
217
		dbo.put("_id", id);
206
        DB db = mongoClient.getDB(FOFO_DB);
218
		collection.update(new BasicDBObject("_id", id), dbo, true, false);
207
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
219
	}
208
        DBCursor cursor = collection.find();
220
 
209
        StringBuilder fofoFormsJsonString = new StringBuilder();
221
	public JSONObject getItemsByBundleId(long bundleId) throws Exception {
210
        fofoFormsJsonString.append("[");
222
		DB db = mongoClient.getDB(CATALOG_DB);
211
        while (cursor.hasNext()) {
223
		DBCollection collection = db.getCollection(MASTER_DATA);
212
            fofoFormsJsonString.append(cursor.next().toString());
224
		BasicDBObject obj = new BasicDBObject();
213
            if (cursor.hasNext()) {
225
		BasicDBObject in_query = new BasicDBObject();
214
                fofoFormsJsonString.append(",");
226
		obj.append("skuBundleId", bundleId);
215
            }
227
		in_query.append("$in", new int[] { 1, 2, 3, 4, 5, 6, 7 });
216
        }
228
		obj.append("source_id", in_query);
217
        fofoFormsJsonString.append("]");
229
		DBObject result = collection.findOne(obj);
218
        return fofoFormsJsonString.toString();
230
		if (result == null) {
219
    }
231
			throw new Exception();
220
 
232
		}
221
    public FofoForm getFofoForm(int fofoId) {
233
		return new JSONObject(JSON.serialize(result));
222
        String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
234
	}
223
        System.out.println(fofoFormJsonString);
235
 
224
        return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
236
	public JSONObject getItemByID(long id) throws Exception {
225
        // return convertJSONToPojo(fofoDbOject.toString());
237
		DB db = mongoClient.getDB(CATALOG_DB);
226
    }
238
		DBCollection collection = db.getCollection(MASTER_DATA);
227
 
239
		BasicDBObject obj = new BasicDBObject();
228
    public FofoForm getFofoForm(String email) {
240
		obj.append("_id", id);
229
        String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
241
		DBObject result = collection.findOne(obj);
230
        System.out.println(fofoFormJsonString);
242
		if (result == null) {
231
        return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
243
			throw new Exception();
232
        // return convertJSONToPojo(fofoDbOject.toString());
244
		}
233
    }
245
		return new JSONObject(JSON.serialize(result));
234
 
246
	}
235
    private static FofoForm convertJSONToPojo(String json) {
247
 
236
 
248
	public void persistFofoRegInfo(FofoForm ff) {
237
        Type type = new TypeToken<FofoForm>() {
249
		DB db = mongoClient.getDB(FOFO_DB);
238
        }.getType();
250
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
239
 
251
		if (ff.get_id() == 0) {
240
        return new Gson().fromJson(json, type);
252
			BasicDBObject orderBy = new BasicDBObject();
241
 
253
			orderBy.put("_id", -1);
242
    }
254
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
243
 
255
			long id = 1l;
244
    public void updateColumnsById(Map<String, Integer> map, int fofoId) {
256
			while (cursor.hasNext()) {
245
        DB db = mongoClient.getDB(FOFO_DB);
257
				FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
246
        BasicDBObject filter = new BasicDBObject();
258
				id = existingFofo.get_id() + 1;
247
        filter.append("_id", fofoId);
259
			}
248
        DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
260
			ff.set_id(id);
249
        BasicDBObject updateFields = new BasicDBObject();
261
		}
250
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
262
		DBObject dbObject = (DBObject) JSON.parse(gson.toJson(ff));
251
            updateFields.append(entry.getKey(), entry.getValue());
263
		collection.save(dbObject);
252
        }
264
	}
253
        BasicDBObject newDocument = new BasicDBObject();
265
 
254
        newDocument.append("$set", updateFields);
266
	public List<FofoForm> getFofoForms(int offset, int limit) {
255
        collection.update(filter, newDocument);
267
		List<FofoForm> ffList = new ArrayList<FofoForm>();
256
    }
268
		DB db = mongoClient.getDB(FOFO_DB);
257
 
269
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
258
    public List<DBObject> getBrandsToDisplay(int categoryId) {
270
		BasicDBObject orderBy = new BasicDBObject();
259
        DB db = mongoClient.getDB(FOFO_DB);
271
		orderBy.put("_id", -1);
260
        BasicDBObject filter = new BasicDBObject();
272
		DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
261
        filter.append("active", true);
273
		while (dbc.hasNext()) {
262
        if (categoryId != 0) {
274
			ffList.add(convertJSONToPojo(dbc.next().toString()));
263
            filter.append("categoryId", categoryId);
275
		}
264
        }
276
		return ffList;
265
        return db.getCollection(FOFO_BRANDS).find(filter).toArray();
277
	}
266
    }
278
 
267
 
279
	public String getFofoFormJsonStringByFofoId(int fofoId) {
268
    public List<DBObject> getAllBrandsToDisplay(int categoryId) {
280
		DB db = mongoClient.getDB(FOFO_DB);
269
        DB db = mongoClient.getDB(FOFO_DB);
281
		BasicDBObject filter = new BasicDBObject();
270
        BasicDBObject filter = new BasicDBObject();
282
		filter.append("_id", fofoId);
271
        if (categoryId != 0) {
283
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
272
            filter.append("categoryId", categoryId);
284
		DBObject fofoDbOject = collection.findOne(filter);
273
        }
285
		if (fofoDbOject != null) {
274
        return db.getCollection(FOFO_BRANDS).find(filter).toArray();
286
			return fofoDbOject.toString();
275
    }
287
		} else {
276
 
288
			return null;
277
    public List<DBObject> getBannersByType(String bannerType) {
289
		}
278
        DB db = mongoClient.getDB(FOFO_DB);
290
	}
279
        BasicDBObject filter = new BasicDBObject();
291
 
280
        filter.append("type", bannerType);
292
	public String getFofoFormJsonStringByEmail(String email) {
281
        BasicDBObject orderBy = new BasicDBObject();
293
		DB db = mongoClient.getDB(FOFO_DB);
282
        orderBy.put("rank", 1);
294
		BasicDBObject filter = new BasicDBObject();
283
        return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
295
		filter.append("registeredEmail1", email);
284
    }
296
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
285
 
297
		DBObject fofoDbOject = collection.findOne(filter);
286
    @SuppressWarnings("unchecked")
298
		if (fofoDbOject != null) {
287
    public List<Document> getSubcategoriesToDisplay() {
299
			return fofoDbOject.toString();
288
        MongoDatabase db = mongoClient.getDatabase("Catalog");
300
		} else {
289
        System.out.println("Connection to MongoDB database successfully");
301
			return null;
290
        MongoCollection<Document> collection = db.getCollection("Deals");
302
		}
291
 
303
	}
292
        Document object = new Document().append("$match", new Document().append("category_id", 6).append("showDeal", 1)
304
 
293
                .append("dealRankPoints", new Document("$gt", 0)));
305
	public String getFofoFormsJsonString() {
294
        Document ob = new Document("$group",
306
		DB db = mongoClient.getDB(FOFO_DB);
295
                new Document().append("_id",
307
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
296
                                new Document().append("subCategoryId", "$subCategoryId").append("subCategory", "$subCategory"))
308
		DBCursor cursor = collection.find();
297
                        .append("count", new Document("$sum", 1)));
309
		StringBuilder fofoFormsJsonString = new StringBuilder();
298
        List<Document> pipeline = Arrays.asList(object, ob);
310
		fofoFormsJsonString.append("[");
299
        AggregateIterable<Document> cursor = collection.aggregate(pipeline);
311
		while (cursor.hasNext()) {
300
 
312
			fofoFormsJsonString.append(cursor.next().toString());
301
        List<Document> resultDocuments = new ArrayList<>();
313
			if (cursor.hasNext()) {
302
        for (Document dbo : cursor) {
314
				fofoFormsJsonString.append(",");
303
            System.out.println(dbo.toString());
315
			}
304
            LOGGER.info("categories" + dbo.toString());
316
		}
305
            resultDocuments.add(dbo);
317
		fofoFormsJsonString.append("]");
306
        }
318
		return fofoFormsJsonString.toString();
307
        return resultDocuments;
319
	}
308
    }
320
 
309
 
321
	public FofoForm getFofoForm(int fofoId) {
310
    public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
322
		String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
311
        DB db = mongoClient.getDB(FOFO_DB);
323
		System.out.println(fofoFormJsonString);
312
        Gson gs = new Gson();
324
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
313
        DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
325
		// return convertJSONToPojo(fofoDbOject.toString());
314
        DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
326
	}
315
        fofoInterestCollection.save(dbObject);
327
 
316
        return true;
328
	public FofoForm getFofoForm(String email) {
317
    }
329
		String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
318
 
330
		System.out.println(fofoFormJsonString);
319
    public boolean hasRetailerShownInterest(int userId) {
331
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
320
        DB db = mongoClient.getDB(FOFO_DB);
332
		// return convertJSONToPojo(fofoDbOject.toString());
321
        BasicDBObject filter = new BasicDBObject();
333
	}
322
        filter.append("userId", userId);
334
 
323
        DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
335
	private static FofoForm convertJSONToPojo(String json) {
324
        return fofoInterestCollection.findOne(filter) != null;
336
 
325
    }
337
		Type type = new TypeToken<FofoForm>() {
326
 
338
		}.getType();
327
    public void persistNotificationCmpInfo(NotificationCampaigns ff) {
339
 
328
        DB db = mongoClient.getDB(USER_DB);
340
		return new Gson().fromJson(json, type);
329
        DBCollection collection = db.getCollection(NOTIFICATION_CAMPAIGNS);
341
 
330
        if (ff.get_id() == 0) {
342
	}
331
            BasicDBObject orderBy = new BasicDBObject();
343
 
332
            orderBy.put("_id", -1);
344
	public void updateColumnsById(Map<String, Integer> map, int fofoId) {
333
            DBCursor cursor = collection.find().sort(orderBy).limit(1);
345
		DB db = mongoClient.getDB(FOFO_DB);
334
            long id = 1l;
346
		BasicDBObject filter = new BasicDBObject();
335
            while (cursor.hasNext()) {
347
		filter.append("_id", fofoId);
336
                Gson gson = new Gson();
348
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
337
                NotificationCampaigns existingFofo = gson.fromJson(cursor.next().toString(),
349
		BasicDBObject updateFields = new BasicDBObject();
338
                        NotificationCampaigns.class);
350
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
339
                id = existingFofo.get_id() + 1;
351
			updateFields.append(entry.getKey(), entry.getValue());
340
            }
352
		}
341
            ff.set_id(id);
353
		BasicDBObject newDocument = new BasicDBObject();
342
        }
354
		newDocument.append("$set", updateFields);
343
        Gson gs = new Gson();
355
		collection.update(filter, newDocument);
344
        DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
356
	}
345
        collection.save(dbObject);
357
 
346
    }
358
	public List<DBObject> getBrandsToDisplay(int categoryId) {
-
 
359
		DB db = mongoClient.getDB(FOFO_DB);
-
 
360
		BasicDBObject filter = new BasicDBObject();
-
 
361
		filter.append("active", true);
-
 
362
		if (categoryId != 0) {
-
 
363
			filter.append("categoryId", categoryId);
-
 
364
		}
-
 
365
		return db.getCollection(FOFO_BRANDS).find(filter).toArray();
-
 
366
	}
-
 
367
 
-
 
368
	public List<DBObject> getAllBrandsToDisplay(int categoryId) {
-
 
369
		DB db = mongoClient.getDB(FOFO_DB);
-
 
370
		BasicDBObject filter = new BasicDBObject();
-
 
371
		if (categoryId != 0) {
-
 
372
			filter.append("categoryId", categoryId);
-
 
373
		}
-
 
374
		return db.getCollection(FOFO_BRANDS).find(filter).toArray();
-
 
375
	}
-
 
376
 
-
 
377
	public List<DBObject> getBannersByType(String bannerType) {
-
 
378
		DB db = mongoClient.getDB(FOFO_DB);
-
 
379
		BasicDBObject filter = new BasicDBObject();
-
 
380
		filter.append("type", bannerType);
-
 
381
		BasicDBObject orderBy = new BasicDBObject();
-
 
382
		orderBy.put("rank", 1);
-
 
383
		return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
-
 
384
	}
-
 
385
 
-
 
386
	@SuppressWarnings("unchecked")
-
 
387
	public List<Document> getSubcategoriesToDisplay() {
-
 
388
		MongoDatabase db = mongoClient.getDatabase("Catalog");
-
 
389
		System.out.println("Connection to MongoDB database successfully");
-
 
390
		MongoCollection<Document> collection = db.getCollection("Deals");
-
 
391
 
-
 
392
		Document object = new Document().append("$match", new Document().append("category_id", 6).append("showDeal", 1)
-
 
393
				.append("dealRankPoints", new Document("$gt", 0)));
-
 
394
		Document ob = new Document("$group",
-
 
395
				new Document().append("_id",
-
 
396
						new Document().append("subCategoryId", "$subCategoryId").append("subCategory", "$subCategory"))
-
 
397
						.append("count", new Document("$sum", 1)));
-
 
398
		List<Document> pipeline = Arrays.asList(object, ob);
-
 
399
		AggregateIterable<Document> cursor = collection.aggregate(pipeline);
-
 
400
 
-
 
401
		List<Document> resultDocuments = new ArrayList<>();
-
 
402
		for (Document dbo : cursor) {
-
 
403
			System.out.println(dbo.toString());
-
 
404
			LOGGER.info("categories" + dbo.toString());
-
 
405
			resultDocuments.add(dbo);
-
 
406
		}
-
 
407
		return resultDocuments;
-
 
408
	}
-
 
409
 
-
 
410
	public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
-
 
411
		DB db = mongoClient.getDB(FOFO_DB);
-
 
412
		Gson gs = new Gson();
-
 
413
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
-
 
414
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
-
 
415
		fofoInterestCollection.save(dbObject);
-
 
416
		return true;
-
 
417
	}
-
 
418
 
-
 
419
	public boolean hasRetailerShownInterest(int userId) {
-
 
420
		DB db = mongoClient.getDB(FOFO_DB);
-
 
421
		BasicDBObject filter = new BasicDBObject();
-
 
422
		filter.append("userId", userId);
-
 
423
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
-
 
424
		return fofoInterestCollection.findOne(filter) != null;
-
 
425
	}
-
 
426
 
-
 
427
	public void persistNotificationCmpInfo(NotificationCampaigns ff) {
-
 
428
		DB db = mongoClient.getDB(USER_DB);
-
 
429
		DBCollection collection = db.getCollection(NOTIFICATION_CAMPAIGNS);
-
 
430
		if (ff.get_id() == 0) {
-
 
431
			BasicDBObject orderBy = new BasicDBObject();
-
 
432
			orderBy.put("_id", -1);
-
 
433
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
-
 
434
			long id = 1l;
-
 
435
			while (cursor.hasNext()) {
-
 
436
				Gson gson = new Gson();
-
 
437
				NotificationCampaigns existingFofo = gson.fromJson(cursor.next().toString(),
-
 
438
						NotificationCampaigns.class);
-
 
439
				id = existingFofo.get_id() + 1;
-
 
440
			}
-
 
441
			ff.set_id(id);
-
 
442
		}
-
 
443
		Gson gs = new Gson();
-
 
444
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
-
 
445
		collection.save(dbObject);
-
 
446
	}
-
 
447
 
347
 
448
}
348
}