public ListGridRecord copyValues(Alert from) {
return convert(from);
}
public static ListGridRecord convert(Alert from) {
ListGridRecord record = new ListGridRecord();
record.setAttribute("id", from.getId());
record.setAttribute("ctime", new Date(from.getCtime()));
if (from.getAcknowledgeTime() != null && from.getAcknowledgeTime().longValue() > 0) {
record.setAttribute("acknowledgeTime", new Date(from.getAcknowledgeTime().longValue()));
}
record.setAttribute("acknowledgingSubject", from.getAcknowledgingSubject());
record.setAttribute("recovered", from.getRecoveryTime());
if(from.getRecoveryTime() != null && from.getRecoveryTime().longValue() > 0) {
record.setAttribute("recoveredTime", new Date(from.getRecoveryTime().longValue()));
}
AlertDefinition alertDefinition = from.getAlertDefinition();
record.setAttribute("definitionId", alertDefinition.getId());
Resource resource = alertDefinition.getResource();
record.setAttribute("name", alertDefinition.getName());
record.setAttribute("description", alertDefinition.getDescription());
record.setAttribute("priority", ImageManager.getAlertIcon(alertDefinition.getPriority()));
// for ancestry handling
record.setAttribute(AncestryUtil.RESOURCE_ID, resource.getId());
record.setAttribute(AncestryUtil.RESOURCE_NAME, resource.getName());
record.setAttribute(AncestryUtil.RESOURCE_ANCESTRY, resource.getAncestry());
record.setAttribute(AncestryUtil.RESOURCE_TYPE_ID, resource.getResourceType().getId());
AlertDefinition groupAlertDefinition = alertDefinition.getGroupAlertDefinition();
Integer parentId = alertDefinition.getParentId();
if (groupAlertDefinition != null && groupAlertDefinition.getGroup() != null) {
boolean isAutogroup = groupAlertDefinition.getGroup().getAutoGroupParentResource() != null;
record.setAttribute(FIELD_PARENT, (isAutogroup ? "#Resource/AutoGroup/" : "#ResourceGroup/")
+ groupAlertDefinition.getGroup().getId() + "/Alerts/Definitions/" + groupAlertDefinition.getId());
record.setLinkText(MSG.view_alert_definition_for_group());
} else if (parentId != null && parentId.intValue() != 0) {
record.setAttribute(
FIELD_PARENT,
LinkManager.getAdminTemplatesEditLink(AlertDefinitionTemplateTypeView.VIEW_ID.getName(), resource
.getResourceType().getId())
+ "/" + parentId);
record.setLinkText(MSG.view_alert_definition_for_type());
}
Set<AlertConditionLog> conditionLogs = from.getConditionLogs();
String conditionText;
String conditionValue;
if (conditionLogs.size() > 1) {
conditionText = MSG.view_alerts_field_condition_text_many();
conditionValue = "--";
} else if (conditionLogs.size() == 1) {
AlertConditionLog conditionLog = conditionLogs.iterator().next();
AlertCondition condition = conditionLog.getCondition();
conditionText = AlertFormatUtility.formatAlertConditionForDisplay(condition);
conditionValue = conditionLog.getValue();
if (condition.getMeasurementDefinition() != null) {
try {
conditionValue = MeasurementConverterClient.format(Double.valueOf(conditionLog.getValue()),
condition.getMeasurementDefinition().getUnits(), true);
} catch (Exception e) {
// the condition log value was probably not a number (most likely a trait). Ignore this exception.
// even if any other errors occur trying to format the value, ignore this and just use the raw value string
}
}
} else {
conditionText = MSG.view_alerts_field_condition_text_none();
conditionValue = "--";
}
record.setAttribute("conditionText", conditionText);
if (conditionValue.contains("extraInfo=")) {
conditionValue = conditionValue.replaceFirst("extraInfo=\\[","");
conditionValue = conditionValue.substring(0,conditionValue.length()-1);
}
record.setAttribute("conditionValue", conditionValue);
// We also need the'raw' notification data to show in details
DataClass[] conditions = new DataClass[from.getConditionLogs().size()];
int i = 0;
for (AlertConditionLog log : from.getConditionLogs()) {
AlertCondition condition = log.getCondition();
DataClass dc = new DataClass();
dc.setAttribute("text", AlertFormatUtility.formatAlertConditionForDisplay(condition));
String value = log.getValue();
if (condition.getMeasurementDefinition() != null) {
try {
value = MeasurementConverterClient.format(Double.valueOf(log.getValue()), condition
.getMeasurementDefinition().getUnits(), true);
} catch (Exception e) {
// the condition log value was probably not a number (most likely a trait). Ignore this exception.
// even if any other errors occur trying to format the value, ignore this and just use the raw value string
}
}
// Remove the extraInfo=[ ] that is added when storing the raw event data in the data base
if (value.contains("extraInfo=")) {
value = value.replaceFirst("extraInfo=\\[","");
value = value.substring(0,value.length()-1);
}
dc.setAttribute("value", value);
conditions[i++] = dc;
}
record.setAttribute("conditionLogs", conditions);
record.setAttribute("conditionExpression", alertDefinition.getConditionExpression());
String recoveryInfo = AlertFormatUtility.getAlertRecoveryInfo(from);
record.setAttribute("recoveryInfo", recoveryInfo);
// Alert notification logs
DataClass[] notifications = new DataClass[from.getAlertNotificationLogs().size()];
i = 0;
for (AlertNotificationLog log : from.getAlertNotificationLogs()) {
DataClass dc = new DataClass();
dc.setAttribute("sender", log.getSender());
dc.setAttribute("status", log.getResultState().name());
dc.setAttribute("message", log.getMessage());
notifications[i++] = dc;
}
record.setAttribute("notificationLogs", notifications);
return record;
}