Subversion Repositories SmartDukaan

Rev

Rev 791 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 791 Rev 1050
Line 4... Line 4...
4
package in.shop2020.metamodel.definitions;
4
package in.shop2020.metamodel.definitions;
5
 
5
 
6
import in.shop2020.metamodel.core.Entity;
6
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.core.Feature;
7
import in.shop2020.metamodel.core.Feature;
8
import in.shop2020.metamodel.core.Slide;
8
import in.shop2020.metamodel.core.Slide;
-
 
9
import in.shop2020.metamodel.util.CN;
-
 
10
import in.shop2020.metamodel.util.CreationUtils;
9
import in.shop2020.metamodel.util.ExpandedBullet;
11
import in.shop2020.metamodel.util.ExpandedBullet;
10
import in.shop2020.metamodel.util.ExpandedEntity;
12
import in.shop2020.metamodel.util.ExpandedEntity;
11
import in.shop2020.metamodel.util.ExpandedSlide;
13
import in.shop2020.metamodel.util.ExpandedSlide;
-
 
14
import in.shop2020.storage.bdb.StorageManager;
12
import in.shop2020.util.DBUtils;
15
import in.shop2020.util.DBUtils;
13
import in.shop2020.util.Utils;
16
import in.shop2020.util.Utils;
14
 
17
 
15
import java.io.File;
18
import java.io.File;
16
import java.io.Serializable;
19
import java.io.Serializable;
Line 32... Line 35...
32
	 * 
35
	 * 
33
	 */
36
	 */
34
	private static final long serialVersionUID = 1L;
37
	private static final long serialVersionUID = 1L;
35
	
38
	
36
	/**
39
	/**
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
40
	 * Hashtable of Feature Definition ID to list of learned Bullet objects
48
	 */
41
	 */
49
	private Map<Long, List<ExpandedBullet>> learnedBullets;
42
	private Map<Long, List<ExpandedBullet>> learnedBullets;
50
	
43
	
51
	/**
44
	/**
Line 71... Line 64...
71
	
64
	
72
	/**
65
	/**
73
	 * Instantiates required data structures only when needed
66
	 * Instantiates required data structures only when needed
74
	 */
67
	 */
75
	public EntityContainer() {
68
	public EntityContainer() {
76
		// Lazy initialization
69
		initialize();
77
	}
70
	}
78
 
71
 
79
	public EntityContainer(String dbPath) {
72
	public EntityContainer(String dbPath) {
80
		this.dbPath = dbPath;
73
		this.dbPath = dbPath;
81
		// Lazy initialization
-
 
82
	}
74
	}
83
	
75
	
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;
76
	private void initialize(){
110
	}
77
	}
111
	
78
	
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
	
79
	
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
	
80
	
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
	
81
	
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
	
82
	
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
	
83
	
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
		
84
		
263
		// Keep index by category ID
-
 
264
		List<Entity> catentities = 
-
 
265
			this.entitiesbycategory.get(newEntity.getCategoryID());
-
 
266
		
85
	
267
		// As it is the reference, remove(obj) should work
-
 
268
		catentities.remove(newEntity);
-
 
269
	}
86
	
270
	
87
	
271
	/**
88
	/**
272
	 * Returns expand form of entity object. All references are resolved into 
89
	 * Returns expand form of entity object. All references are resolved into 
273
	 * corresponding detail object
90
	 * corresponding detail object
274
	 * 
91
	 * 
275
	 * @param entityID
92
	 * @param entityID
276
	 * @return ExpandedEntity 
93
	 * @return ExpandedEntity 
277
	 * @throws Exception 
94
	 * @throws Exception 
278
	 */
95
	 */
279
	public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
96
	public ExpandedEntity getExpandedEntity(long entityID) throws Exception {
280
		Entity entity = this.getEntity(entityID);
97
		Entity entity = CreationUtils.getEntity(entityID);
281
		// Changed by Rajveer to handle null value
-
 
282
		if(entity==null){
98
		if(entity==null){
283
			System.out.println("Entity is null");
99
			System.out.println("Entity is null");
284
			return null;
100
			return null;
285
		}
101
		}
286
		System.out.println( entity.getCategoryID());
102
		System.out.println( entity.getCategoryID());
287
		ExpandedEntity expEntity = new ExpandedEntity(entity);
103
		ExpandedEntity expEntity = new ExpandedEntity(entity);
288
		
-
 
289
		return expEntity;
104
		return expEntity;
290
	}
105
	}
291
 
106
 
292
	
107
	
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
	
108
	
332
	/**
109
	/**
333
	 * Utility method to find out Feature object in Entity instance
110
	 * Utility method to find out Feature object in Entity instance
334
	 * 
111
	 * 
335
	 * @param entityID
112
	 * @param entityID
Line 337... Line 114...
337
	 * @return Feature
114
	 * @return Feature
338
	 * @throws Exception 
115
	 * @throws Exception 
339
	 */
116
	 */
340
	public Feature getFeature(long entityID, long featureDefinitionID) 
117
	public Feature getFeature(long entityID, long featureDefinitionID) 
341
		throws Exception {
118
		throws Exception {
342
		Entity entity = this.getEntity(entityID);
119
		Entity entity = CreationUtils.getEntity(entityID);
343
		
120
		
344
		Feature feature = null;
121
		Feature feature = null;
345
		
122
		
346
		List<Slide> slides = entity.getSlides();
123
		List<Slide> slides = entity.getSlides();
347
		for(Slide slide : slides) {
124
		for(Slide slide : slides) {
Line 403... Line 180...
403
	 * @return
180
	 * @return
404
	 * @throws Exception 
181
	 * @throws Exception 
405
	 */
182
	 */
406
	public Slide getSlide(long entityID, long slideDefinitionID) 
183
	public Slide getSlide(long entityID, long slideDefinitionID) 
407
		throws Exception {
184
		throws Exception {
408
		Entity entity = this.getEntity(entityID);
185
		Entity entity = CreationUtils.getEntity(entityID);
409
		
186
		
410
		List<Slide> slides = entity.getSlides();
187
		List<Slide> slides = entity.getSlides();
411
		
188
		
412
		Slide resultSlide = null;
189
		Slide resultSlide = null;
413
		
190
		
Line 503... Line 280...
503
			}
280
			}
504
		}
281
		}
505
		
282
		
506
		return matchingSlide;
283
		return matchingSlide;
507
	}
284
	}
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
	
285
 
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
}
286
}