| 15272 |
kshitij.so |
1 |
package in.shop2020.dtrapi.Storage;
|
|
|
2 |
|
|
|
3 |
|
| 21920 |
rajender |
4 |
import java.lang.reflect.Type;
|
| 15272 |
kshitij.so |
5 |
import java.net.UnknownHostException;
|
| 21920 |
rajender |
6 |
import java.util.List;
|
| 22003 |
rajender |
7 |
|
|
|
8 |
import org.apache.log4j.Logger;
|
|
|
9 |
|
| 15381 |
kshitij.so |
10 |
import java.util.ArrayList;
|
| 15272 |
kshitij.so |
11 |
|
|
|
12 |
import com.google.gson.Gson;
|
| 21920 |
rajender |
13 |
import com.google.gson.reflect.TypeToken;
|
| 15272 |
kshitij.so |
14 |
import com.mongodb.BasicDBObject;
|
|
|
15 |
import com.mongodb.DB;
|
|
|
16 |
import com.mongodb.DBCollection;
|
| 15381 |
kshitij.so |
17 |
import com.mongodb.DBCursor;
|
| 15272 |
kshitij.so |
18 |
import com.mongodb.DBObject;
|
|
|
19 |
import com.mongodb.MongoClient;
|
| 21826 |
kshitij.so |
20 |
import com.mongodb.util.JSON;
|
| 15272 |
kshitij.so |
21 |
|
| 21920 |
rajender |
22 |
import in.shop2020.dtrapi.models.FofoForm;
|
|
|
23 |
import in.shop2020.dtrapi.models.MasterData;
|
| 15272 |
kshitij.so |
24 |
|
| 21920 |
rajender |
25 |
|
| 15272 |
kshitij.so |
26 |
public class Mongo {
|
|
|
27 |
|
|
|
28 |
private static MongoClient mongo;
|
|
|
29 |
private static final String CATALOG_DB = "Catalog";
|
|
|
30 |
private static final String MASTER_DATA = "MasterData";
|
| 21826 |
kshitij.so |
31 |
private static final String FOFO_DB = "Fofo";
|
|
|
32 |
private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
|
| 22003 |
rajender |
33 |
private static Logger log = Logger.getLogger(Mongo.class);
|
| 15272 |
kshitij.so |
34 |
|
|
|
35 |
static {
|
|
|
36 |
try {
|
|
|
37 |
mongo = new MongoClient( "localhost" , 27017 );
|
|
|
38 |
} catch (UnknownHostException e) {
|
|
|
39 |
e.printStackTrace();
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public static MasterData getItemByID(long id) throws Exception{
|
|
|
44 |
DB db = mongo.getDB(CATALOG_DB);
|
|
|
45 |
DBCollection collection = db.getCollection(MASTER_DATA);
|
|
|
46 |
BasicDBObject obj = new BasicDBObject();
|
|
|
47 |
obj.append("_id", id);
|
|
|
48 |
DBObject result = collection.findOne(obj);
|
|
|
49 |
if (result==null){
|
|
|
50 |
throw new Exception();
|
|
|
51 |
}
|
|
|
52 |
Gson gson = new Gson();
|
|
|
53 |
MasterData masterData = gson.fromJson(result.toString(), MasterData.class);
|
|
|
54 |
return masterData;
|
|
|
55 |
}
|
|
|
56 |
|
| 15381 |
kshitij.so |
57 |
public static ArrayList<MasterData> getItemsByBundleId(long bundleId){
|
|
|
58 |
DB db = mongo.getDB(CATALOG_DB);
|
|
|
59 |
DBCollection collection = db.getCollection(MASTER_DATA);
|
|
|
60 |
BasicDBObject obj = new BasicDBObject();
|
|
|
61 |
BasicDBObject in_query = new BasicDBObject();
|
|
|
62 |
obj.append("skuBundleId", bundleId);
|
|
|
63 |
obj.append("in_stock",1);
|
|
|
64 |
in_query.append("$in",new int[] {1,2,3,4});
|
|
|
65 |
obj.append("source_id", in_query);
|
|
|
66 |
ArrayList<MasterData> items = new ArrayList<MasterData>();
|
|
|
67 |
DBCursor result = collection.find(obj);
|
|
|
68 |
Gson gson = new Gson();
|
|
|
69 |
while (result.hasNext()) {
|
|
|
70 |
MasterData masterData = gson.fromJson(result.next().toString(), MasterData.class);
|
|
|
71 |
items.add(masterData);
|
|
|
72 |
}
|
|
|
73 |
return items;
|
|
|
74 |
}
|
| 21826 |
kshitij.so |
75 |
|
|
|
76 |
public static void persistFofoRegInfo(FofoForm ff){
|
| 22003 |
rajender |
77 |
DB db = mongo.getDB(FOFO_DB);
|
| 21826 |
kshitij.so |
78 |
DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
|
| 22003 |
rajender |
79 |
if(ff.get_id()==0) {
|
|
|
80 |
BasicDBObject orderBy = new BasicDBObject();
|
|
|
81 |
orderBy.put("_id", -1);
|
|
|
82 |
DBCursor cursor = collection.find().sort(orderBy).limit(1);
|
|
|
83 |
long id = 1l;
|
|
|
84 |
while (cursor.hasNext()) {
|
|
|
85 |
Gson gson = new Gson();
|
|
|
86 |
FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
|
|
|
87 |
id = existingFofo.get_id() + 1;
|
|
|
88 |
}
|
|
|
89 |
ff.set_id(id);
|
| 21826 |
kshitij.so |
90 |
}
|
|
|
91 |
Gson gs = new Gson();
|
|
|
92 |
DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
|
| 22003 |
rajender |
93 |
collection.save(dbObject);
|
| 21826 |
kshitij.so |
94 |
}
|
| 21920 |
rajender |
95 |
|
|
|
96 |
public static List<FofoForm> getFofoForms(int offset, int limit) {
|
|
|
97 |
List<FofoForm> ffList = new ArrayList<FofoForm>();
|
|
|
98 |
DB db = mongo.getDB(FOFO_DB);
|
|
|
99 |
DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
|
|
|
100 |
BasicDBObject orderBy = new BasicDBObject();
|
|
|
101 |
orderBy.put("_id", -1);
|
|
|
102 |
DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
|
|
|
103 |
while (dbc.hasNext()) {
|
|
|
104 |
ffList.add(convertJSONToPojo(dbc.next().toString()));
|
|
|
105 |
}
|
|
|
106 |
return ffList;
|
|
|
107 |
}
|
|
|
108 |
|
| 21974 |
ashik.ali |
109 |
public static String getFofoFormJsonStringByFofoId(int fofoId){
|
| 21920 |
rajender |
110 |
DB db = mongo.getDB(FOFO_DB);
|
|
|
111 |
BasicDBObject filter = new BasicDBObject();
|
|
|
112 |
filter.append("_id", fofoId);
|
|
|
113 |
DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
|
|
|
114 |
DBObject fofoDbOject = collection.find(filter).one();
|
| 21974 |
ashik.ali |
115 |
return fofoDbOject.toString();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
public static FofoForm getFofoForm(int fofoId) {
|
|
|
119 |
String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
|
|
|
120 |
System.out.println(fofoFormJsonString);
|
|
|
121 |
return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
|
| 21920 |
rajender |
122 |
//return convertJSONToPojo(fofoDbOject.toString());
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
private static FofoForm convertJSONToPojo(String json){
|
| 15381 |
kshitij.so |
126 |
|
| 21920 |
rajender |
127 |
Type type = new TypeToken< FofoForm >(){}.getType();
|
|
|
128 |
|
|
|
129 |
return new Gson().fromJson(json, type);
|
|
|
130 |
|
|
|
131 |
}
|
|
|
132 |
|
| 15272 |
kshitij.so |
133 |
public static void main(String[] args) throws Exception{
|
| 21920 |
rajender |
134 |
System.out.println(getFofoForms(0, 10));
|
|
|
135 |
|
| 15272 |
kshitij.so |
136 |
}
|
|
|
137 |
}
|