Subversion Repositories SmartDukaan

Rev

Rev 7286 | Rev 8749 | 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;
7286 amit.gupta 5
import in.shop2020.metamodel.core.ExpertReview;
6
import in.shop2020.metamodel.core.ExpertReviewSource;
1153 rajveer 7
import in.shop2020.metamodel.core.Feature;
2275 rajveer 8
import in.shop2020.metamodel.core.Helpdoc;
1153 rajveer 9
import in.shop2020.metamodel.core.Slide;
7286 amit.gupta 10
import in.shop2020.metamodel.core.SpecialPage;
1165 rajveer 11
import in.shop2020.metamodel.definitions.Catalog;
12
import in.shop2020.metamodel.definitions.Category;
13
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
1050 rajveer 14
import in.shop2020.storage.bdb.StorageManager;
1584 chandransh 15
import in.shop2020.util.Utils;
479 rajveer 16
 
1584 chandransh 17
import java.io.File;
479 rajveer 18
import java.io.PrintWriter;
19
import java.io.StringWriter;
20
import java.io.Writer;
1153 rajveer 21
import java.util.ArrayList;
1050 rajveer 22
import java.util.Collection;
7286 amit.gupta 23
import java.util.Date;
24
import java.util.HashMap;
25
import java.util.HashSet;
479 rajveer 26
import java.util.List;
27
import java.util.Map;
7286 amit.gupta 28
import java.util.Set;
479 rajveer 29
 
30
/**
1154 rajveer 31
 * All storage and retrival utility methods
1050 rajveer 32
 * @author rajveer
479 rajveer 33
 *
34
 */
35
public class CreationUtils {
1050 rajveer 36
 
37
	private static final String LEARNED_BULLETS = "LEARNED_BULLETS";
38
	private static final String DEFAULT_ENTITY_SCORES = "DEFAULT_ENTITY_SCORES";
39
	private static final String FACET_VALUES = "FACET_VALUES";
40
	private static final String SLIDE_SCORES = "SLIDE_SCORES";
8024 amit.gupta 41
	private static final String CUSTOM_SLIDE_SCORES = "CUSTOM_SLIDE_SCORES";
1443 rajveer 42
	private static final String CONTENT_GENERATION_TIME = "CONTENT_GENERATION_TIME";
2094 rajveer 43
	private static final String INCONSISTENT_ENTITIES = "INCONSISTENT_ENTITIES";
2471 rajveer 44
	private static final String HELPDOC_ENTITYIDS = "HELPDOC_ENTITYIDS";
2651 rajveer 45
	private static final String RELATED_ACCESSORIES = "RELATED_ACCESSORIES";
2733 rajveer 46
	private static final String COMPARISON_STATS = "COMPARISON_STATS";
5600 amit.gupta 47
	private static final String SEARCH_STATS = "SEARCH_STATS";
5074 amit.gupta 48
	private static final String SYNONYMS = "SYNONYMS";
7286 amit.gupta 49
	private static final String EXPERT_REVIEWS = "EXPERT_REVIEWS";
50
	private static final String EXPERT_REVIEW_SOURCES = "EXPERT_REVIEW_SOURCES";
2275 rajveer 51
	private static long helpDocId;
2838 mandeep.dh 52
    private static long specialPageId;
479 rajveer 53
 
54
	/**
55
	 * 
1050 rajveer 56
	 * @param aThrowable
479 rajveer 57
	 * @return
58
	 */
1050 rajveer 59
	public static String getStackTrace(Throwable aThrowable) {
60
	    final Writer result = new StringWriter();
61
	    final PrintWriter printWriter = new PrintWriter(result);
62
	    aThrowable.printStackTrace(printWriter);
63
	    return result.toString();
479 rajveer 64
	}
65
 
2094 rajveer 66
    /**
67
     * 
68
     * @param entity
69
     * @throws Exception
70
     */
71
    public static void storeInconsistentEntities(List<Long> inconsistentEntities) throws Exception {
72
        StorageManager.getStorageManager().storeDataObject(CreationUtils.INCONSISTENT_ENTITIES, inconsistentEntities);      
73
    }
74
 
75
    /**
76
     * 
77
     * @return
78
     * @throws Exception
79
     */
80
    @SuppressWarnings("unchecked")
81
    public static List<Long> getInconsistentEntities() throws Exception {
82
        return (List<Long>) StorageManager.getStorageManager().getDataObject(CreationUtils.INCONSISTENT_ENTITIES);       
83
    }
84
 
85
    /**
86
     * 
87
     * @param entity
88
     * @throws Exception
89
     */
90
    public static void storeLearnedBullets(Map<Long, List<ExpandedBullet>> learnedBullets) throws Exception {
91
        StorageManager.getStorageManager().storeDataObject(CreationUtils.LEARNED_BULLETS, learnedBullets);      
92
    }
93
 
94
    /**
95
     * 
96
     * @return
97
     * @throws Exception
98
     */
99
    @SuppressWarnings("unchecked")
100
    public static Map<Long, List<ExpandedBullet>> getLearnedBullets() throws Exception {
101
        return (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);       
102
    }
103
 
2471 rajveer 104
    /**
105
     * 
106
     * @param entity
107
     * @throws Exception
108
     */
109
    public static void storeHelpdocEntityIds(Map<Long, List<Long>> helpdocEntityIds) throws Exception {
110
        StorageManager.getStorageManager().storeDataObject(CreationUtils.HELPDOC_ENTITYIDS, helpdocEntityIds);      
111
    }
112
 
113
    /**
114
     * 
115
     * @return
116
     * @throws Exception
117
     */
118
    @SuppressWarnings("unchecked")
119
    public static Map<Long, List<Long>> getHelpdocEntityIds() throws Exception {
120
        return (Map<Long, List<Long>>) StorageManager.getStorageManager().getDataObject(CreationUtils.HELPDOC_ENTITYIDS);       
121
    }
122
 
2651 rajveer 123
 
124
    /**
125
     * 
126
     * @param entity
127
     * @throws Exception
128
     */
129
    public static void storeRelatedAccessories(Map<Long, Map<Long, List<Long>>> relatedAccessories) throws Exception {
130
        StorageManager.getStorageManager().storeDataObject(CreationUtils.RELATED_ACCESSORIES, relatedAccessories);      
131
    }
2471 rajveer 132
 
5074 amit.gupta 133
    /**
134
     * 
135
     * @param Synonym that contains map of brand/category name as keys and their synonyms as values. 
136
     * @throws Exception
137
     */
138
    @SuppressWarnings("unchecked")
139
    public static Map<String, Map<String, String>> getSynonyms() throws Exception {
140
    	return (Map<String, Map<String,String>>)StorageManager.getStorageManager().getDataObject(CreationUtils.SYNONYMS);      
141
    }
142
 
143
    /**
144
     * 
145
     * @param Synonym that contains map of brand/category name as keys and their synonyms as values. 
146
     * @throws Exception
147
     */
148
    public static void storeSynonyms(Map<String, Map<String, String>> synonyms) throws Exception {
149
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.SYNONYMS, synonyms);      
150
    }
151
 
7286 amit.gupta 152
 
153
    /**
154
     * 
155
     * @param Synonym that contains map of brand/category name as keys and their synonyms as values. 
156
     * @throws Exception
157
     */
158
    @SuppressWarnings("unchecked")
159
    public static Map<Long, List<ExpertReview>> getExpertReviews() throws Exception {
160
    	return (Map<Long, List<ExpertReview>>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEWS);      
161
    }
162
 
163
    /**
164
     * 
165
     * @param Synonym that contains map of brand/category name as keys and their synonyms as values. 
166
     * @throws Exception
167
     */
168
    @SuppressWarnings("unchecked")
169
    public static List<ExpertReview> getExpertReviewByEntity(long entityId) throws Exception {
170
    	return ((Map<Long, List<ExpertReview>>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEWS)).get(entityId);      
171
    }
172
 
173
    public static Set<ExpertReviewSource> getExpertReviewSources() throws Exception {
174
    	return (Set<ExpertReviewSource>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEW_SOURCES);      
175
    }
176
 
177
    public static void addExpertReviewSource(ExpertReviewSource expertReviewSource) throws Exception {
178
    	Set<ExpertReviewSource> expertReviewSources = (Set<ExpertReviewSource>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEW_SOURCES);
179
    	if(expertReviewSources == null){
180
    		expertReviewSources = new HashSet<ExpertReviewSource>();
181
    	}
182
    	expertReviewSources.add(expertReviewSource);
183
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.EXPERT_REVIEW_SOURCES, expertReviewSources);      
184
 
185
    }
186
 
187
    public static void deleteExpertReviewSource(ExpertReviewSource expertReviewSource) throws Exception {
188
    	Set<ExpertReviewSource> expertReviewSources = (Set<ExpertReviewSource>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEW_SOURCES);
189
    	expertReviewSources.remove(expertReviewSource);
190
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.EXPERT_REVIEW_SOURCES, expertReviewSources);      
191
 
192
    }
193
 
194
    /**
195
     * 
196
     * @param Synonym that contains map of brand/category name as keys and their synonyms as values. 
197
     * @throws Exception
198
     */
199
    public static void storeExpertReview(long entityId, ExpertReview expertReview) throws Exception {
200
    	Map<Long, List<ExpertReview>> expertReviewMap = (Map<Long, List<ExpertReview>>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEWS);
201
    	if(expertReviewMap == null){
202
    		expertReviewMap = new HashMap<Long, List<ExpertReview>>();
203
    	}
204
    	EntityState es = CreationUtils.getEntityState(entityId);
205
    	es.setMerkedReadyOn(new Date());
206
    	CreationUtils.updateEntityState(es);
207
    	List<ExpertReview> er  = expertReviewMap.get(entityId);
208
    	if(er == null) {
209
    		er = new ArrayList<ExpertReview>();
210
    		expertReviewMap.put(entityId, er);
211
    	}
212
    	er.add(expertReview);
213
 
214
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.EXPERT_REVIEWS, expertReviewMap);      
215
    }
216
 
217
    public static void storeExpertReview(long entityId, List<ExpertReview> expertReviews) throws Exception {
218
    	Map<Long, List<ExpertReview>> expertReviewMap = (Map<Long, List<ExpertReview>>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEWS);
219
    	expertReviewMap.put(entityId, expertReviews);
220
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.EXPERT_REVIEWS, expertReviewMap);      
221
    }
222
 
223
    public static void deleteExpertReview(long entityId, int index) throws Exception {
224
    	Map<Long, List<ExpertReview>> expertReviewMap = (Map<Long, List<ExpertReview>>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEWS);
225
    	List<ExpertReview> er  = expertReviewMap.get(entityId);
226
    	er.remove(index);
227
    	EntityState es = CreationUtils.getEntityState(entityId);
228
    	es.setMerkedReadyOn(new Date());
229
    	CreationUtils.updateEntityState(es);
230
 
231
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.EXPERT_REVIEWS, expertReviewMap);      
232
    }
233
 
234
    /**
235
     * 
236
     * @param Synonym that contains map of brand/category name as keys and their synonyms as values. 
237
     * @throws Exception
238
     */
239
    public static void deleteExpertReviews(long entityId) throws Exception {
240
    	Map<Long, List<ExpertReview>> expertReviewMap = (Map<Long, List<ExpertReview>>)StorageManager.getStorageManager().getDataObject(CreationUtils.EXPERT_REVIEWS);
241
    	expertReviewMap.remove(entityId);
242
    	EntityState es = CreationUtils.getEntityState(entityId);
243
    	es.setMerkedReadyOn(new Date());
244
    	CreationUtils.updateEntityState(es);
245
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.EXPERT_REVIEWS, expertReviewMap);      
246
    }
247
 
2651 rajveer 248
 
249
    /**
250
     * 
251
     * @return
252
     * @throws Exception
253
     */
254
    @SuppressWarnings("unchecked")
255
    public static Map<Long, Map<Long, List<Long>>> getRelatedAccessories() throws Exception {
256
        return (Map<Long, Map<Long, List<Long>>>) StorageManager.getStorageManager().getDataObject(CreationUtils.RELATED_ACCESSORIES);       
257
    }
258
 
2733 rajveer 259
 
260
    /**
261
     * 
262
     * @param entity
263
     * @throws Exception
264
     */
265
    public static void storeComparisonStats(Map<Long, Map<Long, Long>> comparisonStats) throws Exception {
266
        StorageManager.getStorageManager().storeDataObject(CreationUtils.COMPARISON_STATS, comparisonStats);      
267
    }
2651 rajveer 268
 
2733 rajveer 269
 
270
    /**
271
     * 
272
     * @return
273
     * @throws Exception
274
     */
275
    @SuppressWarnings("unchecked")
276
    public static Map<Long, Map<Long, Long>> getComparisonStats() throws Exception {
277
        return (Map<Long, Map<Long, Long>>) StorageManager.getStorageManager().getDataObject(CreationUtils.COMPARISON_STATS);       
278
    }
5600 amit.gupta 279
 
280
    /**
281
     * 
282
     * @param entity
283
     * @throws Exception
284
     */
285
    public static void storeSearchStats(String date, List<String> searchStats) throws Exception {
286
    	StorageManager.getStorageManager().storeDataObject(CreationUtils.SEARCH_STATS + date, searchStats);      
287
    }
2733 rajveer 288
 
5600 amit.gupta 289
    /**
290
     * 
291
     * @param entity
292
     * @throws Exception
293
     */
294
    public static void deleteSearchStats(String date) throws Exception {
295
    	StorageManager.getStorageManager().deleteDataObject(CreationUtils.SEARCH_STATS + date);      
296
    }
297
 
298
 
299
    /**
300
     * 
301
     * @return
302
     * @throws Exception
303
     */
304
    @SuppressWarnings("unchecked")
305
    public static List<String> getSearchStats(String date) throws Exception {
306
    	return (List<String>) StorageManager.getStorageManager().getDataObject(CreationUtils.SEARCH_STATS + date);       
307
    }
308
 
479 rajveer 309
	/**
310
	 * 
1154 rajveer 311
	 * @param featureDefinitionID
312
	 * @return
313
	 * @throws Exception
314
	 */
1050 rajveer 315
	@SuppressWarnings("unchecked")
316
	public static List<ExpandedBullet> getLearnedBullets(long featureDefinitionID) throws Exception {
317
		Map<Long, List<ExpandedBullet>> learnedBullets = (Map<Long, List<ExpandedBullet>>) StorageManager.getStorageManager().getDataObject(CreationUtils.LEARNED_BULLETS);
318
		if(learnedBullets==null){
319
			return null;
479 rajveer 320
		}
1050 rajveer 321
		return learnedBullets.get(featureDefinitionID);
479 rajveer 322
	}
1050 rajveer 323
 
1154 rajveer 324
	/**
325
	 * 
326
	 * @param facetIDFacetValues
327
	 * @throws Exception
328
	 */
1050 rajveer 329
	public static void storeFacetValues(Map<Long, List<String>> facetIDFacetValues) throws Exception {
330
		StorageManager.getStorageManager().storeDataObject(CreationUtils.FACET_VALUES, facetIDFacetValues);
479 rajveer 331
	}
1050 rajveer 332
 
1154 rajveer 333
	/**
334
	 * 
335
	 * @return
336
	 * @throws Exception
337
	 */
1050 rajveer 338
	@SuppressWarnings("unchecked")
339
	public static Map<Long, List<String>>  getFacetValues() throws Exception {
340
		return (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
479 rajveer 341
	}
342
 
1154 rajveer 343
	/**
344
	 * 
345
	 * @param facetDefinitionID
346
	 * @return
347
	 * @throws Exception
348
	 */
1050 rajveer 349
	@SuppressWarnings("unchecked")
350
	public static List<String>  getFacetValues(long facetDefinitionID) throws Exception {
351
		Map<Long, List<String>> facetValues = (Map<Long, List<String>>) StorageManager.getStorageManager().getDataObject(CreationUtils.FACET_VALUES);
352
		return facetValues.get(facetDefinitionID);
479 rajveer 353
	}
1050 rajveer 354
 
1154 rajveer 355
	/**
356
	 * 
357
	 * @param slideScoresByEntity
358
	 * @throws Exception
359
	 */
1929 rajveer 360
	public static void storeSlideScores( Map<Long, Map<Long, Double>> slideScoresByEntity) throws Exception {
1050 rajveer 361
		StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES, slideScoresByEntity);
362
	}
363
 
1154 rajveer 364
	/**
365
	 * 
366
	 * @return
367
	 * @throws Exception
368
	 */
479 rajveer 369
	@SuppressWarnings("unchecked")
1929 rajveer 370
	public static Map<Long, Map<Long, Double>> getSlideScores() throws Exception {
371
		return (Map<Long, Map<Long, Double>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
479 rajveer 372
	}
373
 
1154 rajveer 374
	/**
375
	 * 
376
	 * @param entityID
377
	 * @return
378
	 * @throws Exception
379
	 */
1050 rajveer 380
	@SuppressWarnings("unchecked")
1929 rajveer 381
	public static Map<Long, Double> getSlideComparisonScores(long entityID) throws Exception{
382
		Map<Long, Map<Long, Double>> slideScores = (Map<Long, Map<Long, Double>>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES);
1050 rajveer 383
		return slideScores.get(entityID);
479 rajveer 384
	}
385
 
8024 amit.gupta 386
	/**
387
	 * 
388
	 * @param entityID
389
	 * @return
390
	 * @throws Exception
391
	 */
392
	@SuppressWarnings("unchecked")
393
	public static Map<Long, Double> getCustomSlideComparisonScores(long entityID) throws Exception{
394
		return  (Map<Long, Double>) StorageManager.getStorageManager().getDataObject(CreationUtils.SLIDE_SCORES + entityID);
395
	}
1050 rajveer 396
 
1154 rajveer 397
	/**
398
	 * 
8024 amit.gupta 399
	 * @param entityID
400
	 * @return
401
	 * @throws Exception
402
	 */
403
	public static void storeCustomSlideComparisonScores(long entityID, Map<Long, Double> customSlideScores) throws Exception{
404
		StorageManager.getStorageManager().storeDataObject(CreationUtils.SLIDE_SCORES + entityID, customSlideScores);
405
	}
406
 
407
 
408
	/**
409
	 * 
1154 rajveer 410
	 * @param finalScoreByEntityID
411
	 * @throws Exception
412
	 */
1929 rajveer 413
	public static void storeDefaultEntityScores(Map<Long, Double> finalScoreByEntityID) throws Exception {
1050 rajveer 414
		StorageManager.getStorageManager().storeDataObject(CreationUtils.DEFAULT_ENTITY_SCORES, finalScoreByEntityID);
479 rajveer 415
	}
416
 
1154 rajveer 417
	/**
418
	 * 
419
	 * @return
420
	 * @throws Exception
421
	 */
1050 rajveer 422
	@SuppressWarnings("unchecked")
423
	public static Map<Long, Integer> getDefaultEntityScores() throws Exception {
424
		return (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
479 rajveer 425
	}
426
 
1154 rajveer 427
	/**
428
	 * 
429
	 * @param entityID
430
	 * @return
431
	 * @throws Exception
432
	 */
1050 rajveer 433
	@SuppressWarnings("unchecked")
434
	public static Integer getEntityComparisonScores(long entityID) throws Exception {
435
		Map<Long, Integer> scoresMap =  (Map<Long, Integer>) StorageManager.getStorageManager().getDataObject(CreationUtils.DEFAULT_ENTITY_SCORES);
436
		Integer objScore = scoresMap.get(entityID);
437
		if(objScore==null){
438
			return -1;
479 rajveer 439
		}
1050 rajveer 440
		return objScore;
479 rajveer 441
	}
442
 
443
 
1443 rajveer 444
	/**
445
	 * 
446
	 * @return
447
	 * @throws Exception
448
	 */
449
	public static Long getLastContentGenerationTime() throws Exception {
450
		return (Long) StorageManager.getStorageManager().getDataObject(CreationUtils.CONTENT_GENERATION_TIME);
451
	}
479 rajveer 452
 
453
	/**
1443 rajveer 454
	 * 
455
	 * @param storageTime
456
	 * @throws Exception
457
	 */
458
	public static void storeLastContentGenerationTime(Long storageTime) throws Exception {
459
		StorageManager.getStorageManager().storeDataObject(CreationUtils.CONTENT_GENERATION_TIME, storageTime);
460
	}
461
	/**
1050 rajveer 462
	 * Resolves Entity ID into Entity object
479 rajveer 463
	 * 
464
	 * @param entityID
1050 rajveer 465
	 * @return Entity
466
	 * @throws Exception 
479 rajveer 467
	 */
1050 rajveer 468
	public static Entity getEntity(long entityID) throws Exception{
469
		return StorageManager.getStorageManager().getEntity(entityID);
479 rajveer 470
	}
471
 
1154 rajveer 472
	/**
473
	 * 
474
	 * @param entity
475
	 * @throws Exception
476
	 */
1050 rajveer 477
	public static void updateEntity(Entity entity) throws Exception {
2471 rajveer 478
		//FIXME This should not happen here. 
1379 rajveer 479
		entity.reorderSlides(entity.getSlideSequence());
2471 rajveer 480
		CreationUtils.learn(entity);
1050 rajveer 481
		StorageManager.getStorageManager().updateEntity(entity);
1226 rajveer 482
 
1379 rajveer 483
 
2471 rajveer 484
 
479 rajveer 485
	}
486
 
1154 rajveer 487
	/**
488
	 * 
489
	 * @param entity
490
	 * @param entityMetadata
491
	 * @throws Exception
492
	 */
1050 rajveer 493
	public static void createEntity(Entity entity, EntityState entityMetadata) throws Exception {
1584 chandransh 494
		/*
495
		 * Creating media directory by default. Even if there is no media uploaded yet.
496
		 */
497
		String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + entity.getID();
498
		File mediaDir = new File(mediaDirPath);
499
		if(!mediaDir.exists()) {
500
			mediaDir.mkdir();
501
		}
1050 rajveer 502
		StorageManager.getStorageManager().createEntity(entity, entityMetadata);
479 rajveer 503
	}
1050 rajveer 504
 
1154 rajveer 505
	/**
506
	 * 
507
	 * @param entityId
508
	 * @throws Exception
509
	 */
1050 rajveer 510
	public static void deleteEntity(long entityId) throws Exception  {
511
		StorageManager.getStorageManager().deleteEntity(entityId);
479 rajveer 512
	}
513
 
1050 rajveer 514
 
1154 rajveer 515
	/**
516
	 * 
517
	 * @return
518
	 * @throws Exception
519
	 */
1050 rajveer 520
	public static Map<Long, EntityState> getEntitiesState() throws Exception {
521
		return StorageManager.getStorageManager().getEntitiesMetadata();
479 rajveer 522
	}
523
 
1154 rajveer 524
	/**
525
	 * 
526
	 * @param entityId
527
	 * @return
528
	 * @throws Exception
529
	 */
1050 rajveer 530
	public static EntityState getEntityState(long entityId) throws Exception {
531
		return StorageManager.getStorageManager().getEntityMetadata(entityId);
532
	}
479 rajveer 533
 
1154 rajveer 534
	/**
535
	 * 
536
	 * @param entityMetadata
537
	 * @throws Exception
538
	 */
1050 rajveer 539
	public static void updateEntityState(EntityState entityMetadata) throws Exception {
540
		StorageManager.getStorageManager().updateEntityMetadata(entityMetadata);
479 rajveer 541
	}
1050 rajveer 542
 
1154 rajveer 543
	/**
544
	 * 
545
	 * @param entity
546
	 * @throws Exception
547
	 */
1050 rajveer 548
	private static void learn(Entity entity) throws Exception{
549
		CN cn = new CN();
2471 rajveer 550
		cn.learn(entity);
551
		cn.learnHelpdocs(entity);
1050 rajveer 552
	}
553
 
1154 rajveer 554
	/**
555
	 * 
556
	 * @return
557
	 * @throws Exception
558
	 */
1050 rajveer 559
	public static Map<Long, Entity> getEntities() throws Exception {
560
		return StorageManager.getStorageManager().getEntities();
561
	}
562
 
1154 rajveer 563
	/**
564
	 * 
565
	 * @param categoryId
566
	 * @return
567
	 * @throws Exception
568
	 */
1050 rajveer 569
	public static Collection<Entity> getEntities(long categoryId) throws Exception {
570
		return StorageManager.getStorageManager().getEntitisByCategory(categoryId);
571
	}
1153 rajveer 572
 
2275 rajveer 573
 
574
	public static void storeHelpdoc(Helpdoc helpdoc) throws Exception {
575
		StorageManager.getStorageManager().updateHelpdoc(helpdoc);
576
	}
577
 
578
	public static Helpdoc getHelpdoc(Long helpdocId) throws Exception {
579
		return StorageManager.getStorageManager().getHelpdoc(helpdocId);
580
	}
581
 
582
	public static Map<Long, Helpdoc> getHelpdocs() throws Exception {
583
		return StorageManager.getStorageManager().getHelpdocs();
584
	}
3485 rajveer 585
 
586
	public static void deleteHelpdoc(Long helpdocId) throws Exception {
587
		StorageManager.getStorageManager().deleteHelpdoc(helpdocId);
588
	}
589
 
2275 rajveer 590
	public static long getNewHelpdocId() {
591
		if(helpDocId == 0){
592
			helpDocId = 200000;
593
			Map<Long, Helpdoc> helpdocs = StorageManager.getStorageManager().getHelpdocs();
594
			if(helpdocs != null){
595
				for(long id : StorageManager.getStorageManager().getHelpdocs().keySet()){
596
					if(helpDocId <= id){
597
						helpDocId = id;
598
					}
599
				}
600
			}	
601
		}
602
		return ++helpDocId;
603
	}
604
 
605
 
1153 rajveer 606
	/**
607
	 * 
608
	 * @param slide
609
	 * @param featureDefinitionID
610
	 * @return Feature
611
	 */
612
	public static Feature getFeature(Slide slide, long featureDefinitionID) {
613
		List<Feature> features = slide.getFeatures();
614
 
615
		if(features != null) {
616
			for(Feature feature : features) {
617
				if(feature.getFeatureDefinitionID() == featureDefinitionID) {
618
					return feature;
619
				}
620
			}
621
		}
622
 
623
		Feature feature = null;
624
 
625
		List<Slide> childrenSlides = slide.getChildrenSlides();
626
		if(childrenSlides != null) {
627
			for(Slide childSlide : childrenSlides) {
628
 
629
				feature = CreationUtils.getFeature(childSlide, featureDefinitionID);
630
				if(feature == null) {
631
					continue;
632
				}
633
				else {
634
					break;
635
				}
636
			}
637
		}
638
 
639
		return feature;
640
	}
479 rajveer 641
 
1153 rajveer 642
 
643
	/**
644
	 * Utility method to find out Slide object in Entity instance
645
	 * 
646
	 * @param entityID
647
	 * @param slideDefinitionID
648
	 * @return
649
	 * @throws Exception 
650
	 */
651
	public static Slide getSlide(long entityID, long slideDefinitionID) 
652
		throws Exception {
653
		Entity entity = CreationUtils.getEntity(entityID);
654
 
655
		List<Slide> slides = entity.getSlides();
656
 
657
		Slide resultSlide = null;
658
 
659
		if(slides != null) {
660
			for(Slide slide : slides) {
661
 
662
				if(slide.getSlideDefinitionID() == slideDefinitionID) {
663
					return slide;
664
				}
665
 
666
				resultSlide = CreationUtils.getSlide(slide, slideDefinitionID);
667
 
668
				if(resultSlide == null) {
669
					continue;
670
				}
671
				else {
672
					break;
673
				}
674
			}
675
		}
676
 
677
		return resultSlide;
678
	}
679
 
680
 
681
	/**
682
	 * 
683
	 * @param slide
684
	 * @param slideDefinitionID
685
	 * @return
686
	 */
687
	public static Slide getSlide(Slide slide, long slideDefinitionID) {
688
 
689
		List<Slide> childrenSlides = slide.getChildrenSlides();
690
 
691
		Slide resultSlide = null;
692
 
693
		if(childrenSlides != null) {
694
			for(Slide childSlide : childrenSlides) {
695
				if(childSlide.getSlideDefinitionID() == slideDefinitionID) {
696
					return childSlide;
697
				}
698
 
699
				resultSlide = CreationUtils.getSlide(childSlide, slideDefinitionID);
700
				if(resultSlide == null) {
701
					continue;
702
				}
703
				else {
704
					break;
705
				}
706
			}
707
		}
708
 
709
		return resultSlide;
710
	}
711
 
712
	/**
713
	 * Utility method to find out Feature object in Entity instance
714
	 * 
715
	 * @param entityID
716
	 * @param featureDefinitionID
717
	 * @return Feature
718
	 * @throws Exception 
719
	 */
720
	public static Feature getFeature(long entityID, long featureDefinitionID) 
721
		throws Exception {
722
		Entity entity = CreationUtils.getEntity(entityID);
723
 
724
		Feature feature = null;
725
 
726
		List<Slide> slides = entity.getSlides();
727
		for(Slide slide : slides) {
728
			feature = CreationUtils.getFeature(slide, featureDefinitionID);
729
 
730
			// Until all slides are searched
731
			if(feature == null) {
732
				continue;
733
			}
734
			else {
735
				break;
736
			}
737
		}
738
 
739
		return feature;
740
	}
741
 
742
 
743
	/**
744
	 * Returns expand form of entity object. All references are resolved into 
745
	 * corresponding detail object
746
	 * 
747
	 * @param entityID
748
	 * @return ExpandedEntity 
749
	 * @throws Exception 
750
	 */
751
	public static ExpandedEntity getExpandedEntity(long entityID) throws Exception {
752
		Entity entity = CreationUtils.getEntity(entityID);
753
		if(entity==null){
754
			System.out.println("Entity is null");
755
			return null;
756
		}
757
		System.out.println( entity.getCategoryID());
758
		ExpandedEntity expEntity = new ExpandedEntity(entity);
759
		return expEntity;
760
	}
761
 
762
 
763
	/**
764
	 * Returns list of borrowed slides
765
	 * 
766
	 * @param entity
767
	 * @return list of borrowed slides
768
	 */
769
	public static List<Slide> getBorrowedSlides(Entity entity) {
770
		List<Slide> borrowedSlides = new ArrayList<Slide>();
771
 
772
		List<Slide> slides = entity.getSlides();
773
 
774
		for(Slide slide : slides) {
1165 rajveer 775
			if(CreationUtils.isBorrowedSlide(slide.getSlideDefinitionID(), entity.getCategoryID())) {
1153 rajveer 776
				borrowedSlides.add(slide);
777
			}
778
		}
779
 
780
		return borrowedSlides;
781
	}
782
 
783
	/**
784
	 * Returns first parent slide with matching label
785
	 * 
786
	 * @param expEntity2
787
	 * @param slideLabel
788
	 * @return Null if not found
789
	 */
790
	public static ExpandedSlide getParentExpandedSlide(ExpandedEntity expEntity2, String slideLabel) {
791
 
792
		ExpandedSlide matchingSlide = null;
793
		List<ExpandedSlide> expSlides = expEntity2.getExpandedSlides();
794
 
795
		for(ExpandedSlide expSlide : expSlides) {
796
			if(expSlide.getSlideDefinition().getLabel().equals(slideLabel)) {
797
				matchingSlide = expSlide;
798
				break;
799
			}
800
		}
801
 
802
		return matchingSlide;
803
	}
1165 rajveer 804
 
805
	/**
806
	 * 
807
	 * @param slideDefinitionID
808
	 * @param categoryID
809
	 * @return
810
	 */
811
	public static boolean isBorrowedSlide(long slideDefinitionID,long categoryID) {
1226 rajveer 812
		List<CategorySlideDefinition> slideDefinitions;
1165 rajveer 813
		try {
1226 rajveer 814
			slideDefinitions = Catalog.getInstance().getDefinitionsContainer().getCategorySlideDefinitions(categoryID);
815
			for(CategorySlideDefinition slideDef: slideDefinitions){
816
				if(slideDefinitionID == slideDef.getSlideDefintionID()){
817
					return false;
818
				}
1165 rajveer 819
			}
820
		} catch (Exception e) {
821
			// TODO Auto-generated catch block
822
			e.printStackTrace();
823
		}
1226 rajveer 824
		return true;
1165 rajveer 825
	}
826
 
827
	/**
828
	 * 
829
	 * @param slideDefinitionID
830
	 * @param categoryID
831
	 * @return
832
	 */
833
	public static long getBorrowedCategoryID(long slideDefinitionID, long categoryID) {
834
		try {
1226 rajveer 835
			List<CategorySlideDefinition> slideDefinitions = Catalog.getInstance().getDefinitionsContainer().getCategorySlideDefinitions(categoryID);
836
			for(CategorySlideDefinition slideDef: slideDefinitions){
837
				if(slideDefinitionID == slideDef.getSlideDefintionID()){
838
					return categoryID;
839
				}
840
			}
841
 
842
 
843
			List<Category> cats =  Catalog.getInstance().getDefinitionsContainer().getCategory(categoryID).getParentCategory().getChildrenCategory();
844
			for(Category cat: cats){
845
				if(cat.getID() == categoryID){
846
					continue;
847
				}else{
848
					List<CategorySlideDefinition> slideDefs = Catalog.getInstance().getDefinitionsContainer().getCategorySlideDefinitions(cat.getID());
849
					for(CategorySlideDefinition slideDef: slideDefs){
850
						if(slideDefinitionID == slideDef.getSlideDefintionID()){
1165 rajveer 851
							return cat.getID();
852
						}
853
					}
1226 rajveer 854
 
1165 rajveer 855
				}
856
			}
1226 rajveer 857
 
1165 rajveer 858
		} catch (Exception e) {
859
			// TODO Auto-generated catch block
860
			e.printStackTrace();
861
		}
1226 rajveer 862
		return categoryID;
1165 rajveer 863
	}
864
 
2768 mandeep.dh 865
	/**
2838 mandeep.dh 866
	 * Special page ids start from 300000. Here, we pick the last one used and return
2768 mandeep.dh 867
     * a value one more than it.
868
	 */
2838 mandeep.dh 869
    public static long getNewSpecialPageId() {
870
        if (specialPageId == 0) {
871
            specialPageId = 300000;
872
            Map<Long, SpecialPage> specialPages = StorageManager.getStorageManager().getSpecialPages();
873
            if (specialPages != null) {
874
                for (long id : StorageManager.getStorageManager().getSpecialPages().keySet()) {
875
                    if (specialPageId <= id) {
876
                        specialPageId = id;
2768 mandeep.dh 877
                    }
878
                }
879
            }
880
        }
881
 
2838 mandeep.dh 882
        return ++specialPageId;
2768 mandeep.dh 883
    }
884
 
885
    /**
2838 mandeep.dh 886
     * Updates a given special page object in database.
2768 mandeep.dh 887
     */
2838 mandeep.dh 888
    public static void storeSpecialPage(SpecialPage specialPage) {
889
        StorageManager.getStorageManager().updateSpecialPage(specialPage);        
2768 mandeep.dh 890
    }
891
 
892
    /**
2838 mandeep.dh 893
     * Looks up a special page given its Id from database and returns the same.
2768 mandeep.dh 894
     */
2838 mandeep.dh 895
    public static SpecialPage getSpecialPage(long specialPageId) {
896
        return StorageManager.getStorageManager().getSpecialPage(specialPageId);
2768 mandeep.dh 897
    }
898
 
899
    /**
2838 mandeep.dh 900
     * Returns all special pages in the database.
2768 mandeep.dh 901
     */
2838 mandeep.dh 902
    public static Map<Long, SpecialPage> getSpecialPages() {
903
        return StorageManager.getStorageManager().getSpecialPages();
2768 mandeep.dh 904
    }
3356 rajveer 905
 
906
    /**
907
     * delete the special pages from database.
908
     */
909
    public static void deleteSpecialPage(long specialPageId) {
910
        StorageManager.getStorageManager().deleteSpecialPage(specialPageId);
911
    }
479 rajveer 912
}