Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1072 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.metamodel.definitions;
5
 
6
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.core.Feature;
8
import in.shop2020.metamodel.core.Slide;
9
import in.shop2020.metamodel.util.ExpandedBullet;
10
import in.shop2020.metamodel.util.ExpandedEntity;
11
import in.shop2020.metamodel.util.ExpandedSlide;
12
import in.shop2020.util.DBUtils;
13
import in.shop2020.util.Utils;
14
 
15
import java.io.File;
16
import java.io.Serializable;
17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.TreeMap;
22
 
23
/**
24
 * Single point access to stored shop2020 core objects
25
 * 
26
 * @author naveen
27
 *
28
 */
29
public class OldEntityContainer implements Serializable {
30
 
31
	/**
32
	 * 
33
	 */
34
	private static final long serialVersionUID = 1L;
35
 
36
	/**
37
	 * Hashtable of Entity ID to Entity object
38
	 */
39
	private Map<Long, Entity> entities;
40
 
41
	/**
42
	 * Hashtable of Category ID to list of Entity objects
43
	 */
44
	private Map<Long, List<Entity>> entitiesbycategory;
45
 
46
	/**
47
	 * Hashtable of Feature Definition ID to list of learned Bullet objects
48
	 */
49
	private Map<Long, List<ExpandedBullet>> learnedBullets;
50
 
51
	/**
52
	 * Hashtable of Facet Definition ID to list of unique facet values found 
53
	 * across entities
54
	 */
55
	private Map<Long, List<String>> facetDefinitionIDFacetValues;
56
 
57
	/**
58
	 * Hashtable of Entity ID > Map(Slide ID > Score)
59
	 */
60
	private Map<Long, Map<Long, Integer>> entityIDSlideIDsScore;
61
 
62
	/**
63
	 * Hashtable of Entity ID > Final Score
64
	 */
65
	private Map<Long, Integer> entityIDFinalScore;
66
 
67
	/**
68
	 * Database path. Just in case we want to override
69
	 */
70
	private String dbPath =  Utils.CONTENT_DB_PATH;
71
 
72
	/**
73
	 * Instantiates required data structures only when needed
74
	 */
75
	public OldEntityContainer() {
76
		// Lazy initialization
77
	}
78
 
79
	public OldEntityContainer(String dbPath) {
80
		this.dbPath = dbPath;
81
		// Lazy initialization
82
	}
83
 
84
	/**
85
	 * Returns all stored Entity objects 
86
	 * 
87
	 * @return Map
88
	 * @throws Exception 
89
	 */
90
	@SuppressWarnings("unchecked")
91
	public Map<Long, Entity> getEntities() throws Exception {
92
		// De-serialize
93
		if(this.entities == null) {
94
			String entitiesDBFile = this.dbPath + "entities" + File.separator + "entities.ser";
95
			String entitiesbycategoryDBFile = this.dbPath + "entities" + File.separator + 
96
				"entitiesbycategory.ser";
97
 
98
			this.entities = (Map<Long, Entity>) DBUtils.read(entitiesDBFile);
99
			this.entitiesbycategory = (Map<Long, List<Entity>>) 
100
				DBUtils.read(entitiesbycategoryDBFile);
101
		}
102
 
103
		if(this.entities == null) {
104
			// A sorted map to ensure sequence
105
			this.entities = new TreeMap<Long, Entity>();
106
			this.entitiesbycategory = new TreeMap<Long, List<Entity>>();
107
		}
108
 
109
		return this.entities;
110
	}
111
 
112
	/**
113
	 * Returns hashtable of list bullet objects to feature definition ID
114
	 * 
115
	 * @return Map<Long, List<ExpandedBullet>> 
116
	 * @throws Exception
117
	 */
118
	@SuppressWarnings("unchecked")
119
	public Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception{
120
		// De-serialize
121
		if(this.learnedBullets == null) {
122
			String dbFile = this.dbPath + "entities" + File.separator + "learnedbullets.ser";
123
 
124
			this.learnedBullets = 
125
				(Map<Long, List<ExpandedBullet>>) DBUtils.read(dbFile);
126
		}
127
 
128
		if(this.learnedBullets == null) {
129
			this.learnedBullets = new HashMap<Long, List<ExpandedBullet>>();
130
		}
131
 
132
		return this.learnedBullets;
133
	}
134
 
135
	/**
136
	 * Resolves Feature Definition ID into list of learned Bullet objects
137
	 * 
138
	 * @param featureDefinitionID
139
	 * @return list of ExpandedBullets
140
	 * @throws Exception
141
	 */
142
	public List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) 
143
		throws Exception {
144
		if(this.learnedBullets == null) {
145
			this.getLearnedBullets();
146
		}
147
 
148
		return this.learnedBullets.get(new Long(featureDefinitionID));
149
	}
150
 
151
	/** 
152
	 * Returns hashtable of list entities to category ID
153
	 * 
154
	 * @return the entitiesbycategory Entities by category
155
	 * @throws Exception 
156
	 */
157
	public Map<Long, List<Entity>> getEntitiesbyCategory() throws Exception {
158
		if(this.entities == null) {
159
			this.getEntities();
160
		}
161
		String entitiesbycategoryDBFile = this.dbPath + "entities" + File.separator + 
162
		"entitiesbycategory.ser";
163
		this.entitiesbycategory = (Map<Long, List<Entity>>)DBUtils.read(entitiesbycategoryDBFile);
164
 
165
 
166
		return this.entitiesbycategory;
167
	}
168
 
169
	/**
170
	 * Resolves Entity ID into Entity object
171
	 * 
172
	 * @param entityID
173
	 * @return Entity
174
	 * @throws Exception 
175
	 */
176
	public Entity getEntity(long entityID) throws Exception {
177
		if(this.entities == null) {
178
			this.getEntities();
179
		}
180
 
181
		return this.entities.get(new Long(entityID));
182
	}
183
 
184
	/**
185
	 * Returns list of entities for a category ID
186
	 * 
187
	 * @param categoryID
188
	 * @return List<Entity>
189
	 * @throws Exception 
190
	 */
191
	public List<Entity> getEntities(long categoryID) throws Exception {
192
		if(this.entities == null) {
193
			this.getEntities();
194
		}
195
		//Utils.info("this.entitiesbycategory=" + this.entitiesbycategory);
196
 
197
		return this.entitiesbycategory.get(new Long(categoryID));
198
	}
199
 
200
	/**
201
	 * Convenience method to add new Entity 
202
	 * 
203
	 * @param newEntity
204
	 * @throws Exception 
205
	 */
206
	public void addEntity(Entity newEntity) throws Exception {
207
		if(this.entities == null) {
208
			this.getEntities();
209
		}
210
 
211
		this.entities.put(new Long(newEntity.getID()), newEntity);
212
 
213
		// Keep index by category ID
214
		List<Entity> catentities = 
215
			this.entitiesbycategory.get(newEntity.getCategoryID());
216
 
217
		if(catentities == null) {
218
			catentities = new ArrayList<Entity>();
219
		}
220
 
221
		catentities.add(newEntity);
222
		this.entitiesbycategory.put(new Long(newEntity.getCategoryID()), 
223
				catentities);
224
	}
225
 
226
 
227
	/**
228
	 * Convenience method to add new Entity 
229
	 * 
230
	 * @param newEntity
231
	 * @throws Exception 
232
	 */
233
	public void updateEntity(Entity newEntity) throws Exception {
234
		if(this.entities == null) {
235
			this.getEntities();
236
		}
237
 
238
		this.entities.remove(new Long(newEntity.getID()));
239
 
240
		// Keep index by category ID
241
		List<Entity> catentities = 
242
			this.entitiesbycategory.get(newEntity.getCategoryID());
243
 
244
		// As it is the reference, remove(obj) should work
245
		catentities.remove(newEntity);
246
 
247
		this.addEntity(newEntity);
248
	}
249
 
250
	/**
251
	 * Convenience method to remove an Entity 
252
	 * 
253
	 * @param newEntity
254
	 * @throws Exception 
255
	 */
256
	public void deleteEntity(Entity newEntity) throws Exception {
257
		if(this.entities == null) {
258
			this.getEntities();
259
		}
260
 
261
		this.entities.remove(new Long(newEntity.getID()));
262
 
263
		// Keep index by category ID
264
		List<Entity> catentities = 
265
			this.entitiesbycategory.get(newEntity.getCategoryID());
266
 
267
		// As it is the reference, remove(obj) should work
268
		catentities.remove(newEntity);
269
	}
270
 
271
	/**
272
	 * Returns expand form of entity object. All references are resolved into 
273
	 * corresponding detail object
274
	 * 
275
	 * @param entityID
276
	 * @return ExpandedEntity 
277
	 * @throws Exception 
278
	 */
279
	public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
280
		Entity entity = this.getEntity(entityID);
281
		// Changed by Rajveer to handle null value
282
		if(entity==null){
283
			System.out.println("Entity is null");
284
			return null;
285
		}
286
		System.out.println( entity.getCategoryID());
287
		ExpandedEntity expEntity = new ExpandedEntity(entity);
288
 
289
		return expEntity;
290
	}
291
 
292
 
293
	/**
294
	 * Returns hashtable of list facet values to facet definition ID
295
	 * 
296
	 * @return
297
	 * @throws Exception
298
	 */
299
	@SuppressWarnings("unchecked")
300
	public Map<Long, List<String>> getFacetValues() throws Exception {
301
		// De-serialize
302
		if(this.facetDefinitionIDFacetValues == null) {
303
			String dbFile = this.dbPath + "entities" + File.separator + "facetvalues.ser";
304
 
305
			this.facetDefinitionIDFacetValues = 
306
				(Map<Long, List<String>>) DBUtils.read(dbFile);
307
		}
308
 
309
		if(this.facetDefinitionIDFacetValues == null) {
310
			this.facetDefinitionIDFacetValues = new HashMap<Long, 
311
				List<String>>();
312
		}
313
 
314
		return this.facetDefinitionIDFacetValues;
315
	}
316
 
317
	/**
318
	 * Returns list of facet values for a facet definition ID
319
	 * 
320
	 * @param facetDefinitionID
321
	 * @return List<String> 
322
	 * @throws Exception
323
	 */
324
	public List<String> getFacetValues(long facetDefinitionID) 
325
		throws Exception {
326
		this.getFacetValues();
327
 
328
		return this.facetDefinitionIDFacetValues.get(
329
				new Long(facetDefinitionID));
330
	}
331
 
332
	/**
333
	 * Utility method to find out Feature object in Entity instance
334
	 * 
335
	 * @param entityID
336
	 * @param featureDefinitionID
337
	 * @return Feature
338
	 * @throws Exception 
339
	 */
340
	public Feature getFeature(long entityID, long featureDefinitionID) 
341
		throws Exception {
342
		Entity entity = this.getEntity(entityID);
343
 
344
		Feature feature = null;
345
 
346
		List<Slide> slides = entity.getSlides();
347
		for(Slide slide : slides) {
348
			feature = this.getFeature(slide, featureDefinitionID);
349
 
350
			// Until all slides are searched
351
			if(feature == null) {
352
				continue;
353
			}
354
			else {
355
				break;
356
			}
357
		}
358
 
359
		return feature;
360
	}
361
 
362
	/**
363
	 * 
364
	 * @param slide
365
	 * @param featureDefinitionID
366
	 * @return Feature
367
	 */
368
	public Feature getFeature(Slide slide, long featureDefinitionID) {
369
		List<Feature> features = slide.getFeatures();
370
 
371
		if(features != null) {
372
			for(Feature feature : features) {
373
				if(feature.getFeatureDefinitionID() == featureDefinitionID) {
374
					return feature;
375
				}
376
			}
377
		}
378
 
379
		Feature feature = null;
380
 
381
		List<Slide> childrenSlides = slide.getChildrenSlides();
382
		if(childrenSlides != null) {
383
			for(Slide childSlide : childrenSlides) {
384
 
385
				feature = this.getFeature(childSlide, featureDefinitionID);
386
				if(feature == null) {
387
					continue;
388
				}
389
				else {
390
					break;
391
				}
392
			}
393
		}
394
 
395
		return feature;
396
	}
397
 
398
	/**
399
	 * Utility method to find out Slide object in Entity instance
400
	 * 
401
	 * @param entityID
402
	 * @param slideDefinitionID
403
	 * @return
404
	 * @throws Exception 
405
	 */
406
	public Slide getSlide(long entityID, long slideDefinitionID) 
407
		throws Exception {
408
		Entity entity = this.getEntity(entityID);
409
 
410
		List<Slide> slides = entity.getSlides();
411
 
412
		Slide resultSlide = null;
413
 
414
		if(slides != null) {
415
			for(Slide slide : slides) {
416
 
417
				if(slide.getSlideDefinitionID() == slideDefinitionID) {
418
					return slide;
419
				}
420
 
421
				resultSlide = this.getSlide(slide, slideDefinitionID);
422
 
423
				if(resultSlide == null) {
424
					continue;
425
				}
426
				else {
427
					break;
428
				}
429
			}
430
		}
431
 
432
		return resultSlide;
433
	}
434
 
435
	/**
436
	 * 
437
	 * @param slide
438
	 * @param slideDefinitionID
439
	 * @return
440
	 */
441
	public Slide getSlide(Slide slide, long slideDefinitionID) {
442
 
443
		List<Slide> childrenSlides = slide.getChildrenSlides();
444
 
445
		Slide resultSlide = null;
446
 
447
		if(childrenSlides != null) {
448
			for(Slide childSlide : childrenSlides) {
449
				if(childSlide.getSlideDefinitionID() == slideDefinitionID) {
450
					return childSlide;
451
				}
452
 
453
				resultSlide = this.getSlide(childSlide, slideDefinitionID);
454
				if(resultSlide == null) {
455
					continue;
456
				}
457
				else {
458
					break;
459
				}
460
			}
461
		}
462
 
463
		return resultSlide;
464
	}
465
 
466
	/**
467
	 * Returns list of borrowed slides
468
	 * 
469
	 * @param entity
470
	 * @return list of borrowed slides
471
	 */
472
	public List<Slide> getBorrowedSlides(Entity entity) {
473
		List<Slide> borrowedSlides = new ArrayList<Slide>();
474
 
475
		List<Slide> slides = entity.getSlides();
476
 
477
		for(Slide slide : slides) {
478
			if(slide.isBorrowed()) {
479
				borrowedSlides.add(slide);
480
			}
481
		}
482
 
483
		return borrowedSlides;
484
	}
485
 
486
	/**
487
	 * Returns first parent slide with matching label
488
	 * 
489
	 * @param expEntity2
490
	 * @param slideLabel
491
	 * @return Null if not found
492
	 */
493
	public ExpandedSlide getParentExpandedSlide(ExpandedEntity expEntity2,
494
			String slideLabel) {
495
 
496
		ExpandedSlide matchingSlide = null;
497
		List<ExpandedSlide> expSlides = expEntity2.getExpandedSlides();
498
 
499
		for(ExpandedSlide expSlide : expSlides) {
500
			if(expSlide.getSlideDefinition().getLabel().equals(slideLabel)) {
501
				matchingSlide = expSlide;
502
				break;
503
			}
504
		}
505
 
506
		return matchingSlide;
507
	}
508
 
509
	/**
510
	 * Returns per slide comparison score for all entities in DB
511
	 * 
512
	 * @return Map<Long, Map<Long, Integer>> Entity ID > Map(Slide ID > Score)
513
	 * @throws Exception 
514
	 */
515
	@SuppressWarnings("unchecked")
516
	public Map<Long, Map<Long, Integer>> getEntitySlideComparisonScores() 
517
		throws Exception {
518
		// De-serialize
519
		if(this.entityIDSlideIDsScore == null) {
520
			String dbFile = this.dbPath + "comparisons" + File.separator + "slidescores.ser";
521
 
522
			this.entityIDSlideIDsScore = 
523
				(Map<Long, Map<Long, Integer>>) DBUtils.read(dbFile);
524
		}
525
 
526
		if(this.entityIDSlideIDsScore == null) {
527
			this.entityIDSlideIDsScore = new HashMap<Long, 
528
				Map<Long, Integer>>();
529
		}
530
 
531
		return this.entityIDSlideIDsScore;
532
	}
533
 
534
	/**
535
	 * Returns final comparison score for all entities in DB
536
	 * 
537
	 * @return Map<Long, Integer> Entity ID > Final Score
538
	 * @throws Exception 
539
	 */
540
	@SuppressWarnings("unchecked")
541
	public Map<Long, Integer> getEntityComparisonScores() throws Exception {
542
		// De-serialize
543
		if(this.entityIDFinalScore == null) {
544
			String dbFile = this.dbPath + "comparisons" + File.separator + 
545
				"defaultentityscores.ser";
546
 
547
			this.entityIDFinalScore = 
548
				(Map<Long, Integer>) DBUtils.read(dbFile);
549
		}
550
 
551
		if(this.entityIDFinalScore == null) {
552
			this.entityIDFinalScore = new HashMap<Long, Integer>();
553
		}
554
 
555
		return this.entityIDFinalScore;
556
	}
557
 
558
	/**
559
	 * Returns slide wise comparison scores for an entity
560
	 * 
561
	 * @param entityID
562
	 * @return Map<Long, Integer> Slide ID to Comparison score, Null if no data
563
	 * @throws Exception
564
	 */
565
	public Map<Long, Integer> getSlideComparisonScores(long entityID) 
566
		throws Exception {
567
		this.getEntitySlideComparisonScores();
568
 
569
		return this.entityIDSlideIDsScore.get(new Long(entityID));
570
	}
571
 
572
	/**
573
	 * 
574
	 * @param entityID
575
	 * @return int Comparison score
576
	 * @throws Exception
577
	 */
578
	public int getEntityComparisonScore(long entityID) throws Exception {
579
		this.getEntityComparisonScores();
580
 
581
		Integer objScore = this.entityIDFinalScore.get(new Long(entityID));
582
		if(objScore != null) {
583
			return objScore.intValue();
584
		}
585
 
586
		return -1;
587
	}
588
}