Subversion Repositories SmartDukaan

Rev

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