Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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