/*******************************************************************************
 *  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.templates.generaltemplates;


import cdac.medinfo.sdk.dcm30_04.baselibs.ContentSequenceItem;
import cdac.medinfo.sdk.dcm30_04.srtree.SRTree;
import cdac.medinfo.sdk.dcm30_04.templates.cid.ObserverType_CID270;
import cdac.medinfo.sdk.dcm30_04.templates.enums.EnumCodingSchemeDesignator;
import cdac.medinfo.sdk.dcm30_04.templates.enums.cm.EnumCM_ObserverContext;
import cdac.medinfo.sdk.dcm30_04.templates.enums.cm.EnumCM_ObserverType;
import cdac.medinfo.sdk.dcm30_04.templates.enums.cv.EnumCV_ObserverContext;

/**
 * TID 1002 Observer Context
 * The observer (person or device) that created the Content Items to which this context applies.
 * Whenever this template is invoked, all previously inherited attributes of Observer Context are discarded
 * and replaced.
 * There may be more than one observer, as this template may be invoked with a VM 1-n, and both person
 * and device observers. In such a case, the Content Items of TID 1003 and 1004 shall be included in the
 * order in which the values of Observer Type are specified. Since TID 1003 and 1004 both include a single
 * mandatory Content Item as their first Content Item, which observer is being described can be determined
 */
/**
 * This class represents ObserverContext - TID1002
 */
public class ObserverContext_TID1002 extends Template
{
	/**
	 * Object of CID270_ObserverType which represents the CID270 Template. 
	 */
	private ObserverType_CID270 objObserverType = null;

	/**
	 * Object of TID1003_PersonObserverIdentifyingAttributes which represents the TID1003 Template. 
	 */

	private PersonObserverIdentifyingAttributes_TID1003 objPersonObserverIdentifyingAttributes = null;

	/**
	 * Object of TID1004_DeviceObserverIdentifyingAttributes which represents the TID1004 Template. 
	 */

	private DeviceObserverIdentifyingAttributes_TID1004 objDeviceObserverIdentifyingAttributes = null;

	/**
	 * Instantiates a new ObserverContext_TID1002 Template.
	 * @param objTree Object of SRTree.
	 */
	public ObserverContext_TID1002(SRTree objTree)
	{
		super(objTree);
		if(null== this.objObserverType)
			this.objObserverType = new ObserverType_CID270();
	}



	/**
	 * Retrieves the Object of PersonObserverIdentifyingAttributes_TID1003 template
	 * @return objPersonObserverIdentifyingAttributes Object of PersonObserverIdentifyingAttributes_TID1003 template
	 */
	public PersonObserverIdentifyingAttributes_TID1003 getPersonObserverIdentifyingAttributes() 
	{
		if(null == objPersonObserverIdentifyingAttributes)
			objPersonObserverIdentifyingAttributes = new PersonObserverIdentifyingAttributes_TID1003(getSRTree());
		return objPersonObserverIdentifyingAttributes;
	}

	/**
	 * Retrieves the Object of DeviceObserverIdentifyingAttributes_TID1004
	 * @return objDeviceObserverIdentifyingAttributes Object of DeviceObserverIdentifyingAttributes_TID1004
	 */
	public DeviceObserverIdentifyingAttributes_TID1004 getDeviceObserverIdentifyingAttributes()
	{
		if(null == objDeviceObserverIdentifyingAttributes)
			objDeviceObserverIdentifyingAttributes = new DeviceObserverIdentifyingAttributes_TID1004(getSRTree());
		return objDeviceObserverIdentifyingAttributes;
	}

	/**
	 * Sets the value for ObserverType.
	 * @param arrPosition Identifier which shows the position of the node in the SR Tree.
	 * @param enumObserverType Object of EnumCM_ObserverType
	 * @param strCodingSchemeVersionForCode CodingSchemeVersion 
	 */
	public void setObserverType(int [] arrPosition , EnumCM_ObserverType enumObserverType,String strCodingSchemeVersionForCode)
	{
		/**
		 * Sets the value for Observer Type which follows the template CID270
		 */
		this.objObserverType.setObserverType(enumObserverType);

		/**
		 * Retrieves the corresponding values of CodeMeaning,CodeValue and CodingSchemeDesignator for Value Type CODE.
		 */
		String strCodeMeaning = objObserverType.getCodeMeaning();
		String strCodeValue = objObserverType.getCodeValue();
		String strCodingSchemeDesignator = objObserverType.getCodingSchemeDesignator();

		/**
		 * Retrieves the corresponding values of CodeMeaning,CodeValue and CodingSchemeDesignator for ObserverContext.
		 */
		String strCMObserverContext = EnumCM_ObserverContext.OBSERVER_TYPE.getValue();
		String strCVForObserverType = EnumCV_ObserverContext.OBSERVER_TYPE_121005.getValue();
		String strCSD = EnumCodingSchemeDesignator.DCM.getValue();

		/**
		 * Creating a node of Value Type CODE and adding this node at specified identifier in the SR Tree.
		 */
		ContentSequenceItem objContentSequenceItem = getSRTree().createCodeNode(arrPosition, strCMObserverContext,strCVForObserverType , strCSD, strCodeValue, strCodingSchemeDesignator, strCodingSchemeVersionForCode, strCodeMeaning);

		/**
		 * Depending upon the set Observer Type, corresponding Template for Person or Device will be instantiated.
		 */
		if(enumObserverType.getValue().equalsIgnoreCase(EnumCM_ObserverType.PERSON.getValue()))
		{
			if(null== this.objPersonObserverIdentifyingAttributes)
				this.objPersonObserverIdentifyingAttributes = new PersonObserverIdentifyingAttributes_TID1003(getSRTree());
		}
		else if(enumObserverType.getValue().equalsIgnoreCase(EnumCM_ObserverType.DEVICE.getValue()))
		{
			if(null== this.objDeviceObserverIdentifyingAttributes)
				this.objDeviceObserverIdentifyingAttributes = new DeviceObserverIdentifyingAttributes_TID1004(getSRTree());
		}

		getSRTree().addChild(objContentSequenceItem);
	}
}
