package com.eactive.eai.rms.onl.common.service.syncup;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.onl.common.util.DateUtil;

import com.eactive.eai.rms.common.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;

public class PortalApiSpecSyncUpJob  implements Job {
	
	 private static final Logger logger = Logger.getLogger(PortalApiSpecSyncUpJob.class);
	 
	 private PortalApiSpecSyncUpService portalApiSpecSyncUpService;
		
	 @Autowired
	 public PortalApiSpecSyncUpJob(PortalApiSpecSyncUpService portalApiSpecSyncUpService) {
		this.portalApiSpecSyncUpService=portalApiSpecSyncUpService;
	 }
	 
	 @Override
	 public void execute(JobExecutionContext context) throws JobExecutionException {
		 
		 if (logger.isInfoEnabled()) {
	            logger.info("*** START PortalApiSpecSyncUpJob run("
	                    + CommonUtil.getToday("yyyy-MM-dd HH:mm") + ")");
	     }
		 
		 ApplicationContext appContext = null;
		 
		 try {
			appContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
		 } catch (SchedulerException e) {
		 	logger.error("applicationContext get module error",e);
		 	return ;
		 }
		 
		 portalApiSpecSyncUpService=(PortalApiSpecSyncUpService)appContext.getBean("portalApiSpecSyncUpService");
		 
		// Portal API SPEC SyncUp		
		try {
			logger.info("START Portal API SPEC SyncUp SERVICE SCHECULER ...");
				
			DataSourceType dataSourceType = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.MONITORING);
			DataSourceContextHolder.setDataSourceType(dataSourceType);
							
			portalApiSpecSyncUpService.deletePtlApiSpecInfoRcv(dataSourceType.getSchema());
			portalApiSpecSyncUpService.insertPtlApiSpecInfoRcv(dataSourceType.getSchema());
			portalApiSpecSyncUpService.updatePtlApiSpecInfoIntfRcv(dataSourceType.getSchema());
				
		} catch (Exception e) {
			logger.error("Portal API SPEC SyncUp SERVICE ERROR",e);
		}		 		 
		 
     	if (logger.isInfoEnabled()) {
	       logger.info("*** END PortalApiSpecSyncUpJob run("
	                    + DateUtil.getDateTime("yyyy-MM-dd HH:mm") + ")");
	    }		 
	 }
}
