/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/domain/impl/PropertyChecklistImpl.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.45 build 2425.18037)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.domain.impl;

import java.util.ArrayList;
import ims.core.admin.vo.CareContextRefVo;
import ims.core.vo.LocShortMappingsVoCollection;
import ims.core.vo.domain.LocShortMappingsVoAssembler;
import ims.core.vo.lookups.LocationType;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.exceptions.CodingRuntimeException;
import ims.nursing.domain.base.impl.BasePropertyChecklistImpl;
import ims.nursing.domain.objects.PropertyChecklist;
import ims.nursing.vo.PropertyChecklistRefVo;
import ims.nursing.vo.PropertyChecklistVo;
import ims.nursing.vo.domain.PropertyChecklistShortVoAssembler;
import ims.nursing.vo.domain.PropertyChecklistVoAssembler;

public class PropertyChecklistImpl extends BasePropertyChecklistImpl
{
	private static final long serialVersionUID = 1L;

	public ims.nursing.vo.PropertyChecklistShortVoCollection list(CareContextRefVo careContext)
	{
		return PropertyChecklistShortVoAssembler.createPropertyChecklistShortVoCollectionFromPropertyChecklist(
				getDomainFactory().find("from PropertyChecklist as dsp where dsp.careContext.id = :CARE_CONTEXT", 
						new String[] {"CARE_CONTEXT"}, new Object[] {careContext.getID_CareContext()}));
	}

	public PropertyChecklistVo save(PropertyChecklistVo record) throws StaleObjectException 
	{	
		if(record == null)
		{
			throw new DomainRuntimeException("Invalid record");
		}
		if(!record.isValidated())
		{
			throw new CodingRuntimeException("Record not validated");
		}
			
		DomainFactory factory = getDomainFactory();
		PropertyChecklist domainRecord = PropertyChecklistVoAssembler.extractPropertyChecklist(factory, record);		
		
		factory.save(domainRecord);
			
		return PropertyChecklistVoAssembler.create(domainRecord);
	}
	
 	public LocShortMappingsVoCollection listWards(String name) 	
	{
		java.util.List locations = listLocations(LocationType.WARD,Boolean.TRUE, name, Boolean.FALSE); //WDEV-19532
		return  LocShortMappingsVoAssembler.createLocShortMappingsVoCollectionFromLocation(locations).sort();
	}

	private java.util.List listLocations(LocationType locType, Boolean activeOnly, String recordSearch, Boolean isVirtual)
	{
		DomainFactory factory = getDomainFactory();
		
		java.util.List locations;

		String hql = " from Location loc ";
		StringBuffer condStr = new StringBuffer();
		String andStr = " ";

		ArrayList markers = new ArrayList();
		ArrayList values = new ArrayList();

		if (locType != null)
		{
			condStr.append(andStr + " loc.type = :locType");
			markers.add("locType");
			values.add(getDomLookup(locType));
			andStr = " and ";
		}
		
		if (activeOnly != null && activeOnly.booleanValue())
		{
			condStr.append(andStr + " loc.isActive = :active");
			markers.add("active");
			values.add(activeOnly);
			andStr = " and ";
		}
		
		if (recordSearch != null)
		{
			condStr.append(andStr + " loc.upperName like :name"); //WDEV-20219 upper(loc.name)
			markers.add("name");
			values.add("%" + recordSearch.toUpperCase() + "%");
			andStr = " and ";
		}
		//WDEV-19532
		if (isVirtual != null)
		{
			condStr.append(andStr + " loc.isVirtual = :isVirtual");
			markers.add("isVirtual");
			values.add(isVirtual.booleanValue());
			andStr = " and ";			
		}
		if (andStr.equals(" and "))
		{
			hql += " where ";
		}
		
		hql += condStr.toString();
		locations = factory.find(hql, markers, values);
		return locations;
	}

	public PropertyChecklistVo get(PropertyChecklistRefVo record) 
	{
		if(record == null || record.getID_PropertyChecklist() == null)
		{
			throw new DomainRuntimeException("Invalid record");
		}
		
		DomainFactory factory = getDomainFactory();
		PropertyChecklist domainRecord = (PropertyChecklist)factory.getDomainObject
					(PropertyChecklist.class, record.getID_PropertyChecklist().intValue());
		
		return PropertyChecklistVoAssembler.create(domainRecord);
		
	}
}
