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