@Override public PolicyEntity getByUUIDorName(String uuid, String name) { Preconditions.checkArgument(uuid != null || name != null, "Both uuid and name are null"); if (uuid != null) { return policyProtoMap.get(uuid); } else { return policyProtoMap.values().stream().filter(o -> o.getName().equals(name)).findAny() .orElseThrow(() -> new IllegalArgumentException("Policy proto named " + name + " is not found")); } }
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); } }
@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; }