Subversion Repositories SmartDukaan

Rev

Rev 9218 | Rev 11958 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8749 amit.gupta 1
package in.shop2020.storage.mongo;
2
 
3
import in.shop2020.metamodel.core.BulletDataObject;
4
import in.shop2020.metamodel.core.Entity;
5
import in.shop2020.metamodel.core.EntityState;
6
import in.shop2020.metamodel.core.ExpertReview;
7
import in.shop2020.metamodel.core.ExpertReviewSource;
8
import in.shop2020.metamodel.core.Helpdoc;
9
import in.shop2020.metamodel.core.SpecialPage;
10
import in.shop2020.metamodel.util.ExpandedBullet;
11957 amit.gupta 11
import in.shop2020.metamodel.util.ItemPojo;
12
import in.shop2020.metamodel.util.PrivateDealPojo;
8749 amit.gupta 13
import in.shop2020.storage.mongo.adapters.BDOAdapter;
14
 
15
import java.lang.reflect.Type;
16
import java.util.ArrayList;
17
import java.util.Collection;
18
import java.util.HashMap;
19
import java.util.HashSet;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.TreeMap;
25
 
8810 amit.gupta 26
import org.apache.commons.logging.Log;
27
import org.apache.commons.logging.LogFactory;
28
 
8749 amit.gupta 29
import com.google.gson.Gson;
30
import com.google.gson.GsonBuilder;
31
import com.google.gson.reflect.TypeToken;
32
import com.mongodb.BasicDBObject;
33
import com.mongodb.DB;
34
import com.mongodb.DBCollection;
35
import com.mongodb.DBObject;
36
import com.mongodb.MongoClient;
37
import com.mongodb.util.JSON;
38
 
39
/**
40
 * Entry point for storing everything in berkley database. Singleton class which initialises the berkley database.
41
 * @author rajveer
42
 *
43
 */
44
public class StorageManager {
8810 amit.gupta 45
	private static Log            log                         = LogFactory.getLog(StorageManager.class);
8749 amit.gupta 46
	private static DB db;
9218 amit.gupta 47
    public static ContentViews views;
8749 amit.gupta 48
    private static StorageManager storageUtils;
49
    private static final Gson gs = new GsonBuilder().
50
    	registerTypeAdapter(BulletDataObject.class, new BDOAdapter()).
8865 amit.gupta 51
    	//registerTypeAdapter(FreeformContent.class, new FFCAdapter()).
8749 amit.gupta 52
    	create();
53
 
54
    static{
55
		synchronized(StorageManager.class){
56
			try {
57
				storageUtils = new StorageManager("CONTENT");
58
			} catch (Exception e) {
59
				// TODO Auto-generated catch block
60
				e.printStackTrace();
61
			}
62
		}
63
	}
64
 
65
    /**
66
     * Run the sample program.
67
     * @throws Exception 
68
     */
69
    public static void main(String[] args) throws Exception {
11957 amit.gupta 70
    	String jsonDeals = StorageManager.getStorageManager().getDealsJson(1007521l);
71
    	System.out.println(jsonDeals);
8749 amit.gupta 72
    }
73
 
74
    /**
75
     * get storage manager
76
     * @return MongoStorageManager
77
     */
78
    public static StorageManager getStorageManager(){
79
		return storageUtils;
80
    }
81
 
82
    /**
83
     * Open the database and views.
84
     */
85
    private StorageManager(String mongoDB) throws Exception {
86
			MongoClient mongo = new MongoClient( "localhost" , 27017 );
87
			db = mongo.getDB(mongoDB);	
88
			views = ContentViews.getInstance(db);
89
    }
90
 
91
    /**
92
     * Get the entities for a given category
93
     * @param categoryId
94
     * @return Collection<Entity>
95
     */
96
    public Collection<Entity> getEntitisByCategory(long categoryId){
97
    	List <Entity> list = new ArrayList<Entity>();
98
    	DBObject dbo = new BasicDBObject();
8870 amit.gupta 99
    	dbo.put("categoryID", categoryId);
8749 amit.gupta 100
    	Iterator<DBObject> it = views.entity.find(dbo).iterator();
101
    	while (it.hasNext()) {
102
    		list.add((Entity)toObject(it.next(),Entity.class));
103
    	}
104
    	return list;
105
 
106
    }
107
 
108
 
109
    /**
110
     * get all helpdocs
111
     * @return
112
     */
113
	public Map<Long, Helpdoc> getHelpdocs(){
114
    	Map<Long, Helpdoc> helpdocs = new HashMap<Long, Helpdoc>();
115
    	Iterator<DBObject> iterator = views.helpdoc.find().iterator();
116
    	while (iterator.hasNext()){
117
    		DBObject dbo = iterator.next();
118
    		helpdocs.put((Long)dbo.get("_id"), (Helpdoc)toObject(dbo, Helpdoc.class));
119
    	}
120
    	return helpdocs;
121
    }
122
 
123
    /**
124
     * Get helpdoc for an given id
125
     * @param helpdocId
126
     * @return
127
     */
128
    public Helpdoc getHelpdoc(long helpdocId){
129
    	return getById(views.helpdoc, helpdocId, Helpdoc.class);
130
    }
131
 
132
    /**
133
     * Delete helpdoc for an given id
134
     * @param helpdocId
135
     * @return
136
     */
137
    public void deleteHelpdoc(long helpdocId){
138
    	views.helpdoc.remove(new BasicDBObject("_id", helpdocId));
139
    }
140
 
141
	public void updateHelpdoc(Helpdoc helpdoc){
142
    	insertOrUpdateById(views.helpdoc, helpdoc.getID(), helpdoc);
143
    }
144
 
145
	/**
146
     * get metadata for all entities
147
     * @return
148
     */
149
	public Map<Long, EntityState> getEntitiesMetadata(){
150
    	Map<Long,EntityState> es = new TreeMap<Long, EntityState>();
151
    	Iterator<DBObject> iterator = views.entityMetadata.find().iterator();
152
    	while (iterator.hasNext()){
153
    		DBObject dbo = iterator.next();
154
    		es.put((Long)dbo.get("_id"),(EntityState)toObject(dbo, EntityState.class));
155
    	}
156
    	return es;
157
    }
158
 
159
    /**
160
     * Get metadata for an given entity.
161
     * @param entityId
162
     * @return
163
     */
164
    public EntityState getEntityMetadata(long entityId){
165
    	return (EntityState)getById(views.entityMetadata, entityId, EntityState.class);
166
    }
167
 
168
    /**
169
     * update metadata for an given entity
170
     * @param entityMetadata
171
     */
172
	public void updateEntityMetadata(EntityState entityMetadata){
173
    	insertOrUpdateById(views.entityMetadata, entityMetadata.getID(), entityMetadata);
174
    }
175
 
176
    /**
177
     * Get entity for given id.
178
     * @param entityId
179
     * @return
180
     * @throws Exception
181
     */
182
    public Entity getEntity(long entityId) throws Exception{
183
    	return (Entity)getById(views.entity, entityId, Entity.class);
184
    }
185
 
186
    /**
187
     * Update an existing entity
188
     * @param entity
189
     * @throws Exception
190
     */
191
    public void updateEntity(Entity entity) throws Exception{
192
    	insertOrUpdateById(views.entity,entity.getID(),entity);
193
    }
194
 
195
    /**
196
     * Get all entities.
197
     * @return
198
     * @throws Exception
199
     */
200
	public Map<Long, Entity> getEntities() throws Exception{
201
    	Map<Long,Entity> map = new TreeMap<Long, Entity>(); 
202
    	Iterator<DBObject> iterator = views.entity.find().iterator();
203
    	while(iterator.hasNext()){
204
    		DBObject dbo = iterator.next();
205
    		map.put((Long)dbo.get("_id"), (Entity)toObject(dbo, Entity.class));
206
    	}
207
    	return map;
208
    }
209
 
210
 
211
 
212
    /**
213
     * Returns all special pages from the database.
214
     */
215
    public Map<Long, SpecialPage> getSpecialPages() {
216
    	Map<Long,SpecialPage> map = new TreeMap<Long, SpecialPage>(); 
217
    	Iterator<DBObject> iterator = views.specialPages.find().iterator();
218
    	while(iterator.hasNext()){
219
    		DBObject dbo = iterator.next();
220
    		Long pageId = (Long)dbo.removeField("_id");
221
    		map.put(pageId, (SpecialPage)toObject(dbo, SpecialPage.class));
222
    	}
223
    	return map;
224
    }
225
 
226
    /**
227
     * Updates a given special page in the database.
228
     */
229
    public void updateSpecialPage(SpecialPage specialPage) {
230
    	insertOrUpdateById(views.specialPages, specialPage.getID(), specialPage);
231
    }
232
 
233
    /**
234
     * Returns a special page object given a id after looking it up from map
235
     * of values loaded from database.
236
     */
237
    public SpecialPage getSpecialPage(long specialPageId) {
238
        return (SpecialPage)getById(views.specialPages, specialPageId, SpecialPage.class);
239
    }
240
 
241
    /**
242
     * Removes a special page from database, if exists in database.
243
     */
244
    public void deleteSpecialPage(long specialPageId) {
245
    	removeById(views.specialPages, specialPageId);
246
    }
247
 
8750 amit.gupta 248
    @SuppressWarnings("unchecked")
8749 amit.gupta 249
    private static <T> T toObject(DBObject dbo, Type t) {
250
    	dbo.removeField("_id");
8751 amit.gupta 251
    	return (T)gs.fromJson(dbo.toString(), t);
8749 amit.gupta 252
    }
253
 
8750 amit.gupta 254
    @SuppressWarnings("unchecked")
9218 amit.gupta 255
    public static <T> T getById(DBCollection collection, Long id, Type t) {
8749 amit.gupta 256
    	DBObject dbo = collection.findOne(id);
257
    	if(dbo ==null) return null;
258
    	if (dbo.get(id.toString()) != null){
8751 amit.gupta 259
    		return (T)gs.fromJson(dbo.get(id.toString()).toString(), t);
8749 amit.gupta 260
    	}
261
    	dbo.removeField("_id");
8751 amit.gupta 262
    	return (T)gs.fromJson(dbo.toString(), t);
8749 amit.gupta 263
    }
264
 
9218 amit.gupta 265
    public static <T> void insertOrUpdateById  (DBCollection collection, long id, T obj) {
8770 amit.gupta 266
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
8749 amit.gupta 267
    	dbo.put("_id", id);
268
    	collection.update(new BasicDBObject("_id", id ), dbo, true, false);
9218 amit.gupta 269
    }
270
 
271
    public static <T> void addById  (DBCollection collection, long id, T obj) {
272
    	DBObject dbo = (DBObject)JSON.parse(gs.toJson(obj));
8749 amit.gupta 273
 
9218 amit.gupta 274
    	//dbo.put("_id", id);
275
    	collection.update(new BasicDBObject("_id", id ), new BasicDBObject("$set", dbo), true, false);
8749 amit.gupta 276
    }
277
 
278
    private static void removeById(DBCollection collection, Long id) {
279
    	collection.remove(new BasicDBObject("_id", id));
280
    }
281
 
282
    public void storeDataObject(String dataObjectName, Object dataObject) throws Exception{
283
    	DBCollection collection = db.getCollection(dataObjectName);
284
    	Map <String, Object>  obj = new HashMap<String, Object>();
285
    	obj.put("1", dataObject);
286
    	insertOrUpdateById(collection, 1L, obj);
287
    }
288
 
8750 amit.gupta 289
    @SuppressWarnings("unchecked")
8749 amit.gupta 290
    public <T> T getDataObject(String dataObjectName, Type t) throws Exception{
291
    	DBCollection collection = db.getCollection(dataObjectName);
8751 amit.gupta 292
    	return (T)getById(collection, 1L, t);
8749 amit.gupta 293
    }
294
 
295
	public Long getMaxPageId() {
296
		Iterator<DBObject> dbo = views.specialPages.find().sort(new BasicDBObject("_id", -1)).limit(1).iterator();
297
		while  (dbo.hasNext()) {
298
			DBObject dbo1 = dbo.next();
299
			return (Long) dbo1.get("_id");
300
		}
301
		return 0l;
302
	}
303
 
304
	public void deleteEntity(long entityId) {
305
		removeById(views.entity, entityId);
306
		removeById(views.entityMetadata, entityId);
307
	}
308
 
309
	public void createEntity(Entity entity, EntityState entityMetadata) {
8770 amit.gupta 310
		insertOrUpdateById(views.entity, entity.getID(), entity);
311
		insertOrUpdateById(views.entityMetadata, entity.getID(), entityMetadata);
8749 amit.gupta 312
	}
313
 
314
	public void storeCustomSlideScores(long entityId, Map<Long, Double> customSlideScores) {
315
		insertOrUpdateById(views.customSlideScores, entityId, customSlideScores);
316
	}
317
 
318
	public Map<Long,Double> getCustomSlideScores(long entityId) {
319
		Type t = new TypeToken<Map<Long,Double>>() {}.getType();
320
		return getById(views.customSlideScores, entityId, t);
321
	}
322
 
323
	public void storeLearnedBullets( Map<Long, List<ExpandedBullet>> learnedBullets) {
324
 
325
		for(Map.Entry<Long, List<ExpandedBullet>> learnedBullet : learnedBullets.entrySet()) {
326
			insertOrUpdateById(views.learnedBullets, learnedBullet.getKey(), new BasicDBObject(learnedBullet.getKey().toString(), learnedBullet.getValue()));
327
		}
328
	}
329
 
330
 
8750 amit.gupta 331
	@SuppressWarnings("unchecked")
8749 amit.gupta 332
	public Map<Long, List<ExpandedBullet>> getLearnedBullets() {
333
		Type t = new TypeToken<List<ExpandedBullet>>() {}.getType();
334
		Map<Long, List<ExpandedBullet>> learnedBullets  = new HashMap<Long, List<ExpandedBullet>>();
335
		Iterator<DBObject> iter = views.learnedBullets.find();
336
		while (iter.hasNext()) {
337
			DBObject dbo = iter.next();
338
			String featureId = dbo.get("_id").toString();
339
			learnedBullets.put(Long.parseLong(featureId), (List<ExpandedBullet>)gs.fromJson(dbo.get(featureId).toString(), t));
340
		}
341
		return learnedBullets;
342
	}
343
 
344
	public Set<ExpandedBullet> getLearnedBulletsByFeatureId (long featureId){
345
		Type t = new TypeToken<Set<ExpandedBullet>>() {}.getType();
346
		return getById(views.learnedBullets, featureId, t);
347
	}
348
 
349
 
350
	public void storeSearchStats(Long date, Map<String,List<String>> searchTerms){
351
		insertOrUpdateById(views.searchStats, date, searchTerms);
352
	}
353
 
354
	public void deleteSearchStats(Long date){
355
		removeById(views.searchStats, date);
356
	}
357
 
358
	public List<String> getSearchStats(Long date){
359
		Type t = new TypeToken<List<String>>() {}.getType();
360
		return getById(views.searchStats, date, t);
361
	}
362
 
363
	public ExpertReviewSource getExpertReviewSource(long sourceId) {
364
		return getById(views.expertReviewSource, sourceId, ExpertReviewSource.class);
365
	}
366
 
367
	public Set<ExpertReviewSource> getExpertReviewSources() {
368
		Iterator<DBObject> sourcesObj = views.expertReviewSource.find().iterator();
369
		Set<ExpertReviewSource> expSources = new HashSet<ExpertReviewSource>();
370
		while(sourcesObj.hasNext()){
371
			DBObject dbobj = sourcesObj.next();
372
			expSources.add((ExpertReviewSource)toObject(dbobj, ExpertReviewSource.class));
373
		}
374
		return expSources;
375
	}
376
 
377
	public void storeExpertReviewSource(ExpertReviewSource expertReviewSource) {
378
		insertOrUpdateById(views.expertReviewSource, (long)expertReviewSource.hashCode(), expertReviewSource);
379
	}
380
 
381
	public void deleteExpertReviewSource(ExpertReviewSource expertReviewSource) {
382
		removeById(views.expertReviewSource, (long)expertReviewSource.hashCode());
383
	}
384
 
8905 amit.gupta 385
	public void storeExpertReview(long entityId, ExpertReview expertReview){
386
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
387
		List<ExpertReview> expertReviews = getById(views.expertReviews, entityId, t);
388
		if(expertReviews == null) {
389
			expertReviews = new ArrayList<ExpertReview>();
390
		}
391
		expertReviews.add(expertReview);
392
		Map<Long,List<ExpertReview>> erMap = new HashMap<Long, List<ExpertReview>>();
393
		erMap.put(entityId, expertReviews);
8906 amit.gupta 394
		insertOrUpdateById(views.expertReviews, entityId, erMap);
8905 amit.gupta 395
 
396
	}
397
 
8749 amit.gupta 398
	public void storeExpertReview(long entityId, List<ExpertReview> expertReviews) {
399
		Map<Long, List<ExpertReview>> map = new HashMap<Long, List<ExpertReview>>();
400
		map.put(entityId, expertReviews);
401
		insertOrUpdateById(views.expertReviews, entityId, map);
402
	}
403
 
404
	public void deleteExpertReview(long entityId, int index) {
405
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
406
		List<ExpertReview> er = getById(views.expertReviews, entityId, t);
407
		er.remove(index);
408
		storeExpertReview(entityId, er);
409
	}
410
 
8750 amit.gupta 411
	@SuppressWarnings("unchecked")
8749 amit.gupta 412
	public Map<Long, List<ExpertReview>> getExpertReviews() {
413
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
414
		Map<Long, List<ExpertReview>> er  = new HashMap<Long, List<ExpertReview>>();
415
		Iterator<DBObject> iter = views.expertReviews.find();
416
		while (iter.hasNext()) {
417
			DBObject dbo = iter.next();
418
			String entityId = dbo.get("_id").toString();
419
			er.put(Long.parseLong(entityId), (List<ExpertReview>)gs.fromJson(dbo.get(entityId).toString(), t));
420
		}
421
		return er;
422
	}
423
 
424
	public void deleteExpertReview(long entityId) {
425
		removeById(views.expertReviews, entityId);
426
	}
427
 
428
 
8750 amit.gupta 429
	@SuppressWarnings("unchecked")
8749 amit.gupta 430
	public Map<Long, List<Long>> getHelpdocEntityIds() {
431
		Type t = new TypeToken<List<Long>>() {}.getType();
432
		Map<Long, List<Long>> er  = new HashMap<Long, List<Long>>();
433
		Iterator<DBObject> iter = views.helpdocEntityIds.find().iterator();
434
		while (iter.hasNext()) {
435
			DBObject dbo = iter.next();
436
			String helpdocId = dbo.get("_id").toString();
437
			er.put(Long.parseLong(helpdocId), (List<Long>)gs.fromJson(dbo.get(helpdocId).toString(), t));
438
		}
439
		return er;
440
	}
441
 
442
	public List<ExpertReview> getExpertReviewByEntity(long entityId) {
443
		Type t = new TypeToken<List<ExpertReview>>() {}.getType();
444
		return getById(views.expertReviews, entityId, t);
445
	}
11957 amit.gupta 446
 
447
	public String getDealsJson(Long entityId){
448
		Type t = new TypeToken<ItemPojo>() {}.getType();
449
		DBObject dboQuery=  new BasicDBObject().append("_id", entityId);
450
		DBObject dboProjection = new BasicDBObject("items.dealPojo", 1).append("items.id", 1);
451
		DBObject dbo =  views.siteContent.findOne(dboQuery,dboProjection);
452
		if(dbo != null && dbo.get("items")!=null){
453
			return dbo.get("items").toString();
454
		}
455
		return null;
456
	}
8749 amit.gupta 457
 
458
}