/*******************************************************************************
 *  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.enums.EnumItemType;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.exceptions.SerializeException;




/**
 * This class implements Application Context Item which is a sequence of
 * mandatory fields followed by a variable length field.
 */
public class AppContextItem extends AssociationItem 
{

	/** The application context name. */
	String strAppContextName;

	/**
	 * This constructor initializes Application Context item with its Item Type.
	 */
	public AppContextItem()
	{
		super(EnumItemType.APPLICATION_CONTEXT_ITEM.getValue());
	}

	/**
	 * Retrieves Application Context Name structured as UIDs.
	 * @return String - String value of AppContextName.
	 */
	public String getAppContextName() 
	{
		return strAppContextName;
	}

	/**
	 * Sets Application Context Name structured as UIDs.
	 * @param strAppContextName String value of Application Context name.
	 */
	public void setAppContextName(String strAppContextName)
	{
		this.strAppContextName = strAppContextName;
	}

	/**
	 * Retrieves length of this Item.
	 * @return short - value of length of this Item.
	 */
	public short getItemLength() 
	{
		return (short)strAppContextName.length();
	}

	/**
	 * This method parses this Item from the given Stream.
	 * @param objIDicomStream  Stream on which the DICOM data is available.
	 * @throws ParseException Signals that parse exception has occurred.
	 * @throws IOException Signals that an IO Exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream)throws IOException,ParseException
	{
		super.parse(objIDicomStream);
		short shItemLength = objIDicomStream.readShort();
		strAppContextName = objIDicomStream.readString(shItemLength);
		getItemLength();
	}

	/**
	 * This method serialize this 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 SerializeException Signals that Serialize Exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException,SerializeException
	{
		super.serialize(objIDicomStream);
		objIDicomStream.writeShort(getItemLength());
		objIDicomStream.writeString(strAppContextName);
	}

	/**
	 * Populate Application COntext item.
	 * @param strAppContextName String value of Application Context item.
	 */
	public void populate(String strAppContextName)
	{
		this.strAppContextName = strAppContextName;
	}
}
