/*******************************************************************************
 *  Copyright 2017-2018 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.dcm30_04.services.javadbhelper;



import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IQueryModel;
import cdac.medinfo.sdk.dcm30_04.utils.ICollectionIterator;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 * The class CServices_QueryModel defines the find and retrieval query structure. 
 */
public class CServices_QueryModel implements IQueryModel
{

	private ISingleCollection<Patient> objPatientList = null;

	/**
	 * @return the objPatientList
	 */
	public ISingleCollection<Patient> getPatientList() 
	{
		return objPatientList;
	}

	/**
	 * @param objPatientList the objPatientList to set.
	 */
	public void setPatientList(ISingleCollection<Patient> objPatientList) 
	{
		this.objPatientList = objPatientList;
	}

	/**
	 * Verifies whether PatientID is already present or not and Retrieves that instance of Patient.
	 * @param strPID value of PatientID.
	 * @return Patient - instance of Patient.
	 */
	public Patient isPIDPresent(String strPID)
	{
		if(objPatientList != null && strPID != null)
		{
			Patient objRsPatient = null;
			ICollectionIterator<Patient> objPListIterator = objPatientList.iterator();
			while(objPListIterator.hasNext())
			{
				objRsPatient = objPListIterator.next();
				if(strPID.equals(objRsPatient.getPatientID()))
				{
					return objRsPatient;
				}
			}
		}
		return null;
	}

	/**
	 * Initializes the CServices_QueryModel structure according to the passed DataSet.   
	 * @param objIDataSet instance of IDataSet.
	 * @return boolean - shows status True if DataSet successfully decoded else False.
	 */
	public boolean decode(IDataSet objIDataSet)
	{
		objPatientList = new SingleCollection<Patient>();
		Patient objPatient = new Patient();
		objPatient.populate(objIDataSet);

		ISingleCollection<Study> objStudyList = new SingleCollection<Study>();
		Study objStudy = new Study();
		objStudy.populate(objIDataSet);

		ISingleCollection<Series> objSeriesList = new SingleCollection<Series>();
		Series objSeries = new Series();
		objSeries.populate(objIDataSet);

		ISingleCollection<DicomImage> objImageList = new SingleCollection<DicomImage>();
		DicomImage objDicomImage = new DicomImage();
		objDicomImage.populate(objIDataSet);
		objImageList.add(objDicomImage);

		objSeries.setDicomImageList(objImageList);

		objSeriesList.add(objSeries);

		objStudy.setSeriesList(objSeriesList);

		objStudyList.add(objStudy);

		objPatient.setStudyList(objStudyList);

		objPatientList.add(objPatient);

		this.setPatientList(objPatientList);

		return true;
	}

	/**
	 * Populates the collection of DataSet in given TransferSyntax according to the CServices_QueryModel Structure.   
	 * @param objITransferSyntax instance of TransferSyntax to be applied on DataSet.
	 * @param objDataSetList collection of DataSet.
	 * @return boolean - shows status True if CServices_QueryModel successfully encoded to DataSet collection else False.
	 */
	public boolean encode(ITransferSyntax objITransferSyntax, ISingleCollection<IDataSet> objDataSetList)
	{
		if(objPatientList != null)
		{
			ICollectionIterator<Patient> objPatListIterator = objPatientList.iterator();
			while(objPatListIterator.hasNext())
			{
				Patient objPatient = objPatListIterator.next();
				ICollectionIterator<Study> objStudyListIterator = objPatient.getStudyList().iterator();
				while(objStudyListIterator.hasNext())
				{
					Study objStudy = objStudyListIterator.next();
					ICollectionIterator<Series> objSeriesIterator = objStudy.getSeriesList().iterator();
					while(objSeriesIterator.hasNext())
					{
						Series objSeries = objSeriesIterator.next();
						ICollectionIterator<DicomImage> objImageIterator = objSeries.getDicomImageList().iterator();
						while(objImageIterator.hasNext())
						{
							DicomImage objDicomImage = objImageIterator.next();
							IDataSet objIDataSet = new DataSet(objITransferSyntax);
							objDicomImage.fill(objIDataSet);
							objSeries.fill(objIDataSet);
							objStudy.fill(objIDataSet);
							objPatient.fill(objIDataSet);
							objDataSetList.add(objIDataSet);
						}
					}
				}
			}
			return true;
		}
		else
			return false;
	}
}
