Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 shop2020 1
/**
2
 * 
3
 */
4
package in.shop2020.metamodel.util;
5
 
16 naveen 6
import in.shop2020.metamodel.definitions.BulletDefinition;
45 naveen 7
import in.shop2020.metamodel.definitions.Catalog;
10 shop2020 8
import in.shop2020.metamodel.definitions.Category;
57 naveen 9
import in.shop2020.metamodel.definitions.CategoryFacetDefinition;
10 shop2020 10
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
16 naveen 11
import in.shop2020.metamodel.definitions.CompositeDefinition;
12
import in.shop2020.metamodel.definitions.CompositePartDefinition;
13
import in.shop2020.metamodel.definitions.DatatypeDefinition;
45 naveen 14
import in.shop2020.metamodel.definitions.DefinitionsContainer;
10 shop2020 15
import in.shop2020.metamodel.definitions.EditorialImportance;
16 naveen 16
import in.shop2020.metamodel.definitions.EnumDefinition;
17
import in.shop2020.metamodel.definitions.EnumValue;
57 naveen 18
import in.shop2020.metamodel.definitions.FacetDefinition;
16 naveen 19
import in.shop2020.metamodel.definitions.FeatureDefinition;
61 naveen 20
import in.shop2020.metamodel.definitions.RuleDefinition;
16 naveen 21
import in.shop2020.metamodel.definitions.SlideDefinition;
22
import in.shop2020.metamodel.definitions.SlideFeatureDefinition;
10 shop2020 23
import in.shop2020.metamodel.definitions.Unit;
57 naveen 24
import in.shop2020.metamodel.jaxb.core.BulletDefinitionType;
25
import in.shop2020.metamodel.jaxb.core.CategorySlideDefinitionType;
26
import in.shop2020.metamodel.jaxb.core.CategoryType;
27
import in.shop2020.metamodel.jaxb.core.CompositeDefinitionType;
28
import in.shop2020.metamodel.jaxb.core.CompositePartDefinitionType;
29
import in.shop2020.metamodel.jaxb.core.DatatypeDefinitionType;
30
import in.shop2020.metamodel.jaxb.core.DefinitionType;
31
import in.shop2020.metamodel.jaxb.core.EditorialImportanceType;
32
import in.shop2020.metamodel.jaxb.core.EnumDefinitionType;
33
import in.shop2020.metamodel.jaxb.core.EnumValueType;
34
import in.shop2020.metamodel.jaxb.core.FeatureDefinitionType;
35
import in.shop2020.metamodel.jaxb.core.SlideDefinitionType;
36
import in.shop2020.metamodel.jaxb.core.SlideFeatureDefinitionType;
37
import in.shop2020.metamodel.jaxb.core.UnitType;
61 naveen 38
import in.shop2020.metamodel.jaxb.facets.CategoryFacetDefinitionType;
39
import in.shop2020.metamodel.jaxb.facets.FacetDefinitionType;
57 naveen 40
import in.shop2020.metamodel.jaxb.facets.IRDefinitionType;
61 naveen 41
import in.shop2020.metamodel.jaxb.facets.RuleDefinitionType;
64 naveen 42
import in.shop2020.util.DBUtils;
18 naveen 43
import in.shop2020.util.Utils;
10 shop2020 44
 
45
import java.io.FileInputStream;
16 naveen 46
import java.util.ArrayList;
10 shop2020 47
import java.util.HashMap;
48
import java.util.Iterator;
49
import java.util.List;
50
import java.util.Map;
24 naveen 51
import java.util.TreeMap;
10 shop2020 52
 
53
import javax.xml.bind.JAXBContext;
54
import javax.xml.bind.JAXBElement;
55
import javax.xml.bind.Unmarshaller;
56
 
57
import org.apache.commons.lang.ArrayUtils;
58
import org.apache.commons.lang.StringUtils;
59
 
60
/**
51 naveen 61
 * Command line utility that imports model properties defined in XML
62
 * 
17 naveen 63
 * MM - Meta-Model Tool
10 shop2020 64
 * 
51 naveen 65
 * It can be used to do following things
66
 * 
67
 * - Import
68
 * 	 - Datatype definitions
69
 * 	 - Feature definitions
70
 * 	 - Slide definitions
71
 * 	 - Categories
63 naveen 72
 *   - Facet Definition
73
 *   - Category Facet Definition
74
 *   - IR Data Rules
75
 *   - IR Meta-data Rules
51 naveen 76
 * 
77
 * - Show
78
 * 	 - Datatype definitions
79
 * 	 - Feature definitions
80
 * 	 - Slide definitions
81
 * 	 - Categories
63 naveen 82
 *   - Facet Definition
83
 *   - Category Facet Definition
84
 *   - IR Data Rules
85
 *   - IR Meta-data Rules
51 naveen 86
 * 
87
 * - Show all definition objects that define a category 
88
 * 
89
 * 
10 shop2020 90
 * @author naveen
91
 *
92
 */
93
public class MM {
49 naveen 94
	/**
95
	 * 
96
	 */
16 naveen 97
	public static final String DEFINITIONS_SRC_PATH = 
98
		"/home/naveen/workspace/eclipse/webapp/src/xml/model/";
34 naveen 99
 
43 naveen 100
	// LOCAL
49 naveen 101
	/**
102
	 * 
103
	 */
16 naveen 104
	public static final String DEFINITIONS_DB_PATH = 
26 naveen 105
		"/home/naveen/workspace/eclipse/db/definitions/";
49 naveen 106
 
107
	/**
108
	 * 
109
	 */
16 naveen 110
	public static final String ENTITIES_DB_PATH = 
26 naveen 111
		"/home/naveen/workspace/eclipse/db/entities/";
10 shop2020 112
 
34 naveen 113
	/** WEB
114
	public static final String DEFINITIONS_DB_PATH = 
115
		"/var/lib/tomcat6/webapps/shop2020/db/definitions/";
116
	public static final String ENTITIES_DB_PATH = 
117
		"/var/lib/tomcat6/webapps/shop2020/db/entities/";
118
	*/
43 naveen 119
 
120
	/** Validation tool
121
	public static final String DEFINITIONS_DB_PATH = 
122
		"./db/definitions/";
123
	public static final String ENTITIES_DB_PATH = 
124
		"./db/entities/";
125
	*/
34 naveen 126
 
49 naveen 127
	/**
128
	 * 
129
	 */
10 shop2020 130
	private String definitionSetName;
49 naveen 131
 
132
	/**
133
	 * 
134
	 */
10 shop2020 135
	private String srcFile;
49 naveen 136
 
137
	/**
138
	 * 
139
	 */
10 shop2020 140
	private String dbFile;
141
 
142
	/**
61 naveen 143
	 * Usage: MM [show|import|showcategory] [units|datatypedefinitions|featuredefinitions|slidedefinitions|categories|facetdefinitions|categoryfacetdefinitions|irdatarules|irmetadatarules] {Category ID}
51 naveen 144
	 * 
10 shop2020 145
	 * @param args
146
	 */
147
	public static void main(String[] args) throws Exception {
16 naveen 148
		String[] definitionSetNames = new String[] {"units", 
149
				"datatypedefinitions", "featuredefinitions", "slidedefinitions", 
61 naveen 150
				"categories", "facetdefinitions", "categoryfacetdefinitions", 
151
				"irdatarules", "irmetadatarules"};
10 shop2020 152
 
45 naveen 153
		String[] commands = new String[] {"show", "import", "showcategory"};
10 shop2020 154
 
16 naveen 155
		String usage = "Usage: MM ["+ StringUtils.join(commands, "|") +"] ["+ 
45 naveen 156
			StringUtils.join(definitionSetNames, "|") + "] {Category ID}";
16 naveen 157
 
10 shop2020 158
		if(args.length < 2) {
159
			System.out.println(usage);
160
			System.exit(-1);
161
		}
42 naveen 162
		Utils.info("MM "+ args[0] + " " + args[1]);
17 naveen 163
 
10 shop2020 164
		String inputCommand = args[0];
165
 
166
		if(!ArrayUtils.contains(commands, inputCommand)) {
167
			System.out.println(usage);
168
			System.exit(-1);
169
		}
170
 
45 naveen 171
		if(inputCommand.equals("show") || inputCommand.endsWith("import")) {
172
			String inputDefinitionSet = args[1];
173
			if(!ArrayUtils.contains(definitionSetNames, inputDefinitionSet)) {
174
				System.out.println(usage);
175
				System.exit(-1);
176
			}
177
 
178
			MM mm = new MM(inputDefinitionSet);
179
			if(inputCommand.equals("import")) {
180
				mm.importDefinitionSet();
181
			}
182
			else if(inputCommand.equals("show")) {
183
				mm.showDefinitionSet();
184
			}
10 shop2020 185
		}
186
 
45 naveen 187
		if(inputCommand.equals("showcategory")) {
188
			String categoryID = args[1];
189
			long catID = 0;
190
			try {
191
				catID = Long.parseLong(categoryID);
192
			} 
193
			catch (NumberFormatException nfe) {
194
				System.out.println("Invalid category ID");
195
				System.exit(-1);
196
			}
197
 
198
			MM mm = new MM();
199
			mm.showCategory(catID);
10 shop2020 200
		}
201
	}
202
 
203
	/**
204
	 * 
45 naveen 205
	 */
206
	public MM() {
207
	}
208
 
209
	/**
210
	 * 
10 shop2020 211
	 * @param definitionSet
212
	 */
213
	public MM(String definitionSetName) {
214
		this.definitionSetName = definitionSetName;
215
		this.srcFile = DEFINITIONS_SRC_PATH + this.definitionSetName + ".xml";
216
		this.dbFile = DEFINITIONS_DB_PATH + this.definitionSetName + ".ser";
217
	}
218
 
219
	/**
220
	 * 
45 naveen 221
	 * @param categoryID
222
	 * @throws Exception 
10 shop2020 223
	 */
45 naveen 224
	public void showCategory(long categoryID) throws Exception {
225
		DefinitionsContainer defs = 
226
			Catalog.getInstance().getDefinitionsContainer();
227
 
228
		ExpandedCategory expCategory = defs.getExpandedCategory(categoryID);
229
 
230
		Utils.info(expCategory);
231
	}
232
 
233
	/**
234
	 * 
235
	 */
10 shop2020 236
	@SuppressWarnings("unchecked")
237
	public void importDefinitionSet() throws Exception {
57 naveen 238
		String type = "core";
239
		if(this.definitionSetName.equals("facetdefinitions") || 
61 naveen 240
				this.definitionSetName.equals("categoryfacetdefinitions") ||
241
				this.definitionSetName.equals("irdatarules") || 
242
						this.definitionSetName.equals("irmetadatarules")) {
57 naveen 243
			type = "facets";
244
		}
245
 
246
		Object oDefType = this.unmarshallSrcFile(this.srcFile, type);
247
		DefinitionType defType = null;
248
		IRDefinitionType irDefType = null;
249
 
250
		if(type.equals("core")) {
251
			defType = (DefinitionType)oDefType;
252
		} 
253
		else if (type.equals("facets")) {
254
			irDefType = (IRDefinitionType)oDefType;
255
		}
256
 
16 naveen 257
		Object objectToStore = null;
10 shop2020 258
 
57 naveen 259
		// CORE
260
 
10 shop2020 261
		// Units
262
		if(this.definitionSetName.equals("units")) {
263
			Map unitsMap = this.convertUnits(defType);
16 naveen 264
			objectToStore = unitsMap;
10 shop2020 265
		}
266
 
267
		// Categories
268
		else if(this.definitionSetName.equals("categories")) {
269
			Map<Long, Category> categoriesMap = new HashMap<Long, Category>();
270
			Category root = this.convertCategories(defType, categoriesMap);
42 naveen 271
			//Utils.info(categoriesMap);
272
			//Utils.info(root);
10 shop2020 273
			//System.exit(0);
274
 
275
			// Tree
16 naveen 276
			String categoryTreeDBFile = DEFINITIONS_DB_PATH + "categorytree" + 
277
				".ser";
278
 
10 shop2020 279
			if(root != null) {
17 naveen 280
				DBUtils.store(root, categoryTreeDBFile);
10 shop2020 281
			}
282
 
283
			// Map
16 naveen 284
			objectToStore = categoriesMap;
10 shop2020 285
		}
16 naveen 286
 
287
		// Datatype Definitions
288
		else if(this.definitionSetName.equals("datatypedefinitions")) {
24 naveen 289
			Map<Long, DatatypeDefinition> datatypeDefsMap = 
290
				this.convertDatatypeDefinition(defType);
16 naveen 291
 
24 naveen 292
			// Store enum value id to value map separately
293
			String enumValueDBFile = DEFINITIONS_DB_PATH + "enumvalues" + 
294
				".ser";
295
 
296
			// RE-VISIT - May not be optimal
297
			Map<Long, EnumValue> enumValuesMap = new TreeMap<Long, EnumValue>();
298
			for(Long datatypeDefID : datatypeDefsMap.keySet()) {
299
				DatatypeDefinition datatypeDef = 
300
					datatypeDefsMap.get(datatypeDefID);
301
 
302
				if(datatypeDef instanceof EnumDefinition) {
303
					List<EnumValue> enumValues = 
304
						((EnumDefinition) datatypeDef).getEnumValues();
305
 
306
					for(EnumValue enumValue : enumValues) {
307
						enumValuesMap.put(new Long(enumValue.getID()), 
308
								enumValue);
309
					}
310
				}
311
			}
42 naveen 312
			Utils.info("enumValuesMap=" + enumValuesMap);
24 naveen 313
			DBUtils.store(enumValuesMap, enumValueDBFile);
314
 
16 naveen 315
			objectToStore = datatypeDefsMap;
316
		}
317
 
318
		// Feature Definitions
319
		else if(this.definitionSetName.equals("featuredefinitions")) {
320
			Map featureDefsMap = this.convertFeatureDefinitions(defType);
321
 
322
			objectToStore = featureDefsMap;
323
		}
324
 
325
		// Slide Definitions
326
		else if(this.definitionSetName.equals("slidedefinitions")) {
327
			Map slideDefsMap = this.convertSlideDefinitions(defType);
328
 
329
			objectToStore = slideDefsMap;
330
		}
331
 
57 naveen 332
		// FACETS
333
 
334
		// Facet Definitions
335
		else if(this.definitionSetName.equals("facetdefinitions")) {
336
			Map facetDefsMap = this.convertFacetDefinitions(irDefType);
337
 
338
			objectToStore = facetDefsMap;
339
		}
340
 
341
 
342
		// Category Facet Definitions
343
		else if(this.definitionSetName.equals("categoryfacetdefinitions")) {
344
			Map categoryFacetDefsMap = 
345
				this.convertCategoryFacetDefinitions(irDefType);
346
 
347
			objectToStore = categoryFacetDefsMap;
348
		}
349
 
61 naveen 350
		// IR Data Rules
351
		else if(this.definitionSetName.equals("irdatarules")) {
352
			Map irdataRuleDefsMap = 
353
				this.convertRuleDefinitions(irDefType);
354
 
355
			objectToStore = irdataRuleDefsMap;
356
		}
357
 
358
		// IR Meta-Data Rules
359
		else if(this.definitionSetName.equals("irmetadatarules")) {
360
			Map irmetadataRuleDefsMap = 
361
				this.convertRuleDefinitions(irDefType);
362
 
363
			objectToStore = irmetadataRuleDefsMap;
364
		}
365
 
16 naveen 366
		if(objectToStore != null) {
17 naveen 367
			DBUtils.store(objectToStore, this.dbFile);
16 naveen 368
		}
10 shop2020 369
	}
370
 
371
	/**
372
	 * 
373
	 */
374
	public void showDefinitionSet() throws Exception {
17 naveen 375
		Object obj = DBUtils.read(this.dbFile);
42 naveen 376
		Utils.info(obj.toString());
10 shop2020 377
 
378
		if(this.definitionSetName.equals("categories")) {
16 naveen 379
			String categoryTreeDBFile = DEFINITIONS_DB_PATH + "categorytree" + 
380
				".ser";
381
 
17 naveen 382
			Category root = (Category)DBUtils.read(categoryTreeDBFile);
42 naveen 383
			Utils.info(root.toString());
384
			//Utils.info("root.getChildrenCategory().size: " + 
18 naveen 385
			//		root.getChildrenCategory().size());
10 shop2020 386
		}
387
	}
388
 
389
	/**
390
	 * 
57 naveen 391
	 * @param defType
392
	 * @return
393
	 */
61 naveen 394
	private Map<Long, RuleDefinition> convertRuleDefinitions(
395
			IRDefinitionType defType) {
396
		Map<Long, RuleDefinition> idRuleDef = 
397
			new TreeMap<Long, RuleDefinition>();
398
 
399
		List<RuleDefinitionType> jaxbRuleDefs = defType.getRuleDefinition();
400
 
401
		for(RuleDefinitionType jaxbRuleDef : jaxbRuleDefs) {
402
			long id = jaxbRuleDef.getID();
403
			String script = jaxbRuleDef.getScript();
404
 
405
			RuleDefinition ruleDef = new RuleDefinition(id, script);
406
			ruleDef.setDescription(jaxbRuleDef.getDescription());
407
 
408
			idRuleDef.put(new Long(id), ruleDef);
409
		}
410
 
411
		Utils.info("idRuleDef=" + idRuleDef);
412
		return idRuleDef;
413
	}
414
 
415
	/**
416
	 * 
417
	 * @param defType
418
	 * @return
419
	 */
57 naveen 420
	private Map<Long, FacetDefinition> convertFacetDefinitions(
421
			IRDefinitionType defType) {
61 naveen 422
		List<FacetDefinitionType> jaxbFacetDefs = defType.getFacetDefinition();
423
 
424
		Map<Long, FacetDefinition> idFacetDefs = 
425
			new TreeMap<Long, FacetDefinition>();
426
 
427
		for(FacetDefinitionType jaxbFacetDef : jaxbFacetDefs) {
428
			long id = jaxbFacetDef.getID();
429
			String target = jaxbFacetDef.getTarget();
430
 
431
			FacetDefinition facetDef = new FacetDefinition(id, target);
432
 
433
			if(jaxbFacetDef.getFeatureDefinitionID() != null) {
434
				long featureDefID = jaxbFacetDef.getFeatureDefinitionID();
435
				facetDef.setFeatureDefinitionID(featureDefID);
436
			}
437
 
438
			if(jaxbFacetDef.getSlideDefinitionID() != null) {
439
				long slideDefID = jaxbFacetDef.getSlideDefinitionID();
440
				facetDef.setSlideDefinitionID(slideDefID);
441
			}
442
 
62 naveen 443
			long irDataRuleID = jaxbFacetDef.getIRDataRuleID();
444
			facetDef.setIrDataRuleID(irDataRuleID);
61 naveen 445
 
62 naveen 446
			long irMetaDataRuleID = jaxbFacetDef.getIRMetaDataRuleID();
447
			facetDef.setIrMetaDataRuleID(irMetaDataRuleID);
448
 
61 naveen 449
			facetDef.setDescription(jaxbFacetDef.getDescription());
450
 
451
			idFacetDefs.put(new Long(id), facetDef);
452
		}
453
		Utils.info("idFacetDefs=" + idFacetDefs);
454
		return idFacetDefs;
57 naveen 455
	}
456
 
457
	/**
458
	 * 
459
	 * @param defType
460
	 * @return
61 naveen 461
	 * @throws Exception 
57 naveen 462
	 */
463
	private Map<Long, CategoryFacetDefinition> convertCategoryFacetDefinitions(
61 naveen 464
			IRDefinitionType defType) throws Exception {
465
		List<CategoryFacetDefinitionType> jaxbCategoryFacetDefs = 
466
			defType.getCategoryFacetDefinition();
467
 
468
		Map<Long, CategoryFacetDefinition> idCategoryFacetDefs = 
469
			new TreeMap<Long, CategoryFacetDefinition>();
470
 
471
		for(CategoryFacetDefinitionType jaxbCategoryFacetDef : 
472
			jaxbCategoryFacetDefs) {
473
			long categoryID = jaxbCategoryFacetDef.getCategoryID();
474
 
62 naveen 475
			List<Long> jaxbFacetDefIDs = 
476
				jaxbCategoryFacetDef.getFacetDefinitionID();
61 naveen 477
 
478
			CategoryFacetDefinition categoryFacetDef = 
479
				new CategoryFacetDefinition(categoryID);
480
 
62 naveen 481
			categoryFacetDef.setFacetDefinitionIDs(jaxbFacetDefIDs);
61 naveen 482
 
483
			idCategoryFacetDefs.put(new Long(categoryID), categoryFacetDef);
484
		}
485
 
486
		// Append parent's facets 
487
		// Go only one level for now
488
		// Categories need to be imported first
489
		DefinitionsContainer defs = 
490
			Catalog.getInstance().getDefinitionsContainer();
491
 
492
		for(Long categoryID : idCategoryFacetDefs.keySet()) {
493
			Category category = defs.getCategory(categoryID.longValue());
494
			Category parentCategory = category.getParentCategory();
495
 
496
			if(parentCategory != null) {
497
				long parentCategoryID = parentCategory.getID();
498
 
499
				CategoryFacetDefinition parentCategoryFacetDef = 
500
					idCategoryFacetDefs.get(new Long(parentCategoryID));
501
 
502
				if(parentCategoryFacetDef != null) {
503
					List<Long> parentsFacetIDs = 
62 naveen 504
						parentCategoryFacetDef.getFacetDefinitionIDs();
61 naveen 505
 
506
					if(parentsFacetIDs != null) {
507
						CategoryFacetDefinition categoryFacetDef = 
508
							idCategoryFacetDefs.get(categoryID);
509
 
62 naveen 510
						categoryFacetDef.appendFacetDefinitionIDs(
511
								parentsFacetIDs);
61 naveen 512
					}
513
				}
514
			}
515
		}
516
 
517
		Utils.info("idCategoryFacetDefs=" + idCategoryFacetDefs);
518
		return idCategoryFacetDefs;
57 naveen 519
	}
520
 
521
	/**
522
	 * 
10 shop2020 523
	 * @return Map
524
	 */
16 naveen 525
	private Map<Long, SlideDefinition> convertSlideDefinitions(
526
			DefinitionType defType) {
527
 
528
		Map<Long, SlideDefinition> defs = 
529
			new HashMap<Long, SlideDefinition>();
530
 
531
		List<SlideDefinitionType> jaxbDefs = defType.getSlideDefinition();
532
		Iterator<SlideDefinitionType> itjaxbDefs = jaxbDefs.iterator();
533
 
534
		// Slide definitions
535
		while(itjaxbDefs.hasNext()) {
536
			SlideDefinitionType jaxbDef = itjaxbDefs.next();
537
 
42 naveen 538
			Utils.info("Slide label=" + jaxbDef.getLabel());
16 naveen 539
			SlideDefinition def = new SlideDefinition(jaxbDef.getID(),
540
					jaxbDef.getLabel());
541
			def.setDescription(jaxbDef.getDescription());
542
 
42 naveen 543
			// Children Slides
544
			Utils.info("jaxbDef.getChildSlideID=" + jaxbDef.getChildSlideID());
545
			def.setChildrenSlideDefinitionIDs(jaxbDef.getChildSlideID());
546
 
16 naveen 547
			// Slide Feature Definition
548
			List<SlideFeatureDefinitionType> jaxbSlideFeatureDefs = 
549
				jaxbDef.getSlideFeatureDefinition();
550
 
551
			if(jaxbSlideFeatureDefs != null) {
552
				def.setSlideFeatureDefinitions((
553
						this.convertSlideFeatureDefinitions(
554
								jaxbSlideFeatureDefs)));
555
			}
42 naveen 556
			Utils.info("def=" + def);
16 naveen 557
 
558
			defs.put(new Long(jaxbDef.getID()), def);
559
		}
560
 
561
		return defs;
562
	}
563
 
564
	/**
565
	 * 
566
	 * @param jaxbDefs
567
	 * @return List<SlideFeatureDefinition>
568
	 */
569
	private List<SlideFeatureDefinition> convertSlideFeatureDefinitions(
570
			List<SlideFeatureDefinitionType> jaxbDefs) {
571
 
572
		List<SlideFeatureDefinition> defs = 
573
			new ArrayList<SlideFeatureDefinition>();
574
 
575
		Iterator<SlideFeatureDefinitionType> itjaxbDefs = jaxbDefs.iterator();
576
 
577
		// Slide-feature definitions
578
		while(itjaxbDefs.hasNext()) {
579
			SlideFeatureDefinitionType jaxbDef = itjaxbDefs.next();
580
			SlideFeatureDefinition def = new SlideFeatureDefinition(
581
					jaxbDef.getFeatureDefinitionID());
582
 
583
			def.setEditorialImportance(this.convertEditorialImportance(
584
					jaxbDef.getEditorialImportance()));
585
 
586
			def.setDescription(jaxbDef.getDescription());
587
 
588
			defs.add(def);
589
		}
590
 
591
		return defs;
592
	}
593
 
594
	/**
595
	 * 
49 naveen 596
	 * @param defType
597
	 * @return
16 naveen 598
	 */
599
	private Map<Long, FeatureDefinition> convertFeatureDefinitions(
600
			DefinitionType defType) {
601
 
602
		Map<Long, FeatureDefinition> defs = 
603
			new HashMap<Long, FeatureDefinition>();
604
 
605
		List<FeatureDefinitionType> jaxbDefs = defType.getFeatureDefinition();
606
		Iterator<FeatureDefinitionType> itjaxbDefs = jaxbDefs.iterator();
607
 
608
		// Feature definitions
609
		while(itjaxbDefs.hasNext()) {
610
			FeatureDefinitionType jaxbDef = itjaxbDefs.next();
611
 
42 naveen 612
			//Utils.info("Feature label=" + jaxbDef.getLabel());
16 naveen 613
			FeatureDefinition def = new FeatureDefinition(jaxbDef.getID(),
614
					jaxbDef.getLabel());
615
			def.setAllowsBlank(jaxbDef.isCanbeBlank());
616
			def.setDescription(jaxbDef.getDescription());
617
 
618
			// Bullet Definition
619
			BulletDefinitionType jaxbBulletDef = jaxbDef.getBulletDefinition();
620
			if(jaxbBulletDef != null) {
621
				def.setBulletDefinition(this.convertBulletDefinition(
622
						jaxbBulletDef));
623
			}
42 naveen 624
			Utils.info("def=" + def);
16 naveen 625
 
626
			defs.put(new Long(jaxbDef.getID()), def);
627
		}
628
 
629
		return defs;
630
	}
631
 
632
	/**
633
	 * 
634
	 * @param jaxbDef BulletDefinitionType
635
	 * @return BulletDefinition
636
	 */
637
	private BulletDefinition convertBulletDefinition(
638
			BulletDefinitionType jaxbDef) {
639
 
640
		BulletDefinition def = new BulletDefinition(
641
				jaxbDef.getDatatypeDefinitionID());
642
 
643
		Long unitID = jaxbDef.getUnitID();
644
		if(unitID != null) {
645
			def.setUnitID(unitID.longValue());
646
		}
47 naveen 647
		Utils.info("jaxbDef.isIsMultivalue=" + jaxbDef.isIsMultivalue());
648
 
16 naveen 649
		def.setMultivalue(jaxbDef.isIsMultivalue());
650
		def.setLearned(jaxbDef.isIsLearned());
651
		def.setDescription(jaxbDef.getDescription());
652
 
653
		return def;
654
	}
49 naveen 655
 
16 naveen 656
	/**
657
	 * 
49 naveen 658
	 * @param defType
659
	 * @return
16 naveen 660
	 */
661
	private Map<Long, DatatypeDefinition> convertDatatypeDefinition(
662
			DefinitionType defType) {
663
 
664
		Map<Long, DatatypeDefinition> defs = 
665
			new HashMap<Long, DatatypeDefinition>();
666
 
667
		List<DatatypeDefinitionType> jaxbDefs = defType.getDatatypeDefinition();
668
		Iterator<DatatypeDefinitionType> it = jaxbDefs.iterator();
669
 
670
		while(it.hasNext()) {
671
			DatatypeDefinitionType jaxbDef = it.next();
672
 
673
			DatatypeDefinition def = null;
674
			CompositeDefinitionType compositeDefType = 
675
				jaxbDef.getCompositeDefinition();
676
 
677
			EnumDefinitionType enumDefType = 
678
				jaxbDef.getEnumDefinition();
679
 
680
			// Composite data type
681
			if(compositeDefType != null) {
682
				CompositeDefinition compDef = new CompositeDefinition(
20 naveen 683
						jaxbDef.getID(), jaxbDef.getName(), 
684
						compositeDefType.getSeparator());
16 naveen 685
 
686
				List<CompositePartDefinitionType> jaxbPartDefs = 
687
					compositeDefType.getCompositePartDefinition();
688
 
689
				Iterator<CompositePartDefinitionType> itPartDefs = 
690
					jaxbPartDefs.iterator();
691
 
692
				// Collect parts
693
				while(itPartDefs.hasNext()) {
694
					CompositePartDefinitionType jaxbPartDef = itPartDefs.next();
695
					CompositePartDefinition partDef = 
696
						this.convertCompositePartDefinition(jaxbPartDef);
697
 
698
					compDef.addCompositePartDefinition(partDef);
699
				}
700
 
701
				def = compDef;
702
			}
703
 
704
			// Enumerated values
705
			else if(enumDefType != null) {
706
				EnumDefinition enumDef = new EnumDefinition(jaxbDef.getID(), 
707
						jaxbDef.getName());
708
 
709
				List<EnumValueType> jaxbEnumValues = 
710
					enumDefType.getEnumValue();
711
 
712
				Iterator<EnumValueType> itEnumValues = 
713
					jaxbEnumValues.iterator();
714
 
715
				// Collect individual value
716
				while(itEnumValues.hasNext()) {
717
					EnumValueType jaxbEnumValue = itEnumValues.next();
718
					EnumValue enumValue = 
719
						this.convertEnumValueDefinition(jaxbEnumValue);
720
 
721
					enumDef.addEnumValue(enumValue);
722
				}
723
 
724
				def = enumDef;
725
			}
726
 
727
			// Primitive
728
			else {
729
				def = new DatatypeDefinition(jaxbDef.getID(), 
730
						jaxbDef.getName());
731
			}
732
 
733
			def.setDescription(jaxbDef.getDescription());
42 naveen 734
			Utils.info("def=" + def);
16 naveen 735
 
736
			defs.put(new Long(jaxbDef.getID()), def);
737
		}
738
 
739
		return defs;
740
	}
741
 
742
	/**
743
	 * 
744
	 * @param jaxbEnumValue
745
	 * @return EnumValue
746
	 */
747
	private EnumValue convertEnumValueDefinition(EnumValueType jaxbEnumValue) {
748
 
749
		EnumValue enumValue = new EnumValue(jaxbEnumValue.getID(), 
750
				jaxbEnumValue.getValue());
751
 
752
		return enumValue;
753
	}
754
	/**
755
	 * 
756
	 * @param jaxbPartDef
757
	 * @return CompositePartDefinition
758
	 */
759
	private CompositePartDefinition convertCompositePartDefinition(
760
			CompositePartDefinitionType jaxbPartDef) {
761
 
762
		CompositePartDefinition partDef = new CompositePartDefinition(
763
				jaxbPartDef.getLabel(), jaxbPartDef.getDatatypeDefinitionID(), 
764
				jaxbPartDef.getUnitID());
765
 
766
		partDef.setDescription(jaxbPartDef.getDescription());
767
 
768
		return partDef;
769
	}
49 naveen 770
 
16 naveen 771
	/**
772
	 * 
49 naveen 773
	 * @param defType
774
	 * @param catMap
775
	 * @return
16 naveen 776
	 */
777
	private Category convertCategories(DefinitionType defType, 
778
			Map<Long, Category> catMap) {
10 shop2020 779
 
780
		List<CategoryType> jaxbCats = defType.getCategory();
781
		Iterator<CategoryType> it = jaxbCats.iterator();
782
 
783
		// Get root category
784
		CategoryType jaxbCat = it.next();
785
		if(jaxbCat == null) {
42 naveen 786
			Utils.info("Invalid file: " + this.srcFile);
10 shop2020 787
			return null;
788
		}
789
 
790
		Category root = this.convertCategoryType(jaxbCat, null, catMap);
42 naveen 791
		Utils.info("catMap.get(10002)=" + catMap.get(new Long(10002)));
32 naveen 792
 
10 shop2020 793
		return root;
794
	}
795
 
796
	/**
797
	 * 
798
	 * @param jaxbCat
49 naveen 799
	 * @param parentCat
800
	 * @param catMap
801
	 * @return
10 shop2020 802
	 */
32 naveen 803
	@SuppressWarnings("unchecked")
16 naveen 804
	private Category convertCategoryType(CategoryType jaxbCat, 
805
			Category parentCat, Map<Long, Category> catMap) {
18 naveen 806
 
10 shop2020 807
		Category cat = new Category(jaxbCat.getID());
808
		cat.setLabel(jaxbCat.getName());
809
		cat.setDescription(jaxbCat.getDescription());
810
		if(parentCat != null) {
811
			cat.setParentCategory(parentCat);
32 naveen 812
 
813
			// Copy parent's category-slide definitions
814
			ArrayList<CategorySlideDefinition> parentCSDef = 
815
				(ArrayList<CategorySlideDefinition>) 
816
				parentCat.getCategorySlideDefintions();
817
 
818
			if(parentCSDef != null) {
819
				cat.setCategorySlideDefintions(
820
					(List<CategorySlideDefinition>) parentCSDef.clone());
821
			}
10 shop2020 822
		}
823
 
824
		// Category Slide Definition
32 naveen 825
		List<CategorySlideDefinitionType> jaxbDefs = 
826
			jaxbCat.getCategorySlideDefinition();
827
 
10 shop2020 828
		Iterator<CategorySlideDefinitionType> it = jaxbDefs.iterator();
829
		while(it.hasNext()) {
830
			CategorySlideDefinitionType jaxbDef = it.next();
32 naveen 831
			CategorySlideDefinition catSlideDef 
832
			= this.convertCategorySlideDefinitionType(jaxbCat.getID(), jaxbDef);
833
 
10 shop2020 834
			cat.addCategorySlideDefintion(catSlideDef);
835
		}
836
 
837
		// Children
838
		List<CategoryType> jaxbChildren = jaxbCat.getChildCategory();
42 naveen 839
		//Utils.info("jaxbChildren.size: " + jaxbChildren.size());
10 shop2020 840
 
841
		Iterator<CategoryType> itChildren = jaxbChildren.iterator();
842
		while(itChildren.hasNext()) {
843
			CategoryType jaxbChildCat = itChildren.next();
32 naveen 844
			Category childCat = this.convertCategoryType(jaxbChildCat, cat, 
845
					catMap);
10 shop2020 846
			cat.addChild(childCat);
847
		}
42 naveen 848
		//Utils.info("Parent cat: " + cat);
32 naveen 849
		// DEBUG ONLY
42 naveen 850
		Utils.info("####### Cat ID: " + cat.getID());
32 naveen 851
		if(cat.getCategorySlideDefintions() != null) {
852
			for(CategorySlideDefinition csd : cat.getCategorySlideDefintions()){
42 naveen 853
				Utils.info("####### SlideDefintionID=" + 
32 naveen 854
						csd.getSlideDefintionID());
855
			}
856
		}
857
 
10 shop2020 858
		catMap.put(new Long(jaxbCat.getID()), cat);
859
		return cat;
860
	}
861
 
862
	/**
863
	 * 
49 naveen 864
	 * @param categoryID
10 shop2020 865
	 * @param jaxbDef
866
	 * @return
867
	 */
16 naveen 868
	private CategorySlideDefinition convertCategorySlideDefinitionType(
869
			long categoryID, CategorySlideDefinitionType jaxbDef) {
870
 
10 shop2020 871
		CategorySlideDefinition catSlideDef = new CategorySlideDefinition(
32 naveen 872
				jaxbDef.getSlideDefinitionID());
10 shop2020 873
 
874
		catSlideDef.setDescription(jaxbDef.getDescription());
875
 
876
		EditorialImportanceType jaxbEdImp = jaxbDef.getEditorialImportance();
877
		EditorialImportance edImp = this.convertEditorialImportance(jaxbEdImp);
878
		catSlideDef.setEditorialImportance(edImp);
879
 
880
		return catSlideDef;
881
	}
882
 
883
	/**
884
	 * 
885
	 * @return Map
886
	 */
887
	private Map<Long, Unit> convertUnits(DefinitionType defType) {
888
 
889
		Map<Long, Unit> allUnits = new HashMap<Long, Unit>();
890
		List<UnitType> jaxbUnits = defType.getUnit();
891
		Iterator<UnitType> it = jaxbUnits.iterator();
892
 
893
		while(it.hasNext()) {
894
			UnitType jaxbUnit = it.next();
895
 
896
			Unit unit = new Unit(jaxbUnit.getID());
897
			unit.setDescription(jaxbUnit.getDescription());
898
			unit.setFullForm(jaxbUnit.getFullform());
899
			unit.setShortForm(jaxbUnit.getShortform());
42 naveen 900
			Utils.info("unit=" + unit);
10 shop2020 901
 
902
			allUnits.put(new Long(jaxbUnit.getID()), unit);
903
		}
904
 
905
		return allUnits;
906
	}
49 naveen 907
 
10 shop2020 908
	/**
909
	 * 
49 naveen 910
	 * @param srcFile
911
	 * @return
912
	 * @throws Exception
10 shop2020 913
	 */
57 naveen 914
	private Object unmarshallSrcFile(String srcFile, String type) 
915
		throws Exception {
10 shop2020 916
 
57 naveen 917
        JAXBContext jc = JAXBContext.newInstance("in.shop2020.metamodel.jaxb." +
918
        		type);
10 shop2020 919
 
920
        // create an Unmarshaller
921
        Unmarshaller u = jc.createUnmarshaller();
922
 
16 naveen 923
        JAXBElement<?> element = (JAXBElement<?>)u.unmarshal(
924
        		new FileInputStream(srcFile));
10 shop2020 925
 
57 naveen 926
        return element.getValue();
10 shop2020 927
	}
928
 
929
	/**
930
	 * 
16 naveen 931
	 * @param jaxbEdImp
932
	 * @return EditorialImportance
933
	 */
934
	private EditorialImportance convertEditorialImportance(
935
			EditorialImportanceType jaxbEdImp) {
936
 
937
		String strEdImp = jaxbEdImp.value();
938
		EditorialImportance edImp = null;
939
 
940
		if(strEdImp.equals("Mandatory"))
941
			edImp = EditorialImportance.MANDATORY;
942
		else if(strEdImp.equals("Optional"))
943
			edImp = EditorialImportance.OPTIONAL;
944
		else 
945
			edImp = EditorialImportance.RECOMMENDED;
946
 
947
		return edImp;
948
	}
10 shop2020 949
}