Subversion Repositories SmartDukaan

Rev

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