/*******************************************************************************
 *  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 java.util.Locale;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IItem;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITransferSyntax;
import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidItemException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ItemLimitException;
import cdac.medinfo.sdk.dcm30_04.exceptions.ValidationException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.utils.ICollectionIterator;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;


/**
 * A sequence which provides overlay pixel data. A single item shall be present.
 */
public class OverlayPixelDataSequence extends SQ  
{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/*This constructor create sequence with specified transfersyntax and dictionaryreader*/
	/**
	 * This constructor initialize SQ with specified transferSyntax and specified DictionaryReader
	 * @param objTransferSyntax specifies the DICOM Standard defined TransfeSyntax
	 * @param objDictionaryReader specifies the DICOM Dictionary reader to be used 
	 */
	OverlayPixelDataSequence(ITransferSyntax objTransferSyntax,IDictionaryReader objDictionaryReader)
	{
		super(objTransferSyntax, objDictionaryReader);
	}

	/*
	 * This is the overriding method isValidItem which checks whether the item being added is of OverlayPixelDataSequence
	 * @param objIItem instance of Item class
	 * @return boolean - status indicating valid/invalid.
	 */
	protected boolean isValidItem(IItem objIItem)
	{
		return objIItem instanceof OverlayPixelDataSequenceItem ? true : false;
	}


	/*
	 * (non-Javadoc) This overriding method creates the item of type OverlayPixelDataSequenceItem
	 * @see cdac.medinfo.sdk.dcm30_04.baselibs.dicomv3.SQ#createItem()
	 * @return IItem - instance of Item class.
	 */
	protected IItem createItem()
	{
		return new OverlayPixelDataSequenceItem(objITransferSyntax,objIDictionaryReader);
	}

	/*
	 * When a new Item is requested, it should be added in the internal collection of sequence
	 * before giving it to the user
	 */
	/**
	 * Retrieves preinitialized object of Overlay Pixel Data Sequence Item.
	 * User has to add this item to sequence by calling additem of SQ.
	 * @return OverlayPixelDataSequenceItem - instance of Item of this sequence
	 */
	public OverlayPixelDataSequenceItem getNewItem()
	{
		OverlayPixelDataSequenceItem objOverlayPixelDataSequenceItem = new OverlayPixelDataSequenceItem(objITransferSyntax,objIDictionaryReader);

		return objOverlayPixelDataSequenceItem;
	}

	/**
	 * This method Validates the all Sequence Items according to the rules specified by DICOM Satndard
	 * @return boolean - returns status indicating validation success or failure
	 * @throws ValidationException If validation of Item fails.
	 */
	public boolean validateForModule() throws ValidationException
	{
		try
		{
			ICollectionIterator<IItem> objIterator = objSingleCollection.iterator();
			if(objSingleCollection.size() > 1)
				throw new ItemLimitException("Overlay Pixel Data Sequence");
			while(objIterator.hasNext())
			{
				IItem objIItem = objIterator.next();
				objIItem.validateForModule();
			}
		}
		catch( InvalidItemException ie)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ie.getMessage());
			}
			throw new ValidationException(ResourceBundleWrapper.getValue("exceptions", "InvalidSequence", new Locale(DicomConfig.createInstance().getLocale()))+ Integer.toHexString(iGroupNumber) + "," + Integer.toHexString(iElementNumber), ie);
		}
		return true;
	}
}
