/*******************************************************************************
 *  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.ups.testcases.watch.scu;
import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplicitDE;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataElement;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDictionaryReader;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.enums.EnumPDUType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.services.association.AAbort;
import cdac.medinfo.sdk.dcm30_04.services.association.AReleaseRQorRP;
import cdac.medinfo.sdk.dcm30_04.services.association.AssociateRQ;
import cdac.medinfo.sdk.dcm30_04.services.association.DefaultAssociation;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumActionTypeID;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumServiceType;
import cdac.medinfo.sdk.dcm30_04.services.naction.commons.NActionRQ;
import cdac.medinfo.sdk.dcm30_04.services.naction.commons.NActionRSP;
import cdac.medinfo.sdk.dcm30_04.services.scu.commons.SCUSession;
import cdac.medinfo.sdk.dcm30_04.services.ups.scu.UPS_SCU;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;

/**
* This sample code specifies the role of UPS Push service class with N-Action for Unsubscribe  user for UPS SOP Class.
*/

public class UPS_Watch_NAction_Unscribe_SCU {
	public static void main(String[] args)
	{
		DicomConfig objDicomConfig = DicomConfig.createInstance();

		//enable logging,specify the path of the directory where log files are created.
		//User can specify any directory in place of 'c:\DCM_Logs'.
		//If no directory is defined by user to create logs then it is created in User Temp DIR.
		objDicomConfig.enableLogging(EnumLoggingLevel.INFO, "C:\\DCM_Logs");

		/*
		 * Creates instance of UPS_SCU_Test
		 */
		UPS_Watch_NAction_Unscribe_SCU objTestUPS_SCU = new UPS_Watch_NAction_Unscribe_SCU();

		/*
		 * Performs operation of UPS Services 
		 */
		objTestUPS_SCU.doUPS();
	}
	
	/**
	 * This function performs the operation for all supported services of UPS
	 * IAN operation involves following steps :-
	 * 1. Establish the connection with SCP
	 * 2. Establish the association with SCP
	 * 3. Initializes the SCU for corresponding N-Service and performs the operation using service request
	 * 4. Releases the association after successful processing of service request
	 * 5. Closes the connection with SCP. 
	 */
	public void doUPS()
	{
		try
		{
			/*
			 * *************************************************************************
			 * This block of code performs the connection process with SCP.
			 * It creates the SCUSession and connects with SCP using this session.
			 * *************************************************************************
			 */
			//Create SCUSession object
			SCUSession objSession = new SCUSession();
			
			/*
			 * Setting time out as 5000 milli second. It will wait 5000 mili seconds for read and write operation.
			 * If in the time specified (5000 ms) read or write operation is not performed then 
			 * session is time out.
			 */ 
			//objSession.setTimeOut(5000);
			
			//Start SCU Session by providing the IP address and Port of SCP.
			objSession.start("localhost", 9000);

			/*
			 * *************************************************************************
			 * This block of code initializes the DefaultAssociation information
			 * Using the DefaultAssociation it generates the Association Request and 
			 * creates association  with SCP.
			 * *************************************************************************
			 */

			// Initializes the service types and populates the Default Association
			EnumServiceType[] arrenumServiceType = new EnumServiceType[1];
			arrenumServiceType[0] = EnumServiceType.NACTION_UPS;
			DefaultAssociation objDefaultAssociation = new DefaultAssociation("SDK");
			objDefaultAssociation.loadAssociation(arrenumServiceType);

			//Create Association RQ
			AssociateRQ objAssociateRQ = objDefaultAssociation.getAssociationRQ("SDK");

			//Send the Association Request
			if(objSession.sendAssociationMessage(objAssociateRQ))
			{	
				//When association is successfully established the "objSession.sendAssociationMessage()" method returns true.
				System.out.println("Association established");

				/*
				 * *************************************************************************
				 * This block of code creates the UPS SCU to perform the process of 
				 * SOP Instance creation operation for UPS.
				 * It initializes the NCreate RQ and sends it to SCP.
				 * *************************************************************************
				 */
				//Create UPS SCU object using the current SCU Session.
				UPS_SCU objUPS_SCU = new UPS_SCU(objSession);

				/*
				 * *************************************************************************
				 * This block of code creates the NAction SCU to perform the process of Media Creation.
				 * It initializes the NAction RQ and sends it to SCP.
				 * *************************************************************************
				 */
				
				IDataSet objUPSDataSet = getUPSForAction();
				//Initializing NAction RQ for same SOP Instance to initiate the operation
				NActionRQ objNActionInitiateRQ = NActionRQ.getDefaultRQ(EnumSOPClassUIDs.DCM_UPS_PUSH.getValue(), "1.2.840.10008.5.1.4.34.6.1",EnumActionTypeID.UPS_UNSUBSCRIBE,objUPSDataSet);
				System.out.println("N-Action Request for UPS initialized");
				
				//sending RQ to SCP
				if(objUPS_SCU.sendMessage(objNActionInitiateRQ)) //sending request
				{
					System.out.println("Initiate Operation for NACTION");
					
					NActionRSP objNActionRSP = new NActionRSP(objUPS_SCU.getDIMSEResponse().get(0)); 
					System.out.println("N-Action Response Status :" + objNActionRSP.getStatus());
				}
				else
					System.out.println("Initiate Operation failed for NACTION");

				/*
				 * *************************************************************************
				 * This block of code releases the association and 
				 * removes the connection with SCP.  
				 * *************************************************************************
				 */
				//Create A-Release-RQ to release the association
				AReleaseRQorRP objAReleaseRQorRP = new AReleaseRQorRP(EnumPDUType.RELEASE_RQ);

				//Send the A-Release-RQ
				if(objSession.sendAssociationMessage(objAReleaseRQorRP))
				{
					System.out.println("Association is released");
					//When successful A-Release-RP is received, close the Session.
					//Please note that instead of closing the session we can create 
					//another association and send more requests. Also it is possible
					//to create multiple N-Get-RQs and send them one by one.
					objSession.close();
				}

				//In case an ABORT is received from SCP during the UPS operation
				if(objSession.getDIMSEAssociation() instanceof AAbort)
				{
					System.out.println("Abort arrives. Closing session.");
					objSession.close();
				}
			}
			else
			{
				System.out.println("Association cannot to extablished");
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private IDataSet getUPSForAction() {
				IDataSet objIDataSet = new DataSet();
				IDictionaryReader objDictionaryReader = objIDataSet.getDictionaryReader();
				//SOP Instance UID
				objIDataSet.addDataElement(getDataElement(0x0008,0x0018,"1.2.840.10008.5.1.4.34.6.1", objDictionaryReader));
		
		return objIDataSet;
	}


	/*
	 * This function create the data element from the information provided.
	 */
	private IDataElement getDataElement(int GroupNo,int ElementNo,Object objVal,IDictionaryReader objDictionaryReader)
	{
		IDataElement objIDataElement = new ImplicitDE(GroupNo, ElementNo,objVal,objDictionaryReader);
		return objIDataElement;
	}

}

