@POST @Path("/create/{policyId}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public RESTResponse<PolicyEntity> saveAsPolicyProto(@PathParam("policyId") String policyId) { return RESTResponse.async(() -> { Preconditions.checkNotNull(policyId, "policyId should not be null"); PolicyDefinition policyDefinition = metadataResource.getPolicyById(policyId); if (policyDefinition == null) { throw new IllegalArgumentException("policy does not exist: " + policyId); } PolicyEntity policyEntity = new PolicyEntity(); policyEntity.setDefinition(policyDefinition); List<Publishment> alertPublishments = metadataResource.getPolicyPublishments(policyId); if (alertPublishments != null && !alertPublishments.isEmpty()) { List<String> alertPublisherIds = new ArrayList<>(); for (Publishment publishment : alertPublishments) { alertPublisherIds.add(publishment.getName()); } policyEntity.setAlertPublishmentIds(alertPublisherIds); } return importPolicyProto(policyEntity); }).get(); }
@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; }
private PolicyEntity importPolicyProto(PolicyEntity policyEntity) { PolicyDefinition policyDefinition = policyEntity.getDefinition(); checkOutputStream(policyDefinition.getInputStreams(), policyDefinition.getOutputStreams()); List<String> inputStreamType = new ArrayList<>(); String newDefinition = policyDefinition.getDefinition().getValue(); for (String inputStream : policyDefinition.getInputStreams()) { String streamDef = StreamIdConversions.parseStreamTypeId(policyDefinition.getSiteId(), inputStream); inputStreamType.add(streamDef); newDefinition = newDefinition.replaceAll(inputStream, streamDef); } policyDefinition.setInputStreams(inputStreamType); policyDefinition.getDefinition().setValue(newDefinition); policyDefinition.setName(PolicyIdConversions.parsePolicyId(policyDefinition.getSiteId(), policyDefinition.getName())); policyDefinition.setSiteId(null); policyDefinition.getPartitionSpec().clear(); policyEntity.setDefinition(policyDefinition); return policyEntityService.createOrUpdatePolicyProto(policyEntity); }