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

package ims.nursing.domain.impl;

import ims.admin.domain.HcpAdmin;
import ims.admin.domain.impl.HcpAdminImpl;
import ims.core.resource.people.domain.objects.Hcp;
import ims.core.vo.CareContextShortVo;
import ims.core.vo.HcpCollection;
import ims.core.vo.HcpFilter;
import ims.core.vo.HcpLiteVo;
import ims.core.vo.domain.HcpLiteVoAssembler;
import ims.domain.DomainFactory;
import ims.domain.exceptions.DomainRuntimeException;
import ims.domain.exceptions.StaleObjectException;
import ims.domain.impl.DomainImpl;
import ims.domain.lookups.LookupMapping;
import ims.nursing.domain.objects.NursingClinicalNotes;
import ims.nursing.vo.CarePlanTitleCollection;
import ims.nursing.vo.NursingClinicalNotesVo;
import ims.nursing.vo.domain.CarePlanTitleAssembler;
import ims.nursing.vo.domain.NursingClinicalNotesVoAssembler;
import ims.vo.LookupInstVo;
import ims.vo.LookupMappingVo;
import ims.vo.LookupMappingVoCollection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class ClinicalNotesImpl extends DomainImpl implements ims.nursing.domain.ClinicalNotes, ims.domain.impl.Transactional
{
	
	/**
	* Gets the Clinical Notes Details for the given Clinical Note ID
	*/
	public ims.nursing.vo.NursingClinicalNotesVo getClinicalNotes(ims.core.vo.ClinicalNotesVo clinicalNote)
	{
		DomainFactory factory = getDomainFactory();
		NursingClinicalNotes notes = (NursingClinicalNotes)factory.getDomainObject(NursingClinicalNotes.class, clinicalNote.getID_ClinicalNotes());
		
		ims.nursing.vo.NursingClinicalNotesVo voNote = NursingClinicalNotesVoAssembler.create(notes);
		return voNote;
	}

	public HcpCollection listHcps(HcpFilter filter)
	{
		HcpAdmin hcpAdmin = (HcpAdmin)getDomainImpl(HcpAdminImpl.class);
		return hcpAdmin.listHCPs(filter);
	}


	/**
	 * list care plans, by clinical contact id
	 */
	public CarePlanTitleCollection listCarePlans(CareContextShortVo careContextShortVo) 
	{
	
		DomainFactory factory = getDomainFactory();	
		StringBuffer hql = new StringBuffer(" ");
		String query = "from CarePlan careplan ";
		ArrayList<String> markers = new ArrayList<String>();
		ArrayList<Object> values = new ArrayList<Object>();
		String andStr = " ";

		if (careContextShortVo != null)
		{
			hql.append(andStr + " careplan.careContext.id = :rcc");
			markers.add("rcc");
			values.add(careContextShortVo.getID_CareContext());
			andStr = " and ";
			hql.append(andStr + " careplan.currentCarePlanStatus.status = :status");
			markers.add("status");
			values.add(getDomLookup(ims.nursing.vo.lookups.CarePlanStatus.ACTIVE));
		}
		
		if (markers.size() > 0) query += " where ";
		query += hql.toString();
		List<?> cpTitle = factory.find(query, markers, values);		  					
		
		return(CarePlanTitleAssembler.createCarePlanTitleCollectionFromCarePlan(cpTitle).sort()); //this is the problem
	}

	/* (non-Javadoc)
	 * @see ims.nursing.domain.ClinicalNotes#saveClinicalNotes(ims.nursing.vo.NursingClinicalNotesVo)
	 */
	public NursingClinicalNotesVo saveClinicalNotes(NursingClinicalNotesVo notesVo) throws StaleObjectException 
	{
		if (!notesVo.isValidated())
			throw new DomainRuntimeException("This Clinical Note has not been validated");
			
		DomainFactory factory = getDomainFactory();
				
		NursingClinicalNotes domNotes = NursingClinicalNotesVoAssembler.extractNursingClinicalNotes(factory, notesVo);				
					
		// Save is needed as we then need to save this off with the careplans too being a bidirectional relationship
		factory.save(domNotes);
		 
	 	return NursingClinicalNotesVoAssembler.create(domNotes);
	}	
	public HcpLiteVo getHcpLite(Integer idHcp) {
		DomainFactory factory = getDomainFactory();
		Hcp hcp = (Hcp) factory.getDomainObject(Hcp.class,idHcp);
		return HcpLiteVoAssembler.create(hcp);
	}
	
	//WDEV-12236 
	public LookupInstVo getMappings(LookupInstVo instance)
	{
		DomainFactory factory = getDomainFactory();
		ims.domain.lookups.LookupInstance doInst = factory.getLookupInstance(instance.getId());
		Set mappings = doInst.getMappings();
		LookupMappingVoCollection mapColl = new LookupMappingVoCollection();
		Iterator iter = mappings.iterator();
		LookupMapping doMapping;
		while (iter.hasNext())
		{
			doMapping = (LookupMapping) iter.next();
			mapColl.add(new LookupMappingVo(doMapping.getExtSystem(), doMapping.getExtCode()));
		}
		mapColl.sort();
		instance.setMappings(mapColl);
		return instance;
	}

}
