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.handler;
2
 
3
import java.util.ArrayList;
4
import java.util.List;
5
 
6
import in.shop2020.alert.AlertMapper;
7
import in.shop2020.alert.AlertServiceException;
8
import in.shop2020.alert.AlertType;
9
import in.shop2020.alert.AlertedEntity;
10
import in.shop2020.alert.EntityProcessedState;
11
import in.shop2020.alert.EntityType;
12
import in.shop2020.alert.MonitoredEntity;
13
import in.shop2020.alert.SearchFilter;
14
import in.shop2020.alert.persistence.EntityMapper;
15
import in.shop2020.alert.util.Converter;
16
 
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19
import org.apache.thrift.TException;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Service;
22
 
23
@Service
24
public class EntityHandler {
25
 
26
	@Autowired
27
	private EntityMapper entityMapper;
28
 
29
	private static final Log log = LogFactory.getLog(EntityHandler.class);
30
 
31
	public List<MonitoredEntity> getEntities(SearchFilter searchFilter) {
32
		List<in.shop2020.alert.domain.MonitoredEntity> entities = 
33
			entityMapper.getEntities(Converter.toDomainSearchFilter
34
					(searchFilter));
35
		if(entities!=null) {
36
			List<MonitoredEntity> tEntities = new ArrayList<MonitoredEntity>();
37
			for(in.shop2020.alert.domain.MonitoredEntity entity : entities) {
38
				tEntities.add(Converter.toThriftMonitoredEntity(entity));
39
			}
40
		}
41
		return null;
42
	}
43
 
44
	public List<AlertedEntity> getAlertedEntities(SearchFilter searchFilter) {
45
		List<in.shop2020.alert.domain.AlertedEntity> alertedEntities = 
46
			entityMapper.getAlertedEntities(Converter.toDomainSearchFilter
47
					(searchFilter));
48
		if(alertedEntities!=null) {
49
			List<AlertedEntity> tAlertedEntities = new ArrayList<AlertedEntity>();
50
			for(in.shop2020.alert.domain.AlertedEntity alertedEntity : alertedEntities) {
51
				tAlertedEntities.add(Converter.toThriftAlertedEntity(alertedEntity));
52
			}
53
		}
54
		return null;
55
	}
56
 
57
	public void endMonitoring(EntityType entityType, String key) {
58
		in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(entityType, key);
59
		if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
60
			if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
61
				entityMapper.insertAlertedEntity(entity, entity.getWarnExpiryTime());
62
			}
63
			else {
64
				entityMapper.insertAlertedEntity(entity, entity.getCriticalExpiryTime());
65
			}
66
		}
67
		entityMapper.removeMonitoredEntity(entityType, key);
68
	}
69
 
70
	public void updateMonitoredObject(MonitoredEntity monitoredEntity) {
71
		in.shop2020.alert.domain.MonitoredEntity entity = entityMapper.getEntity(monitoredEntity.getEntityType(), monitoredEntity.getEntityIdentifier());
72
		if(entity.getEntityProcessedState().getValue() > EntityProcessedState.UNEXPIRED.getValue()) {
73
			if(entity.getEntityProcessedState().getValue() == EntityProcessedState.WARNING_SENT.getValue()) {
74
				entityMapper.insertAlertedEntity(entity, entity.getWarnExpiryTime());
75
			}
76
			else {
77
				entityMapper.insertAlertedEntity(entity, entity.getCriticalExpiryTime());
78
			}
79
		}
80
			monitoredEntity.setId(entity.getId());
81
			entityMapper.updateEntity(Converter.toDbEntity(monitoredEntity));
82
	}
83
 
84
	public void scheduleAlert(MonitoredEntity monitoredEntity) 
85
		throws AlertServiceException {
86
		if(isEntityMonitorable(monitoredEntity.getEntityType())) {
87
			entityMapper.insertEntity(Converter.toDbEntity(monitoredEntity));
88
		} else {
89
			log.error("EntityType : " + monitoredEntity.getEntityType() + 
90
					"is not monitorable currently");
91
			throw new AlertServiceException("EntityType : " + monitoredEntity.getEntityType() + 
92
					"is not monitorable currently", 103);
93
		}
94
 
95
	}
96
 
97
	public List<MonitoredEntity> getEntitiesToBeAlerted() {
98
		List<in.shop2020.alert.domain.MonitoredEntity> entitiesTobeAlerted = 
99
			entityMapper.getEntitiesToBeAlerted();
100
		if(entitiesTobeAlerted != null) {
101
			List<MonitoredEntity> tEntitiesTobeAlerted = new ArrayList<MonitoredEntity>();
102
			for(in.shop2020.alert.domain.MonitoredEntity entity : entitiesTobeAlerted) {
103
				tEntitiesTobeAlerted.add(Converter.toThriftMonitoredEntity(entity));
104
			}
105
			return tEntitiesTobeAlerted;
106
		}
107
		return null;
108
	}
109
 
110
	public void updateEntityProcessedState(long id, EntityProcessedState state) {
111
		entityMapper.updateEntityProcessedState(id, state);
112
	}
113
 
114
 
115
	private boolean isEntityMonitorable(EntityType entityType) {
116
		return entityMapper.isEntityMonitorable(entityType.getValue());
117
	}
118
 
119
	public void registerEventType(AlertMapper alertMap) {
120
		if(entityMapper.getAlertMapper(alertMap.getEntityType(), alertMap.getEventType())==null){
121
			entityMapper.registerEventType(Converter.toDbAlertMap(alertMap));
122
		} {
123
		//TODO update existing alertMap
124
		}
125
	}
126
 
127
	public AlertMapper getAlertMapper(EntityType entityType, int eventType) {
128
		return Converter.toThriftAlertMapper(entityMapper.getAlertMapper(entityType, eventType));
129
	}
130
 
131
	public void activateEntityMonitoring(EntityType entityType, String userIds) throws TException {
132
		if(userIds == null) {
133
			throw new TException("Default Ids for alert cannot be null while " + 
134
					"activating Entity Monitoring");
135
		}
136
		entityMapper.activateEntityMonitoring(entityType, userIds);
137
	}
138
 
139
	public void deActivateEntityMonitoring(EntityType entityType) {
140
		entityMapper.deActivateEntityMonitoring(entityType);
141
	}
142
 
143
}