Subversion Repositories SmartDukaan

Rev

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

Rev 8909 Rev 9218
Line 40... Line 40...
40
 *
40
 *
41
 */
41
 */
42
public class StorageManager {
42
public class StorageManager {
43
	private static Log            log                         = LogFactory.getLog(StorageManager.class);
43
	private static Log            log                         = LogFactory.getLog(StorageManager.class);
44
	private static DB db;
44
	private static DB db;
45
    private static ContentViews views;
45
    public static ContentViews views;
46
    private static StorageManager storageUtils;
46
    private static StorageManager storageUtils;
47
    private static final Gson gs = new GsonBuilder().
47
    private static final Gson gs = new GsonBuilder().
48
    	registerTypeAdapter(BulletDataObject.class, new BDOAdapter()).
48
    	registerTypeAdapter(BulletDataObject.class, new BDOAdapter()).
49
    	//registerTypeAdapter(FreeformContent.class, new FFCAdapter()).
49
    	//registerTypeAdapter(FreeformContent.class, new FFCAdapter()).
50
    	create();
50
    	create();
Line 248... Line 248...
248
    	dbo.removeField("_id");
248
    	dbo.removeField("_id");
249
    	return (T)gs.fromJson(dbo.toString(), t);
249
    	return (T)gs.fromJson(dbo.toString(), t);
250
    }
250
    }
251
 
251
 
252
    @SuppressWarnings("unchecked")
252
    @SuppressWarnings("unchecked")
253
    private static <T> T getById(DBCollection collection, Long id, Type t) {
253
    public static <T> T getById(DBCollection collection, Long id, Type t) {
254
    	DBObject dbo = collection.findOne(id);
254
    	DBObject dbo = collection.findOne(id);
255
    	if(dbo ==null) return null;
255
    	if(dbo ==null) return null;
256
    	if (dbo.get(id.toString()) != null){
256
    	if (dbo.get(id.toString()) != null){
257
    		return (T)gs.fromJson(dbo.get(id.toString()).toString(), t);
257
    		return (T)gs.fromJson(dbo.get(id.toString()).toString(), t);
258
    	}
258
    	}
259
    	dbo.removeField("_id");
259
    	dbo.removeField("_id");
260
    	return (T)gs.fromJson(dbo.toString(), t);
260
    	return (T)gs.fromJson(dbo.toString(), t);
261
    }
261
    }
262
 
262
 
263
    private static <T> void insertOrUpdateById  (DBCollection collection, long id, T obj) {
263
    public static <T> void insertOrUpdateById  (DBCollection collection, long id, T obj) {
264
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
264
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
265
    	dbo.put("_id", id);
265
    	dbo.put("_id", id);
266
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
266
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
-
 
267
    }
-
 
268
 
-
 
269
    public static <T> void addById  (DBCollection collection, long id, T obj) {
-
 
270
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
267
    	
271
    	
-
 
272
    	//dbo.put("_id", id);
-
 
273
    	collection.update(new BasicDBObject("_id", id ), new BasicDBObject("$set", dbo), true, false);
268
    }
274
    }
269
 
275
 
270
    private static void removeById(DBCollection collection, Long id) {
276
    private static void removeById(DBCollection collection, Long id) {
271
    	collection.remove(new BasicDBObject("_id", id));
277
    	collection.remove(new BasicDBObject("_id", id));
272
    }
278
    }