package siprp.database.cayenne.objects;

import java.util.Collections;
import java.util.List;

import siprp.database.cayenne.objects.auto._Empresas;

import com.evolute.utils.strings.UnicodeChecker;

public class Empresas extends _Empresas {
	private static final long serialVersionUID = 1L;
	
	private String name = null;
	
	private String convertedName = null;

	@Override
	public String getDesignacaoSocial()
	{
		String currentName = super.getDesignacaoSocial();
		if( name == null || !name.equals( currentName ))
		{
			name = currentName;
			convertedName = null;
		}
		return convertName();
	}
	
	@Override
	public void setDesignacaoSocial( String nome )
	{
		super.setDesignacaoSocial( parseToUnicode( name ) );
		getDesignacaoSocial();
	}

	private String convertName()
	{
		if( name == null )
		{
			convertedName = null;
		}
		else
		{
			convertedName = parseFromUnicode( name );
		}
		return convertedName;
	}
	
	@Override
	public String toString()
	{
		return getDesignacaoSocial();
	}
	
	@Override
	public List<Estabelecimentos> getEstabelecimentosArray()
	{
		List<Estabelecimentos> result = super.getEstabelecimentosArray();
		for(int i = 0; i < result.size(); ++i)
		{
			Estabelecimentos current = result.get(i);
			if( current == null || "y".equals(current.getInactivo()))
			{
				result.remove( i );
				--i;
			}
		}
		Collections.sort(result);
		return result;
	}
}



