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

import cdac.medinfo.sdk.dcm30_04.baselibs.ApplicatorSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ApplicatorSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.BeamLimitingDevicePositionSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.BeamLimitingDevicePositionSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.ConventionalControlPointVerificationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ConventionalControlPointVerificationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.ConventionalMachineVerificationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ConventionalMachineVerificationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.GeneralMachineVerificationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.GeneralMachineVerificationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.ImplicitDE;
import cdac.medinfo.sdk.dcm30_04.baselibs.RecordedBlockSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.RecordedBlockSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.RecordedCompensatorSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.RecordedCompensatorSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedBolusSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedBolusSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.ReferencedRTPlanSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.SOPInstanceReferenceMacro;
import cdac.medinfo.sdk.dcm30_04.baselibs.SOPInstanceReferenceMacroDependentItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.WedgePositionSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.WedgePositionSequenceItem;
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.dicommodel.modules.RTConventionalMachineVerificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.RTGeneralMachineVerificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.rt.RTConventionalMachineVerificationIOD;
import cdac.medinfo.sdk.dcm30_04.enums.EnumApplicatorType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumGantryRotationDirection;
import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRTBeamLimitingDeviceType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumRadiationType;
import cdac.medinfo.sdk.dcm30_04.enums.EnumSOPClassUIDs;
import cdac.medinfo.sdk.dcm30_04.enums.EnumWedgePosition;
import cdac.medinfo.sdk.dcm30_04.services.rtmachineverificationservice.dbhelper.RTMachineVerification_DBAgent;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;

/**
 * This is the implementation to store RT Plan in the database for RT Conventional Verification Service Classes.
 * To create DB for RT Machine Verification Service Classes refer SQL Script RTMachineVerificationDB.sql provided in install.
 */
public class RTConventionalPlan_Store
{
	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.
			 */
			RTConventionalPlan_Store objTest = new RTConventionalPlan_Store();
			IDataSet objIDataSet = objTest.populateDataset();
			//(300A,00EF)
			objIDataSet.addDataElement(getDataElement(0x300A,0x00EF,"22",objIDataSet.getDictionaryReader()));
			objTest.storeInstanceInfo(objIDataSet);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * This function populates the data set. 
	 */
	public IDataSet populateDataset()
	{
		IDataSet objIDataSet = new DataSet();
		objIDataSet.getDictionaryReader();
		objIDataSet.getTransferSyntax();
		RTConventionalMachineVerificationIOD objRTConventionalMachineVerificationIOD=new RTConventionalMachineVerificationIOD();			
		
				
		//SOP Common Module
		SOPCommonModule objSOPCommonModule=objRTConventionalMachineVerificationIOD.getSOPCommonModule();
		String []arrStrSpecificCharacterSet = new String[1];
		arrStrSpecificCharacterSet[0] = "ISO 2022 IR 87";
		objSOPCommonModule.setSpecificCharacterSet(arrStrSpecificCharacterSet);
		objSOPCommonModule.setSOPClassUID(EnumSOPClassUIDs.RTCONVENTIONALMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue().toString());
		objSOPCommonModule.setSOPInstanceUID("1.2.840.10008.5.1.2.69.1");
		
		//RT General Machine Verification Module
		RTGeneralMachineVerificationModule objRTGeneralMachineVerificationModule=objRTConventionalMachineVerificationIOD.getRTGeneralMachineVerificationModule();		
		objRTGeneralMachineVerificationModule.setPatientID("SAL520");
		objRTGeneralMachineVerificationModule.setReferencedFractionGroupNumber("1");
		
		//Referenced RT Plan Sequence
		ReferencedRTPlanSequence objReferencedRTPlanSequence=objRTGeneralMachineVerificationModule.getReferencedRTPlanSequence();
		SOPInstanceReferenceMacroDependentItem objSOPInstanceReferenceMacroDependentItem=objReferencedRTPlanSequence.getNewItem();
		SOPInstanceReferenceMacro objSOPInstanceReferenceMacro=objSOPInstanceReferenceMacroDependentItem.getSOPInstanceReferenceMacro();
		objSOPInstanceReferenceMacro.setReferencedSOPClassUID(EnumSOPClassUIDs.RTCONVENTIONALMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue().toString());
		objSOPInstanceReferenceMacro.setReferencedSOPInstanceUID("1.2.840.10008.5.1.2.69.1");
		objReferencedRTPlanSequence.addItem(objSOPInstanceReferenceMacroDependentItem);

		
		//General Machine Verification Sequence
		GeneralMachineVerificationSequence objGeneralMachineVerificationSequence=objRTGeneralMachineVerificationModule.getGeneralMachineVerificationSequence();
		GeneralMachineVerificationSequenceItem objGeneralMachineVerificationSequenceItem=objGeneralMachineVerificationSequence.getNewItem();
		objGeneralMachineVerificationSequenceItem.setSpecifiedTreatmentTime("300");		
		objGeneralMachineVerificationSequenceItem.setTreatmentMachineName("Test123");
		objGeneralMachineVerificationSequenceItem.setRadiationType(EnumRadiationType.PHOTON);
		objGeneralMachineVerificationSequenceItem.setNumberOfWedges("20");
		objGeneralMachineVerificationSequenceItem.setNumberOfBlocks("22");
		objGeneralMachineVerificationSequenceItem.setNumberOfCompensators("24");
		objGeneralMachineVerificationSequenceItem.setNumberOfBoli("26");		
		objGeneralMachineVerificationSequenceItem.setNumberOfControlPoints("1");
		objGeneralMachineVerificationSequenceItem.setReferencedBeamNumber("28");
		
		//Recorded Compensator Sequence
		RecordedCompensatorSequence objRecordedCompensatorSequence=objGeneralMachineVerificationSequenceItem.getRecordedCompensatorSequence();
		RecordedCompensatorSequenceItem objRecordedCompensatorSequenceItem=objRecordedCompensatorSequence.getNewItem();
		objRecordedCompensatorSequenceItem.setReferencedCompensatorNumber("101");
		objRecordedCompensatorSequence.addItem(objRecordedCompensatorSequenceItem);
		
		//Recorded Block Sequence
		RecordedBlockSequence objRecordedBlockSequence=objGeneralMachineVerificationSequenceItem.getRecordedBlockSequence();
		RecordedBlockSequenceItem objRecordedBlockSequenceItem=objRecordedBlockSequence.getNewItem();
		objRecordedBlockSequenceItem.setReferencedBlockNumber("33");
		objRecordedBlockSequence.addItem(objRecordedBlockSequenceItem);
		
		
		//Applicator Sequence
		ApplicatorSequence objApplicatorSequence=objGeneralMachineVerificationSequenceItem.getApplicatorSequence();
		ApplicatorSequenceItem objApplicatorSequenceItem=objApplicatorSequence.getNewItem();
		objApplicatorSequenceItem.setApplicatorType(EnumApplicatorType.ELECTRON_RECTANGULAR);
		objApplicatorSequence.addItem(objApplicatorSequenceItem);
				
		//Referenced Bolus Sequence
		ReferencedBolusSequence objReferencedBolusSequence=objGeneralMachineVerificationSequenceItem.getReferencedBolusSequence();
		ReferencedBolusSequenceItem objReferencedBolusSequenceItem=objReferencedBolusSequence.getNewItem();
		objReferencedBolusSequenceItem.setReferencedROINumber("101010");
		objReferencedBolusSequence.addItem(objReferencedBolusSequenceItem);		
		
		objGeneralMachineVerificationSequence.addItem(objGeneralMachineVerificationSequenceItem);
		
		RTConventionalMachineVerificationModule objRTConventionalMachineVerificationModule=objRTConventionalMachineVerificationIOD.getRTConventionalMachineVerificationModule();
		
		//Conventional Machine Verification Sequence
		ConventionalMachineVerificationSequence objConventionalMachineVerificationSequence=objRTConventionalMachineVerificationModule.getConventionalMachineVerificationSequence();
		ConventionalMachineVerificationSequenceItem objConventionalMachineVerificationSequenceItem=objConventionalMachineVerificationSequence.getNewItem();
		
		//ConventionalControlPointVerificationSequence
		ConventionalControlPointVerificationSequence objConventionalControlPointVerificationSequence=objConventionalMachineVerificationSequenceItem.getConventionalControlPointVerificationSequence();
		ConventionalControlPointVerificationSequenceItem objConventionalControlPointVerificationSequenceItem=objConventionalControlPointVerificationSequence.getNewItem();
		objConventionalControlPointVerificationSequenceItem.setNominalBeamEnergy("250");
		objConventionalControlPointVerificationSequenceItem.setDoseRateSet("50");
		objConventionalControlPointVerificationSequenceItem.setGantryAngle("45");
		objConventionalControlPointVerificationSequenceItem.setGantryRotationDirection(EnumGantryRotationDirection.NO_ROTATION);
		objConventionalControlPointVerificationSequenceItem.setReferencedControlPointIndex("66");

		//WedgePositionSequence
		WedgePositionSequence objWedgePositionSequence=objConventionalControlPointVerificationSequenceItem.getWedgePositionSequence();
		WedgePositionSequenceItem objWedgePositionSequenceItem=objWedgePositionSequence.getNewItem();
		objWedgePositionSequenceItem.setWedgePosition(EnumWedgePosition.IN);
		objWedgePositionSequenceItem.setReferencedWedgeNumber("3");
		objWedgePositionSequence.addItem(objWedgePositionSequenceItem);
		
		//BeamLimitingDevicePositionSequence
		BeamLimitingDevicePositionSequence objBeamLimitingDevicePositionSequence=objConventionalControlPointVerificationSequenceItem.getBeamLimitingDevicePositionSequence();
		BeamLimitingDevicePositionSequenceItem objBeamLimitingDevicePositionSequenceItem=objBeamLimitingDevicePositionSequence.getNewItem();
		objBeamLimitingDevicePositionSequenceItem.setRTBeamLimitingDeviceType(EnumRTBeamLimitingDeviceType.SYMMETRIC_IEC_X_DIRECTION);
		String[]arr={"101","102"};
		objBeamLimitingDevicePositionSequenceItem.setLeaf_JawPositions(arr);		
		objBeamLimitingDevicePositionSequence.addItem(objBeamLimitingDevicePositionSequenceItem);		
		
		objConventionalControlPointVerificationSequence.addItem(objConventionalControlPointVerificationSequenceItem);	
		
		objConventionalMachineVerificationSequence.addItem(objConventionalMachineVerificationSequenceItem);
		
		return  objRTConventionalMachineVerificationIOD.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/RTMachineVerificationDB";
			String ConnectionString = "jdbc:derby://localhost:5050/" + dataBasePath;
			String driverString = "org.apache.derby.jdbc.ClientDriver";
			RTMachineVerification_DBAgent objRTMachineVerification_DBAgent= new RTMachineVerification_DBAgent(ConnectionString, driverString);
			objRTMachineVerification_DBAgent.createRTPlan(objIDataSet,"1.2.840.10008.5.1.2.69.1");
			System.out.println("Plan added Succesfully");

		}
		catch(Exception e)
		{
			System.out.println("Exception occured while insertion"+e.toString());
		}
	}
	
	/**
	 * This function create the data element from the information provided.
	 */
	private static IDataElement getDataElement(int GroupNo,int ElementNo,Object objVal,IDictionaryReader objDictionaryReader)
	{
		IDataElement objIDataElement = new ImplicitDE(GroupNo, ElementNo,objVal,objDictionaryReader);
		return objIDataElement;
	}

}
