Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.alert.handler;

import in.shop2020.alert.AlertMapper;
import in.shop2020.alert.AlertService.Iface;
import in.shop2020.alert.AlertServiceException;
import in.shop2020.alert.AlertType;
import in.shop2020.alert.AlertedEntity;
import in.shop2020.alert.EntityType;
import in.shop2020.alert.MonitoredEntity;
import in.shop2020.alert.SearchFilter;
import in.shop2020.alert.handler.EntityHandler;

import java.util.List;

import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AlertServiceHandler implements Iface {

        private static Logger logger = LoggerFactory.getLogger(AlertServiceHandler.class);
        
        private EntityHandler entityHandler;
        private ApplicationContext context;
        
        
        public AlertServiceHandler() {
                logger.info("Creating context");
                context = new ClassPathXmlApplicationContext("context.xml");
                entityHandler              = context.getBean(EntityHandler.class);
        }

        @Override
        public void scheduleAlert(MonitoredEntity monitoredEntity)
                        throws AlertServiceException, TException {
                entityHandler.scheduleAlert(monitoredEntity);
                
        }

        @Override
        public void updateMonitoredObject(MonitoredEntity monitoredEntity)
                        throws AlertServiceException, TException {
                entityHandler.updateMonitoredObject(monitoredEntity);
                
        }
        @Override
        public void endMonitoringEntity(EntityType entityType, String key)
                        throws AlertServiceException, TException {
                entityHandler.endMonitoring(entityType, key);
                
        }

        @Override
        public List<MonitoredEntity> getEntities(SearchFilter searchFilter)
                        throws AlertServiceException, TException {
                return entityHandler.getEntities(searchFilter);
        }
        
        @Override
        public List<AlertedEntity> getAlertedEntities(SearchFilter searchFilter)
                        throws AlertServiceException, TException {
                return entityHandler.getAlertedEntities(searchFilter);
        }

        @Override
        public void registerEventType(AlertMapper alertMap)
                        throws AlertServiceException, TException {
                entityHandler.registerEventType(alertMap);
        }

        @Override
        public AlertMapper getAlertMapper(EntityType entityType, int eventType)
                        throws AlertServiceException, TException {
                return entityHandler.getAlertMapper(entityType, eventType);
        }
        
        @Override
        public void activateEntityMonitoring(EntityType entityType, String userIds)
                        throws AlertServiceException, TException {
                entityHandler.activateEntityMonitoring(entityType, userIds);
                
        }

        @Override
        public void deActivateEntityMonitoring(EntityType entityType)
                        throws AlertServiceException, TException {
                entityHandler.deActivateEntityMonitoring(entityType);
        }
        
        @Override
        public boolean isAlive() throws TException {
                 try {
                    return true;
                } catch (Exception e) {
                    logger.error("Alert Service not running", e);
                    return false;
                }
        }

        @Override
        public void closeSession() throws TException {
        }

        public void setDataSourceUrl(String dbHost) {
                org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
                ds.setUrl(dbHost);
        }

        public String getDataSourceUrl() {
                org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
                return ds.getUrl();
        }

}