Subversion Repositories SmartDukaan

Rev

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