/*
 * 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 java.util.LinkedList;
import java.util.List;

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.ManyToMany;
import javax.persistence.Table;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.annotations.Cascade;


/**
 * Title:		EXtendedSchemaDefinition
 * @author: 	rob
 * @version:	$Id: v Exp $ 
 * @since			
 * Copyright:	1999-2007 Knowledge Integration Ltd
 * Company:		Knowledge Integration Ltd
 * Description:
 *
 *
 *
 * Created:		28 Dec 2007
 *
 *
 * History:
 *				$Log: v $
 *
 * 
 */
@Entity
@Table(name="CP_EXTENDED_SCHEMA_DEF")
public class ExtendedSchemaDefinitionHDO
{
    public static Log log = LogFactory.getLog(ExtendedSchemaDefinitionHDO.class);

    private Long id;
    private String schema_uri;
    private String name;
    private List<SchemaPropertyDefinitionHDO> schema_properties = new LinkedList<SchemaPropertyDefinitionHDO>();
   
    public ExtendedSchemaDefinitionHDO() 
    {;}
    
    
    public ExtendedSchemaDefinitionHDO (String uri, String name) 
    {
      super();
      this.schema_uri = uri;
      this.name = name;
    }
    
    @Id
    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
      return id;
    }
    
    public void setId(Long id) {
      this.id = id;
    }

    @Column(name="SCHEMA_URI",length=256)
    public String getSchemaURI() {
      return schema_uri;
    }

    public void setSchemaURI(String schema_uri) {
      this.schema_uri = schema_uri;
    }

    @Column(name="SCHEMA_NAME",length=256)
    public String getSchemaName() {
      return name;
    }

    public void setSchemaName(String name) {
      this.name = name;
    }


    @ManyToMany(cascade=CascadeType.PERSIST)
    @JoinTable(name="CP_EXTENDED_SCHEMA_PROPERTY",
         joinColumns=@JoinColumn(name="EXTENDED_SCHEMA_DEF_FK", referencedColumnName="ID"),
         inverseJoinColumns= @JoinColumn(name="EXTENDED_SCHEMA_PROP_DEF_FK", referencedColumnName="ID") )
    @Cascade(value = org.hibernate.annotations.CascadeType.ALL)
     
    public List<SchemaPropertyDefinitionHDO> getProperties() {
      return schema_properties;
    }

    
    public void setProperties(List<SchemaPropertyDefinitionHDO> properties) 
    {
      schema_properties.addAll(properties); //maintain linked list
    }

    public static ExtendedSchemaDefinitionHDO lookup(org.hibernate.Session session, String uri) {
      log.debug("lookup("+uri+")");
      ExtendedSchemaDefinitionHDO result = null;
      if ( uri != null ) {
        org.hibernate.Query q = session.createQuery("Select x from com.k_int.svc.custprops.datamodel.ExtendedSchemaDefinitionHDO x where x.schemaURI=?");
        q.setParameter(0,uri,org.hibernate.Hibernate.STRING);
        result = (ExtendedSchemaDefinitionHDO) q.uniqueResult();
      }
      return result;
    }

    public static ExtendedSchemaDefinitionHDO lookupOrCreate(org.hibernate.Session session, String uri) {
      return lookupOrCreate(session,uri,null);
    }

    public static ExtendedSchemaDefinitionHDO lookupOrCreate(org.hibernate.Session session, String uri, String name) {
      log.debug("lookupOrCreate("+uri+")");
      ExtendedSchemaDefinitionHDO result = null;
      if ( uri != null ) {
        result = lookup(session,uri);
        if ( result == null ) {
          result = new ExtendedSchemaDefinitionHDO(uri,name);
          session.save(result);
        }
      }
      return result;
    }

    public void checkOrAddProperty(Session session, SchemaPropertyDefinitionHDO property) 
    {
     
      if(!schema_properties.contains(property))
      {
        schema_properties.add(property);
        session.update(this);
      }
    }

    // Override for hibernate comparisons
    //  
    public boolean equals(Object o) {
      if ( ( o != null ) && 
           ( o instanceof SchemaDefinitionHDO ) &&
           ( ((SchemaDefinitionHDO)o).getSchemaURI().equals(schema_uri) ) )
        return true;

      return false;
    }

    public String toString() 
    {
      return schema_uri;
    }
}



