Subversion Repositories SmartDukaan

Rev

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