/*******************************************************************************
 *  Copyright 2017 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.hl7282.baselibs.segments;

import cdac.medinfo.sdk.hl7282.baselibs.NM;
import cdac.medinfo.sdk.hl7282.baselibs.RCD;
import cdac.medinfo.sdk.hl7282.commoninterface.IDataType;
import cdac.medinfo.sdk.hl7282.commoninterface.ISingleCollection;
import cdac.medinfo.sdk.hl7282.exceptions.FieldMissingException;
import cdac.medinfo.sdk.hl7282.exceptions.InvalidSegmentException;
import cdac.medinfo.sdk.hl7282.utils.EnumSegments;
import cdac.medinfo.sdk.hl7282.utils.SingleCollection;

/**
 * RDF - table row definition segment.<p>
 * The RDF segment defines the content of the row data segments (RDT) in the tabular response (RTB).
 * <p>Represents an HL7 RDF message segment. 
 * This segment has the following fields:</p><p>
 * RDF-1: Number of Columns per Row (NM)<b>Required</b><br> 
 * RDF-2: Column Description (RCD)<b>Required</b><br><p>
 * This class provides mechanism to validate message against HL7 v 2.8.2 standard.
 * The class provides accessors & mutators for the elements mentioned above.
 * The class has validate method which validates Message against HL7 v 2.8.2 standard.
 *
 * <p>User can also create Segment through {@link SegmentFactory}
 */
 public class RDF extends Segment    
{
	






	/**
	 *  Constucts the RDF segment.
	 */

	public RDF()
	{super(EnumSegments.HL7_SEG_RDF);	
	}
	//Integer values used in getter/setter is unique item id defined 
	//for each attribute.

	/**
	 * Retrieves the Column Description.
	 * @return ISingleCollection<RCD> - columnDescription
	 */
	public ISingleCollection<RCD> getColumnDescription() 
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(702);
		ISingleCollection<RCD> objDataList = new SingleCollection<RCD>();
		if(objDataCollection != null && objDataCollection.size() != 0)
		{
			for(IDataType objDataType : objDataCollection)
			{
				RCD objRCD = (RCD)objDataType;
				objDataList.add(objRCD);
			}
		}

		return objDataList;
	}

	/**
	 * Retrieves the Number of Columns per Row. 
	 * @return NM - numberofColumnsperRow
	 */
	public NM getNumberofColumnsperRow()
	{
		ISingleCollection<IDataType> objDataCollection =getDataType(701);
		if(objDataCollection != null && objDataCollection.size() != 0) 
			return (NM)objDataCollection.elementAt(0);
		else
			return null;
	}

	/**
	 * Set the Column Description.
	 * @param strSegmentFieldName Identifies the field occupying the column. 
	 * @param strHL7DataType The two or three character HL7 data type. 
	 * @param strMaxColumnWidth The maximum width of the column, as dictated by the responding system. 
	 */ 
	public void addColumnDescription (String strSegmentFieldName,String strHL7DataType,String strMaxColumnWidth)
	{
		RCD tempColumnDescription  = (RCD)getDataElement(702);
		tempColumnDescription.setSegmentFieldName(strSegmentFieldName);
		tempColumnDescription.setHL7DataType(strHL7DataType);
		tempColumnDescription.setMaximumColumnWidth(strMaxColumnWidth);
		addColumnDescription(tempColumnDescription);

	} 

	/** Set the Column Description.
	 * @param objtempColumnDescription object of ColumnDescription 
	 */ 
	public void addColumnDescription(RCD objtempColumnDescription)
	{
		addDataType(702,objtempColumnDescription);
	}	/**
	 * Set the Column Description.
	 * @param objColumnDescriptionList List of Column Description
	 */	
	@SuppressWarnings("unchecked")
	public void setColumnDescription(ISingleCollection<RCD> objColumnDescriptionList)
	{
		ISingleCollection<IDataType> objCollection = null;
		ISingleCollection<?> objUnknownCollection = (ISingleCollection<?>)objColumnDescriptionList;
		objCollection = (ISingleCollection<IDataType>)objUnknownCollection;
		setDataType(702,objCollection);
	}


	/** 
	 * Set the Number of Columns per Row.
	 * @param strPolicyLimitDays Specifies the maximum number of days that the insurance policy will cover.
	 */ 
	public void setNumberofColumnsperRow (String strPolicyLimitDays) 
	{
		NM numberofColumnsperRow  = (NM)getDataElement(701);
		numberofColumnsperRow.setValue(strPolicyLimitDays); 
		setNumberofColumnsperRow (numberofColumnsperRow);
	} 

	/** Set the Number of Columns per Row.
	 * @param objnumberofColumnsperRow object of NumberofColumnsperRow
	 */ 
	public void setNumberofColumnsperRow(NM objnumberofColumnsperRow)
	{
		setDataType(701,objnumberofColumnsperRow);

	}

	/**
	 * Validates HL7 Message objects .
	 * Validate method checks for validation each and every segments in side the Message object against specified message Structure. 
	 * Validation chcecks include optionality of segment or group.
	 * It also verifies the number of times segment or group is repeated in message.
	 * e.g 
	 * {PSH} while parsing PSH segment validate method will check for its mandatory presence.
	 * [PSH] in this case validate will throw exception if one tries to enter more than one PSH segment.
	 * @return boolean - true or false.
	 * @throws InvalidSegmentException Throws when Segment Validation fails.
	 */
	@Override
	public boolean validate() throws InvalidSegmentException 
	{
		/*
		 * Number of Columns per Row, Column Description are required fields.
		 */
		try 
		{
			super.validate();
			return true;		
		}
		catch(FieldMissingException ex)
		{
			throw new InvalidSegmentException(enumSegments.getValue(),ex);
		}
	}
}