Subversion Repositories SmartDukaan

Rev

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