/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/domain/impl/PathwayTargetConfigDialogImpl.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 Daniel Laffan using IMS Development Environment (version 1.60 build 2838.16667)
// Copyright (C) 1995-2007 IMS MAXIMS plc. All rights reserved.

package ims.pathways.domain.impl;

import java.util.List;
import ims.core.vo.lookups.PreActiveActiveInactiveStatus;
import ims.domain.DomainFactory;
import ims.framework.exceptions.CodingRuntimeException;
import ims.pathways.configuration.domain.objects.Target;
import ims.pathways.configuration.vo.EventRefVo;
import ims.pathways.domain.EventConfiguration;
import ims.pathways.domain.base.impl.BasePathwayTargetConfigDialogImpl;
import ims.pathways.vo.ExternalEventMappingVoCollection;
import ims.pathways.vo.RTTEventVoCollection;
import ims.pathways.vo.TargetShortVoCollection;
import ims.pathways.vo.domain.EventTargetShortVoAssembler;
import ims.pathways.vo.domain.TargetShortVoAssembler;
import ims.pathways.vo.lookups.EventLinkType;
import ims.vo.LookupInstanceCollection;

public class PathwayTargetConfigDialogImpl extends BasePathwayTargetConfigDialogImpl
{

	private static final long serialVersionUID = 1L;

	/**
	* get
	*/
	public ims.pathways.vo.TargetShortVo get(ims.pathways.configuration.vo.TargetRefVo refVo)
	{
		if(refVo.getID_Target() == null)
			throw new CodingRuntimeException("No id provided for Target in method get");
		
		return TargetShortVoAssembler.create( (Target) getDomainFactory().getDomainObject(refVo));
	}

	/**
	* listEventTargetByTarget - using this to retrieve the events and mappings for each target
	*/
	public ims.pathways.vo.EventTargetShortVoCollection listEventTargetByTarget(ims.pathways.configuration.vo.TargetRefVo target,Boolean isViewMode)
	{
		if(target.getID_Target() == null)
			throw new CodingRuntimeException("No id provided for Target in method listEventTargetByTarget");
	
		if(isViewMode == null)
			throw new CodingRuntimeException("isViewMode is mandatory in method listEventTargetByTarget");
		
		DomainFactory factory = getDomainFactory();
		List evTargets = null; 
		
		if(!isViewMode)
			evTargets= factory.find("from EventTarget et where et.target.id = :idTarget and et.status = :status", new String[] {"idTarget", "status"}, new Object[] {target.getID_Target(), getDomLookup(PreActiveActiveInactiveStatus.ACTIVE)});
		else
			evTargets= factory.find("from EventTarget et where et.target.id = :idTarget", new String[] {"idTarget"}, new Object[] {target.getID_Target()});
			
		return EventTargetShortVoAssembler.createEventTargetShortVoCollectionFromEventTarget(evTargets);		
	}

	public TargetShortVoCollection listTarget()
	{
		return TargetShortVoAssembler.createTargetShortVoCollectionFromTarget(getDomainFactory().listDomainObjects(Target.class));	
	}

	public TargetShortVoCollection listTargetByCriteria(String name, EventLinkType eventLinkType)
	{
		//TODO temporary measure until component or new control created
		if(name != null)
			name = name.replaceAll("%", ""); 
		
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer("from Target target where target.status = :status");
		
		java.util.ArrayList markers = new java.util.ArrayList();		
		java.util.ArrayList values  = new java.util.ArrayList();
		
		markers.add("status");
		values.add(getDomLookup(PreActiveActiveInactiveStatus.ACTIVE));
		
		if(name != null)
		{
			hql.append(" and target.upperName like :name");
			markers.add("name");
			values.add(name.toUpperCase() + "%");
		}
		if(eventLinkType != null)
		{
			hql.append(" and target.eventLinkType = :linkType");
			markers.add("linkType");
			values.add(getDomLookup(eventLinkType));	
		}
		
		List targets = factory.find(hql.toString(), markers, values);
		return TargetShortVoAssembler.createTargetShortVoCollectionFromTarget(targets).sort();
	}

	private String getIdString(LookupInstanceCollection statusCollection)
	{
		String idString = "";
		
		for(int i=0;i<statusCollection.size();i++)
		{
			idString += statusCollection.getIndex(i).getID();
			idString += ((statusCollection.size() - (i+1)) > 0 ? "," : "");
		}
		
		return idString;
	}

	public ExternalEventMappingVoCollection getEventMappingsByEventAndStatus(EventRefVo voRef, PreActiveActiveInactiveStatus status)
	{
		EventConfiguration impl = (EventConfiguration) getDomainImpl(EventConfigurationImpl.class);
		return impl.getEventMappingsByEventAndStatus(voRef, status);
	}

	public RTTEventVoCollection getRTTByEventAndStatus(EventRefVo voRef, PreActiveActiveInactiveStatus status)
	{
		EventConfiguration impl = (EventConfiguration) getDomainImpl(EventConfigurationImpl.class);
		return impl.getRTTByEventAndStatus(voRef, status);
	}
}
