@Override public PolicyEntity update(PolicyEntity policyEntity) { Preconditions.checkNotNull(policyEntity, "entity is null: " + policyEntity); Preconditions.checkNotNull(policyEntity.getUuid(), "uuid is null: " + policyEntity.getUuid()); policyProtoMap.put(policyEntity.getUuid(), policyEntity); return policyEntity; } }
@Override public PolicyEntity create(PolicyEntity entity) { Preconditions.checkNotNull(entity, "entity is null: " + entity); entity.ensureDefault(); policyProtoMap.put(entity.getUuid(), entity); return entity; }
@Override public PolicyEntity update(PolicyEntity policyProto) { Preconditions.checkNotNull(policyProto, "Entity should not be null"); Preconditions.checkNotNull(policyProto.getUuid(), "uuid should not be null"); PolicyEntity current = getByUUIDorName(policyProto.getUuid(), null); if (policyProto.getName() != null) { current.setName(policyProto.getName()); } if (policyProto.getAlertPublishmentIds() != null) { current.setAlertPublishmentIds(policyProto.getAlertPublishmentIds()); } if (policyProto.getDefinition() != null) { current.setDefinition(policyProto.getDefinition()); } current.ensureDefault(); try { if (!queryService.execute(updateSqlByUUID, current, policyEntityWriter)) { throw new IllegalArgumentException("Failed to update policyProto"); } } catch (SQLException e) { LOGGER.error("Error to execute {}: {}", updateSqlByUUID, policyProto, e); throw new IllegalArgumentException("SQL execution error: " + e.getMessage(), e); } return current; }
default PolicyEntity createOrUpdatePolicyProto(PolicyEntity policyProto) { Preconditions.checkNotNull(policyProto, "PolicyProto should not be null"); Preconditions.checkNotNull(policyProto.getDefinition(), "PolicyProto definition should not be null"); if (policyProto.getName() == null) { policyProto.setName(String.format("[%s]%s", policyProto.getDefinition().getAlertCategory(), policyProto.getDefinition().getName())); } if (policyProto.getUuid() == null) { return create(policyProto); } else { return update(policyProto); } }