Subversion Repositories SmartDukaan

Rev

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