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