/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * published by the Free Software Foundation; either version 2.1 of
 * the license, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.k_int.svc.custprops.datamodel;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;


/**
 * Title:		SchemaPropertyHDO
 * @author: 	rob
 * @version:	$Id: v Exp $ 
 * Copyright:	1999-2007 Knowledge Integration Ltd
 * Company:		Knowledge Integration Ltd
 * Description: Defines the cardinality and another attributes of a 
 *              property definition when it is used in a particular schema
 *       
 *            Use -1 for unbounded
 *            
 *            NOTE equals is overwridden and simply performs an equals on the
 *            underlying property defintion. 
 *
 * Created:		4 Dec 2007
 *
 *
 * History:
 *				$Log: v $
 *
 * 
 */
@Entity
@Table(name="CP_EXTENDED_SCHEMA_PROPERTY_DEF")
public class SchemaPropertyDefinitionHDO
{
  private Long id;
  private  int min_occurs      = 0;
  private  int max_occurs      = 1;  
  private  boolean  writable   = true;
  private  boolean  visible    = true;
  private Integer display_index;
  private String  display_name;
  private PropertyGeneratorHDO generator;
  private PropertyValidatorHDO validator;
  private PropertyDefinitionHDO prop_def;
 
  
  public SchemaPropertyDefinitionHDO()
  {;}
    

  public SchemaPropertyDefinitionHDO(PropertyDefinitionHDO prop_def)
  {
    this.prop_def=prop_def;
  }
  
  
  @Id
  @Column(name="ID")
  @GeneratedValue(strategy=GenerationType.AUTO)
   public Long getId() 
  {
    return id;
  }
  
  public void setId(Long id) 
  {
    this.id = id;
  }
  
  @Column(name="MIN_OCCURS")
  public int getMinOccurs() 
  {
    return min_occurs;
  }
  
  public void setMinOccurs(int  min_occurs) 
  {
    if(min_occurs<0)
      throw new RuntimeException("Min occurs must be >=0");
    this.min_occurs = min_occurs;
  }
  
  @Column(name="MAX_OCCURS")
  public int getMaxOccurs() 
  {
    return max_occurs;
  }
  
  public void setMaxOccurs(int  max_occurs) 
  {
    if(max_occurs<-1 || max_occurs==0)
      throw new RuntimeException("Max occurs must be -1 or > 0");
    
    this.max_occurs = max_occurs;
  }
  
  @Column(name="WRITABLE")
  public boolean getWritable()
  {
    return writable;
  }
  
  public void setWritable(boolean writable)
  {
    this.writable=writable;
  }
  
  @Column(name="VISIBLE")
  public boolean getVisible()
  {
    return visible;
  }
  
  public void setVisible(boolean visible)
  {
    this.visible=visible;
  }
  
  @Column(name="DISPLAY_INDEX")
  public Integer getDisplayIndex()
  {
    return display_index;
  }
  
  public void setDisplayIndex(Integer display_index)
  {
    this.display_index=display_index;
  }
  
  @Column(name="DISPLAY_NAME", length=128)
  public String getDisplayName()
  {
    return display_name;
  }
  
  public void setDisplayName(String display_name)
  {
    this.display_name=display_name;
  }
  
  
 // make these extend an abstract class for management in the ui ?
  public void setGenerator(PropertyGeneratorHDO generator)
  {
    this.generator=generator;
  }
  
  @ManyToOne(cascade=CascadeType.PERSIST)
  @JoinTable(name="CP_EXTENDED_SCHEMA_PROP_GEN",
       joinColumns=@JoinColumn(name="SCHEMA_PROP_FK", referencedColumnName="ID"),
       inverseJoinColumns= @JoinColumn(name="PROP_GENERATOR_FK", referencedColumnName="ID") )
  @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
  
  public PropertyGeneratorHDO getGenerator()
  {
    return generator;
  }
  
  
  public void setValidator(PropertyValidatorHDO validator)
  {
    this.validator=validator;
  }
  
  @ManyToOne(cascade=CascadeType.PERSIST)
  @JoinTable(name="CP_EXTENDED_SCHEMA_PROP_VALIDATOR",
       joinColumns=@JoinColumn(name="SCHEMA_PROP_FK", referencedColumnName="ID"),
       inverseJoinColumns= @JoinColumn(name="PROP_VALIDATOR_FK", referencedColumnName="ID") )
       @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
  
  public PropertyValidatorHDO getValidator()
  {
    return validator;
  }
  
    
  @ManyToOne(cascade=CascadeType.PERSIST)
  @JoinTable(name="CP_SCHEMA_DEF_PROPERTY",
       joinColumns=@JoinColumn(name="SCHEMA_PROP_FK", referencedColumnName="ID"),
       inverseJoinColumns= @JoinColumn(name="PROP_DEF_FK", referencedColumnName="ID") )
  @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
  public PropertyDefinitionHDO getPropertyDefinition()
  {
    return prop_def;
  }
  
  
  public void setPropertyDefinition(PropertyDefinitionHDO prop_def)
  {
    this.prop_def=prop_def;
  }
  
  
 /** public boolean isValidCardinality()
  {
    boolean retval=true;
    
    if(max_occurs!=-1 && max_occurs<min_occurs)
      retval=false;
    
    return retval;
  }**/
  
  
 /** public String getURI()
  {
    return prop_def.getPropertyURI();
  }
  **/
  public boolean equals(Object o) 
  {
    boolean retval=false;
    if(o!=null && prop_def!=null && (o instanceof SchemaPropertyDefinitionHDO))
    {
      PropertyDefinitionHDO def = ((SchemaPropertyDefinitionHDO)o).getPropertyDefinition();
      if(def!=null && (def.getPropertyURI().equals(prop_def.getPropertyURI())))
          retval=true;
    }

    return retval;
  }
  
 
  public boolean usesDefinition(PropertyDefinitionHDO def)
  {
    boolean retval=false;
    if(prop_def!=null)
      retval = def.equals(prop_def);
    
    return retval;
  }
  
  public String toString()
  {
    String retval=null;
    if(prop_def!=null)
      retval = display_index+": "+prop_def.toString()+"min: "+min_occurs+" max: "+max_occurs;
    
    return retval;
  }
  
}
