/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/ValueObjects/src/ims/core/vo/PersonName.java
//#############################################################################
//#                                                                           #
//#  Copyright (C) <2015>  <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/>.    #
//#                                                                           #
//#  IMS MAXIMS provides absolutely NO GUARANTEE OF THE CLINICAL SAFTEY of    #
//#  this program.  Users of this software do so entirely at their own risk.  #
//#  IMS MAXIMS only ensures the Clinical Safety of unaltered run-time        #
//#  software that it builds, deploys and maintains.                          #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Barbara Worwood using IMS Development Environment (version 1.80 build 5589.25814)
// Copyright (C) 1995-2015 IMS MAXIMS. All rights reserved.
// WARNING: DO NOT MODIFY the content of this file

package ims.core.vo;

/**
 * Linked to core.generic.Person Name business object (ID: 1002100001).
 */
public class PersonName extends ims.core.generic.vo.PersonNameRefVo implements ims.vo.ImsCloneable, Comparable
{
	private static final long serialVersionUID = 1L;

	public PersonName()
	{
	}
	public PersonName(Integer id, int version)
	{
		super(id, version);
	}
	public PersonName(ims.core.vo.beans.PersonNameBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.nametype = bean.getNameType() == null ? null : ims.core.vo.lookups.NameType.buildLookup(bean.getNameType());
		this.surname = bean.getSurname();
		this.forename = bean.getForename();
		this.middlename = bean.getMiddleName();
		this.title = bean.getTitle() == null ? null : ims.core.vo.lookups.PersonTitle.buildLookup(bean.getTitle());
		this.uppersurname = bean.getUpperSurname();
		this.upperforename = bean.getUpperForename();
		this.namesuffix = bean.getNamesuffix();
		this.objectidentifier = bean.getObjectidentifier();
		this.beffdate = bean.getBeffdate() == null ? null : bean.getBeffdate().buildDate();
		this.beffdatecnf = bean.getBeffdatecnf();
		this.betdate = bean.getBetdate() == null ? null : bean.getBetdate().buildDate();
		this.betdatecnf = bean.getBetdatecnf();
		this.pdsupdatemode = bean.getPdsUpdateMode() == null ? null : ims.core.vo.lookups.PDSUpdateMode.buildLookup(bean.getPdsUpdateMode());
	}
	public void populate(ims.vo.ValueObjectBeanMap map, ims.core.vo.beans.PersonNameBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.nametype = bean.getNameType() == null ? null : ims.core.vo.lookups.NameType.buildLookup(bean.getNameType());
		this.surname = bean.getSurname();
		this.forename = bean.getForename();
		this.middlename = bean.getMiddleName();
		this.title = bean.getTitle() == null ? null : ims.core.vo.lookups.PersonTitle.buildLookup(bean.getTitle());
		this.uppersurname = bean.getUpperSurname();
		this.upperforename = bean.getUpperForename();
		this.namesuffix = bean.getNamesuffix();
		this.objectidentifier = bean.getObjectidentifier();
		this.beffdate = bean.getBeffdate() == null ? null : bean.getBeffdate().buildDate();
		this.beffdatecnf = bean.getBeffdatecnf();
		this.betdate = bean.getBetdate() == null ? null : bean.getBetdate().buildDate();
		this.betdatecnf = bean.getBetdatecnf();
		this.pdsupdatemode = bean.getPdsUpdateMode() == null ? null : ims.core.vo.lookups.PDSUpdateMode.buildLookup(bean.getPdsUpdateMode());
	}
	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.PersonNameBean bean = null;
		if(map != null)
			bean = (ims.core.vo.beans.PersonNameBean)map.getValueObjectBean(this);
		if (bean == null)
		{
			bean = new ims.core.vo.beans.PersonNameBean();
			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("NAMETYPE"))
			return getNameType();
		if(fieldName.equals("SURNAME"))
			return getSurname();
		if(fieldName.equals("FORENAME"))
			return getForename();
		if(fieldName.equals("MIDDLENAME"))
			return getMiddleName();
		if(fieldName.equals("TITLE"))
			return getTitle();
		if(fieldName.equals("UPPERSURNAME"))
			return getUpperSurname();
		if(fieldName.equals("UPPERFORENAME"))
			return getUpperForename();
		if(fieldName.equals("NAMESUFFIX"))
			return getNamesuffix();
		if(fieldName.equals("OBJECTIDENTIFIER"))
			return getObjectidentifier();
		if(fieldName.equals("BEFFDATE"))
			return getBeffdate();
		if(fieldName.equals("BEFFDATECNF"))
			return getBeffdatecnf();
		if(fieldName.equals("BETDATE"))
			return getBetdate();
		if(fieldName.equals("BETDATECNF"))
			return getBetdatecnf();
		if(fieldName.equals("PDSUPDATEMODE"))
			return getPdsUpdateMode();
		return super.getFieldValueByFieldName(fieldName);
	}
	public boolean getNameTypeIsNotNull()
	{
		return this.nametype != null;
	}
	public ims.core.vo.lookups.NameType getNameType()
	{
		return this.nametype;
	}
	public void setNameType(ims.core.vo.lookups.NameType value)
	{
		this.isValidated = false;
		this.nametype = value;
	}
	public boolean getSurnameIsNotNull()
	{
		return this.surname != null;
	}
	public String getSurname()
	{
		return this.surname;
	}
	public static int getSurnameMaxLength()
	{
		return 40;
	}
	public void setSurname(String value)
	{
		this.isValidated = false;
		this.surname = value;
	}
	public boolean getForenameIsNotNull()
	{
		return this.forename != null;
	}
	public String getForename()
	{
		return this.forename;
	}
	public static int getForenameMaxLength()
	{
		return 40;
	}
	public void setForename(String value)
	{
		this.isValidated = false;
		this.forename = value;
	}
	public boolean getMiddleNameIsNotNull()
	{
		return this.middlename != null;
	}
	public String getMiddleName()
	{
		return this.middlename;
	}
	public static int getMiddleNameMaxLength()
	{
		return 100;
	}
	public void setMiddleName(String value)
	{
		this.isValidated = false;
		this.middlename = value;
	}
	public boolean getTitleIsNotNull()
	{
		return this.title != null;
	}
	public ims.core.vo.lookups.PersonTitle getTitle()
	{
		return this.title;
	}
	public void setTitle(ims.core.vo.lookups.PersonTitle value)
	{
		this.isValidated = false;
		this.title = value;
	}
	public boolean getUpperSurnameIsNotNull()
	{
		return this.uppersurname != null;
	}
	public String getUpperSurname()
	{
		return this.uppersurname;
	}
	public static int getUpperSurnameMaxLength()
	{
		return 40;
	}
	public void setUpperSurname(String value)
	{
		this.isValidated = false;
		this.uppersurname = value;
	}
	public boolean getUpperForenameIsNotNull()
	{
		return this.upperforename != null;
	}
	public String getUpperForename()
	{
		return this.upperforename;
	}
	public static int getUpperForenameMaxLength()
	{
		return 40;
	}
	public void setUpperForename(String value)
	{
		this.isValidated = false;
		this.upperforename = value;
	}
	public boolean getNamesuffixIsNotNull()
	{
		return this.namesuffix != null;
	}
	public String getNamesuffix()
	{
		return this.namesuffix;
	}
	public static int getNamesuffixMaxLength()
	{
		return 35;
	}
	public void setNamesuffix(String value)
	{
		this.isValidated = false;
		this.namesuffix = value;
	}
	public boolean getObjectidentifierIsNotNull()
	{
		return this.objectidentifier != null;
	}
	public String getObjectidentifier()
	{
		return this.objectidentifier;
	}
	public static int getObjectidentifierMaxLength()
	{
		return 14;
	}
	public void setObjectidentifier(String value)
	{
		this.isValidated = false;
		this.objectidentifier = value;
	}
	public boolean getBeffdateIsNotNull()
	{
		return this.beffdate != null;
	}
	public ims.framework.utils.Date getBeffdate()
	{
		return this.beffdate;
	}
	public void setBeffdate(ims.framework.utils.Date value)
	{
		this.isValidated = false;
		this.beffdate = value;
	}
	public boolean getBeffdatecnfIsNotNull()
	{
		return this.beffdatecnf != null;
	}
	public Boolean getBeffdatecnf()
	{
		return this.beffdatecnf;
	}
	public void setBeffdatecnf(Boolean value)
	{
		this.isValidated = false;
		this.beffdatecnf = value;
	}
	public boolean getBetdateIsNotNull()
	{
		return this.betdate != null;
	}
	public ims.framework.utils.Date getBetdate()
	{
		return this.betdate;
	}
	public void setBetdate(ims.framework.utils.Date value)
	{
		this.isValidated = false;
		this.betdate = value;
	}
	public boolean getBetdatecnfIsNotNull()
	{
		return this.betdatecnf != null;
	}
	public Boolean getBetdatecnf()
	{
		return this.betdatecnf;
	}
	public void setBetdatecnf(Boolean value)
	{
		this.isValidated = false;
		this.betdatecnf = value;
	}
	public boolean getPdsUpdateModeIsNotNull()
	{
		return this.pdsupdatemode != null;
	}
	public ims.core.vo.lookups.PDSUpdateMode getPdsUpdateMode()
	{
		return this.pdsupdatemode;
	}
	public void setPdsUpdateMode(ims.core.vo.lookups.PDSUpdateMode value)
	{
		this.isValidated = false;
		this.pdsupdatemode = value;
	}
	/**
	* toString: Returns a formatted String suitable for display purposes
	*/
	public String toString()
	{
		java.lang.StringBuffer sb = new java.lang.StringBuffer("");
		if (this.title != null) {
			sb.append(this.title.getText() + " ");
		}
		if (this.forename != null && this.forename.length() > 0) {
			sb.append(this.forename + " ");
		}
		if (this.middlename != null && this.middlename.length() > 0) {
			sb.append(this.middlename + " ");
		}
		if (this.surname != null && this.surname.length() > 0) {
			sb.append(this.surname + " ");
		}
		if (sb.length() > 0)
		{
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}
	/**
	* setUppers() : Sets the upper case surname and forename first 10 chars
	*/
	public void setUppers()
	{
		String nonReplaceableCharsRegExp = ims.configuration.gen.ConfigFlag.GEN.PATIENT_SEARCH_ALLOW_NUMERIC_CHARS.getValue() ? "[^a-zA-Z0-9]" : "[^a-zA-Z]";
		if (this.surname != null)
		{
			//this.uppersurname = this.surname.toUpperCase().substring(0, Math.min(this.getUpperSurnameMaxLength(),this.surname.length()));
			this.uppersurname = this.surname.replaceAll(nonReplaceableCharsRegExp, "");
			this.uppersurname = this.uppersurname.toUpperCase().substring(0, Math.min(this.getUpperSurnameMaxLength(),this.uppersurname.length()));
		}
		if (this.forename != null)
		{
			//this.upperforename = this.forename.toUpperCase().substring(0, Math.min(this.getUpperForenameMaxLength(),this.forename.length()));
			this.upperforename = this.forename.replaceAll(nonReplaceableCharsRegExp, "");
			this.upperforename = this.upperforename.toUpperCase().substring(0, Math.min(this.getUpperForenameMaxLength(),this.upperforename.length()));
		}
	}
	/**
	* toShortForm(): Returns the Name in a short format
	*/
	public String toShortForm()
	{
		java.lang.StringBuffer sb = new java.lang.StringBuffer("");
		if (this.surname != null && this.surname.length() > 0) {
			sb.append(this.surname + ", ");
		}
		if (this.forename != null && this.forename.length() > 0) {
			sb.append(this.forename + ", ");
		}
		if (sb.length() > 0)
		{
			sb.setLength(sb.length() - 2);
		}
		return sb.toString();
	}
	/**
	* upperFirstChar() : sets the first charachter of a string to uppercase  
	*/
	public static String upperFirstChar(String value)
	{
		char arrVal[] = null;
		if(value != null && value.length() > 0)
		{
			String lowerValue = value.toLowerCase();
		
			arrVal = new char[lowerValue.length()];
			arrVal = lowerValue.toCharArray();
			arrVal[0] = String.valueOf(arrVal[0]).toUpperCase().charAt(0);
		}
			
		if(arrVal != null)
			return String.valueOf(arrVal);
		else
			return null;
	}
	/**
	* toProperCase
	*/
	public String toProperCase()
	{
		java.lang.StringBuffer sb = new java.lang.StringBuffer("");
		if (this.title != null) {
			sb.append(this.title.getText() + " ");
		}
		if (this.forename != null && this.forename.length() > 0) {
			sb.append(upperFirstChar(this.forename) + " ");
		}
		if (this.middlename != null && this.middlename.length() > 0) {
			sb.append(upperFirstChar(this.middlename) + " ");
		}
		if (this.surname != null && this.surname.length() > 0) {
			sb.append(upperFirstChar(this.surname) + " ");
		}
		if (sb.length() > 0)
		{
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}
	/**
	* toStringSurnameFirst
	*/
	public String toStringSurnameFirst()
	{
		java.lang.StringBuffer sb = new java.lang.StringBuffer("");
		if (this.surname != null && this.surname.length() > 0) {
			sb.append(this.surname + " ");
		}
	
		if (this.middlename != null && this.middlename.length() > 0) {
			sb.append(this.middlename + " ");
		}
		if (this.forename != null && this.forename.length() > 0) {
			sb.append(this.forename);
		}
		if (this.title != null) {
			sb.append(", " + this.title.getText() + " ");
		}
		//WDEV-20474
		if (sb.length() > 0)
		{
			sb.setLength(sb.length());
		}
		return sb.toString();
	}
	/**
	* pdsEquals
	*/
		public boolean pdsEquals(Object obj)
		{
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			PersonName other = (PersonName) obj;
			if (beffdate == null)
			{
				if (other.beffdate != null)
					return false;
			}
			else if (!beffdate.equals(other.beffdate))
				return false;
			if (beffdatecnf == null)
			{
				if (other.beffdatecnf != null)
					return false;
			}
			else if (!beffdatecnf.equals(other.beffdatecnf))
				return false;
			if (betdate == null)
			{
				if (other.betdate != null)
					return false;
			}
			else if (!betdate.equals(other.betdate))
				return false;
			if (betdatecnf == null)
			{
				if (other.betdatecnf != null)
					return false;
			}
			else if (!betdatecnf.equals(other.betdatecnf))
				return false;
			if (forename == null)
			{
				if (other.forename != null)
					return false;
			}
			else if (!forename.equals(other.forename))
				return false;
			if (isBusy != other.isBusy)
				return false;
			if (isValidated != other.isValidated)
				return false;
			if (middlename == null)
			{
				if (other.middlename != null)
					return false;
			}
			else if (!middlename.equals(other.middlename))
				return false;
			if (namesuffix == null)
			{
				if (other.namesuffix != null)
					return false;
			}
			else if (!namesuffix.equals(other.namesuffix))
				return false;
			if (nametype == null)
			{
				if (other.nametype != null)
					return false;
			}
			else if (!nametype.equals(other.nametype))
				return false;
			if (objectidentifier == null)
			{
				if (other.objectidentifier != null)
					return false;
			}
			else if (!objectidentifier.equals(other.objectidentifier))
				return false;
			if (surname == null)
			{
				if (other.surname != null)
					return false;
			}
			else if (!surname.equals(other.surname))
				return false;
			if (title == null)
			{
				if (other.title != null)
					return false;
			}
			else if (!title.equals(other.title))
				return false;
			if (upperforename == null)
			{
				if (other.upperforename != null)
					return false;
			}
			else if (!upperforename.equals(other.upperforename))
				return false;
			if (uppersurname == null)
			{
				if (other.uppersurname != null)
					return false;
			}
			else if (!uppersurname.equals(other.uppersurname))
				return false;
			return true;
		}
	
	public boolean isValidated()
	{
		if(this.isBusy)
			return true;
		this.isBusy = true;
	
		if(!this.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.surname != null)
			if(this.surname.length() > 40)
				listOfErrors.add("The length of the field [surname] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 40");
		if(this.forename != null)
			if(this.forename.length() > 40)
				listOfErrors.add("The length of the field [forename] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 40");
		if(this.middlename != null)
			if(this.middlename.length() > 100)
				listOfErrors.add("The length of the field [middlename] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 100");
		if(this.uppersurname != null)
			if(this.uppersurname.length() > 40)
				listOfErrors.add("The length of the field [uppersurname] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 40");
		if(this.upperforename != null)
			if(this.upperforename.length() > 40)
				listOfErrors.add("The length of the field [upperforename] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 40");
		if(this.namesuffix != null)
			if(this.namesuffix.length() > 35)
				listOfErrors.add("The length of the field [namesuffix] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 35");
		if(this.objectidentifier != null)
			if(this.objectidentifier.length() > 14)
				listOfErrors.add("The length of the field [objectidentifier] in the value object [ims.core.vo.PersonName] is too big. It should be less or equal to 14");
		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;
	
		PersonName clone = new PersonName(this.id, this.version);
		
		if(this.nametype == null)
			clone.nametype = null;
		else
			clone.nametype = (ims.core.vo.lookups.NameType)this.nametype.clone();
		clone.surname = this.surname;
		clone.forename = this.forename;
		clone.middlename = this.middlename;
		if(this.title == null)
			clone.title = null;
		else
			clone.title = (ims.core.vo.lookups.PersonTitle)this.title.clone();
		clone.uppersurname = this.uppersurname;
		clone.upperforename = this.upperforename;
		clone.namesuffix = this.namesuffix;
		clone.objectidentifier = this.objectidentifier;
		if(this.beffdate == null)
			clone.beffdate = null;
		else
			clone.beffdate = (ims.framework.utils.Date)this.beffdate.clone();
		clone.beffdatecnf = this.beffdatecnf;
		if(this.betdate == null)
			clone.betdate = null;
		else
			clone.betdate = (ims.framework.utils.Date)this.betdate.clone();
		clone.betdatecnf = this.betdatecnf;
		if(this.pdsupdatemode == null)
			clone.pdsupdatemode = null;
		else
			clone.pdsupdatemode = (ims.core.vo.lookups.PDSUpdateMode)this.pdsupdatemode.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 (!(PersonName.class.isAssignableFrom(obj.getClass())))
		{
			throw new ClassCastException("A PersonName object cannot be compared an Object of type " + obj.getClass().getName());
		}
		PersonName compareObj = (PersonName)obj;
		int retVal = 0;
		if (retVal == 0)
		{
			if(this.getSurname() == null && compareObj.getSurname() != null)
				return -1;
			if(this.getSurname() != null && compareObj.getSurname() == null)
				return 1;
			if(this.getSurname() != null && compareObj.getSurname() != null)
			{
				if(caseInsensitive)
					retVal = this.getSurname().toLowerCase().compareTo(compareObj.getSurname().toLowerCase());
				else
					retVal = this.getSurname().compareTo(compareObj.getSurname());
			}
		}
		if (retVal == 0)
		{
			if(this.getForename() == null && compareObj.getForename() != null)
				return -1;
			if(this.getForename() != null && compareObj.getForename() == null)
				return 1;
			if(this.getForename() != null && compareObj.getForename() != null)
			{
				if(caseInsensitive)
					retVal = this.getForename().toLowerCase().compareTo(compareObj.getForename().toLowerCase());
				else
					retVal = this.getForename().compareTo(compareObj.getForename());
			}
		}
		return retVal;
	}
	public synchronized static int generateValueObjectUniqueID()
	{
		return ims.vo.ValueObject.generateUniqueID();
	}
	public int countFieldsWithValue()
	{
		int count = 0;
		if(this.nametype != null)
			count++;
		if(this.surname != null)
			count++;
		if(this.forename != null)
			count++;
		if(this.middlename != null)
			count++;
		if(this.title != null)
			count++;
		if(this.uppersurname != null)
			count++;
		if(this.upperforename != null)
			count++;
		if(this.namesuffix != null)
			count++;
		if(this.objectidentifier != null)
			count++;
		if(this.beffdate != null)
			count++;
		if(this.beffdatecnf != null)
			count++;
		if(this.betdate != null)
			count++;
		if(this.betdatecnf != null)
			count++;
		if(this.pdsupdatemode != null)
			count++;
		return count;
	}
	public int countValueObjectFields()
	{
		return 14;
	}
	protected ims.core.vo.lookups.NameType nametype;
	protected String surname;
	protected String forename;
	protected String middlename;
	protected ims.core.vo.lookups.PersonTitle title;
	protected String uppersurname;
	protected String upperforename;
	protected String namesuffix;
	protected String objectidentifier;
	protected ims.framework.utils.Date beffdate;
	protected Boolean beffdatecnf;
	protected ims.framework.utils.Date betdate;
	protected Boolean betdatecnf;
	protected ims.core.vo.lookups.PDSUpdateMode pdsupdatemode;
	private boolean isValidated = false;
	private boolean isBusy = false;
}
