Subversion Repositories SmartDukaan

Rev

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

Rev 21723 Rev 22097
Line 1... Line 1...
1
package com.spice.profitmandi.dao.repository.dtr;
1
package com.spice.profitmandi.dao.repository.dtr;
2
 
2
 
-
 
3
import java.lang.reflect.Type;
3
import java.net.UnknownHostException;
4
import java.net.UnknownHostException;
-
 
5
import java.util.ArrayList;
-
 
6
import java.util.List;
4
 
7
 
5
import org.json.JSONObject;
8
import org.json.JSONObject;
6
 
9
 
-
 
10
import com.google.gson.Gson;
-
 
11
import com.google.gson.reflect.TypeToken;
7
import com.mongodb.BasicDBObject;
12
import com.mongodb.BasicDBObject;
8
import com.mongodb.DB;
13
import com.mongodb.DB;
9
import com.mongodb.DBCollection;
14
import com.mongodb.DBCollection;
-
 
15
import com.mongodb.DBCursor;
10
import com.mongodb.DBObject;
16
import com.mongodb.DBObject;
11
import com.mongodb.MongoClient;
17
import com.mongodb.MongoClient;
12
import com.mongodb.util.JSON;
18
import com.mongodb.util.JSON;
-
 
19
import com.spice.profitmandi.dao.model.FofoForm;
13
 
20
 
14
 
21
 
15
public class Mongo {
22
public class Mongo {
16
 
23
 
17
	private static MongoClient mongo_local;
24
	private static MongoClient mongo_local;
18
	private static MongoClient mongo_dtr;
25
	private static MongoClient mongo_dtr;
19
	private static final String CONTENT ="CONTENT";
26
	private static final String CONTENT ="CONTENT";
20
	private static final String SITE_CONTENT = "siteContent";
27
	private static final String SITE_CONTENT = "siteContent";
21
	private static final String CATALOG_DB = "Catalog";
28
	private static final String CATALOG_DB = "Catalog";
22
	private static final String MASTER_DATA = "MasterData";
29
	private static final String MASTER_DATA = "MasterData";
-
 
30
	private static final String FOFO_DB = "Fofo";
-
 
31
	private static final String FOFO_FORM_COLLECTION = "RegistrationForm";
23
	private static final int MONGO_PORT = 27017;
32
	private static final int MONGO_PORT = 27017;
24
 
33
 
25
	static {
34
	static {
26
		synchronized(Mongo.class){
35
		synchronized(Mongo.class){
27
			try {
36
			try {
Line 69... Line 78...
69
		if (result==null){
78
		if (result==null){
70
			throw new Exception();
79
			throw new Exception();
71
		}
80
		}
72
		return new JSONObject(JSON.serialize(result));
81
		return new JSONObject(JSON.serialize(result));
73
	}
82
	}
-
 
83
	
-
 
84
	public static void persistFofoRegInfo(FofoForm ff){
-
 
85
	    DB db = mongo_dtr.getDB(FOFO_DB);
-
 
86
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
-
 
87
		if(ff.get_id()==0) {
-
 
88
			BasicDBObject orderBy = new BasicDBObject();
-
 
89
			orderBy.put("_id", -1);
-
 
90
			DBCursor cursor = collection.find().sort(orderBy).limit(1);
-
 
91
			long id = 1l;
-
 
92
			while (cursor.hasNext()) {
-
 
93
				Gson gson = new Gson();
-
 
94
				FofoForm existingFofo = gson.fromJson(cursor.next().toString(), FofoForm.class);
-
 
95
				id = existingFofo.get_id() + 1;
-
 
96
			}
-
 
97
			ff.set_id(id);
-
 
98
		}
-
 
99
		Gson gs = new Gson();
-
 
100
		DBObject dbObject = (DBObject) JSON.parse(gs.toJson(ff));
-
 
101
		collection.save(dbObject);
-
 
102
	}
-
 
103
	
-
 
104
	public static List<FofoForm> getFofoForms(int offset, int limit) {
-
 
105
		List<FofoForm> ffList = new ArrayList<FofoForm>(); 
-
 
106
		DB db = mongo_dtr.getDB(FOFO_DB);
-
 
107
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
-
 
108
		BasicDBObject orderBy = new BasicDBObject();
-
 
109
		orderBy.put("_id", -1);
-
 
110
		DBCursor dbc = collection.find().sort(orderBy).limit(limit).skip(offset);
-
 
111
		while (dbc.hasNext()) {
-
 
112
			ffList.add(convertJSONToPojo(dbc.next().toString()));
-
 
113
		}
-
 
114
		return ffList;
-
 
115
	}
-
 
116
	
-
 
117
	public static String getFofoFormJsonStringByFofoId(int fofoId){
-
 
118
		DB db = mongo_dtr.getDB(FOFO_DB);
-
 
119
		BasicDBObject filter = new BasicDBObject();
-
 
120
		filter.append("_id", fofoId);
-
 
121
		DBCollection collection = db.getCollection(FOFO_FORM_COLLECTION);
-
 
122
		DBObject fofoDbOject = collection.find(filter).one();
-
 
123
		return fofoDbOject.toString();
-
 
124
	}
-
 
125
	
-
 
126
	public static FofoForm getFofoForm(int fofoId) {
-
 
127
		String fofoFormJsonString = getFofoFormJsonStringByFofoId(fofoId);
-
 
128
		System.out.println(fofoFormJsonString);
-
 
129
		return new Gson().fromJson(fofoFormJsonString, FofoForm.class);
-
 
130
		//return convertJSONToPojo(fofoDbOject.toString());
-
 
131
	}
-
 
132
	
-
 
133
	private static FofoForm convertJSONToPojo(String json){
-
 
134
 
-
 
135
	    Type type = new TypeToken< FofoForm >(){}.getType();
-
 
136
 
-
 
137
	    return new Gson().fromJson(json, type);
-
 
138
 
-
 
139
	}
74
 
140
 
75
}
141
}