Subversion Repositories SmartDukaan

Rev

Rev 22496 | Rev 23008 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21723 ashik.ali 1
package com.spice.profitmandi.dao.repository.dtr;
21545 ashik.ali 2
 
22097 kshitij.so 3
import java.lang.reflect.Type;
4
import java.util.ArrayList;
5
import java.util.List;
22125 ashik.ali 6
import java.util.Map;
21545 ashik.ali 7
 
8
import org.json.JSONObject;
22170 amit.gupta 9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
21545 ashik.ali 11
 
22097 kshitij.so 12
import com.google.gson.Gson;
13
import com.google.gson.reflect.TypeToken;
21545 ashik.ali 14
import com.mongodb.BasicDBObject;
15
import com.mongodb.DB;
16
import com.mongodb.DBCollection;
22097 kshitij.so 17
import com.mongodb.DBCursor;
21545 ashik.ali 18
import com.mongodb.DBObject;
19
import com.mongodb.MongoClient;
20
import com.mongodb.util.JSON;
22097 kshitij.so 21
import com.spice.profitmandi.dao.model.FofoForm;
22496 amit.gupta 22
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
21545 ashik.ali 23
 
24
public class Mongo {
22384 amit.gupta 25
 
22170 amit.gupta 26
	private static final Logger LOGGER = LoggerFactory.getLogger(Mongo.class);
21545 ashik.ali 27
 
22165 amit.gupta 28
	private static final String CONTENT = "CONTENT";
21545 ashik.ali 29
	private static final String SITE_CONTENT = "siteContent";
30
	private static final String CATALOG_DB = "Catalog";
31
	private static final String MASTER_DATA = "MasterData";
22097 kshitij.so 32
	private static final String FOFO_DB = "Fofo";
22335 amit.gupta 33
	private static final String FOFO_BRANDS = "brands";
22446 amit.gupta 34
	private static final String PROFITMANDI_BANNERS = "banners";
22496 amit.gupta 35
	private static final String RETAILER_FOFO_INTEREST = "RetailerFofoInterest";
22097 kshitij.so 36
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
21545 ashik.ali 37
	private static final int MONGO_PORT = 27017;
22165 amit.gupta 38
 
22162 amit.gupta 39
	private MongoClient mongoClient;
40
	private MongoClient contentMongoClient;
41
 
22171 amit.gupta 42
	public Mongo(String mongoHost, String contentMongoHost) {
22165 amit.gupta 43
		try {
22170 amit.gupta 44
			LOGGER.info("mongoHost => {}, contentMongoHost {} ", mongoHost, contentMongoHost);
22165 amit.gupta 45
			mongoClient = new MongoClient(mongoHost, MONGO_PORT);
22169 amit.gupta 46
			contentMongoClient = new MongoClient(contentMongoHost, MONGO_PORT);
22165 amit.gupta 47
		} catch (Exception e) {
48
			e.printStackTrace();
49
		}
21545 ashik.ali 50
	}
51
 
22165 amit.gupta 52
	public JSONObject getEntityById(long id) throws Exception {
22162 amit.gupta 53
		DB db = contentMongoClient.getDB(CONTENT);
21545 ashik.ali 54
		DBCollection collection = db.getCollection(SITE_CONTENT);
55
		BasicDBObject obj = new BasicDBObject();
56
		obj.append("_id", id);
57
		DBObject result = collection.findOne(obj);
22165 amit.gupta 58
		if (result == null) {
21545 ashik.ali 59
			throw new Exception();
60
		}
61
		return new JSONObject(JSON.serialize(result));
62
	}
22165 amit.gupta 63
 
22384 amit.gupta 64
	public JSONObject getEntityByName(String name) throws Exception {
22385 amit.gupta 65
		LOGGER.info("Name --- {}", name);
22384 amit.gupta 66
		DB db = contentMongoClient.getDB(CONTENT);
67
		DBCollection collection = db.getCollection(SITE_CONTENT);
68
		BasicDBObject obj = new BasicDBObject();
69
		obj.append("title", name);
70
		DBObject result = collection.findOne(obj);
71
		if (result == null) {
72
			throw new Exception();
73
		}
74
		return new JSONObject(JSON.serialize(result));
75
	}
76
 
22165 amit.gupta 77
	public JSONObject getItemsByBundleId(long bundleId) throws Exception {
22162 amit.gupta 78
		DB db = mongoClient.getDB(CATALOG_DB);
21545 ashik.ali 79
		DBCollection collection = db.getCollection(MASTER_DATA);
80
		BasicDBObject obj = new BasicDBObject();
81
		BasicDBObject in_query = new BasicDBObject();
82
		obj.append("skuBundleId", bundleId);
22165 amit.gupta 83
		in_query.append("$in", new int[] { 1, 2, 3, 4, 5, 6, 7 });
21545 ashik.ali 84
		obj.append("source_id", in_query);
85
		DBObject result = collection.findOne(obj);
22165 amit.gupta 86
		if (result == null) {
21545 ashik.ali 87
			throw new Exception();
88
		}
89
		return new JSONObject(JSON.serialize(result));
90
	}
22165 amit.gupta 91
 
92
	public JSONObject getItemByID(long id) throws Exception {
22162 amit.gupta 93
		DB db = mongoClient.getDB(CATALOG_DB);
21545 ashik.ali 94
		DBCollection collection = db.getCollection(MASTER_DATA);
95
		BasicDBObject obj = new BasicDBObject();
96
		obj.append("_id", id);
97
		DBObject result = collection.findOne(obj);
22165 amit.gupta 98
		if (result == null) {
21545 ashik.ali 99
			throw new Exception();
100
		}
101
		return new JSONObject(JSON.serialize(result));
102
	}
22165 amit.gupta 103
 
104
	public void persistFofoRegInfo(FofoForm ff) {
105
		DB db = mongoClient.getDB(FOFO_DB);
22097 kshitij.so 106
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
22165 amit.gupta 107
		if (ff.get_id() == 0) {
22097 kshitij.so 108
			BasicDBObject orderBy = new BasicDBObject();
109
			orderBy.put("_id", -1);
110
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
111
			long id = 1l;
112
			while (cursor.hasNext()) {
113
				Gson gson = new Gson();
114
				FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
115
				id = existingFofo.get_id() + 1;
116
			}
117
			ff.set_id(id);
118
		}
119
		Gson gs = new Gson();
120
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
121
		collection.save(dbObject);
122
	}
22165 amit.gupta 123
 
22162 amit.gupta 124
	public List<FofoForm> getFofoForms(int offset, int limit) {
22165 amit.gupta 125
		List<FofoForm> ffList = new ArrayList<FofoForm>();
22162 amit.gupta 126
		DB db = mongoClient.getDB(FOFO_DB);
22097 kshitij.so 127
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
128
		BasicDBObject orderBy = new BasicDBObject();
129
		orderBy.put("_id", -1);
130
		DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
131
		while (dbc.hasNext()) {
132
			ffList.add(convertJSONToPojo(dbc.next().toString()));
133
		}
134
		return ffList;
135
	}
22165 amit.gupta 136
 
137
	public String getFofoFormJsonStringByFofoId(int fofoId) {
22162 amit.gupta 138
		DB db = mongoClient.getDB(FOFO_DB);
22097 kshitij.so 139
		BasicDBObject filter = new BasicDBObject();
140
		filter.append("_id", fofoId);
141
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
22105 ashik.ali 142
		DBObject fofoDbOject = collection.findOne(filter);
22165 amit.gupta 143
		if (fofoDbOject != null) {
22161 amit.gupta 144
			return fofoDbOject.toString();
145
		} else {
146
			return null;
147
		}
22097 kshitij.so 148
	}
22165 amit.gupta 149
 
150
	public String getFofoFormJsonStringByEmail(String email) {
22162 amit.gupta 151
		DB db = mongoClient.getDB(FOFO_DB);
22155 amit.gupta 152
		BasicDBObject filter = new BasicDBObject();
153
		filter.append("registeredEmail1", email);
154
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
155
		DBObject fofoDbOject = collection.findOne(filter);
22165 amit.gupta 156
		if (fofoDbOject != null) {
22161 amit.gupta 157
			return fofoDbOject.toString();
158
		} else {
159
			return null;
160
		}
22155 amit.gupta 161
	}
22165 amit.gupta 162
 
163
	public String getFofoFormsJsonString() {
22162 amit.gupta 164
		DB db = mongoClient.getDB(FOFO_DB);
22125 ashik.ali 165
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
166
		DBCursor cursor = collection.find();
167
		StringBuilder fofoFormsJsonString = new StringBuilder();
168
		fofoFormsJsonString.append("[");
22165 amit.gupta 169
		while (cursor.hasNext()) {
22125 ashik.ali 170
			fofoFormsJsonString.append(cursor.next().toString());
22165 amit.gupta 171
			if (cursor.hasNext()) {
22125 ashik.ali 172
				fofoFormsJsonString.append(",");
173
			}
174
		}
175
		fofoFormsJsonString.append("]");
176
		return fofoFormsJsonString.toString();
177
	}
22165 amit.gupta 178
 
22162 amit.gupta 179
	public FofoForm getFofoForm(int fofoId) {
22097 kshitij.so 180
		String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
181
		System.out.println(fofoFormJsonString);
182
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
22165 amit.gupta 183
		// return convertJSONToPojo(fofoDbOject.toString());
22097 kshitij.so 184
	}
22165 amit.gupta 185
 
22162 amit.gupta 186
	public FofoForm getFofoForm(String email) {
22155 amit.gupta 187
		String fofoFormJsonString = getFofoFormJsonStringByEmail(email);
188
		System.out.println(fofoFormJsonString);
189
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
22165 amit.gupta 190
		// return convertJSONToPojo(fofoDbOject.toString());
22155 amit.gupta 191
	}
21545 ashik.ali 192
 
22165 amit.gupta 193
	private static FofoForm convertJSONToPojo(String json) {
22097 kshitij.so 194
 
22165 amit.gupta 195
		Type type = new TypeToken<FofoForm>() {
196
		}.getType();
22097 kshitij.so 197
 
22165 amit.gupta 198
		return new Gson().fromJson(json, type);
199
 
22097 kshitij.so 200
	}
22165 amit.gupta 201
 
202
	public void updateColumnsById(Map<String, Integer> map, int fofoId) {
22162 amit.gupta 203
		DB db = mongoClient.getDB(FOFO_DB);
22125 ashik.ali 204
		BasicDBObject filter = new BasicDBObject();
205
		filter.append("_id", fofoId);
206
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
207
		BasicDBObject updateFields = new BasicDBObject();
22165 amit.gupta 208
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
22125 ashik.ali 209
			updateFields.append(entry.getKey(), entry.getValue());
210
		}
211
		BasicDBObject newDocument = new BasicDBObject();
212
		newDocument.append("$set", updateFields);
213
		collection.update(filter, newDocument);
214
	}
22335 amit.gupta 215
 
216
	public List<DBObject> getBrandsToDisplay() {
217
		DB db = mongoClient.getDB(FOFO_DB);
218
		BasicDBObject filter = new BasicDBObject();
219
		filter.append("active", true);
220
		return db.getCollection(FOFO_BRANDS).find(filter).toArray();
221
	}
22446 amit.gupta 222
 
22447 amit.gupta 223
	public List<DBObject> getBannersByType(String bannerType) {
22446 amit.gupta 224
		DB db = mongoClient.getDB(CATALOG_DB);
225
		BasicDBObject filter = new BasicDBObject();
22449 amit.gupta 226
		filter.append("type", bannerType);
22992 amit.gupta 227
		BasicDBObject orderBy = new BasicDBObject();
228
		orderBy.put("_id", -1);
229
		return db.getCollection(PROFITMANDI_BANNERS).find(filter).sort(orderBy).toArray();
22446 amit.gupta 230
	}
22496 amit.gupta 231
 
232
	public boolean saveRetailerInterestOnFofo(RetailerFofoInterest retailerInterest) {
233
		DB db = mongoClient.getDB(FOFO_DB);
234
		Gson gs = new Gson();
235
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
236
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(retailerInterest));
237
		fofoInterestCollection.save(dbObject);
238
		return true;
239
	}
240
 
241
	public boolean hasRetailerShownInterest(int userId) {
242
		DB db = mongoClient.getDB(FOFO_DB);
243
		BasicDBObject filter = new BasicDBObject();
244
		filter.append("userId", userId);		
245
		DBCollection fofoInterestCollection = db.getCollection(RETAILER_FOFO_INTEREST);
246
		return fofoInterestCollection.findOne(filter)!=null;
247
	}
22097 kshitij.so 248
 
21545 ashik.ali 249
}