/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/domain/impl/EncounterOutcomeConfigurationImpl.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 Bogdan Tofei using IMS Development Environment (version 1.80 build 5308.16958)
// Copyright (C) 1995-2014 IMS MAXIMS. All rights reserved.

package ims.pathways.domain.impl;

import ims.core.vo.lookups.PreActiveActiveInactiveStatus;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.pathways.configuration.vo.EventRefVo;
import ims.pathways.domain.base.impl.BaseEncounterOutcomeConfigurationImpl;
import ims.pathways.domain.objects.RTTStatusEventMap;
import ims.pathways.vo.EventLiteVoCollection;
import ims.pathways.vo.RTTStatusEventMapRefVo;
import ims.pathways.vo.RTTStatusEventMapVo;
import ims.pathways.vo.RTTStatusEventMapVoCollection;
import ims.pathways.vo.RTTStatusPointVo;
import ims.pathways.vo.RTTStatusPointVoCollection;
import ims.pathways.vo.domain.EventLiteVoAssembler;
import ims.pathways.vo.domain.RTTStatusEventMapVoAssembler;
import ims.pathways.vo.domain.RTTStatusPointVoAssembler;
import ims.pathways.vo.lookups.EventEncounterType;

import java.util.ArrayList;
import java.util.List;

public class EncounterOutcomeConfigurationImpl extends BaseEncounterOutcomeConfigurationImpl
{

	private static final long serialVersionUID = 1L;
	
	private static final int DECISION_NOT_TO_TREAT_NATIONAL_CODE = 34;
	private static final int PATIENT_DECLINED_OFFERED_TREATMENT = 35;
	private static final int PATIENT_DIED_BEFORE_TREATMENT = 36;

	public ims.pathways.vo.RTTStatusPointVoCollection listRTTStatusPoints(Boolean excludeDeclinedTreatmentCodes)
	{
		DomainFactory fact = getDomainFactory();
		StringBuilder hqlMain = new StringBuilder();
		hqlMain.append("select rt from RTTStatusPoint as rt");
		if (Boolean.TRUE.equals(excludeDeclinedTreatmentCodes))
		{
			hqlMain.append(" WHERE nationalco NOT IN" +  "(" + DECISION_NOT_TO_TREAT_NATIONAL_CODE + ", " + PATIENT_DECLINED_OFFERED_TREATMENT + ", " +  PATIENT_DIED_BEFORE_TREATMENT + " )");
		}
		hqlMain.append(" ORDER BY rt.nationalCode ASC"); //WDEV-20640

		return RTTStatusPointVoAssembler.createRTTStatusPointVoCollectionFromRTTStatusPoint(fact.find(hqlMain.toString()));
	}
	
	//WDEV-19572
	public EventLiteVoCollection listEvents(String value, EventRefVo excludedEvent)
	{
		if (value == null || value.length() == 0)
			throw new DomainRuntimeException("Value passed to the search query is null");

		DomainFactory factory = getDomainFactory();
		
		ArrayList<String> marques = new ArrayList<String>();
		ArrayList<Object> values = new ArrayList<Object>();
				
		String hqlquery ="select ev from Event as ev left join ev.status as stat where ev.upperName like :EVENT_NAME and stat.id = :STATUS_ID ";
		marques.add("EVENT_NAME");
		values.add( value.trim().toUpperCase() + "%");
		marques.add("STATUS_ID");
		values.add(PreActiveActiveInactiveStatus.ACTIVE.getID());
		
		if (excludedEvent != null)
		{
			hqlquery += " and ev.id <> :EVENT_ID";
			marques.add("EVENT_ID");
			values.add(excludedEvent.getID_Event());
		}
		
		hqlquery += " order by ev.upperName asc";
		
		List<?> eventList = factory.find(hqlquery, marques.toArray(new String[marques.size()]), values.toArray());
		if (eventList == null || eventList.isEmpty())
			return null;
		
		return EventLiteVoAssembler.createEventLiteVoCollectionFromEvent(eventList);
	}

	public RTTStatusEventMapVo saveRTTStatusPointEvent(RTTStatusEventMapVo rttStatPointEvent) throws StaleObjectException
	{
		if (rttStatPointEvent == null)

			throw new CodingRuntimeException("Cannot save a null RTTStatusEventMapVo object.");

		if (!rttStatPointEvent.isValidated())

			throw new CodingRuntimeException("Cannot save an invalid RTTStatusEventMapVo object." );


		DomainFactory factory = getDomainFactory();

		RTTStatusEventMap statusToEventDO = RTTStatusEventMapVoAssembler.extractRTTStatusEventMap(factory, rttStatPointEvent);

		factory.save(statusToEventDO);

		return RTTStatusEventMapVoAssembler.create(statusToEventDO);
	}

	public RTTStatusEventMapVo getRTTStatusPointToEvent(RTTStatusEventMapRefVo recordRef)
	{
		if (recordRef == null || recordRef.getID_RTTStatusEventMap() == null)
		{
			throw new CodingRuntimeException("Cannot get details for a null RTTStatusEventMapVo reference");
		}
		DomainFactory factory = getDomainFactory();

		RTTStatusEventMap rttStatusEventDO = (RTTStatusEventMap) factory.getDomainObject(RTTStatusEventMap.class, recordRef.getID_RTTStatusEventMap());

		return RTTStatusEventMapVoAssembler.create(rttStatusEventDO);

	}

	public RTTStatusPointVoCollection listTargetRTTStatusPoints(RTTStatusPointVo currentRTT)
	{
		if (currentRTT == null)
		{
			return listRTTStatusPoints(false);
		}
		String hql = "Select targetRTT from RTTStatusPoint as rTT left join rTT.children as targetRTT where rTT.id = " + currentRTT.getID_RTTStatusPoint() + "order by targetRTT.nationalCode asc"; //WDEV-18594 
		
		return RTTStatusPointVoAssembler.createRTTStatusPointVoCollectionFromRTTStatusPoint(getDomainFactory().find(hql));
		
	}

	public RTTStatusEventMapVoCollection listRTTEventsMapped(EventEncounterType eventType, Integer currentRTTCode,
			Integer targetRTTCode, Boolean activeFlag, String eventName)
	{
	
		String hqlquery ="select rttevent from RTTStatusEventMap as rttevent  left join rttevent.currentRTTStatus as crtstat left join rttevent.targetRTTStatus as trtstat ";

		ArrayList<String> markers = new ArrayList<String>();
		ArrayList<Object> values = new ArrayList<Object>();

		String and = " ";
		String hqlconditions = "";

		if (eventType != null)
		{
			hqlconditions+= " rttevent.encounterType = :encType";
			markers.add("encType");
			values.add(getDomLookup(eventType));
			and = " and ";
		}
		
		if (currentRTTCode != null)
		{
			hqlconditions+= and;
			hqlconditions+= " crtstat.nationalCode = :cnatCode";
			markers.add("cnatCode");
			values.add(currentRTTCode);
			and = " and ";
		}
		
		if (targetRTTCode != null)
		{
			hqlconditions+= and;
			hqlconditions+= " trtstat.nationalCode = :tnatCode";
			markers.add("tnatCode");
			values.add(targetRTTCode);
			and = " and ";
		}
		
		if (Boolean.TRUE.equals(activeFlag))
		{
			hqlconditions+= and;
			hqlconditions+= " rttevent.active = :actFlag";
			markers.add("actFlag");
			values.add(activeFlag.booleanValue());
			and = " and ";
		}
		
		if (eventName != null && eventName.length() > 0)
		{
			hqlquery+= " left join rttevent.event as evt";
			hqlconditions+= and;
			hqlconditions+= " evt.upperName like :evName";
			markers.add("evName");
			values.add(eventName.toUpperCase()+ "%");
			and = " and ";
		}

		if (markers.size()> 0)
		{
			hqlquery+= " where ";
		}

		hqlquery += hqlconditions;;

		hqlquery += " order by crtstat.nationalCode asc";

		DomainFactory factory = getDomainFactory();

		List<?> records = factory.find(hqlquery, markers, values);

		return RTTStatusEventMapVoAssembler.createRTTStatusEventMapVoCollectionFromRTTStatusEventMap(records);
	}
}
