/*******************************************************************************
 *  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.DisplaySystemManagement.testcases.defaultscp;

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.DisplaySubsystemSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.DisplaySubsystemSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.EquipmentAdministratorSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.EquipmentAdministratorSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.QAResultsSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.QAResultsSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.TargetLuminanceCharacteristicsSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.TargetLuminanceCharacteristicsSequenceItem;
import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicommodel.displaysystem.DisplaySystemIOD;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.DisplaySystemModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.QAResultsModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.TargetLuminanceCharacteristicsModule;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumDataSource;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumServiceType;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.IDataSource;
import cdac.medinfo.sdk.dcm30_04.services.displaysystemmanagement.dbhelper.DisplaySystem_DBAgent;
import cdac.medinfo.sdk.dcm30_04.services.displaysystemmanagement.sourceimpl.DisplaySystem_SourceImpl;
import cdac.medinfo.sdk.dcm30_04.services.scp.commons.ProcessHandler;
import cdac.medinfo.sdk.dcm30_04.services.scp.commons.SCPSession;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;

/**
 * This is the default implementation of the sample test code for  Display System Services.
 * This sample codes is to show the handling of connection, association and service request  by the default Process Handler Implementation of the SDK.
 * The Sample Test code should be extended from the ProcessHandler class of SDK for DICOM.
 * Then all the connection , association and service requests are handled by the default implementation
 * of the SDK.
 * To create DB for Display System Information  refer SQL Script DisplaySystemManagementDB.sql provided in install.
 */
public class Display_System_NGet_SCP_Def_ProcHandler
{
	public static void main(String[] args) 
	{
		try
		{
			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");
			/*
			 * Populating Database with dataset on which find request will be processed.
			 */
			Display_System_NGet_SCP_Def_ProcHandler objTest = new Display_System_NGet_SCP_Def_ProcHandler();
			IDataSet objIDataSet = objTest.populateDataset();
			objTest.storeInstanceInfo(objIDataSet);
			Display_System_NGet_SCPHandler objDisplay_System_NGet_SCPHandler = new Display_System_NGet_SCPHandler();
			objDisplay_System_NGet_SCPHandler.start();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * This function populates the data set. Display System Management service query on instance populated.
	 */
	public IDataSet populateDataset()
	{
		IDataSet objIDataSet = new DataSet();
		objIDataSet.getDictionaryReader();
		objIDataSet.getTransferSyntax();
		DisplaySystemIOD objDisplaySystemIOD=new DisplaySystemIOD();			

		
		SOPCommonModule objSOPCommonModule=objDisplaySystemIOD.getSOPCommonModule();
		String []arrStrSpecificCharacterSet = new String[1];
		arrStrSpecificCharacterSet[0] = "ISO 2022 IR 87";
		objSOPCommonModule.setSpecificCharacterSet(arrStrSpecificCharacterSet);
		objSOPCommonModule.setSOPClassUID(EnumSOPClassUIDs.DISPLAYSYSTEMIOD.getValue().toString());
		objSOPCommonModule.setSOPInstanceUID("1.2.840.10008.5.1.1.40.1");
		
		DisplaySystemModule objDisplaySystemModule= objDisplaySystemIOD.getDisplaySystemModule();
		objDisplaySystemModule.setManufacturer("Tablet Corp");
		objDisplaySystemModule.setInstitutionName("JIRA Hospital");
		objDisplaySystemModule.setInstitutionAddress("Bunkyo-ku, Tokyo, Japan");
		objDisplaySystemModule.setDeviceSerialNumber("AA1B22CCCC3D");
		objDisplaySystemModule.setStationName("TABLET1");
		objDisplaySystemModule.setInstitutionalDepartmentName("Radiology Dept.");
		
		EquipmentAdministratorSequence objEquipmentAdministratorSequence=objDisplaySystemModule.getEquipmentAdministratorSequence();
		EquipmentAdministratorSequenceItem objEquipmentAdministratorSequenceItem=objEquipmentAdministratorSequence.getNewItem();
		objEquipmentAdministratorSequenceItem.setPersonName("Yamada^Tarou");
		
		objDisplaySystemModule.setNumberOfDisplaySubsystems((short)2);		

		DisplaySubsystemSequence objDisplaySubsystemSequence=objDisplaySystemModule.getDisplaySubsystemSequence();
		
		DisplaySubsystemSequenceItem objDisplaySubsystemSequenceItem1=objDisplaySubsystemSequence.getNewItem();
		objDisplaySubsystemSequenceItem1.setDisplaySubsystemID((short)2);
		objDisplaySubsystemSequenceItem1.setDisplaySubsystemDescription("Embedded LCD");
		objDisplaySubsystemSequenceItem1.setDisplaySubsystemName("DS1");
		objDisplaySubsystemSequence.addItem(objDisplaySubsystemSequenceItem1);		
		
		DisplaySubsystemSequenceItem objDisplaySubsystemSequenceItem2=objDisplaySubsystemSequence.getNewItem();
		objDisplaySubsystemSequenceItem2.setDisplaySubsystemID((short)1);
		objDisplaySubsystemSequenceItem2.setDisplaySubsystemDescription("DSS2ofWSX");
		objDisplaySubsystemSequenceItem2.setDisplaySubsystemName("Diagnostic");
		objDisplaySubsystemSequence.addItem(objDisplaySubsystemSequenceItem2);

		TargetLuminanceCharacteristicsModule objTargetLuminanceCharacteristicsModule=objDisplaySystemIOD.getTargetLuminanceCharacteristicsModule();		
		TargetLuminanceCharacteristicsSequence objTargetLuminanceCharacteristicsSequence=objTargetLuminanceCharacteristicsModule.getTargetLuminanceCharacteristicsSequence();
		
		TargetLuminanceCharacteristicsSequenceItem objTargetLuminanceCharacteristicsSequenceItem1=objTargetLuminanceCharacteristicsSequence.getNewItem();		
		objTargetLuminanceCharacteristicsSequenceItem1.setLuminanceCharacteristicsID((short)1);
		objTargetLuminanceCharacteristicsSequenceItem1.setDisplayFunctionType("GAMMA");
		objTargetLuminanceCharacteristicsSequenceItem1.setTargetMinimumLuminance((float)0.75);
		objTargetLuminanceCharacteristicsSequenceItem1.setTargetMaximumLuminance((float)300);
		objTargetLuminanceCharacteristicsSequenceItem1.setGammaValue((float)2.2);		
		objTargetLuminanceCharacteristicsSequence.addItem(objTargetLuminanceCharacteristicsSequenceItem1);

		TargetLuminanceCharacteristicsSequenceItem objTargetLuminanceCharacteristicsSequenceItem2=objTargetLuminanceCharacteristicsSequence.getNewItem();		
		objTargetLuminanceCharacteristicsSequenceItem2.setLuminanceCharacteristicsID((short)2);
		objTargetLuminanceCharacteristicsSequenceItem2.setDisplayFunctionType("GSDF");
		objTargetLuminanceCharacteristicsSequenceItem2.setTargetMinimumLuminance((float)0.75);
		objTargetLuminanceCharacteristicsSequenceItem2.setTargetMaximumLuminance((float)521);
		objTargetLuminanceCharacteristicsSequenceItem2.setGammaValue((float).210);		
		objTargetLuminanceCharacteristicsSequence.addItem(objTargetLuminanceCharacteristicsSequenceItem2);

		QAResultsModule objQAResultsModule=objDisplaySystemIOD.getQAResultsModule();
		QAResultsSequence objQAResultsSequence=objQAResultsModule.getQAResultsSequence();
		QAResultsSequenceItem objQAResultsSequenceItem=objQAResultsSequence.getNewItem();
		objQAResultsSequenceItem.setDisplaySubsystemID((short)1);
		objQAResultsSequence.addItem(objQAResultsSequenceItem);		
		
		return  objDisplaySystemIOD.getDataSet();
	}

	/*
	 * This function stores the dataset instance to database.
	 */
	public void storeInstanceInfo(IDataSet objIDataSet)
	{
		try
		{
			/*
			 * Create DataBase and store dataset to database using DisplaySystem_DBAgent.
			 */
			String dataBasePath = "D:/SDKDatabase/DisplaySystemManagementDB";
			String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
			String driverString = "org.apache.derby.jdbc.ClientDriver";
			DisplaySystem_DBAgent objDisplaySystem_DBAgent= new DisplaySystem_DBAgent(ConnectionString, driverString);
			objDisplaySystem_DBAgent.createDisplaySystemInstance(objIDataSet,DicomConstants.DCM_DISPLAY_SYSTEM_SOP_INSTANCE.toString());
		}
		catch(Exception e)
		{
			System.out.println("Exception occured while insertion"+e.toString());
		}
	}
}

/**
 * This class inherits all the lifecycle methods involved in a communication with client.
 * This class is extended from the ProcessHandler class of the SDK for DICOM.
 * ProcessHandler class is the default processor of SDK for DICOM which is capable
 * of handling connection , association and service requests. It process all the request , 
 * generates response and sends it to SCU.
 * Once the required parameters are set on ProcessHandler class of SDK for DICOM
 * then it will handle all the Association and Service requests.
 */
class Display_System_NGet_SCPHandler extends ProcessHandler
{
	/**
	 * Starts SCP server at iPort and local host.
	 * @throws IOException Signals IOException if IO operation fails.
	 */
	public void start()
	{
		try
		{
			/*
			 * Create SCP session.
			 */
			SCPSession objSCPSession = new SCPSession();

			/*
			 * Create DataSourceImpl with ConnectionString and driver String.
			 * DataSourceImpl is default implementation of library which uses JavaDB to 
			 * Store Data Elements.The user has to start javaDB server before connection
			 * if Client Server model in java DB is used.
			 * This DataSourceImpl should be set in CFind SCP.
			 * User can also provide its own implementation of IDataSource interface
			 * to store the attributes.
			 */
			String dataBasePath = "D:/SDKDatabase/DisplaySystemManagementDB";
			String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
			String driverString = "org.apache.derby.jdbc.ClientDriver";

			IDataSource objDisplaySystem_SourceImpl = new DisplaySystem_SourceImpl(ConnectionString,driverString);
			addDataSource(EnumDataSource.DISPLAY_SYSTEM_MANAGEMENT,objDisplaySystem_SourceImpl);

			/*
			 * The session needs to be configured to load customized association according to service types selected. 
			 */
			EnumServiceType[] arrenumServiceType = new EnumServiceType[1];
			arrenumServiceType[0] = EnumServiceType.NGET_DISPLAY_SYSTEM_MANAGEMENT;
			objSCPSession.selectServices(arrenumServiceType);

			/*
			 * Set Process handler.
			 */
			objSCPSession.setIProcessHandler(this);
			/*
			 * Start the server.
			 */
			objSCPSession.start("SDK",9000);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	} 
}
