Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
479 rajveer 1
package in.shop2020.metamodel.util;
2
 
3
import in.shop2020.metamodel.core.Entity;
1050 rajveer 4
import in.shop2020.metamodel.core.EntityState;
1153 rajveer 5
import in.shop2020.metamodel.core.Feature;
6
import in.shop2020.metamodel.core.Slide;
479 rajveer 7
import in.shop2020.metamodel.util.CN;
1050 rajveer 8
import in.shop2020.storage.bdb.StorageManager;
479 rajveer 9
 
10
import java.io.PrintWriter;
11
import java.io.StringWriter;
12
import java.io.Writer;
1153 rajveer 13
import java.util.ArrayList;
1050 rajveer 14
import java.util.Collection;
479 rajveer 15
import java.util.List;
16
import java.util.Map;
17
 
18
/**
1154 rajveer 19
 * All storage and retrival utility methods
1050 rajveer 20
 * @author rajveer
479 rajveer 21
 *
22
 */
23
public class CreationUtils {
1050 rajveer 24
 
25
	private static final String LEARNED_BULLETS = "LEARNED_BULLETS";
26
	private static final String DEFAULT_ENTITY_SCORES = "DEFAULT_ENTITY_SCORES";
27
	private static final String FACET_VALUES = "FACET_VALUES";
28
	private static final String SLIDE_SCORES = "SLIDE_SCORES";
29
 
479 rajveer 30
 
31
	/**
32
	 * 
1050 rajveer 33
	 * @param aThrowable
479 rajveer 34
	 * @return
35
	 */
1050 rajveer 36
	public static String getStackTrace(Throwable aThrowable) {
37
	    final Writer result = new StringWriter();
38
	    final PrintWriter printWriter = new PrintWriter(result);
39
	    aThrowable.printStackTrace(printWriter);
40
	    return result.toString();
479 rajveer 41
	}
42
 
43
	/**
44
	 * 
1050 rajveer 45
	 * @param entity
479 rajveer 46
	 * @throws Exception
47
	 */
1050 rajveer 48
	public static void storeLearnedBullets(Map<Long, List<ExpandedBullet>> learnedBullets) throws Exception {
49
		StorageManager.getStorageManager().storeDataObject(CreationUtils.LEARNED_BULLETS, learnedBullets);		
479 rajveer 50
	}
51
 
1154 rajveer 52
	/**
53
	 * 
54
	 * @return
55
	 * @throws Exception
56
	 */
479 rajveer 57
	@SuppressWarnings("unchecked")
1050 rajveer 58
	public static Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception {
59
		return (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);		
479 rajveer 60
	}
61
 
1154 rajveer 62
	/**
63
	 * 
64
	 * @param featureDefinitionID
65
	 * @return
66
	 * @throws Exception
67
	 */
1050 rajveer 68
	@SuppressWarnings("unchecked")
69
	public static List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) throws Exception {
70
		Map<Long, List<ExpandedBullet>> learnedBullets = (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);
71
		if(learnedBullets==null){
72
			return null;
479 rajveer 73
		}
1050 rajveer 74
		return learnedBullets.get(featureDefinitionID);
479 rajveer 75
	}
1050 rajveer 76
 
1154 rajveer 77
	/**
78
	 * 
79
	 * @param facetIDFacetValues
80
	 * @throws Exception
81
	 */
1050 rajveer 82
	public static void storeFacetValues(Map<Long, List<String>> facetIDFacetValues) throws Exception {
83
		StorageManager.getStorageManager().storeDataObject(CreationUtils.FACET_VALUES, facetIDFacetValues);
479 rajveer 84
	}
1050 rajveer 85
 
1154 rajveer 86
	/**
87
	 * 
88
	 * @return
89
	 * @throws Exception
90
	 */
1050 rajveer 91
	@SuppressWarnings("unchecked")
92
	public static Map<Long, List<String>>  getFacetValues() throws Exception {
93
		return (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
479 rajveer 94
	}
95
 
1154 rajveer 96
	/**
97
	 * 
98
	 * @param facetDefinitionID
99
	 * @return
100
	 * @throws Exception
101
	 */
1050 rajveer 102
	@SuppressWarnings("unchecked")
103
	public static List<String>  getFacetValues(long facetDefinitionID) throws Exception {
104
		Map<Long, List<String>> facetValues = (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
105
		return facetValues.get(facetDefinitionID);
479 rajveer 106
	}
1050 rajveer 107
 
1154 rajveer 108
	/**
109
	 * 
110
	 * @param slideScoresByEntity
111
	 * @throws Exception
112
	 */
1050 rajveer 113
	public static void storeSlideScores( Map<Long, Map<Long, Integer>> slideScoresByEntity) throws Exception {
114
		StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES, slideScoresByEntity);
115
	}
116
 
1154 rajveer 117
	/**
118
	 * 
119
	 * @return
120
	 * @throws Exception
121
	 */
479 rajveer 122
	@SuppressWarnings("unchecked")
1050 rajveer 123
	public static Map<Long, Map<Long, Integer>> getSlideScores() throws Exception {
124
		return (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
479 rajveer 125
	}
126
 
1154 rajveer 127
	/**
128
	 * 
129
	 * @param entityID
130
	 * @return
131
	 * @throws Exception
132
	 */
1050 rajveer 133
	@SuppressWarnings("unchecked")
134
	public static Map<Long, Integer> getSlideComparisonScores(long entityID) throws Exception{
135
		Map<Long, Map<Long, Integer>> slideScores = (Map<Long, Map<Long, Integer>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
136
		return slideScores.get(entityID);
479 rajveer 137
	}
138
 
1050 rajveer 139
 
1154 rajveer 140
	/**
141
	 * 
142
	 * @param finalScoreByEntityID
143
	 * @throws Exception
144
	 */
1050 rajveer 145
	public static void storeDefaultEntityScores(Map<Long, Integer> finalScoreByEntityID) throws Exception {
146
		StorageManager.getStorageManager().storeDataObject(CreationUtils.DEFAULT_ENTITY_SCORES, finalScoreByEntityID);
479 rajveer 147
	}
148
 
1154 rajveer 149
	/**
150
	 * 
151
	 * @return
152
	 * @throws Exception
153
	 */
1050 rajveer 154
	@SuppressWarnings("unchecked")
155
	public static Map<Long, Integer> getDefaultEntityScores() throws Exception {
156
		return (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
479 rajveer 157
	}
158
 
1154 rajveer 159
	/**
160
	 * 
161
	 * @param entityID
162
	 * @return
163
	 * @throws Exception
164
	 */
1050 rajveer 165
	@SuppressWarnings("unchecked")
166
	public static Integer getEntityComparisonScores(long entityID) throws Exception {
167
		Map<Long, Integer> scoresMap =  (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
168
		Integer objScore = scoresMap.get(entityID);
169
		if(objScore==null){
170
			return -1;
479 rajveer 171
		}
1050 rajveer 172
		return objScore;
479 rajveer 173
	}
174
 
175
 
176
 
177
	/**
1050 rajveer 178
	 * Resolves Entity ID into Entity object
479 rajveer 179
	 * 
180
	 * @param entityID
1050 rajveer 181
	 * @return Entity
182
	 * @throws Exception 
479 rajveer 183
	 */
1050 rajveer 184
	public static Entity getEntity(long entityID) throws Exception{
185
		return StorageManager.getStorageManager().getEntity(entityID);
479 rajveer 186
	}
187
 
1154 rajveer 188
	/**
189
	 * 
190
	 * @param entity
191
	 * @throws Exception
192
	 */
1050 rajveer 193
	public static void updateEntity(Entity entity) throws Exception {
194
		entity.reorderSlides(entity.getSlideSequence());
195
		StorageManager.getStorageManager().updateEntity(entity);
196
		CreationUtils.learn(entity);
479 rajveer 197
	}
198
 
1154 rajveer 199
	/**
200
	 * 
201
	 * @param entity
202
	 * @param entityMetadata
203
	 * @throws Exception
204
	 */
1050 rajveer 205
	public static void createEntity(Entity entity, EntityState entityMetadata) throws Exception {
206
		StorageManager.getStorageManager().createEntity(entity, entityMetadata);
479 rajveer 207
	}
1050 rajveer 208
 
1154 rajveer 209
	/**
210
	 * 
211
	 * @param entityId
212
	 * @throws Exception
213
	 */
1050 rajveer 214
	public static void deleteEntity(long entityId) throws Exception  {
215
		StorageManager.getStorageManager().deleteEntity(entityId);
479 rajveer 216
	}
217
 
1050 rajveer 218
 
1154 rajveer 219
	/**
220
	 * 
221
	 * @return
222
	 * @throws Exception
223
	 */
1050 rajveer 224
	public static Map<Long, EntityState> getEntitiesState() throws Exception {
225
		return StorageManager.getStorageManager().getEntitiesMetadata();
479 rajveer 226
	}
227
 
1154 rajveer 228
	/**
229
	 * 
230
	 * @param entityId
231
	 * @return
232
	 * @throws Exception
233
	 */
1050 rajveer 234
	public static EntityState getEntityState(long entityId) throws Exception {
235
		return StorageManager.getStorageManager().getEntityMetadata(entityId);
236
	}
479 rajveer 237
 
1154 rajveer 238
	/**
239
	 * 
240
	 * @param entityMetadata
241
	 * @throws Exception
242
	 */
1050 rajveer 243
	public static void updateEntityState(EntityState entityMetadata) throws Exception {
244
		StorageManager.getStorageManager().updateEntityMetadata(entityMetadata);
479 rajveer 245
	}
1050 rajveer 246
 
1154 rajveer 247
	/**
248
	 * 
249
	 * @param entity
250
	 * @throws Exception
251
	 */
1050 rajveer 252
	private static void learn(Entity entity) throws Exception{
253
		CN cn = new CN();
254
		cn.learn(entity);	
255
	}
256
 
1154 rajveer 257
	/**
258
	 * 
259
	 * @return
260
	 * @throws Exception
261
	 */
1050 rajveer 262
	public static Map<Long, Entity> getEntities() throws Exception {
263
		return StorageManager.getStorageManager().getEntities();
264
	}
265
 
1154 rajveer 266
	/**
267
	 * 
268
	 * @param categoryId
269
	 * @return
270
	 * @throws Exception
271
	 */
1050 rajveer 272
	public static Collection<Entity> getEntities(long categoryId) throws Exception {
273
		return StorageManager.getStorageManager().getEntitisByCategory(categoryId);
274
	}
1153 rajveer 275
 
276
	/**
277
	 * 
278
	 * @param slide
279
	 * @param featureDefinitionID
280
	 * @return Feature
281
	 */
282
	public static Feature getFeature(Slide slide, long featureDefinitionID) {
283
		List<Feature> features = slide.getFeatures();
284
 
285
		if(features != null) {
286
			for(Feature feature : features) {
287
				if(feature.getFeatureDefinitionID() == featureDefinitionID) {
288
					return feature;
289
				}
290
			}
291
		}
292
 
293
		Feature feature = null;
294
 
295
		List<Slide> childrenSlides = slide.getChildrenSlides();
296
		if(childrenSlides != null) {
297
			for(Slide childSlide : childrenSlides) {
298
 
299
				feature = CreationUtils.getFeature(childSlide, featureDefinitionID);
300
				if(feature == null) {
301
					continue;
302
				}
303
				else {
304
					break;
305
				}
306
			}
307
		}
308
 
309
		return feature;
310
	}
479 rajveer 311
 
1153 rajveer 312
 
313
	/**
314
	 * Utility method to find out Slide object in Entity instance
315
	 * 
316
	 * @param entityID
317
	 * @param slideDefinitionID
318
	 * @return
319
	 * @throws Exception 
320
	 */
321
	public static Slide getSlide(long entityID, long slideDefinitionID) 
322
		throws Exception {
323
		Entity entity = CreationUtils.getEntity(entityID);
324
 
325
		List<Slide> slides = entity.getSlides();
326
 
327
		Slide resultSlide = null;
328
 
329
		if(slides != null) {
330
			for(Slide slide : slides) {
331
 
332
				if(slide.getSlideDefinitionID() == slideDefinitionID) {
333
					return slide;
334
				}
335
 
336
				resultSlide = CreationUtils.getSlide(slide, slideDefinitionID);
337
 
338
				if(resultSlide == null) {
339
					continue;
340
				}
341
				else {
342
					break;
343
				}
344
			}
345
		}
346
 
347
		return resultSlide;
348
	}
349
 
350
 
351
	/**
352
	 * 
353
	 * @param slide
354
	 * @param slideDefinitionID
355
	 * @return
356
	 */
357
	public static Slide getSlide(Slide slide, long slideDefinitionID) {
358
 
359
		List<Slide> childrenSlides = slide.getChildrenSlides();
360
 
361
		Slide resultSlide = null;
362
 
363
		if(childrenSlides != null) {
364
			for(Slide childSlide : childrenSlides) {
365
				if(childSlide.getSlideDefinitionID() == slideDefinitionID) {
366
					return childSlide;
367
				}
368
 
369
				resultSlide = CreationUtils.getSlide(childSlide, slideDefinitionID);
370
				if(resultSlide == null) {
371
					continue;
372
				}
373
				else {
374
					break;
375
				}
376
			}
377
		}
378
 
379
		return resultSlide;
380
	}
381
 
382
	/**
383
	 * Utility method to find out Feature object in Entity instance
384
	 * 
385
	 * @param entityID
386
	 * @param featureDefinitionID
387
	 * @return Feature
388
	 * @throws Exception 
389
	 */
390
	public static Feature getFeature(long entityID, long featureDefinitionID) 
391
		throws Exception {
392
		Entity entity = CreationUtils.getEntity(entityID);
393
 
394
		Feature feature = null;
395
 
396
		List<Slide> slides = entity.getSlides();
397
		for(Slide slide : slides) {
398
			feature = CreationUtils.getFeature(slide, featureDefinitionID);
399
 
400
			// Until all slides are searched
401
			if(feature == null) {
402
				continue;
403
			}
404
			else {
405
				break;
406
			}
407
		}
408
 
409
		return feature;
410
	}
411
 
412
 
413
	/**
414
	 * Returns expand form of entity object. All references are resolved into 
415
	 * corresponding detail object
416
	 * 
417
	 * @param entityID
418
	 * @return ExpandedEntity 
419
	 * @throws Exception 
420
	 */
421
	public static ExpandedEntity getExpandedEntity(long entityID) throws Exception {
422
		Entity entity = CreationUtils.getEntity(entityID);
423
		if(entity==null){
424
			System.out.println("Entity is null");
425
			return null;
426
		}
427
		System.out.println( entity.getCategoryID());
428
		ExpandedEntity expEntity = new ExpandedEntity(entity);
429
		return expEntity;
430
	}
431
 
432
 
433
	/**
434
	 * Returns list of borrowed slides
435
	 * 
436
	 * @param entity
437
	 * @return list of borrowed slides
438
	 */
439
	public static List<Slide> getBorrowedSlides(Entity entity) {
440
		List<Slide> borrowedSlides = new ArrayList<Slide>();
441
 
442
		List<Slide> slides = entity.getSlides();
443
 
444
		for(Slide slide : slides) {
445
			if(slide.isBorrowed()) {
446
				borrowedSlides.add(slide);
447
			}
448
		}
449
 
450
		return borrowedSlides;
451
	}
452
 
453
	/**
454
	 * Returns first parent slide with matching label
455
	 * 
456
	 * @param expEntity2
457
	 * @param slideLabel
458
	 * @return Null if not found
459
	 */
460
	public static ExpandedSlide getParentExpandedSlide(ExpandedEntity expEntity2, String slideLabel) {
461
 
462
		ExpandedSlide matchingSlide = null;
463
		List<ExpandedSlide> expSlides = expEntity2.getExpandedSlides();
464
 
465
		for(ExpandedSlide expSlide : expSlides) {
466
			if(expSlide.getSlideDefinition().getLabel().equals(slideLabel)) {
467
				matchingSlide = expSlide;
468
				break;
469
			}
470
		}
471
 
472
		return matchingSlide;
473
	}
474
 
479 rajveer 475
}