use of org.graylog2.plugin.database.Persisted in project graylog2-server by Graylog2.
the class PersistedImplTest method testConstructorWithFieldsOnly.
@Test
public void testConstructorWithFieldsOnly() throws Exception {
Map<String, Object> fields = Maps.newHashMap();
Persisted persisted = new PersistedImplSUT(fields);
assertNotNull(persisted);
assertNotNull(persisted.getId());
assertFalse(persisted.getId().isEmpty());
}
use of org.graylog2.plugin.database.Persisted in project graylog2-server by Graylog2.
the class PersistedImplTest method testConstructorWithFieldsAndId.
@Test
public void testConstructorWithFieldsAndId() throws Exception {
Map<String, Object> fields = Maps.newHashMap();
ObjectId id = new ObjectId();
Persisted persisted = new PersistedImplSUT(id, fields);
assertNotNull(persisted);
assertNotNull(persisted.getId());
assertFalse(persisted.getId().isEmpty());
assertEquals(id.toString(), persisted.getId());
}
use of org.graylog2.plugin.database.Persisted in project graylog2-server by Graylog2.
the class PersistedServiceImpl method save.
@Override
public <T extends Persisted> String save(T model) throws ValidationException {
Map<String, List<ValidationResult>> errors = validate(model, model.getFields());
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
BasicDBObject doc = new BasicDBObject(model.getFields());
// ID was created in constructor or taken from original doc already.
doc.put("_id", new ObjectId(model.getId()));
// Do field transformations
fieldTransformations(doc);
/*
* We are running an upsert. This means that the existing
* document will be updated if the ID already exists and
* a new document will be created if it doesn't.
*/
BasicDBObject q = new BasicDBObject("_id", new ObjectId(model.getId()));
collection(model).update(q, doc, true, false);
return model.getId();
}
use of org.graylog2.plugin.database.Persisted in project graylog2-server by Graylog2.
the class PersistedServiceImpl method embed.
protected <T extends Persisted> void embed(T model, String key, EmbeddedPersistable o) throws ValidationException {
Map<String, List<ValidationResult>> errors = validate(model.getEmbeddedValidations(key), o.getPersistedFields());
if (!errors.isEmpty()) {
throw new ValidationException(errors);
}
Map<String, Object> fields = Maps.newHashMap(o.getPersistedFields());
fieldTransformations(fields);
BasicDBObject dbo = new BasicDBObject(fields);
collection(model).update(new BasicDBObject("_id", new ObjectId(model.getId())), new BasicDBObject("$push", new BasicDBObject(key, dbo)));
}
use of org.graylog2.plugin.database.Persisted in project graylog2-server by Graylog2.
the class InputServiceImpl method getStaticFields.
@Override
public List<Map.Entry<String, String>> getStaticFields(Input input) {
if (input.getFields().get(InputImpl.EMBEDDED_STATIC_FIELDS) == null) {
return Collections.emptyList();
}
final ImmutableList.Builder<Map.Entry<String, String>> listBuilder = ImmutableList.builder();
final BasicDBList mSF = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_STATIC_FIELDS);
for (final Object element : mSF) {
final DBObject ex = (BasicDBObject) element;
try {
final Map.Entry<String, String> staticField = Maps.immutableEntry((String) ex.get(InputImpl.FIELD_STATIC_FIELD_KEY), (String) ex.get(InputImpl.FIELD_STATIC_FIELD_VALUE));
listBuilder.add(staticField);
} catch (Exception e) {
LOG.error("Cannot build static field from persisted data. Skipping.", e);
}
}
return listBuilder.build();
}
Aggregations