Subversion Repositories SmartDukaan

Rev

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