package com.acme.model.domain.ad.entity;


import com.acme.structure.BaseEntity;
import com.acme.structure.BaseEntityListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity(name = "UnitGroup")
@Table(name = "UNITGROUPS")
//this is for oracle db @SequenceGenerator(name = "UNITGROUP_SEQ", sequenceName = "UNITGROUP_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class UnitGroup extends BaseEntity
{
    private String name;
    private Collection<Unit> units;

    public static String WEIGHT = "WEIGHT";
    public static String COUNTABLE = "COUNTABLE";
    public static String TIME = "TIME";
    public static String DISTANCE = "DISTANCE";
    public static String LIQUID = "LIQUID";

    public static HashMap<String, Integer> unitGroups;

    static
    {
        unitGroups = new HashMap<String, Integer>();

        unitGroups.put(WEIGHT, 0);
        unitGroups.put(COUNTABLE, 1);
        unitGroups.put(TIME, 2);
        unitGroups.put(DISTANCE, 3);
        unitGroups.put(LIQUID, 4);
    }

    public UnitGroup()
    {
        entityName = "UnitGroup";
        units = new ArrayList<Unit>();
    }

    public UnitGroup(int id, String name)
    {
        entityName = "UnitGroup";
        units = new ArrayList<Unit>();

        this.id = id;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) //, this is for oracle db, generator = "UNITGROUP_SEQ")
    @Column(name = "ID")
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    @Column(name = "NAME", length = 20)
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @OneToMany(targetEntity = Unit.class, fetch = (FetchType.LAZY), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "UNITGROUPID")
    public Collection<Unit> getUnits()
    {
        return units;
    }

    public void setUnits(Collection<Unit> units)
    {
        this.units = units;
    }

    @Override
    @Transient
    public void clearIdFields()
    {
        super.clearIdFields();

        Unit unit = null;
        Iterator itr = units.iterator();

        while (itr.hasNext())
        {
            unit = (Unit)itr.next();
            unit.clearIdFields();
        }
    }

    @Override
    public String toString()
    {
        return getName();
    }
}
