Subversion Repositories SmartDukaan

Rev

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

Rev 8750 Rev 8751
Line 37... Line 37...
37
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
37
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
38
 * @author rajveer
38
 * @author rajveer
39
 *
39
 *
40
 */
40
 */
41
public class StorageManager {
41
public class StorageManager {
42
    private static final String UNCHECKED = "unchecked";
-
 
43
	private static DB db;
42
	private static DB db;
44
    private static ContentViews views;
43
    private static ContentViews views;
45
    private static StorageManager storageUtils;
44
    private static StorageManager storageUtils;
46
    private static final Gson gs = new GsonBuilder().
45
    private static final Gson gs = new GsonBuilder().
47
    	registerTypeAdapter(BulletDataObject.class, new BDOAdapter()).
46
    	registerTypeAdapter(BulletDataObject.class, new BDOAdapter()).
Line 243... Line 242...
243
    }
242
    }
244
    
243
    
245
    @SuppressWarnings("unchecked")
244
    @SuppressWarnings("unchecked")
246
    private static <T> T toObject(DBObject dbo, Type t) {
245
    private static <T> T toObject(DBObject dbo, Type t) {
247
    	dbo.removeField("_id");
246
    	dbo.removeField("_id");
248
    	return gs.fromJson(dbo.toString(), t);
247
    	return (T)gs.fromJson(dbo.toString(), t);
249
    }
248
    }
250
 
249
 
251
    @SuppressWarnings("unchecked")
250
    @SuppressWarnings("unchecked")
252
    private static <T> T getById(DBCollection collection, Long id, Type t) {
251
    private static <T> T getById(DBCollection collection, Long id, Type t) {
253
    	DBObject dbo = collection.findOne(id);
252
    	DBObject dbo = collection.findOne(id);
254
    	if(dbo ==null) return null;
253
    	if(dbo ==null) return null;
255
    	if (dbo.get(id.toString()) != null){
254
    	if (dbo.get(id.toString()) != null){
256
    		return gs.fromJson(dbo.get(id.toString()).toString(), t);
255
    		return (T)gs.fromJson(dbo.get(id.toString()).toString(), t);
257
    	}
256
    	}
258
    	dbo.removeField("_id");
257
    	dbo.removeField("_id");
259
    	return gs.fromJson(dbo.toString(), t);
258
    	return (T)gs.fromJson(dbo.toString(), t);
260
    }
259
    }
261
 
260
 
262
    @SuppressWarnings("unchecked")
-
 
263
    private static <T> void insertOrUpdateById  (DBCollection collection, long id, T t) {
261
    private static <T> void insertOrUpdateById  (DBCollection collection, long id, T t) {
264
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(t));
262
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(t));
265
    	dbo.put("_id", id);
263
    	dbo.put("_id", id);
266
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
264
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
267
    	
265
    	
Line 279... Line 277...
279
    }
277
    }
280
    
278
    
281
    @SuppressWarnings("unchecked")
279
    @SuppressWarnings("unchecked")
282
    public <T> T getDataObject(String dataObjectName, Type t) throws Exception{
280
    public <T> T getDataObject(String dataObjectName, Type t) throws Exception{
283
    	DBCollection collection = db.getCollection(dataObjectName);
281
    	DBCollection collection = db.getCollection(dataObjectName);
284
    	return getById(collection, 1L, t);
282
    	return (T)getById(collection, 1L, t);
285
    }
283
    }
286
 
284
 
287
	public Long getMaxPageId() {
285
	public Long getMaxPageId() {
288
		Iterator<DBObject> dbo = views.specialPages.find().sort(new BasicDBObject("_id", -1)).limit(1).iterator();
286
		Iterator<DBObject> dbo = views.specialPages.find().sort(new BasicDBObject("_id", -1)).limit(1).iterator();
289
		while  (dbo.hasNext()) {
287
		while  (dbo.hasNext()) {