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

import cdac.medinfo.sdk.dcm30_04.baselibs.Tag;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.ITag;

import cdac.medinfo.sdk.dcm30_04.exceptions.InvalidModuleException;
import cdac.medinfo.sdk.dcm30_04.exceptions.MandatoryMissingException;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 \* This module describes attributes in File Set Identification Module.
 */
public class FileSetIdentificationModule extends Module   
{
	/**
	 * The object of single collection.
	 */
	public static ISingleCollection<ITag> objSingleCollection = null;

	/**
	 * Instantiates a new File Set Identification Module.
	 * @param objIDataSet Object of Data Set.
	 */
	public FileSetIdentificationModule(IDataSet objIDataSet)
	{
		super(objIDataSet);
	}

	static
	{
		objSingleCollection = new SingleCollection<ITag>();
		objSingleCollection.add(new Tag(0x0004,0x1130));
		objSingleCollection.add(new Tag(0x0004,0x1141));
		objSingleCollection.add(new Tag(0x0004,0x1142));
	}

	/**
	 * Sets User or implementation specific Identifier (up to 16 characters).
	 * The File-set ID is intended to be a short human readable label to easily (but
	 * not necessarily uniquely) identify a specific File-set to facilitate operator manipulation of the physical media on]
	 * which the File-set is stored.
	 * @param strFileSetID String value of File-Set ID.
	 */
	public void setFileSetID(String strFileSetID)
	{
		/* Accept File-Set ID in String format of type CS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the DataSet
		 */
		addDataElement(0x0004, 0x1130, DicomConstants.DCM_DT_CS, strFileSetID);
	}

	/**
	 * Retrieves  User or implementation specific Identifier of File Set.
	 * @return String - String value of File-Set ID.
	 */
	public String getFileSetID()
	{
		Object objValue = getValue(0x0004,0x1130);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * Sets ID of a File (in the same File-set) used for user comments related to the File-set (e.g. a readme file). The Specific 
	 * Character set used may be specified in the Specific Character Set of the File-set Descriptor File (0004,1142).
	 * @param strArrFileSetDescriptorFileID String array of File-Set Descriptor ID.
	 */
	public void setFileSetDescriptorFileID(String[] strArrFileSetDescriptorFileID)
	{
		/* Accept File Set Descriptor File ID in String format of type CS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the DataSet
		 */
		addDataElement(0x0004, 0x1141, DicomConstants.DCM_DT_CS, strArrFileSetDescriptorFileID);
	}

	/**
	 * Retrieves  ID of a File (in the same File-set) used for user comments related to the File-set .
	 * @return String[] - String array of File-Set Descriptor File ID.
	 */
	public String[] getFileSetDescriptorFileID()
	{
		Object objValue = getValue(0x0004,0x1141);
		if(objValue != null)
			return (String[])objValue;
		return null;
	}

	/**
	 * Sets Specific Character Set of File-set Descriptor File. 
	 * @param strSpecificCharacterSetOfFileSetDescriptorFile String value of Specific Character Set of File-set Descriptor File.
	 */
	public void setSpecificCharacterSetOfFileSetDescriptorFile(String strSpecificCharacterSetOfFileSetDescriptorFile)
	{
		/* Accept Specific Character Set of File-set Descriptor File in String format of type CS.
		 * Read the Transfer Syntax and check if ImplicitDE or ExplicitDE to create.
		 * Set the iGroupNumber and iElementNumber to ImplicitDE object 
		 * Set the iVRType
		 * Set length from the element
		 * Add this element to the DataSet
		 */
		addDataElement(0x0004, 0x1142, DicomConstants.DCM_DT_CS, strSpecificCharacterSetOfFileSetDescriptorFile);
	}

	/**
	 * Retrieves  Specific Character Set Of File Set Descriptor File .
	 * @return String  - String value of Specific Character Set Of File Set Descriptor File .
	 */
	public String getSpecificCharacterSetOfFileSetDescriptorFile()
	{
		Object objValue = getValue(0x0004,0x1142);
		if(objValue != null)
			return objValue.toString();
		return null;
	}

	/**
	 * This method validates each Attribute in the Module according to the Type
	 * of the Attribute.Attributes are of 5 types.
	 * Type 1,Type 2 are Mandatory:
	 * Mandatory Attributes have to present in the Module.
	 * Type 1C,2C are Conditional:
	 * Conditional Attributes' presence depend on a condition.
	 * If the condition is satisfied the Attribute has to be present.
	 * Type 3 are Optional
	 * Optional Attributes can be added as and when required.
	 * @return boolean - returns validation status as success or failure
	 * @throws InvalidModuleException - If validation of Module fails
	 */
	public  boolean validateModule() throws InvalidModuleException
	{

		try
		{
			/*
			 * Validation Rules for Mandatory Elements
			 */
			if(!isPresent(0x0004,0x1130))
				throw new MandatoryMissingException("0004,1130");

			/*
			 * Condition for 0004,1142 is not clear
			 * Required to specify the expanded or replacement character Required to specify the expanded or replacement character
			 * Please refer pdf Chapter 3, page no 827.
			 */
		}
		catch(MandatoryMissingException mme)
		{
			if(DicomConfig.createInstance().isLoggingOn())
				DicomLogger.getLogger().log(mme.getMessage());

			
				throw new InvalidModuleException(this.getClass().getSimpleName(),mme);

		}
		return true;
	}
	/**
	 * This method returns the custom collection containing the list of DataElements belonging to the CT
	 * Image Module specified by DICOM Standard.
	 * 
	 * @return collection - custom collection containing the list of DataElements belonging to the CT Image Module
	 */
	public ISingleCollection<ITag> getVectorOfKeys()
	{
		return objSingleCollection;
	}
}
