org.graylog2.notifications.NotificationImpl Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.notifications;
import com.google.common.collect.Maps;
import org.bson.types.ObjectId;
import org.graylog2.cluster.Node;
import org.graylog2.database.DbEntity;
import org.graylog2.database.PersistedImpl;
import org.graylog2.plugin.Tools;
import org.graylog2.plugin.database.validators.Validator;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import static org.graylog2.notifications.NotificationImpl.FIELD_DETAILS;
import static org.graylog2.shared.security.RestPermissions.NOTIFICATIONS_READ;
@DbEntity(collection = "notifications",
titleField = FIELD_DETAILS + ".title",
readPermission = NOTIFICATIONS_READ)
public class NotificationImpl extends PersistedImpl implements Notification {
static final String FIELD_TYPE = "type";
static final String FIELD_KEY = "key";
static final String FIELD_SEVERITY = "severity";
static final String FIELD_TIMESTAMP = "timestamp";
static final String FIELD_NODE_ID = "node_id";
static final String FIELD_DETAILS = "details";
private Type type;
private String key;
private Severity severity;
private DateTime timestamp;
private String node_id;
protected NotificationImpl(ObjectId id, Map fields) {
super(id, fields);
this.type = Type.valueOf(((String) fields.get(FIELD_TYPE)).toUpperCase(Locale.ENGLISH));
this.severity = Severity.valueOf(((String) fields.get(FIELD_SEVERITY)).toUpperCase(Locale.ENGLISH));
this.timestamp = new DateTime(fields.get(FIELD_TIMESTAMP), DateTimeZone.UTC);
this.node_id = (String) fields.get(FIELD_NODE_ID);
this.key = (String) fields.get(FIELD_KEY);
}
protected NotificationImpl(Map fields) {
super(fields);
this.type = Type.valueOf(((String) fields.get(FIELD_TYPE)).toUpperCase(Locale.ENGLISH));
this.severity = Severity.valueOf(((String) fields.get(FIELD_SEVERITY)).toUpperCase(Locale.ENGLISH));
this.timestamp = new DateTime(fields.get(FIELD_TIMESTAMP), DateTimeZone.UTC);
this.node_id = (String) fields.get(FIELD_NODE_ID);
this.key = (String) fields.get(FIELD_KEY);
}
public NotificationImpl() {
super(new HashMap<>());
}
@Override
public Notification addType(Type type) {
this.type = type;
fields.put(FIELD_TYPE, type.toString().toLowerCase(Locale.ENGLISH));
return this;
}
@Override
public Notification addKey(String key) {
this.key = key;
fields.put(FIELD_KEY, key);
return this;
}
@Override
public Notification addTimestamp(DateTime timestamp) {
this.timestamp = timestamp;
fields.put(FIELD_TIMESTAMP, Tools.getISO8601String(timestamp));
return this;
}
@Override
public Notification addSeverity(Severity severity) {
this.severity = severity;
fields.put(FIELD_SEVERITY, severity.toString().toLowerCase(Locale.ENGLISH));
return this;
}
@Override
public Notification addNode(Node node) {
return addNode(node.getNodeId());
}
@Override
public DateTime getTimestamp() {
return timestamp;
}
@Override
public Type getType() {
return type;
}
@Override
public String getKey() {
return key;
}
@Override
public Severity getSeverity() {
return severity;
}
@Override
public String getNodeId() {
return this.node_id;
}
@Override
public Notification addDetail(String key, Object value) {
Map details;
if (fields.get(FIELD_DETAILS) == null) {
fields.put(FIELD_DETAILS, new HashMap());
}
details = (Map) fields.get(FIELD_DETAILS);
details.put(key, value);
return this;
}
@Override
public Object getDetail(String key) {
final Map details = (Map) fields.get(FIELD_DETAILS);
if (details == null) {
return null;
}
return details.get(key);
}
@Override
public Map getDetails() {
return (Map) fields.get(FIELD_DETAILS);
}
@Override
public Map asMap() {
Map result = Maps.newHashMap(fields);
result.remove("_id");
return result;
}
@Override
public Notification addNode(String nodeId) {
node_id = nodeId;
fields.put(FIELD_NODE_ID, nodeId);
return this; //To change body of created methods use File | Settings | File Templates.
}
@Override
public Map getValidations() {
return Collections.emptyMap();
}
@Override
public Map getEmbeddedValidations(String key) {
return Collections.emptyMap();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy