Subversion Repositories SmartDukaan

Rev

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