| 21723 |
ashik.ali |
1 |
package com.spice.profitmandi.dao.repository.dtr;
|
| 21545 |
ashik.ali |
2 |
|
|
|
3 |
import java.net.UnknownHostException;
|
|
|
4 |
|
|
|
5 |
import org.json.JSONObject;
|
|
|
6 |
|
|
|
7 |
import com.mongodb.BasicDBObject;
|
|
|
8 |
import com.mongodb.DB;
|
|
|
9 |
import com.mongodb.DBCollection;
|
|
|
10 |
import com.mongodb.DBObject;
|
|
|
11 |
import com.mongodb.MongoClient;
|
|
|
12 |
import com.mongodb.util.JSON;
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
public class Mongo {
|
|
|
16 |
|
|
|
17 |
private static MongoClient mongo_local;
|
|
|
18 |
private static MongoClient mongo_dtr;
|
|
|
19 |
private static final String CONTENT ="CONTENT";
|
|
|
20 |
private static final String SITE_CONTENT = "siteContent";
|
|
|
21 |
private static final String CATALOG_DB = "Catalog";
|
|
|
22 |
private static final String MASTER_DATA = "MasterData";
|
|
|
23 |
private static final int MONGO_PORT = 27017;
|
|
|
24 |
|
|
|
25 |
static {
|
|
|
26 |
synchronized(Mongo.class){
|
|
|
27 |
try {
|
|
|
28 |
mongo_local = new MongoClient( "localhost" , MONGO_PORT );
|
|
|
29 |
mongo_dtr = new MongoClient("localhost", MONGO_PORT);
|
|
|
30 |
} catch (UnknownHostException e) {
|
|
|
31 |
e.printStackTrace();
|
|
|
32 |
}
|
|
|
33 |
}
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public static JSONObject getEntityById(long id) throws Exception{
|
|
|
37 |
DB db = mongo_local.getDB(CONTENT);
|
|
|
38 |
DBCollection collection = db.getCollection(SITE_CONTENT);
|
|
|
39 |
BasicDBObject obj = new BasicDBObject();
|
|
|
40 |
obj.append("_id", id);
|
|
|
41 |
DBObject result = collection.findOne(obj);
|
|
|
42 |
if (result==null){
|
|
|
43 |
throw new Exception();
|
|
|
44 |
}
|
|
|
45 |
return new JSONObject(JSON.serialize(result));
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public static JSONObject getItemsByBundleId(long bundleId) throws Exception{
|
|
|
49 |
DB db = mongo_dtr.getDB(CATALOG_DB);
|
|
|
50 |
DBCollection collection = db.getCollection(MASTER_DATA);
|
|
|
51 |
BasicDBObject obj = new BasicDBObject();
|
|
|
52 |
BasicDBObject in_query = new BasicDBObject();
|
|
|
53 |
obj.append("skuBundleId", bundleId);
|
|
|
54 |
in_query.append("$in",new int[] {1,2,3,4,5,6,7});
|
|
|
55 |
obj.append("source_id", in_query);
|
|
|
56 |
DBObject result = collection.findOne(obj);
|
|
|
57 |
if (result==null){
|
|
|
58 |
throw new Exception();
|
|
|
59 |
}
|
|
|
60 |
return new JSONObject(JSON.serialize(result));
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public static JSONObject getItemByID(long id) throws Exception{
|
|
|
64 |
DB db = mongo_dtr.getDB(CATALOG_DB);
|
|
|
65 |
DBCollection collection = db.getCollection(MASTER_DATA);
|
|
|
66 |
BasicDBObject obj = new BasicDBObject();
|
|
|
67 |
obj.append("_id", id);
|
|
|
68 |
DBObject result = collection.findOne(obj);
|
|
|
69 |
if (result==null){
|
|
|
70 |
throw new Exception();
|
|
|
71 |
}
|
|
|
72 |
return new JSONObject(JSON.serialize(result));
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
}
|