/*******************************************************************************
 *  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.services.pdu.AppContextItem;
import cdac.medinfo.sdk.dcm30_04.services.pdu.AssociateRQOrACPDU;
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.ResourceBundleWrapper;

/**
 * This is an abstract class which implements getters and setters for common attributes of Association RQ and AC. 
 */
public abstract class AssociateRQOrAC implements IDIMSEAssociation 
{

	/** The called entity title. */
	String calledEntityTitle;

	/** The calling entity title. */
	String callingEntityTitle;

	/** The protocol version. */
	short protocolversion = 1;

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

	/**
	 * Instantiates AssociateRQOrAC.
	 */
	public AssociateRQOrAC()
	{

	}

	/**
	 * Instantiates AssociateRQOrAC with parameters given.
	 * @param calledEntityTitle String value of Called Entity Title.
	 * @param callingEntityTitle String value of Calling Entity Title.
	 * @param strAppContextName String value of Application Context name
	 * @param protocolversion short value of Protocol version.
	 */
	public AssociateRQOrAC(String calledEntityTitle, String callingEntityTitle,short protocolversion) 
	{
		this.calledEntityTitle = calledEntityTitle;
		this.callingEntityTitle = callingEntityTitle;
		this.strAppContextName = DicomConstants.DCM_APP_CONTEXT_NAME;
		this.protocolversion = protocolversion;
	}
	/**
	 * Retrieves the called Application Entity Title.
	 * @return String - value of called Application Entity Title.
	 */
	public String getCalledEntityTitle() 
	{
		return calledEntityTitle;
	}

	/**
	 * Sets the called Application Entity Title.
	 * @param strCalledEntityTitle String value of called Application Entity Title.
	 */
	public void setCalledEntityTitle(String strCalledEntityTitle) 
	{
		this.calledEntityTitle = strCalledEntityTitle;
	}

	/**
	 * Retrieves the calling Application Entity Title.
	 * @return String - value of calling Application Entity Title.
	 */
	public String getCallingEntityTitle() 
	{
		return callingEntityTitle;
	}

	/**
	 * Sets the calling Application Entity Title.
	 * @param strCallingEntityTitle String value of calling Application Entity Title.
	 */
	public void setCallingEntityTitle(String strCallingEntityTitle) 
	{
		this.callingEntityTitle = strCallingEntityTitle;
	}

	/**
	 * Retrieves the supported Protocol Version
	 * @return short - value of supported Protocol Version
	 */
	public short getProtocolversion() 
	{
		return protocolversion;
	}

	/**
	 * Sets the supported Protocol Version
	 * @param shProtocolversion Short value of supported Protocol Version
	 */
	public void setProtocolversion(short shProtocolversion) 
	{
		this.protocolversion = shProtocolversion;
	}

	/**
	 * Retrieves the Application Context Name
	 * @return String - value of Application Context Name
	 */
	public String getAppContextName() 
	{
		return strAppContextName;
	}

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

	/**
	 * Initializes the AssociateRQOrAC according to the given AssociateRQOrACPDU
	 * @param objAssociateRQOrACPDU instance of AssociateRQOrACPDU
	 */
	public void setAssocRQorACPDU(AssociateRQOrACPDU objAssociateRQOrACPDU)
	{
		this.protocolversion = objAssociateRQOrACPDU.getProtocolVersion();
		this.calledEntityTitle = objAssociateRQOrACPDU.getCalledAETitle();
		this.callingEntityTitle = objAssociateRQOrACPDU.getCallingAETitle();

		AppContextItem objAppContextItem = objAssociateRQOrACPDU.getAppContextItem();
		if(objAppContextItem != null)
			this.strAppContextName = objAppContextItem.getAppContextName();
	}
	
	/**
	 * Validates Association primitive for mandatory attributes
	 * @return boolean - shows status as true or false
	 * @throws InvalidAssociationPrimitiveException if validation fails
	 */
	public boolean validate()throws InvalidAssociationPrimitiveException
	{
		if(strAppContextName == null || calledEntityTitle == null || callingEntityTitle == null)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger.getLogger().log(ResourceBundleWrapper.getValue("exceptions", "AssociationException.InvalidAttribute", new Locale(DicomConfig.createInstance().getLocale())));
			}
			return false;
		}
		
		return true;
	}
}
