Subversion Repositories SmartDukaan

Rev

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