Subversion Repositories SmartDukaan

Rev

Rev 5536 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5519 amar.kumar 1
package in.shop2020.alert.util;
2
 
3
import in.shop2020.alert.AlertType;
4
import in.shop2020.alert.EntityMonitoringStatus;
5
import in.shop2020.alert.EntityProcessedState;
6
import in.shop2020.alert.MonitoredEntity;
7
import in.shop2020.alert.AlertMapper;
8
import in.shop2020.alert.handler.EntityHandler;
9
import in.shop2020.utils.GmailUtils;
10
 
11
import java.util.Date;
12
import java.util.List;
13
 
14
import javax.mail.MessagingException;
15
 
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.springframework.context.ApplicationContext;
19
import org.springframework.context.support.ClassPathXmlApplicationContext;
20
 
21
public class AlertTask {
22
 
23
	private static Log log = LogFactory.getLog(AlertTask.class);
24
	private static EntityHandler      entityHandler;
25
 
26
    private static List<MonitoredEntity> entitiesToBeAlerted;
27
 
28
    public AlertTask() {
29
    	ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
30
        entityHandler              = context.getBean(EntityHandler.class);
31
    }
32
 
33
    public static void main(String[] args) {
34
    	AlertTask alertTask = new AlertTask();
35
    	entitiesToBeAlerted = entityHandler.getEntitiesToBeAlerted();
36
 
37
    	for(MonitoredEntity entity : entitiesToBeAlerted) {
38
    		if(entity.getCriticalExpiryTime() >= (new Date()).getTime()) {
39
    			if(alert(entity, EntityProcessedState.CRITICAL_SENT)) {
40
    				entityHandler.updateEntityProcessedState(entity.getId(), 
41
    						EntityProcessedState.CRITICAL_SENT);
42
    			}
43
    		} else if(entity.getWarnExpiryTime() >= (new Date()).getTime()) {
44
    			if(alert(entity, EntityProcessedState.WARNING_SENT)) {
45
    				entityHandler.updateEntityProcessedState(entity.getId(), 
46
    						EntityProcessedState.WARNING_SENT);
47
    			}
48
    		}
49
    	}
50
    }
51
 
52
	private static boolean alert(MonitoredEntity entity,
53
			EntityProcessedState state) {
54
		String delayedEventDescription = "UNKNOWN";
55
		String userIds = null;
56
		String[] sendTo = {};
57
		try {
58
			AlertMapper alertMap = entityHandler.getAlertMapper(entity.getEntityType(), entity.getEventType());
59
			if(alertMap!=null) {
60
				delayedEventDescription = alertMap.getEventdesciption();
61
				userIds = alertMap.getUserIds();
62
				sendTo = userIds.split(";");
63
			} else {
64
				sendTo[0] = "amar.kumar@shop2020.in";
65
			}
66
			System.out.println(sendTo);
67
			if(userIds!=null && userIds!="") {
68
 
69
				String emailFromAddress = "build@shop2020.in";
70
		        String password = "cafe@nes";
71
		        String emailSubjectTxt = null;
72
		        String emailMsgTxt = null;
73
		        System.out.println("check 1");
74
				if(state.equals(EntityProcessedState.WARNING_SENT)) {
75
					emailSubjectTxt = "Warning Alert for " + entity.getEntityType() 
76
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
77
					emailMsgTxt = "Warning Time expired for "+ entity.getEntityType() 
78
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
79
				} else if(state.equals(EntityProcessedState.CRITICAL_SENT)) {
80
					emailSubjectTxt = "Critical Alert for " + entity.getEntityType() 
81
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
82
					emailMsgTxt = "Critical Time expired for "+ entity.getEntityType() 
83
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
84
				}
85
				GmailUtils utils = new GmailUtils();
86
		        try {
87
		        	System.out.println("Just before mail");
88
					utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password, "");
89
				} catch (MessagingException e) {
90
					log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
91
							+ " : " + entity.getEntityIdentifier() + e);
92
					return false;
93
				}
94
				return true;
95
			}
96
		} catch(Exception e) {
97
			log.error("Unable to send Alert for " + entity.getEntityType() 
98
								+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
99
								e.getCause() + e.getLocalizedMessage());
100
		}
101
        return false;
102
	}
103
 
104
}