//#############################################################################
//#                                                                           #
//#  Copyright (C) <2014>  <IMS MAXIMS>                                       #
//#                                                                           #
//#  This program is free software: you can redistribute it and/or modify     #
//#  it under the terms of the GNU Affero General Public License as           #
//#  published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.                      #
//#                                                                           #
//#  You should have received a copy of the GNU Affero General Public License #
//#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Barbara Worwood using IMS Development Environment (version 1.80 build 5007.25751)
// Copyright (C) 1995-2014 IMS MAXIMS. All rights reserved.
// WARNING: DO NOT MODIFY the content of this file

package ims.core.vo;

/**
 * Linked to core.patient.Patient business object (ID: 1001100000).
 */
public class PatientLite_IdentifiersVo extends ims.core.vo.PatientLiteVo implements ims.vo.ImsCloneable, Comparable
{
	private static final long serialVersionUID = 1L;

	public PatientLite_IdentifiersVo()
	{
	}
	public PatientLite_IdentifiersVo(Integer id, int version)
	{
		super(id, version);
	}
	public PatientLite_IdentifiersVo(ims.core.vo.beans.PatientLite_IdentifiersVoBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.name = bean.getName() == null ? null : bean.getName().buildVo();
		this.sex = bean.getSex() == null ? null : ims.core.vo.lookups.Sex.buildLookup(bean.getSex());
		this.dob = bean.getDob() == null ? null : bean.getDob().buildPartialDate();
		this.isactive = bean.getIsActive();
		this.isquickregistrationpatient = bean.getIsQuickRegistrationPatient();
		this.identifiers = ims.core.vo.PatientIdCollection.buildFromBeanCollection(bean.getIdentifiers());
		this.age = bean.getAge();
		this.agetext = bean.getAgeText();
		this.dod = bean.getDod() == null ? null : bean.getDod().buildDate();
	}
	public void populate(ims.vo.ValueObjectBeanMap map, ims.core.vo.beans.PatientLite_IdentifiersVoBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.name = bean.getName() == null ? null : bean.getName().buildVo(map);
		this.sex = bean.getSex() == null ? null : ims.core.vo.lookups.Sex.buildLookup(bean.getSex());
		this.dob = bean.getDob() == null ? null : bean.getDob().buildPartialDate();
		this.isactive = bean.getIsActive();
		this.isquickregistrationpatient = bean.getIsQuickRegistrationPatient();
		this.identifiers = ims.core.vo.PatientIdCollection.buildFromBeanCollection(bean.getIdentifiers());
		this.age = bean.getAge();
		this.agetext = bean.getAgeText();
		this.dod = bean.getDod() == null ? null : bean.getDod().buildDate();
	}
	public ims.vo.ValueObjectBean getBean()
	{
		return this.getBean(new ims.vo.ValueObjectBeanMap());
	}
	public ims.vo.ValueObjectBean getBean(ims.vo.ValueObjectBeanMap map)
	{
		ims.core.vo.beans.PatientLite_IdentifiersVoBean bean = null;
		if(map != null)
			bean = (ims.core.vo.beans.PatientLite_IdentifiersVoBean)map.getValueObjectBean(this);
		if (bean == null)
		{
			bean = new ims.core.vo.beans.PatientLite_IdentifiersVoBean();
			map.addValueObjectBean(this, bean);
			bean.populate(map, this);
		}
		return bean;
	}
	public Object getFieldValueByFieldName(String fieldName)
	{
		if(fieldName == null)
			throw new ims.framework.exceptions.CodingRuntimeException("Invalid field name");
		fieldName = fieldName.toUpperCase();
		if(fieldName.equals("IDENTIFIERS"))
			return getIdentifiers();
		if(fieldName.equals("AGE"))
			return getAge();
		if(fieldName.equals("AGETEXT"))
			return getAgeText();
		if(fieldName.equals("DOD"))
			return getDod();
		return super.getFieldValueByFieldName(fieldName);
	}
	public boolean getIdentifiersIsNotNull()
	{
		return this.identifiers != null;
	}
	public ims.core.vo.PatientIdCollection getIdentifiers()
	{
		return this.identifiers;
	}
	public void setIdentifiers(ims.core.vo.PatientIdCollection value)
	{
		this.isValidated = false;
		this.identifiers = value;
	}
	public boolean getAgeIsNotNull()
	{
		return this.age != null;
	}
	public Integer getAge()
	{
		return this.age;
	}
	public void setAge(Integer value)
	{
		this.isValidated = false;
		this.age = value;
	}
	public boolean getAgeTextIsNotNull()
	{
		return this.agetext != null;
	}
	public String getAgeText()
	{
		return this.agetext;
	}
	public static int getAgeTextMaxLength()
	{
		return 255;
	}
	public void setAgeText(String value)
	{
		this.isValidated = false;
		this.agetext = value;
	}
	public boolean getDodIsNotNull()
	{
		return this.dod != null;
	}
	public ims.framework.utils.Date getDod()
	{
		return this.dod;
	}
	public void setDod(ims.framework.utils.Date value)
	{
		this.isValidated = false;
		this.dod = value;
	}
	/**
	* getDisplayId() - Gets the default identifier as specified by the ConfigFlag.UI.DEFAULT_PATIENT_ID_TYPE
	*/
	public ims.core.vo.PatientId getDisplayId()
	{
		String name = ims.configuration.ConfigFlag.UI.DISPLAY_PATID_TYPE.getValue();
		ims.core.vo.lookups.PatIdType type = ims.core.vo.lookups.PatIdType.getNegativeInstance(name);
		
		if (type == null) type = ims.core.vo.lookups.PatIdType.HOSPNUM;
		return getPatId(type);
	}
	/**
	* getPatId() - Gets the patient identifier for the type passed in
	*/
	public ims.core.vo.PatientId getPatId(ims.core.vo.lookups.PatIdType idType)
	{
		if (identifiers == null) return null;
		for (int i = 0; i < identifiers.size(); i++)
		{
			ims.core.vo.PatientId id = identifiers.get(i);
			if (id.getType().equals(idType))
			{
				return id;
			}
		}
		return null;
	}
	/**
	* calculateAge
	*/
	public Integer calculateAge()
	{
		calculateAgeText();
		if (dob != null)
		{		
			int patAge = 0;
			if (dod != null)
			{
				patAge = dod.yearDiff(dob);
			}
			else
			{
				patAge = new ims.framework.utils.Age(this.dob, this.dod).getYears();
			}
			setAge(new Integer(patAge));
		}
		return getAge();
	}
	/**
	* getNHS
	*/
	public ims.core.vo.PatientId getNHS()
	{
		return getPatId(ims.core.vo.lookups.PatIdType.NHSN);
	}
	/**
	* calculateAgeText
	*/
	public String calculateAgeText()
	{
		agetext = "-";
		if (dob != null)
		{
			if (ims.configuration.ConfigFlag.UI.HAS_PAEDIATRIC_PATIENTS.getValue())
				agetext = new ims.framework.utils.Age(this.dob, this.dod).toPaediatricString();
			else
				agetext = new ims.framework.utils.Age(this.dob, this.dod).toString();			
		}
		return getAgeText();
	}
	public boolean isValidated()
	{
		if(this.isBusy)
			return true;
		this.isBusy = true;
	
		if(!this.isValidated)
		{
			this.isBusy = false;
			return false;
		}
		if(this.name != null)
		{
			if(!this.name.isValidated())
			{
				this.isBusy = false;
				return false;
			}
		}
		this.isBusy = false;
		return true;
	}
	public String[] validate()
	{
		return validate(null);
	}
	public String[] validate(String[] existingErrors)
	{
		if(this.isBusy)
			return null;
		this.isBusy = true;
	
		java.util.ArrayList<String> listOfErrors = new java.util.ArrayList<String>();
		if(existingErrors != null)
		{
			for(int x = 0; x < existingErrors.length; x++)
			{
				listOfErrors.add(existingErrors[x]);
			}
		}
		if(this.name != null)
		{
			String[] listOfOtherErrors = this.name.validate();
			if(listOfOtherErrors != null)
			{
				for(int x = 0; x < listOfOtherErrors.length; x++)
				{
					listOfErrors.add(listOfOtherErrors[x]);
				}
			}
		}
		int errorCount = listOfErrors.size();
		if(errorCount == 0)
		{
			this.isBusy = false;
			this.isValidated = true;
			return null;
		}
		String[] result = new String[errorCount];
		for(int x = 0; x < errorCount; x++)
			result[x] = (String)listOfErrors.get(x);
		this.isBusy = false;
		this.isValidated = false;
		return result;
	}
	public void clearIDAndVersion()
	{
		this.id = null;
		this.version = 0;
	}
	public Object clone()
	{
		if(this.isBusy)
			return this;
		this.isBusy = true;
	
		PatientLite_IdentifiersVo clone = new PatientLite_IdentifiersVo(this.id, this.version);
		
		if(this.name == null)
			clone.name = null;
		else
			clone.name = (ims.core.vo.PersonName)this.name.clone();
		if(this.sex == null)
			clone.sex = null;
		else
			clone.sex = (ims.core.vo.lookups.Sex)this.sex.clone();
		if(this.dob == null)
			clone.dob = null;
		else
			clone.dob = (ims.framework.utils.PartialDate)this.dob.clone();
		clone.isactive = this.isactive;
		clone.isquickregistrationpatient = this.isquickregistrationpatient;
		if(this.identifiers == null)
			clone.identifiers = null;
		else
			clone.identifiers = (ims.core.vo.PatientIdCollection)this.identifiers.clone();
		clone.age = this.age;
		clone.agetext = this.agetext;
		if(this.dod == null)
			clone.dod = null;
		else
			clone.dod = (ims.framework.utils.Date)this.dod.clone();
		clone.isValidated = this.isValidated;
		
		this.isBusy = false;
		return clone;
	}
	public int compareTo(Object obj)
	{
		return compareTo(obj, true);
	}
	public int compareTo(Object obj, boolean caseInsensitive)
	{
		if (obj == null)
		{
			return -1;
		}
		if(caseInsensitive); // this is to avoid eclipse warning only.
		if (!(PatientLite_IdentifiersVo.class.isAssignableFrom(obj.getClass())))
		{
			throw new ClassCastException("A PatientLite_IdentifiersVo object cannot be compared an Object of type " + obj.getClass().getName());
		}
		if (this.id == null)
			return 1;
		if (((PatientLite_IdentifiersVo)obj).getBoId() == null)
			return -1;
		return this.id.compareTo(((PatientLite_IdentifiersVo)obj).getBoId());
	}
	public synchronized static int generateValueObjectUniqueID()
	{
		return ims.vo.ValueObject.generateUniqueID();
	}
	public int countFieldsWithValue()
	{
		int count = super.countFieldsWithValue();
		if(this.identifiers != null)
			count++;
		if(this.age != null)
			count++;
		if(this.agetext != null)
			count++;
		if(this.dod != null)
			count++;
		return count;
	}
	public int countValueObjectFields()
	{
		return super.countValueObjectFields() + 4;
	}
	protected ims.core.vo.PatientIdCollection identifiers;
	protected Integer age;
	protected String agetext;
	protected ims.framework.utils.Date dod;
	private boolean isValidated = false;
	private boolean isBusy = false;
}
