/*******************************************************************************
 *  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.mwl.mwlsourceimpl;

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


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

/**
 * This class provides functionality to populate the data base and stores the data set.
 */
public class MWL_StoreAgent
{
	

	private MWLDBAgent objMWLAgent = 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 MWL_StoreAgent(String strConnectionString, String strDriverName) throws SQLException,ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		objMWLAgent = MWLDBAgent.createInstance(strConnectionString, strDriverName);
	}

	/**
	 * Stores the Dataset information into database which could be further queried using MWL CFind Request. 
	 * @param objIDataSet instance of Dataset
	 * @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 objMWLAgent.storeInstanceInfo(objIDataSet);
		else
			return false;
		
	}

	@SuppressWarnings("unchecked")
	private boolean validate(IDataSet objIDataSet)
	{
		ISingleCollection<IItem> objScheduledProcStepItemList = (ISingleCollection<IItem>)objIDataSet.getValue(0x0040,0x0100);
		if(objScheduledProcStepItemList == null || objScheduledProcStepItemList.size() != 1)
		{
			if(DicomConfig.createInstance().isLoggingOn())
			{
				DicomLogger objDicomLogger = DicomLogger.getLogger();
				objDicomLogger.log(ResourceBundleWrapper.getValue("warning","DataElementMissing",new Locale(DicomConfig.createInstance().getLocale()))+ "0040,0100");
			}
			return false;
		}
		else
		{
			IItem objScheduledProcStepItem = objScheduledProcStepItemList.elementAt(0);
			
			if(!objScheduledProcStepItem.contains(0x0040,0x0009))
			{
				if(DicomConfig.createInstance().isLoggingOn())
				{
					DicomLogger objDicomLogger = DicomLogger.getLogger();
					objDicomLogger.log(ResourceBundleWrapper.getValue("warning","DataElementMissing",new Locale(DicomConfig.createInstance().getLocale()))+ "0040,0009");
				}
				return false;
			}
		}
		return true;
	}
}