/*******************************************************************************
 *  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.DataSet;
import cdac.medinfo.sdk.dcm30_04.baselibs.GeneralMachineVerificationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.GeneralMachineVerificationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.IonControlPointVerificationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.IonControlPointVerificationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.IonMachineVerificationSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.IonMachineVerificationSequenceItem;
import cdac.medinfo.sdk.dcm30_04.baselibs.RangeShifterSettingsSequence;
import cdac.medinfo.sdk.dcm30_04.baselibs.RangeShifterSettingsSequenceItem;
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.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.RTGeneralMachineVerificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.RTIonMachineVerificationModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.modules.SOPCommonModule;
import cdac.medinfo.sdk.dcm30_04.dicommodel.rt.RTIonMachineVerificationIOD;
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.EnumPatientSupportType;
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.EnumScanMode;
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 Ion Verification Service Classes.
 * To create DB for RT Machine Verification Service Classes refer SQL Script RTMachineVerificationDB.sql provided in install.
 */
public class RTIonPlan_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.
			 */
			RTIonPlan_Store objTest = new RTIonPlan_Store();
			IDataSet objIDataSet = objTest.populateDataset();
			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();
		
		RTIonMachineVerificationIOD objRTIonMachineVerificationIOD=new RTIonMachineVerificationIOD();
		
		//SOP Common Module
		SOPCommonModule objSOPCommonModule=objRTIonMachineVerificationIOD.getSOPCommonModule();
		String []arrStrSpecificCharacterSet = new String[1];
		arrStrSpecificCharacterSet[0] = "ISO 2022 IR 87";
		objSOPCommonModule.setSpecificCharacterSet(arrStrSpecificCharacterSet);
		objSOPCommonModule.setSOPClassUID(EnumSOPClassUIDs.RTIONMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue().toString());
		objSOPCommonModule.setSOPInstanceUID("1.2.840.10008.5.1.2.68.1");
		
		//RT General Machine Verification Module
		RTGeneralMachineVerificationModule objRTGeneralMachineVerificationModule=objRTIonMachineVerificationIOD.getRTGeneralMachineVerificationModule();		
		objRTGeneralMachineVerificationModule.setPatientID("SAM323");
		objRTGeneralMachineVerificationModule.setReferencedFractionGroupNumber("4");
		
		//Referenced RT Plan Sequence
		ReferencedRTPlanSequence objReferencedRTPlanSequence=objRTGeneralMachineVerificationModule.getReferencedRTPlanSequence();
		SOPInstanceReferenceMacroDependentItem objSOPInstanceReferenceMacroDependentItem=objReferencedRTPlanSequence.getNewItem();
		SOPInstanceReferenceMacro objSOPInstanceReferenceMacro=objSOPInstanceReferenceMacroDependentItem.getSOPInstanceReferenceMacro();
		objSOPInstanceReferenceMacro.setReferencedSOPClassUID(EnumSOPClassUIDs.RTIONMACHINEVERIFICATIONINFORMATIONOBJECTDEFINITION.getValue().toString());
		objSOPInstanceReferenceMacro.setReferencedSOPInstanceUID("1.2.840.10008.5.1.2.68.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.ION);
		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);
		
		//RT Ion Machine Verification Module
		RTIonMachineVerificationModule objRTIonMachineVerificationModule=objRTIonMachineVerificationIOD.getRTIonMachineVerificationModule();
		
		//Ion Machine Verification Sequence
		IonMachineVerificationSequence objIonMachineVerificationSequence=objRTIonMachineVerificationModule.getIonMachineVerificationSequence();		
		IonMachineVerificationSequenceItem objIonMachineVerificationSequenceItem=objIonMachineVerificationSequence.getNewItem();
		
		
		objIonMachineVerificationSequenceItem.setRadiationMassNumber("3450");
		objIonMachineVerificationSequenceItem.setRadiationAtomicNumber("26");
		short[]sharr={5,9};
		objIonMachineVerificationSequenceItem.setRadiationChargeState(sharr);
		objIonMachineVerificationSequenceItem.setScanMode(EnumScanMode.UNIFORM);
		objIonMachineVerificationSequenceItem.setNumberofRangeModulators("86");
		objIonMachineVerificationSequenceItem.setNumberofLateralSpreadingDevices("43");
		objIonMachineVerificationSequenceItem.setNumberofRangeShifters("78");		
		objIonMachineVerificationSequenceItem.setPatientSupportType(EnumPatientSupportType.CHAIR);		
		
		//Ion Control Point VerificationSequence
		IonControlPointVerificationSequence objIonControlPointVerificationSequence=objIonMachineVerificationSequenceItem.getIonControlPointVerificationSequence();
		IonControlPointVerificationSequenceItem objIonControlPointVerificationSequenceItem=objIonControlPointVerificationSequence.getNewItem();	
		
		objIonControlPointVerificationSequenceItem.setNominalBeamEnergy("150");
		objIonControlPointVerificationSequenceItem.setMetersetRateSet((short)5.5f);
		objIonControlPointVerificationSequenceItem.setGantryAngle("90");
		objIonControlPointVerificationSequenceItem.setGantryRotationDirection(EnumGantryRotationDirection.COUNTER_CLOCKWISE);
		objIonControlPointVerificationSequenceItem.setReferencedControlPointIndex("10");
		
		//Beam Limiting Device Position Sequence 
		BeamLimitingDevicePositionSequence objBeamLimitingDevicePositionSequence=objIonControlPointVerificationSequenceItem.getBeamLimitingDevicePositionSequence();
		BeamLimitingDevicePositionSequenceItem objBeamLimitingDevicePositionSequenceItem=objBeamLimitingDevicePositionSequence.getNewItem();
		objBeamLimitingDevicePositionSequenceItem.setRTBeamLimitingDeviceType(EnumRTBeamLimitingDeviceType.SYMMETRIC_IEC_X_DIRECTION);
		String[]arr={"101","102"};
		objBeamLimitingDevicePositionSequenceItem.setLeaf_JawPositions(arr);		
		objBeamLimitingDevicePositionSequence.addItem(objBeamLimitingDevicePositionSequenceItem);
		
		//Range Shifter Settings Sequence
		RangeShifterSettingsSequence objRangeShifterSettingsSequence=objIonControlPointVerificationSequenceItem.getRangeShifterSettingsSequence();
		RangeShifterSettingsSequenceItem objRangeShifterSettingsSequenceItem=objRangeShifterSettingsSequence.getNewItem();
		objRangeShifterSettingsSequenceItem.setRangeShifterSetting("IN");
		objRangeShifterSettingsSequenceItem.setReferencedRangeShifterNumber("23");		
		objRangeShifterSettingsSequence.addItem(objRangeShifterSettingsSequenceItem);		
		
		objIonControlPointVerificationSequence.addItem(objIonControlPointVerificationSequenceItem);		
		objIonMachineVerificationSequence.addItem(objIonMachineVerificationSequenceItem);	
		
		return  objRTIonMachineVerificationIOD.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.68.1");
			System.out.println("Plan added Succesfully");

		}
		catch(Exception e)
		{
			System.out.println("Exception occured while insertion"+e.toString());
		}
	}

}
