package com.apelon.dts.webservice;

import com.apelon.dts.client.Concept;
import com.apelon.dts.client.DTSException;
import com.apelon.dts.client.Property;
import com.apelon.dts.client.Role;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;

/**
 * Adapter for Concept
 *
 * @author mmunz
 */
public class ConceptBean extends FormBean
{
  private PropertyBean[] fFetchedProperties;
  private RoleBean[] fFetchedRoles;
  private boolean fFetchedHasSubs;
  private boolean fFetchedHasSups;
  private boolean fIsFetchedHasSubs;
  private boolean fIsFetchedHasSups;
  
  public ConceptBean()
  {
    super();
  }

  public ConceptBean(Concept newDelegate) throws DTSException
  {
    super(newDelegate.getName(), newDelegate.getId(), newDelegate.getCode());  
    System.out.println("@todo log.  creating a new ConceptBean from concept: " + newDelegate);
    setFetchedProperties(adapt(newDelegate.getFetchedProperties()));
    setFetchedRoles(adapt(newDelegate.getFetchedRoles()));
    setFetchedHasSubs(newDelegate.getFetchedHasSubs());
    setFetchedHasSups(newDelegate.getFetchedHasSups());
    setIsFetchedHasSubs(newDelegate.isFetchedHasSubs());
    setIsFetchedHasSups(newDelegate.isFetchedHasSups());
  }

  public Concept adapt() throws DTSException
  {
    System.out.println("@todo log.  adapting.  id: " + getId() + ", code: " + getCode());
    return new Concept(
      getName(),
      getCode(),
      "" + getId(),
      new Vector(Arrays.asList(adapt(getFetchedRoles()))),
      new Vector(Arrays.asList(adapt(getFetchedProperties()))),
      getIsFetchedHasSubs(),
      getIsFetchedHasSups(),
      getFetchedHasSubs(),
      getFetchedHasSups());
  }
  
  protected Role[] adapt(RoleBean[] roles)
  {
    Role[] resultSet = new Role[roles.length];
    for(int index = 0; index < roles.length; index++)
    {
      RoleBean element = roles[index]; 
      resultSet[index] = element.adapt();
    }
    return resultSet;
  }

  protected RoleBean[] adapt(Role[] roles)
  {
    RoleBean[] resultSet = new RoleBean[roles.length];
    for(int index = 0; index < roles.length; index++)
    {
      Role element = roles[index]; 
      resultSet[index] = new RoleBean(element.getName(), element.getValue());
    }
    return resultSet;
  }

  protected Property[] adapt(PropertyBean[] properties)
  {
    Property[] resultSet = new Property[properties.length];
    for(int index = 0; index < properties.length; index++)
    {
      PropertyBean element = properties[index]; 
      resultSet[index] = element.adapt();
    }
    return resultSet;
  }

  protected PropertyBean[] adapt(Property[] properties)
  {
    PropertyBean[] resultSet = new PropertyBean[properties.length];
    for(int index = 0; index < properties.length; index++)
    {
      Property element = properties[index]; 
      resultSet[index] = new PropertyBean(element.getName(), element.getValue());
    }
    return resultSet;
  }

	public void setFetchedProperties(PropertyBean[] fFetchedProperties) {
		this.fFetchedProperties = fFetchedProperties;
	}

	public PropertyBean[] getFetchedProperties() {
		return fFetchedProperties;
	}

	public void setFetchedRoles(RoleBean[] fFetchedRoles) {
		this.fFetchedRoles = fFetchedRoles;
	}

	public RoleBean[] getFetchedRoles() {
		return fFetchedRoles;
	}

	public void setFetchedHasSubs(boolean fFetchedHasSubs) {
		this.fFetchedHasSubs = fFetchedHasSubs;
	}

	public boolean getFetchedHasSubs() {
		return fFetchedHasSubs;
	}

	public void setFetchedHasSups(boolean fFetchedHasSups) {
		this.fFetchedHasSups = fFetchedHasSups;
	}

	public boolean getFetchedHasSups() {
		return fFetchedHasSups;
	}

	public void setIsFetchedHasSubs(boolean fIsFetchedHasSubs) {
		this.fIsFetchedHasSubs = fIsFetchedHasSubs;
	}

	public boolean getIsFetchedHasSubs() {
		return fIsFetchedHasSubs;
	}

	public void setIsFetchedHasSups(boolean fIsFetchedHasSups) {
		this.fIsFetchedHasSups = fIsFetchedHasSups;
	}

	public boolean getIsFetchedHasSups() {
		return fIsFetchedHasSups;
	}
}
