Subversion Repositories SmartDukaan

Rev

Rev 5536 | Rev 5679 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.alert.util;

import in.shop2020.alert.AlertType;
import in.shop2020.alert.EntityMonitoringStatus;
import in.shop2020.alert.EntityProcessedState;
import in.shop2020.alert.MonitoredEntity;
import in.shop2020.alert.AlertMapper;
import in.shop2020.alert.handler.EntityHandler;
import in.shop2020.utils.GmailUtils;

import java.util.Date;
import java.util.List;

import javax.mail.MessagingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AlertTask {
        
        private static Log log = LogFactory.getLog(AlertTask.class);
        private static EntityHandler      entityHandler;
        
    private static List<MonitoredEntity> entitiesToBeAlerted;
    
    public AlertTask() {
        ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
        entityHandler              = context.getBean(EntityHandler.class);
    }
    
    public static void main(String[] args) {
        AlertTask alertTask = new AlertTask();
        entitiesToBeAlerted = entityHandler.getEntitiesToBeAlerted();
        
        for(MonitoredEntity entity : entitiesToBeAlerted) {
                if(entity.getCriticalExpiryTime() <= (new Date()).getTime()) {
                        if(alert(entity, EntityProcessedState.CRITICAL_SENT)) {
                                entityHandler.updateEntityProcessedState(entity.getId(), 
                                                EntityProcessedState.CRITICAL_SENT);
                        }
                } else if(entity.getWarnExpiryTime() <= (new Date()).getTime()) {
                        if(alert(entity, EntityProcessedState.WARNING_SENT)) {
                                entityHandler.updateEntityProcessedState(entity.getId(), 
                                                EntityProcessedState.WARNING_SENT);
                        }
                }
        }
    }

        private static boolean alert(MonitoredEntity entity,
                        EntityProcessedState state) {
                String delayedEventDescription = "UNKNOWN";
                String userIds = null;
                String[] sendTo = {};
                try {
                        AlertMapper alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
                        if(alertMap!=null) {
                                delayedEventDescription = alertMap.getEventdesciption();
                                userIds = alertMap.getUserIds();
                                sendTo = userIds.split(";");
                        } else {
                                EntityMonitoringStatus entityMonitoringStatus = 
                                        entityHandler.getEntityMonitoringStatus(entity.getEntityType());
                                sendTo = entityMonitoringStatus.getUserIds().split(";");
                                //sendTo[0] = "amar.kumar@shop2020.in";
                        }
                        System.out.println(sendTo);
                        //if(userIds!=null && userIds!="") {
                                
                                String emailFromAddress = "build@shop2020.in";
                        String password = "cafe@nes";
                        String emailSubjectTxt = null;
                        String emailMsgTxt = null;
                        System.out.println("check 1");
                                if(state.equals(EntityProcessedState.WARNING_SENT)) {
                                        emailSubjectTxt = "Warning Alert for " + entity.getEntityType() 
                                        + " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
                                        emailMsgTxt = "Warning Time expired for "+ entity.getEntityType() 
                                        + " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
                                } else if(state.equals(EntityProcessedState.CRITICAL_SENT)) {
                                        emailSubjectTxt = "Critical Alert for " + entity.getEntityType() 
                                        + " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
                                        emailMsgTxt = "Critical Time expired for "+ entity.getEntityType() 
                                        + " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
                                }
                                in.shop2020.alert.util.GmailUtils utils = new in.shop2020.alert.util.GmailUtils();
                        try {
                                System.out.println("Just before mail");
                                utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password);
                                } catch (MessagingException e) {
                                        log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
                                                        + " : " + entity.getEntityIdentifier() + e);
                                        return false;
                                }
                                return true;
                        //}
                } catch(Exception e) {
                        log.error("Unable to send Alert for " + entity.getEntityType() 
                                                                + " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
                                                                e.getCause() + e.getLocalizedMessage());
                }
        return false;
        }
        
}