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

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;




/**
 * The Class P_DATA_ValueItem.
 */
public class P_DATA_ValueItem  
{

	/** The byte pres context id. */
	byte bytePresContextId;

	/** The pdv. */
	PDV objPDV;

	/** The Constant PRESENTATION_CONTEXT_ID_LEN. */
	public static final int PRESENTATION_CONTEXT_ID_LEN = 1;

	/** The Constant PDV_HEADER_LEN. */
	public static final int PDV_HEADER_LEN = 1;

	/**
	 * Instantiates a new P-DATA-VALUE-ITEM.
	 */
	public P_DATA_ValueItem()
	{

	}

	/**
	 * Retrieves Presentation Context ID.
	 * @return byte - value of Presentation Context ID.
	 */
	public byte getPresentationContextID() 
	{
		return bytePresContextId;
	}

	/**
	 * Sets Presentation Context ID.
	 * @param bytePresContextId  Byte Presentation Context ID.
	 */
	public void setPresentationContextID(byte bytePresContextId) 
	{
		this.bytePresContextId = bytePresContextId;
	}

	/**
	 * Set PDV.
	 * @param objPDV object of PDV
	 */
	public void setPDV(PDV objPDV)
	{
		this.objPDV = objPDV;
	}

	/**
	 * Retrieves PDV.
	 * @return PDV - object of PDV
	 */
	public PDV getPDV()
	{
		return objPDV;
	}

	/**
	 * The implementation of this method parse the P Data Value item from the
	 * given Stream.
	 * @param objIDicomStream Stream on which the DICOM data is available.
	 * @throws IOException Signals that an IO Exception has occurred.
	 * @throws ParseException Signals that Parse Exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream)throws IOException,ParseException
	{
		int iItemLength = objIDicomStream.readInt();
		bytePresContextId = objIDicomStream.readByte();
		objPDV = new PDV();
		objPDV.parse(objIDicomStream,iItemLength-1);
	}

	/**
	 * Gets the length of P-DATA-VALUE-ITEM.
	 * @return int - the length of P-Data-Value-ITem.
	 */
	public int getLength()
	{
		int P_Data_Value_Item_len = PRESENTATION_CONTEXT_ID_LEN+PDV_HEADER_LEN + objPDV.getFragment().length;
		return P_Data_Value_Item_len;
	}

	/**
	 * The implementation of this method serialize the P Data Value item from
	 * the given Stream.
	 * @param objIDicomStream Stream on which the DICOM data is available.
	 * @throws IOException Signals that an IO Exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException
	{
		objIDicomStream.writeInt(getLength());
		objIDicomStream.writeByte(bytePresContextId);
		objPDV.serialize(objIDicomStream,getLength()-1);
	}
}
