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


import java.util.Locale;

import cdac.medinfo.sdk.dcm30_04.enums.EnumPDUType;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumAbortReason;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumAbortSource;
import cdac.medinfo.sdk.dcm30_04.services.pdu.AAbortPDU;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;
import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * A-ABORT service shall be used by a requester in either of the AEs to cause the abnormal release of the association
 * This Class implements the setters/getters for all attributes of A-Abort service.  
 */
public class AAbort implements IDIMSEAssociation 
{

	/** Enum value of source. */
	EnumAbortSource enumSource;

	/** Enum value of reason. */
	EnumAbortReason enumReason;

	/**
	 * Retrieves the PDU type.
	 * @param EnumPDUType Enum value of PDU type.
	 */
	public EnumPDUType getPDUType()
	{
		return EnumPDUType.ABORT;
	}

	/**
	 * Retrieves the reason of abnormal release.
	 * @return byte - value of reason
	 */
	public EnumAbortReason getReason() 
	{
		return enumReason;
	}

	/**
	 * Sets the reason of abnormal release.
	 * @param enumReason  Enum value of Reason
	 */
	public void setReason(EnumAbortReason enumReason) 
	{
		this.enumReason = enumReason;
	}

	/**
	 * Retrieves the source of abnormal release..
	 * @return byte - value of  source
	 */
	public EnumAbortSource getSource() 
	{
		return enumSource;
	}

	/**
	 * Sets the source of abnormal release..
	 * @param enumSource Enum value of Source
	 */
	public void setSource(EnumAbortSource enumSource) 
	{
		this.enumSource = enumSource;
	}

	/**
	 * Initializes the A-Abort by given A-Abort PDU.
	 * @param objAAbortPDU instance of AAbortPDU.
	 */
	public void setAAbortPDU(AAbortPDU objAAbortPDU)
	{
		byte bReason = objAAbortPDU.getReason();
		byte bSource = objAAbortPDU.getSource();

		switch(bReason)
		{
		case (byte)0:
			enumReason = EnumAbortReason.NOT_SPECIFIED;
		break;
		case (byte)1:
			enumReason = EnumAbortReason.UNRECOGNIZED_PDU;
		break;
		case (byte)2:
			enumReason = EnumAbortReason.UNEXPECTED_PDU;
		break;
		case (byte)4:
			enumReason = EnumAbortReason.UNRECOGNIZED_PDU_PARAM;
		break;
		case (byte)5:
			enumReason = EnumAbortReason.UNEXPECTED_PDU_PARAM;
		break;
		case (byte)6:
			enumReason = EnumAbortReason.INVALID_PDU_PARAM_VALUE;
		}

		switch(bSource)
		{
		case (byte)0:
			enumSource = EnumAbortSource.SERVICE_USER;
		break;
		case (byte)2:
			enumSource = EnumAbortSource.SERVICE_PROVIDER;
		}
	}

	/**
	 * Initializes the A-Abort PDU.
	 * @return AAbortPDU instance of A-Abort PDU
	 */
	public AAbortPDU getAAbortPDU()
	{
		AAbortPDU objAAbortPDU = new AAbortPDU();
		objAAbortPDU.setPDUType(EnumPDUType.ABORT);
		objAAbortPDU.setReason(enumReason);
		objAAbortPDU.setSource(enumSource);
		return objAAbortPDU;
	}
	
	/**
	 * Validates Association Abort primitive for mandatory attributes
	 * @return boolean - shows status as true or false
	 * @throws InvalidAssociationPrimitiveException if validation fails
	 */
	public boolean validate()throws InvalidAssociationPrimitiveException
	{
		if(enumReason == null || enumSource == null)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("exceptions", "AssociationException.InvalidAttribute", new Locale(DicomConfig.createInstance().getLocale())));
			}
			throw new InvalidAssociationPrimitiveException(ResourceBundleWrapper.getValue("exceptions", "AssociationException.InvalidAttribute", new Locale(DicomConfig.createInstance().getLocale())));
		}
		
		return true;
	}
}