Subversion Repositories SmartDukaan

Rev

Rev 5536 | Rev 5674 | Go to most recent revision | Details | Compare with Previous | 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) {
5563 amar.kumar 38
    		if(entity.getCriticalExpiryTime() <= (new Date()).getTime()) {
5519 amar.kumar 39
    			if(alert(entity, EntityProcessedState.CRITICAL_SENT)) {
40
    				entityHandler.updateEntityProcessedState(entity.getId(), 
41
    						EntityProcessedState.CRITICAL_SENT);
42
    			}
5563 amar.kumar 43
    		} else if(entity.getWarnExpiryTime() <= (new Date()).getTime()) {
5519 amar.kumar 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 {
5536 amar.kumar 64
				EntityMonitoringStatus entityMonitoringStatus = 
65
					entityHandler.getEntityMonitoringStatus(entity.getEntityType());
5563 amar.kumar 66
				sendTo = entityMonitoringStatus.getUserIds().split(";");
67
				//sendTo[0] = "amar.kumar@shop2020.in";
5519 amar.kumar 68
			}
69
			System.out.println(sendTo);
5563 amar.kumar 70
			//if(userIds!=null && userIds!="") {
5519 amar.kumar 71
 
72
				String emailFromAddress = "build@shop2020.in";
73
		        String password = "cafe@nes";
74
		        String emailSubjectTxt = null;
75
		        String emailMsgTxt = null;
76
		        System.out.println("check 1");
77
				if(state.equals(EntityProcessedState.WARNING_SENT)) {
78
					emailSubjectTxt = "Warning Alert for " + entity.getEntityType() 
79
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
80
					emailMsgTxt = "Warning Time expired for "+ entity.getEntityType() 
81
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
82
				} else if(state.equals(EntityProcessedState.CRITICAL_SENT)) {
83
					emailSubjectTxt = "Critical Alert for " + entity.getEntityType() 
84
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
85
					emailMsgTxt = "Critical Time expired for "+ entity.getEntityType() 
86
					+ " : " + entity.getEntityIdentifier() + " in "+delayedEventDescription;
87
				}
5563 amar.kumar 88
				in.shop2020.alert.util.GmailUtils utils = new in.shop2020.alert.util.GmailUtils();
5519 amar.kumar 89
		        try {
90
		        	System.out.println("Just before mail");
5563 amar.kumar 91
		        	utils.sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress, password);
5519 amar.kumar 92
				} catch (MessagingException e) {
93
					log.error("Unable to send "+state+" Alert for " + entity.getEntityType() 
94
							+ " : " + entity.getEntityIdentifier() + e);
95
					return false;
96
				}
97
				return true;
5563 amar.kumar 98
			//}
5519 amar.kumar 99
		} catch(Exception e) {
100
			log.error("Unable to send Alert for " + entity.getEntityType() 
101
								+ " : " + entity.getEntityIdentifier() + e.getMessage() + e.getStackTrace() +
102
								e.getCause() + e.getLocalizedMessage());
103
		}
104
        return false;
105
	}
106
 
107
}