Subversion Repositories SmartDukaan

Rev

Rev 102 | Rev 323 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.metamodel.util;

import java.util.ArrayList;
import java.util.List;

import in.shop2020.metamodel.definitions.BulletDefinition;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.CompositeDefinition;
import in.shop2020.metamodel.definitions.DatatypeDefinition;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.EnumDefinition;
import in.shop2020.metamodel.definitions.ExpandedCompositeDefinition;
import in.shop2020.metamodel.definitions.Unit;

/**
 * Utility class that supports BulletDefinition class. All related objects are fetched 
 * from database and aggregated here.
 * 
 * @author naveen
 *
 */
public class ExpandedBulletDefinition extends BulletDefinition {
        
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        /**
         * Expanded from DatatypeDefinition ID 
         */
        private DatatypeDefinition datatypeDefinition ;
        
        /**
         * List of Expanded from unit IDs
         */
        private List<Unit> units;
        
        /**
         * Interpreted from type of DatatypeDefinition object. True if its 
         * Composite type.
         */
        private boolean isComposite;
        
        /**
         * Interpreted from type of DatatypeDefinition object. True if its a 
         * enumerated type.
         */
        private boolean isEnumerated;
        
        /**
         * Interpreted from type of DatatypeDefinition object. True if its a 
         * primitive type.
         */
        private boolean isPrimitive;
        
        /**
         * Takes BulletDefinition object as input and converts all references 
         * into corresponding detail objects
         * 
         * @param bulletDefinition
         * @throws Exception
         */
        public ExpandedBulletDefinition(BulletDefinition bulletDefinition) 
                throws Exception {
                super(bulletDefinition.getDatatypeDefinitionID());
                
                // Copy rest of the properties
                this.setDescription(bulletDefinition.getDescription());
                this.setUnitIDs(bulletDefinition.getUnitIDs());
                this.setLearned(bulletDefinition.isLearned());
                this.setMultivalue(bulletDefinition.isMultivalue());
                
                DefinitionsContainer defs = 
                        Catalog.getInstance().getDefinitionsContainer();
                
                // Expand data type definition id
                this.datatypeDefinition = defs.getDatatypeDefinition(
                                bulletDefinition.getDatatypeDefinitionID());
                
                
                //Utils.info("datatypeDefinition.getClass().getName=" + 
                //              this.datatypeDefinition.getClass().getName());
                
                if(this.datatypeDefinition instanceof CompositeDefinition) {
                        this.isComposite = true;
                        this.datatypeDefinition = new ExpandedCompositeDefinition(
                                        (CompositeDefinition)this.datatypeDefinition);
                }
                else if(this.datatypeDefinition instanceof EnumDefinition) {
                        this.isEnumerated = true;
                }
                else {
                        this.isPrimitive = true;
                }
                
                // Expand unit ids
                List<Long> unitIDs = bulletDefinition.getUnitIDs();
                
                if(unitIDs != null) {
                        this.units = new ArrayList<Unit>();
                        
                        for(Long unitID : unitIDs) {
                                Unit unit = defs.getUnit(unitID);
                                this.units.add(unit);
                        }
                }
        }

        /**
         * @return the datatypeDefinition
         */
        public DatatypeDefinition getDatatypeDefinition() {
                return this.datatypeDefinition;
        }

        /**
         * @return the unit
         */
        public List<Unit> getUnits() {
                return this.units;
        }

        /**
         * @return the isComposite
         */
        public boolean isComposite() {
                return isComposite;
        }

        /**
         * @return the isEnum
         */
        public boolean isEnumerated() {
                return isEnumerated;
        }

        /**
         * @return the isPrimitive
         */
        public boolean isPrimitive() {
                return isPrimitive;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
                return "ExpandedBulletDefinition [datatypeDefinition="
                                + datatypeDefinition + ", isComposite=" + isComposite
                                + ", isEnumerated=" + isEnumerated + ", units=" + units + "]";
        }

}