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.datasource.DataSourceContextHolder;
import com.eactive.eai.rms.common.datasource.DataSourceType;
import com.eactive.eai.rms.common.datasource.DataSourceTypeManager;
import com.eactive.eai.rms.common.util.CommonUtil;
import com.eactive.eai.rms.onl.common.util.DateUtil;

public class PortalOrgSyncUpJob  implements Job {
	
	 private static final Logger logger = Logger.getLogger(PortalOrgSyncUpJob.class);
	 
	 private PortalOrgSyncUpService portalOrgSyncUpService;
	 
	 @Autowired
	 public PortalOrgSyncUpJob(PortalOrgSyncUpService portalOrgSyncUpService) {
		this.portalOrgSyncUpService=portalOrgSyncUpService;
	 }
	 	
	 @Override
	 public void execute(JobExecutionContext context) throws JobExecutionException {
		 
		 if (logger.isInfoEnabled()) {
	            logger.info("*** START PortalOrgSyncUpJob 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 ;
		}
		
		portalOrgSyncUpService=(PortalOrgSyncUpService)appContext.getBean("portalOrgSyncUpService");	
		 
		try {
			
			logger.info("START Portal Org SyncUp SERVICE SCHECULER ...");
			
			DataSourceType dataSourceType = DataSourceTypeManager.getDataSourceType(DataSourceTypeManager.MONITORING);
			DataSourceContextHolder.setDataSourceType(dataSourceType);
			
			portalOrgSyncUpService.deletePtlOrgInfoRcv(dataSourceType.getSchema());
			portalOrgSyncUpService.insertPtlOrgRcv(dataSourceType.getSchema());
			portalOrgSyncUpService.updatePtlOrgIntfRcv(dataSourceType.getSchema());		
						
		} catch (Exception e) {
			logger.error("PortalOrgSyncUpJob ERROR ", e);
		}
		 
 	    if (logger.isInfoEnabled()) {
	            logger.info("*** END PortalOrgSyncUpJob run("
	                    + DateUtil.getDateTime("yyyy-MM-dd HH:mm") + ")");
	    }
		 
	 }

}
