use of io.gravitee.management.model.EventType in project gravitee-management-rest-api by gravitee-io.
the class ApiServiceImpl method deployLastPublishedAPI.
private ApiEntity deployLastPublishedAPI(String apiId, String userId, EventType eventType) throws TechnicalException {
Set<EventEntity> events = eventService.findByApi(apiId);
Optional<EventEntity> optEvent = events.stream().filter(event -> EventType.PUBLISH_API.equals(event.getType())).sorted((e1, e2) -> e2.getCreatedAt().compareTo(e1.getCreatedAt())).findFirst();
try {
if (optEvent.isPresent()) {
EventEntity event = optEvent.get();
JsonNode node = objectMapper.readTree(event.getPayload());
Api lastPublishedAPI = objectMapper.convertValue(node, Api.class);
lastPublishedAPI.setLifecycleState(convert(eventType));
lastPublishedAPI.setUpdatedAt(new Date());
lastPublishedAPI.setDeployedAt(new Date());
Map<String, String> properties = new HashMap<>();
properties.put(Event.EventProperties.API_ID.getValue(), lastPublishedAPI.getId());
properties.put(Event.EventProperties.USER.getValue(), userId);
// Clear useless field for history
lastPublishedAPI.setPicture(null);
// And create event
eventService.create(eventType, objectMapper.writeValueAsString(lastPublishedAPI), properties);
return convert(Collections.singleton(lastPublishedAPI), true).iterator().next();
} else {
if (events.size() == 0) {
// this is the first time we start the api without previously deployed id.
// let's do it.
this.deploy(apiId, userId, EventType.PUBLISH_API);
return deployLastPublishedAPI(apiId, userId, eventType);
}
throw new TechnicalException("No event found for API " + apiId);
}
} catch (Exception e) {
LOGGER.error("An error occurs while trying to deploy last published API {}", apiId, e);
throw new TechnicalException("An error occurs while trying to deploy last published API " + apiId, e);
}
}
Aggregations