/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/ValueObjects/src/ims/core/vo/TrackingForPendingEmergencyAdmitVo.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 emergency.Tracking business object (ID: 1086100005).
 */
public class TrackingForPendingEmergencyAdmitVo extends ims.emergency.vo.TrackingRefVo implements ims.vo.ImsCloneable, Comparable, ims.vo.interfaces.IPendingAdmission, ims.vo.interfaces.IPendingAdmissionDetails
{
	private static final long serialVersionUID = 1L;

	public TrackingForPendingEmergencyAdmitVo()
	{
	}
	public TrackingForPendingEmergencyAdmitVo(Integer id, int version)
	{
		super(id, version);
	}
	public TrackingForPendingEmergencyAdmitVo(ims.core.vo.beans.TrackingForPendingEmergencyAdmitVoBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.attendance = bean.getAttendance() == null ? null : bean.getAttendance().buildVo();
		this.associatedpendingemergencyadmission = bean.getAssociatedPendingEmergencyAdmission() == null ? null : bean.getAssociatedPendingEmergencyAdmission().buildVo();
		this.patient = bean.getPatient() == null ? null : bean.getPatient().buildVo();
		this.los = bean.getLOS();
		this.bedavailability = bean.getBedAvailability() == null ? null : bean.getBedAvailability().buildVo();
	}
	public void populate(ims.vo.ValueObjectBeanMap map, ims.core.vo.beans.TrackingForPendingEmergencyAdmitVoBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.attendance = bean.getAttendance() == null ? null : bean.getAttendance().buildVo(map);
		this.associatedpendingemergencyadmission = bean.getAssociatedPendingEmergencyAdmission() == null ? null : bean.getAssociatedPendingEmergencyAdmission().buildVo(map);
		this.patient = bean.getPatient() == null ? null : bean.getPatient().buildVo(map);
		this.los = bean.getLOS();
		this.bedavailability = bean.getBedAvailability() == null ? null : bean.getBedAvailability().buildVo(map);
	}
	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.TrackingForPendingEmergencyAdmitVoBean bean = null;
		if(map != null)
			bean = (ims.core.vo.beans.TrackingForPendingEmergencyAdmitVoBean)map.getValueObjectBean(this);
		if (bean == null)
		{
			bean = new ims.core.vo.beans.TrackingForPendingEmergencyAdmitVoBean();
			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("ATTENDANCE"))
			return getAttendance();
		if(fieldName.equals("ASSOCIATEDPENDINGEMERGENCYADMISSION"))
			return getAssociatedPendingEmergencyAdmission();
		if(fieldName.equals("PATIENT"))
			return getPatient();
		if(fieldName.equals("LOS"))
			return getLOS();
		if(fieldName.equals("BEDAVAILABILITY"))
			return getBedAvailability();
		return super.getFieldValueByFieldName(fieldName);
	}
	public boolean getAttendanceIsNotNull()
	{
		return this.attendance != null;
	}
	public ims.emergency.vo.EmergencyAttendanceLiteVo getAttendance()
	{
		return this.attendance;
	}
	public void setAttendance(ims.emergency.vo.EmergencyAttendanceLiteVo value)
	{
		this.isValidated = false;
		this.attendance = value;
	}
	public boolean getAssociatedPendingEmergencyAdmissionIsNotNull()
	{
		return this.associatedpendingemergencyadmission != null;
	}
	public ims.core.vo.PendingEmergencyAdmissionAdmitVo getAssociatedPendingEmergencyAdmission()
	{
		return this.associatedpendingemergencyadmission;
	}
	public void setAssociatedPendingEmergencyAdmission(ims.core.vo.PendingEmergencyAdmissionAdmitVo value)
	{
		this.isValidated = false;
		this.associatedpendingemergencyadmission = value;
	}
	public boolean getPatientIsNotNull()
	{
		return this.patient != null;
	}
	public ims.core.vo.PatientLite_IdentifiersVo getPatient()
	{
		return this.patient;
	}
	public void setPatient(ims.core.vo.PatientLite_IdentifiersVo value)
	{
		this.isValidated = false;
		this.patient = value;
	}
	public boolean getLOSIsNotNull()
	{
		return this.los != null;
	}
	public Integer getLOS()
	{
		return this.los;
	}
	public void setLOS(Integer value)
	{
		this.isValidated = false;
		this.los = value;
	}
	public boolean getBedAvailabilityIsNotNull()
	{
		return this.bedavailability != null;
	}
	public ims.emergency.vo.BedAvailabilityForTrackingVo getBedAvailability()
	{
		return this.bedavailability;
	}
	public void setBedAvailability(ims.emergency.vo.BedAvailabilityForTrackingVo value)
	{
		this.isValidated = false;
		this.bedavailability = value;
	}
	/**
	* IPendingAdmission & IPendingAdmissionDetails implementation
	*/
	public ServiceLiteVo getPendingAdmissionService()
	{
		if (this.associatedpendingemergencyadmission != null)
			return this.associatedpendingemergencyadmission.service;
			
		return null;
	}
		
	public ims.core.vo.lookups.Specialty getPendingAdmissionSpecialty()
	{
		if (this.associatedpendingemergencyadmission != null)
			return this.associatedpendingemergencyadmission.specialty;
			
		return null;
	}
	
	public MemberOfStaffLiteVo getPendingAdmissionConsultant()
	{
	
		return null;
	}
	
	public ims.core.vo.lookups.MethodOfAdmission getPendingAdmissionMethodOfAdmission()
	{
		return ims.core.vo.lookups.MethodOfAdmission.EMERGENCY;
	}
	
	public ims.core.vo.lookups.PatientStatus getPendingAdmissionPatientCategory()
	{
		return patient.patientcategory;
	}
	
	public Integer getPendingAdmissionAnticipatedStay()
	{
		return null;
	}
	
	public ims.core.vo.lookups.SourceOfEmergencyReferral getPendingAdmissionSourceOfEmergencyReferral()
	{
		return ims.core.vo.lookups.SourceOfEmergencyReferral.ACCIDENT_EMERGENCY;
	}
	
	public ims.core.vo.lookups.SourceOfReferral getPendingAdmissionSourceOfReferral()
	{
		return null;
	}
	
	public String getPendingAdmissionReasonForAdmission()
	{
		return null;
	}
	
	public MedicLiteVo getPendingAdmissionReferringConsultant()
	{
		return null;
	}
	
	public Boolean getPendingAdmissionSelfAdmit()
	{
		return null;
	}
	
	public String getPatientSurname()
	{
		if (this.patient != null)
			return this.patient.getPatientSurname();
	
		return null;
	}
	
	public String getPatientForename()
	{
		if (this.patient != null)
			return this.patient.getPatientForename();
	
		return null;
	}
	
	public PatientId getPatientDisplayIdentifier()
	{
		if (this.patient != null)
			return this.patient.getPatientDisplayIdentifier();
		
		return null;
	}
	
	public ims.core.vo.lookups.Sex getPatientSex()
	{
		if (this.patient != null)
			return this.patient.getPatientSex();
	
		return null;
	}
	
	public Integer getPatientAge()
	{
		if (this.patient != null)
			return this.patient.getPatientAge();
	
		return null;
	}
	
	public ims.framework.utils.PartialDate getPatientDoB()
	{
		if (this.patient != null)
			return this.patient.getPatientDoB();
	
		return null;
	}
	
	public String getLengthOfStay()
	{
		boolean isDischarged = this.attendance != null && this.attendance.getDischargeDateTime() != null;
		
		ims.framework.utils.DateTime endRegistration = this.attendance != null ? (this.attendance.getEndOfRegistrationDateTime() != null ? this.attendance.getEndOfRegistrationDateTime() : this.attendance.getRegistrationDateTime()) : null;
		ims.framework.utils.DateTime endLoS = isDischarged ? this.attendance.getDischargeDateTime() : new ims.framework.utils.DateTime();
		
		Integer intLOS = null;
		String minHourLOS = null;
		
		if (ims.configuration.gen.ConfigFlag.DOM.STARTTIME_FOR_LOS_AND_BREACH_CALCULATIONS.getValue().equals("RegistrationDateTime"))
		{
			intLOS = calculateDateDiffInMinutes(endLoS, endRegistration);
			minHourLOS = (minutesToHoursAndMinutes(intLOS));
		}
		else
		{
			intLOS = calculateDateDiffInMinutes(endLoS, this.attendance.getArrivalDateTime());
			minHourLOS = (minutesToHoursAndMinutes(intLOS));
		}
		
		return minHourLOS;
	}
	
	
	private Integer calculateDateDiffInMinutes(ims.framework.utils.DateTime currentTime, ims.framework.utils.DateTime registrationDateTime) 
	{
		if(currentTime == null || registrationDateTime == null)
			return null;
		
		long currentTimeInMillis = currentTime.getJavaDate().getTime();
		long registrationDateTimeInMillis = registrationDateTime.getJavaDate().getTime();
		
		return (int)(((currentTimeInMillis < registrationDateTimeInMillis ? registrationDateTimeInMillis - currentTimeInMillis : currentTimeInMillis - registrationDateTimeInMillis)/1000)/60);
	}
	
	
	public String minutesToHoursAndMinutes(Integer minutes) 
	{
		if(minutes == null)
			return null;
		
		int hours = minutes/60;
		int mins = minutes%60;
		
		StringBuilder time = new StringBuilder();
		
		if(hours > 0)
		{
			time.append(hours + "h");
		}
		
		if(mins > 0)
		{
			time.append((time.length() > 0 ? " " : "") + ((mins >= 0 && mins <= 9 && time.length() > 0) ? "0" : "") + mins + "m");
		}
		
		if(time.length() > 0)
			return time.toString();
		
		return null;
	}
	
	
	public ims.framework.utils.DateTime getTCIDateTime()
	{
		return null;
	}
	
	public Boolean hasTCIComments()
	{
		return null;
	}
	
	public ims.framework.utils.Time getWardAttendanceAppointmentTime()
	{
		return null;
	}
	public ims.framework.utils.Date getPatientDod()
	{
		return this.patient.getDod();
	}
	
	public ims.core.patient.vo.PatientRefVo getPatientAssociatedPatient()
	{
		return this.patient.getAssociatedPatient();
	}
	
	public ims.framework.utils.Time getPatientTimeOfDeath()
	{
		return this.patient.getTimeOfDeath();
	}
	
	public String getPatientAgeText()
	{
		return this.patient.calculateAgeText();
	}
	
	public ims.RefMan.vo.CatsReferralRefVo getCatsReferral()
	{
		return null;
	}
	
	/**
	* getIntLengthOfStay()
	*/
	public Integer getIntLengthOfStay()
	{
		boolean isDischarged = this.attendance != null && this.attendance.getDischargeDateTime() != null;
		ims.framework.utils.DateTime registrDateTimeForLOS = this.attendance != null ? (this.attendance.getEndOfRegistrationDateTimeIsNotNull() ? this.attendance.getEndOfRegistrationDateTime() : this.attendance.getRegistrationDateTime()) : null;
		ims.framework.utils.DateTime endTimeForLOS = isDischarged ? this.attendance.getDischargeDateTime() : new ims.framework.utils.DateTime();
	
		java.lang.Integer intLOS = null;
			
		if (ims.configuration.gen.ConfigFlag.DOM.STARTTIME_FOR_LOS_AND_BREACH_CALCULATIONS.getValue().equals("RegistrationDateTime"))	//wdev-18327
		{	
			intLOS = calculateDateDiffInMinutes(endTimeForLOS, registrDateTimeForLOS);
		}	
		else
		{	
			intLOS = calculateDateDiffInMinutes(endTimeForLOS, this.attendance.getArrivalDateTime());
		}
		return intLOS;
	}
	
	/**
	* TrackingForPendingEmergencyAdmitVoLOSComparator (class definition)
	*/
	public static class TrackingForPendingEmergencyAdmitVoLOSComparator implements java.util.Comparator<ims.core.vo.TrackingForPendingEmergencyAdmitVo>
	{
		private int order = 1;
		public TrackingForPendingEmergencyAdmitVoLOSComparator()
		{
			this(ims.framework.enumerations.SortOrder.ASCENDING);
		}
		public TrackingForPendingEmergencyAdmitVoLOSComparator(ims.framework.enumerations.SortOrder order)
		{
			this.order = ims.framework.enumerations.SortOrder.DESCENDING.equals(order) ? -1 : 1;
		}
		public int compare(ims.core.vo.TrackingForPendingEmergencyAdmitVo o1, ims.core.vo.TrackingForPendingEmergencyAdmitVo o2)
		{
			if (o1 != null && o2 != null && o1.getLOSIsNotNull()&& o2.getLOSIsNotNull())
			{
				return order*o1.getLOS().compareTo(o2.getLOS());
			}
			if (o1 == null || !o1.getLOSIsNotNull())
				return -1*order;
			if (o2 == null || !o2.getLOSIsNotNull())
				return order;
			
			return 0;
		}	
	}
	/**
	* getTrackingForPendingEmergencyAdmitVoLOSComparator() - retrieves a new instance of static class TrackingForPendingEmergencyAdmitVoLOSComparator
	*/
	public static TrackingForPendingEmergencyAdmitVoLOSComparator getLOSComparator(ims.framework.enumerations.SortOrder sortOrder)
	{
		return new TrackingForPendingEmergencyAdmitVo.TrackingForPendingEmergencyAdmitVoLOSComparator(sortOrder);
	}
	
	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.attendance == null)
			listOfErrors.add("Attendance is mandatory");
		if(this.patient == null)
			listOfErrors.add("Patient is mandatory");
		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;
	
		TrackingForPendingEmergencyAdmitVo clone = new TrackingForPendingEmergencyAdmitVo(this.id, this.version);
		
		if(this.attendance == null)
			clone.attendance = null;
		else
			clone.attendance = (ims.emergency.vo.EmergencyAttendanceLiteVo)this.attendance.clone();
		if(this.associatedpendingemergencyadmission == null)
			clone.associatedpendingemergencyadmission = null;
		else
			clone.associatedpendingemergencyadmission = (ims.core.vo.PendingEmergencyAdmissionAdmitVo)this.associatedpendingemergencyadmission.clone();
		if(this.patient == null)
			clone.patient = null;
		else
			clone.patient = (ims.core.vo.PatientLite_IdentifiersVo)this.patient.clone();
		clone.los = this.los;
		if(this.bedavailability == null)
			clone.bedavailability = null;
		else
			clone.bedavailability = (ims.emergency.vo.BedAvailabilityForTrackingVo)this.bedavailability.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 (!(TrackingForPendingEmergencyAdmitVo.class.isAssignableFrom(obj.getClass())))
		{
			throw new ClassCastException("A TrackingForPendingEmergencyAdmitVo object cannot be compared an Object of type " + obj.getClass().getName());
		}
		if (this.id == null)
			return 1;
		if (((TrackingForPendingEmergencyAdmitVo)obj).getBoId() == null)
			return -1;
		return this.id.compareTo(((TrackingForPendingEmergencyAdmitVo)obj).getBoId());
	}
	public synchronized static int generateValueObjectUniqueID()
	{
		return ims.vo.ValueObject.generateUniqueID();
	}
	public int countFieldsWithValue()
	{
		int count = 0;
		if(this.attendance != null)
			count++;
		if(this.associatedpendingemergencyadmission != null)
			count++;
		if(this.patient != null)
			count++;
		if(this.los != null)
			count++;
		if(this.bedavailability != null)
			count++;
		return count;
	}
	public int countValueObjectFields()
	{
		return 5;
	}
	protected ims.emergency.vo.EmergencyAttendanceLiteVo attendance;
	protected ims.core.vo.PendingEmergencyAdmissionAdmitVo associatedpendingemergencyadmission;
	protected ims.core.vo.PatientLite_IdentifiersVo patient;
	protected Integer los;
	protected ims.emergency.vo.BedAvailabilityForTrackingVo bedavailability;
	private boolean isValidated = false;
	private boolean isBusy = false;
}
