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



import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRelationshipType;
import cdac.medinfo.sdk.dcm30_04.exceptions.ConditionFailedException;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;




/**
* This item class contains methods to validate,set and get the attributes in {@link ContentSequenceItem}.
 */
public class ContentSequenceItem extends Item  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DocumentRelationshipMacro objDocumentRelationshipMacro= null;
	DocumentContentMacro objDocumentContentMacro = null;

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax 
	 */
	ContentSequenceItem()
	{
		super();
	}

	/**
	 * This constructor creates the item and internal code sequence macro with default TransferSyntax and default DictionaryReader
	 * @param objITransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 */
	ContentSequenceItem(ITransferSyntax objITransferSyntax)
	{
		super(objITransferSyntax);
	}

	/**
	 * This constructor creates Content Sequence Item with specified TransferSyntax and specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	ContentSequenceItem(ITransferSyntax objTransferSyntax, IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax,objDictionaryReader);
	}

	/**
	 * Sets type of relationship between the (enclosing) Source Content Item and the Target Content Item.
	 * Defined Terms:
	 * CONTAINS, HAS PROPERTIES, HAS OBS CONTEXT, HAS ACQ CONTEXT, INFERRED FROM, SELECTED FROM, HAS CONCEPT MOD.
	 * @param enumRelationshipType Enum value of Relationship Type
	 */
	public void setRelationshipType(EnumRelationshipType enumRelationshipType)
	{
		/* Accept the Relationship Type in String format of type CS
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0040,0xA010, enumRelationshipType.getValue(),getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0xA010,DicomConstants.DCM_DT_CS, enumRelationshipType.getValue());

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves type of relationship between the (enclosing) Source Content Item and the Target Content Item.
	 * @return String - value of Relationship Type.
	 */
	public String getRelationshipType()
	{
		Object objValue = getValue(0x0040,0xA010);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Retrieves the instance of Document Relationship Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attributes of Document Relationship Macro.
	 * @return DocumentRelationshipMacro - instance of DocumentRelationshipMacro.
	 */
	public DocumentRelationshipMacro getDocumentRelationshipMacro()
	{
		if(objDocumentRelationshipMacro == null)
		{
			objDocumentRelationshipMacro = new DocumentRelationshipMacro(this);
			return objDocumentRelationshipMacro;
		}
		return objDocumentRelationshipMacro;
	}

	/**
	 * Retrieves the instance of Document Content Macro. If the instance is not already present a new instance 
	 * is created and returned. It is used to set the attributes of Document Content Macro.
	 * @return objDocumentContentMacro - instance of DocumentContentMacro.
	 */
	public DocumentContentMacro getDocumentContentMacro()
	{
		if(objDocumentContentMacro == null)
		{
			objDocumentContentMacro = new DocumentContentMacro(this);
			return objDocumentContentMacro;
		}
		return objDocumentContentMacro;
	}

	/**
	 * Sets An ordered set of one or more integers that uniquely identifies the Target Content Item of the relationship.
	 * The root Content Item is referenced by a single value of 1.
	 * @param lArrReferencedContentItemIdentifier Long value of Referenced Content Item Identifier
	 */
	public void setReferencedContentItemIdentifier(int[] lArrReferencedContentItemIdentifier)
	{
		/* Accept the Referenced Content Item Identifier in long format of type UL
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the Data Set
		 */
		IDataElement objIDataElement = null;
		if(objITransferSyntax.getVRTransferSyntax() == DicomConstants.DCM_VR_IMPLICIT)	
			objIDataElement = new ImplicitDE(0x0040,0xDB73, lArrReferencedContentItemIdentifier,getDictionaryReader());
		else
			objIDataElement = new ExplicitDE(0x0040,0xDB73,DicomConstants.DCM_DT_UL, lArrReferencedContentItemIdentifier);

		addDataElement(objIDataElement);
	}

	/**
	 * Retrieves Referenced Content Item Identifier.
	 * @return long[] - long value of Referenced Content Item Identifier.
	 */
	public int[] getReferencedContentItemIdentifier()
	{
		Object objValue = getValue(0x0040,0xDB73);
		if(objValue != null)
			return (int[])objValue;
		return null;
	}

	/**
	 * Validates all the attributes in {@link ContentSequenceItem} that are present in the DataSet.
	 * @return boolean - Success -true, Failure - false.
	 * @throws InvalidItemException throws InvalidItemException If Mandatory or Conditional element fails.
	 */
	public boolean validateForModule() throws InvalidItemException
	{
		try{
			getDocumentRelationshipMacro().validate();
			getDocumentContentMacro().validate();
			if(!contains(0x0040,0xA010))
				throw new MandatoryMissingException("0040,A010");

			/*
			 * All conditions depending on if relation ship is by value and by reference is decided at IOD level.
			 * This condition is implemented at IOD level as the decision whether a relation ship is by-reference or by value is decided at IOD level.
			 * Referenced Content Item Identifier (0040,DB73)
			 * Required if the Target Content Item is denoted by-reference, i.e. the Document Relationship Macro and Document Content Macro are not included.
			 */
		}
		catch(ConditionFailedException cfe)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(cfe.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),cfe);
		}
		catch( MandatoryMissingException me)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(me.getMessage());
			}
			throw new ValidationException(this.getClass().getSimpleName(),me);
		}
		return true;
	}
}
