Subversion Repositories SmartDukaan

Rev

Rev 22162 | Rev 22169 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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