Subversion Repositories SmartDukaan

Rev

Rev 21920 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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