/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Pathways/src/ims/pathways/domain/impl/TargetConfigurationImpl.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 Catalin Tomozei 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.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import ims.core.vo.lookups.PreActiveActiveInactiveStatus;
import ims.domain.DomainFactory;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.exceptions.UniqueKeyViolationException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.pathways.configuration.domain.objects.Target;
import ims.pathways.configuration.vo.TargetRefVo;
import ims.pathways.domain.base.impl.BaseTargetConfigurationImpl;
import ims.pathways.vo.TargetConfigurationShortVo;
import ims.pathways.vo.TargetConfigurationShortVoCollection;
import ims.pathways.vo.TargetConfigurationVo;
import ims.pathways.vo.domain.TargetConfigurationShortVoAssembler;
import ims.pathways.vo.domain.TargetConfigurationVoAssembler;

public class TargetConfigurationImpl extends BaseTargetConfigurationImpl
{

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	public TargetConfigurationShortVoCollection listByCriteria(TargetConfigurationShortVo filter) 
	{
		DomainFactory factory = getDomainFactory();
		ArrayList<String> names = new ArrayList<String>();
		ArrayList<Serializable> values = new ArrayList<Serializable>();
		StringBuffer hql = new StringBuffer("from Target tg ");
		String andStr = " where ";
		
		if (filter != null)
		{
			if (filter.getNameIsNotNull())
			{
				hql.append(andStr);
				hql.append(" tg.upperName like :name");
				names.add("name");
				values.add(filter.getName().toUpperCase() +"%");			
				andStr = " and ";
			}
			if (filter.getEventLinkTypeIsNotNull())
			{
				hql.append(andStr);
				hql.append(" tg.eventLinkType.id = :id_event");
				names.add("id_event");
				values.add(filter.getEventLinkType().getID());			
				andStr = " and ";
			}
			if (filter.getStatusIsNotNull())
			{
				hql.append(andStr);
				hql.append(" tg.status.id = :id_status");
				names.add("id_status");
				values.add(filter.getStatus().getID());
				andStr = " and ";
			}
			if (filter.getTargetDateCalculationIsNotNull())
			{
				hql.append(andStr);
				hql.append("tg.targetDateCalculation.id = :id_targetDateCalculation");
				names.add("id_targetDateCalculation");
				values.add(filter.getTargetDateCalculation().getID());
				andStr = " and ";
			
			}
		}	
		
		hql.append(" order by tg.upperName asc");
			
		List collection = factory.find(hql.toString(), names, values);
		return TargetConfigurationShortVoAssembler.createTargetConfigurationShortVoCollectionFromTarget(collection);
	}
	
	public TargetConfigurationVo get(TargetRefVo record) 
	{
		if(record.getID_Target() == null)
			throw new CodingRuntimeException("No id provided for Target in method get");

		return TargetConfigurationVoAssembler.create( (Target) getDomainFactory().getDomainObject(record));
	}
	
	@SuppressWarnings("unchecked")
	private TargetConfigurationShortVoCollection checkForUniq(TargetConfigurationVo record) 
	{
		DomainFactory factory = getDomainFactory();
		StringBuffer hql = new StringBuffer();
		ArrayList<String> names = new ArrayList<String>();
		ArrayList<Serializable> values = new ArrayList<Serializable>();
		
		hql.append("select target from Target as target left join target.status as status where target.name = :name and status.id = :id_status");
		names.add("name");
		names.add("id_status");
		values.add(record.getName());
		values.add(PreActiveActiveInactiveStatus.ACTIVE.getID());
		
		if (record.getID_TargetIsNotNull())
		{
			hql.append(" and target.id <> :id ");
			names.add("id");
			values.add(record.getID_Target());	
		}
		
		List collection = factory.find(hql.toString(), names, values);
		return TargetConfigurationShortVoAssembler.createTargetConfigurationShortVoCollectionFromTarget(collection);
	}
	
	public TargetConfigurationVo save(TargetConfigurationVo record) throws StaleObjectException, UniqueKeyViolationException 
	{	
		if(!record.isValidated())
			throw new CodingRuntimeException("Record not validated");
		
		if (record.getStatusIsNotNull() &&
				record.getStatus().equals(PreActiveActiveInactiveStatus.ACTIVE))
		{
			if (checkForUniq(record).size() > 0)
				throw new UniqueKeyViolationException("TargetName must be unique!");
		}
		
		DomainFactory factory = getDomainFactory();
		Target domainRecord = TargetConfigurationVoAssembler.extractTarget(factory, record);		
		
		domainRecord.setUpperName(domainRecord.getName());
		factory.save(domainRecord);
			
		return TargetConfigurationVoAssembler.create(domainRecord);
	}
}
