/*******************************************************************************
 *  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.relevantpatientinfoqry.sourceimpl;

import java.sql.SQLException;
import java.util.Locale;


import cdac.medinfo.sdk.dcm30_04.commoninterface.IDataSet;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;

import cdac.medinfo.sdk.dcm30_04.utils.ResourceBundleWrapper;

/**
 * This class provides functionality to populate the data base and stores the data set.
 */
public class RelevantPatientInfoQry_StoreAgent
{
	private RelevantPatientInfoQryDBAgent objRelevantPatientInfoQryAgent = null;

	/**
	 * Instantiates a new data sink implementation.
	 * @param strConnectionString the string connection string
	 * @param strDriverName the string driver name
	 * @throws SQLException the SQL exception
	 * @throws ClassNotFoundException the class not found exception
	 * @throws InstantiationException the instantiation exception
	 * @throws IllegalAccessException the illegal access exception
	 */
	public RelevantPatientInfoQry_StoreAgent(String strConnectionString, String strDriverName) throws SQLException,ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		objRelevantPatientInfoQryAgent = RelevantPatientInfoQryDBAgent.createInstance(strConnectionString, strDriverName);
	}

	/**
	 * Stores the Data set information into database which could be further queried using RelevantPatientInfoQry CFind Request. 
	 * @param objIDataSet instance of Data set
	 * @return boolean true if successfully inserted else false
	 * @throws SQLException if operation fails
	 * @throws ClassNotFoundException if operation fails
	 * @throws InstantiationException if operation fails
	 * @throws IllegalAccessException if operation fails
	 */
	public boolean storeInstanceInfo(IDataSet objIDataSet)throws SQLException,ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		if(validate(objIDataSet))
			return objRelevantPatientInfoQryAgent.storeInstanceInfo(objIDataSet);
		else
			return false;

	}

	private boolean validate(IDataSet objIDataSet)
	{

		if(objIDataSet.isPresent(0x0010,0x0020))
		{
			//if attribute is present then check for null value
			if(objIDataSet.getValue(0x0010,0x0020) == null)
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("warning","DataElementMissing",new Locale(DicomConfig.createInstance().getLocale()))+ "0010,0020");
				}
				return false;
			}
		}

		if(objIDataSet.isPresent(0x0010,0x0021))
		{
			//if attribute is present then check for null value
			if(objIDataSet.getValue(0x0010,0x0021) == null)
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("warning","DataElementMissing",new Locale(DicomConfig.createInstance().getLocale()))+ "0010,0021");
				}
				return false;
			}
		}
		return true;
	}
}