/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/ValueObjects/src/ims/pci/vo/ScheduleConfigurationVo.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.pci.vo;

/**
 * Linked to PCI.ScheduleConfiguration business object (ID: 1091100002).
 */
public class ScheduleConfigurationVo extends ims.pci.vo.ScheduleConfigurationLiteVo implements ims.vo.ImsCloneable, Comparable
{
	private static final long serialVersionUID = 1L;

	public ScheduleConfigurationVo()
	{
	}
	public ScheduleConfigurationVo(Integer id, int version)
	{
		super(id, version);
	}
	public ScheduleConfigurationVo(ims.pci.vo.beans.ScheduleConfigurationVoBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.schedulename = bean.getScheduleName();
		this.status = bean.getStatus() == null ? null : ims.core.vo.lookups.PreActiveActiveInactiveStatus.buildLookup(bean.getStatus());
		this.startdob = bean.getStartDob() == null ? null : bean.getStartDob().buildDate();
		this.enddob = bean.getEndDob() == null ? null : bean.getEndDob().buildDate();
		this.isdefaultschedule = bean.getIsDefaultSchedule();
		this.scheduletype = bean.getScheduleType() == null ? null : ims.pci.vo.lookups.ScheduleType.buildLookup(bean.getScheduleType());
		this.campaignstartdate = bean.getCampaignStartDate() == null ? null : bean.getCampaignStartDate().buildDate();
		this.stages = ims.pci.vo.StageConfigurationVoCollection.buildFromBeanCollection(bean.getStages());
	}
	public void populate(ims.vo.ValueObjectBeanMap map, ims.pci.vo.beans.ScheduleConfigurationVoBean bean)
	{
		this.id = bean.getId();
		this.version = bean.getVersion();
		this.schedulename = bean.getScheduleName();
		this.status = bean.getStatus() == null ? null : ims.core.vo.lookups.PreActiveActiveInactiveStatus.buildLookup(bean.getStatus());
		this.startdob = bean.getStartDob() == null ? null : bean.getStartDob().buildDate();
		this.enddob = bean.getEndDob() == null ? null : bean.getEndDob().buildDate();
		this.isdefaultschedule = bean.getIsDefaultSchedule();
		this.scheduletype = bean.getScheduleType() == null ? null : ims.pci.vo.lookups.ScheduleType.buildLookup(bean.getScheduleType());
		this.campaignstartdate = bean.getCampaignStartDate() == null ? null : bean.getCampaignStartDate().buildDate();
		this.stages = ims.pci.vo.StageConfigurationVoCollection.buildFromBeanCollection(bean.getStages());
	}
	public ims.vo.ValueObjectBean getBean()
	{
		return this.getBean(new ims.vo.ValueObjectBeanMap());
	}
	public ims.vo.ValueObjectBean getBean(ims.vo.ValueObjectBeanMap map)
	{
		ims.pci.vo.beans.ScheduleConfigurationVoBean bean = null;
		if(map != null)
			bean = (ims.pci.vo.beans.ScheduleConfigurationVoBean)map.getValueObjectBean(this);
		if (bean == null)
		{
			bean = new ims.pci.vo.beans.ScheduleConfigurationVoBean();
			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("STAGES"))
			return getStages();
		return super.getFieldValueByFieldName(fieldName);
	}
	public boolean getStagesIsNotNull()
	{
		return this.stages != null;
	}
	public ims.pci.vo.StageConfigurationVoCollection getStages()
	{
		return this.stages;
	}
	public void setStages(ims.pci.vo.StageConfigurationVoCollection value)
	{
		this.isValidated = false;
		this.stages = value;
	}
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		if(this.schedulename != null)
			sb.append(this.schedulename);
		return sb.toString();
	}
	/**
	* Custom validate method - checks for startDOB < endDOB and if default is set only when the schedule is active. Checks for duplicate stages. Checks if the same vaccine has the same shot number.
	*/
	public String[] validateCustomRules(String[] existingErrors)
	{
		existingErrors = this.validate(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 (ims.pci.vo.lookups.ScheduleType.OUTBREAKSCHEDULE.equals(this.scheduletype))
		{
			if (this.campaignstartdate == null)
				listOfErrors.add("Campaign start date is mandatory");
		}
		else if (ims.pci.vo.lookups.ScheduleType.INVALIDVACCINESCHEDULE.equals(this.scheduletype) || ims.pci.vo.lookups.ScheduleType.PCISCHEDULE.equals(this.scheduletype))
		{
			if (this.startdob == null)
				listOfErrors.add("Start DOB is mandatory");
		}
	
		if ((this.startdob != null) && (this.enddob != null && this.startdob.isGreaterThan(this.enddob)))
			listOfErrors.add("StartDob can not be set after EndDob");
	
		if (ims.core.vo.lookups.PreActiveActiveInactiveStatus.PREACTIVE.equals(this.status) && this.isdefaultschedule)
			listOfErrors.add("Schedule can be set as default only if it is active");
	
		if(this.stages == null || this.stages.size() == 0)
			listOfErrors.add("Stages are mandatory");
		else
		{
			// Check for duplicate stages
			for (int i = 0; i < this.stages.size() - 1; i++)
			{
				StageConfigurationVo markStage = this.stages.get(i);
				if (markStage == null || markStage.stage == null)
					continue;
	
				for (int j = i + 1; j < this.stages.size(); j++)
				{
					StageConfigurationVo stageCompare = this.stages.get(j);
					if (stageCompare == null)
						continue;
	
					if (markStage.stage.equals(stageCompare.stage))
						listOfErrors.add("This schedule contains duplicates stages. " + markStage.stage.getText() + " is present more than once.");
				}
			}
	
	
			// Check for vaccines with the same shot number
			for (int i = 0; i < this.stages.size() - 1; i++)
			{
				StageConfigurationVo markStage = this.stages.get(i);
				if (markStage == null || markStage.stagevaccines == null)
					continue;
	
				for (int k = 0; k < markStage.stagevaccines.size(); k++)
				{
					StageVaccineVo markVaccine = markStage.stagevaccines.get(k);
					if (markVaccine == null || markVaccine.shotno == null || markVaccine.vaccine == null)
						continue;
	
					for (int j = i + 1; j < this.stages.size(); j++)
					{
						StageConfigurationVo stageCompare = this.stages.get(j);
						if (stageCompare == null || stageCompare.stagevaccines == null)
							continue;
	
						for (int index = 0; index < stageCompare.stagevaccines.size(); index++)
						{
							StageVaccineVo vaccineCompare = stageCompare.stagevaccines.get(index);
							if (vaccineCompare == null)
								continue;
	
							if (markVaccine.equals(vaccineCompare) && markVaccine.shotno.equals(vaccineCompare.shotno))
								listOfErrors.add("This schedule contains vaccines with the same shot number. " + markVaccine.vaccine.getVaccineName() + " has the same shot number in more than one stage:" + markVaccine.shotno.toString());
						}
					}
				}
			}
	
			//Check for driver and minimum interval value in months	
			for (int i = 0; i < this.stages.size(); i++)
			{
				StageConfigurationVo stage = this.stages.get(i);
				if (stage == null || stage.stagevaccines == null)
					continue;
						
				Boolean hasNoInterval = false;
					
				for (int j = 0; j < stage.stagevaccines.size(); j++)
				{
					StageVaccineVo vaccine = stage.stagevaccines.get(j);
					if (vaccine == null)
						continue;
								
					if(vaccine.driver != null && vaccine.driver)
					{
						if(vaccine.intervalmonths == null)
						{
							listOfErrors.add("Interval(Months) is mandatory");
							hasNoInterval = true;
							break;
						}
					}
				}	
					
				if(hasNoInterval)
					break;
			}
		}
	
		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 updateVaccinesCodesAndDriver(StageConfigurationVoCollection value)
	*/
	public void updateVaccinesCodesAndDriver(ims.pci.vo.StageConfigurationVoCollection value) throws ims.domain.exceptions.DomainInterfaceException
	{
		if (value == null)
			return;
	
		if (this.stages.size() != value.size())
			throw new ims.domain.exceptions.DomainInterfaceException("Stage configutation provided not matching the current stage configuration");
	
		for (int i = 0; i < this.stages.size(); i++)
		{
			if (this.stages.get(i) == null)
				throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
	
			if (this.stages.get(i).getStage() == null)
				throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
	
			if (!this.stages.get(i).getStage().equals(value.get(i).getStage()))
			{
				throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
			}
	
			if (!ims.pci.vo.lookups.ScheduleType.OUTBREAKSCHEDULE.equals(this.scheduletype))
			{
				if (this.stages.get(i).getAgeInMonths() == null)
					throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
	
				if (!this.stages.get(i).getAgeInMonths().equals(value.get(i).getAgeInMonths()))
				{
					throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
				}
			}
	
			if (this.stages.get(i).getStageVaccines() == null)
			{
				if (value.get(i).getStageVaccines() == null)
					continue;
				else
					throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
			}
	
			if (this.stages.get(i).getStageVaccines().size() != value.get(i).getStageVaccines().size())
			{
				throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
			}
	
			for (int k = 0; k < this.stages.get(i).getStageVaccines().size(); k++)
			{
				if (!this.stages.get(i).getStageVaccines().get(k).getVaccine().equals(value.get(i).getStageVaccines().get(k).getVaccine()))
					throw new ims.domain.exceptions.DomainInterfaceException("Stage configuration provided not matching the current stage configuration");
			}
		}
	
		for (int i = 0; i < this.stages.size(); i++)
		{
			if (this.stages.get(i) == null)
				continue;
	
			for (int k = 0; k < this.stages.get(i).getStageVaccines().size(); k++)
			{
				if (this.stages.get(i).getStageVaccines().get(k) == null)
					continue;
	
				this.stages.get(i).getStageVaccines().get(k).setPcrsCode(value.get(i).getStageVaccines().get(k).getPcrsCode());
				
				//wdev-7566
				this.stages.get(i).getStageVaccines().get(k).setDriver(value.get(i).getStageVaccines().get(k).getDriver());
				this.stages.get(i).getStageVaccines().get(k).setIntervalMonths(value.get(i).getStageVaccines().get(k).getIntervalMonths());
	
				//WDEV-9612 
				this.stages.get(i).getStageVaccines().get(k).setCanBeRescheduled(value.get(i).getStageVaccines().get(k).getCanBeRescheduled());
	
				//WDEV-8654 
				this.stages.get(i).getStageVaccines().get(k).setIsLastShotForVaccine(value.get(i).getStageVaccines().get(k).getIsLastShotForVaccine());
			}
		}
	}
	
	public boolean isValidated()
	{
		if(this.isBusy)
			return true;
		this.isBusy = true;
	
		if(!this.isValidated)
		{
			this.isBusy = false;
			return false;
		}
		if(this.stages != null)
		{
			if(!this.stages.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.schedulename == null || this.schedulename.length() == 0)
			listOfErrors.add("ScheduleName is mandatory");
		else if(this.schedulename.length() > 75)
			listOfErrors.add("The length of the field [schedulename] in the value object [ims.pci.vo.ScheduleConfigurationVo] is too big. It should be less or equal to 75");
		if(this.status == null)
			listOfErrors.add("Status is mandatory");
		if(this.isdefaultschedule == null)
			listOfErrors.add("isDefaultSchedule is mandatory");
		if(this.scheduletype == null)
			listOfErrors.add("ScheduleType is mandatory");
		if(this.stages == null || this.stages.size() == 0)
			listOfErrors.add("Stages are mandatory");
		if(this.stages != null)
		{
			String[] listOfOtherErrors = this.stages.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;
	
		ScheduleConfigurationVo clone = new ScheduleConfigurationVo(this.id, this.version);
		
		clone.schedulename = this.schedulename;
		if(this.status == null)
			clone.status = null;
		else
			clone.status = (ims.core.vo.lookups.PreActiveActiveInactiveStatus)this.status.clone();
		if(this.startdob == null)
			clone.startdob = null;
		else
			clone.startdob = (ims.framework.utils.Date)this.startdob.clone();
		if(this.enddob == null)
			clone.enddob = null;
		else
			clone.enddob = (ims.framework.utils.Date)this.enddob.clone();
		clone.isdefaultschedule = this.isdefaultschedule;
		if(this.scheduletype == null)
			clone.scheduletype = null;
		else
			clone.scheduletype = (ims.pci.vo.lookups.ScheduleType)this.scheduletype.clone();
		if(this.campaignstartdate == null)
			clone.campaignstartdate = null;
		else
			clone.campaignstartdate = (ims.framework.utils.Date)this.campaignstartdate.clone();
		if(this.stages == null)
			clone.stages = null;
		else
			clone.stages = (ims.pci.vo.StageConfigurationVoCollection)this.stages.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 (!(ScheduleConfigurationVo.class.isAssignableFrom(obj.getClass())))
		{
			throw new ClassCastException("A ScheduleConfigurationVo object cannot be compared an Object of type " + obj.getClass().getName());
		}
		if (this.id == null)
			return 1;
		if (((ScheduleConfigurationVo)obj).getBoId() == null)
			return -1;
		return this.id.compareTo(((ScheduleConfigurationVo)obj).getBoId());
	}
	public synchronized static int generateValueObjectUniqueID()
	{
		return ims.vo.ValueObject.generateUniqueID();
	}
	public int countFieldsWithValue()
	{
		int count = super.countFieldsWithValue();
		if(this.stages != null)
			count++;
		return count;
	}
	public int countValueObjectFields()
	{
		return super.countValueObjectFields() + 1;
	}
	protected ims.pci.vo.StageConfigurationVoCollection stages;
	private boolean isValidated = false;
	private boolean isBusy = false;
}
