Subversion Repositories SmartDukaan

Rev

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

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