Search in sources :

Example 1 with Property

use of io.gravitee.definition.model.Property in project gravitee-management-rest-api by gravitee-io.

the class DynamicPropertyUpdater method update.

private void update(Collection<DynamicProperty> dynamicProperties) {
    // Get latest changes
    ApiEntity latestApi = apiService.findById(api.getId());
    List<Property> properties = (latestApi.getProperties() != null) ? latestApi.getProperties().getProperties() : Collections.emptyList();
    List<Property> userDefinedProperties = properties.stream().filter(property -> !property.isDynamic()).collect(Collectors.toList());
    Map<String, Property> propertyMap = properties.stream().collect(Collectors.toMap(Property::getKey, property -> property));
    List<Property> updatedProperties = new ArrayList<>();
    boolean needToBeSaved = false;
    for (DynamicProperty dynamicProperty : dynamicProperties) {
        Property property = propertyMap.get(dynamicProperty.getKey());
        if (property == null || property.isDynamic()) {
            updatedProperties.add(dynamicProperty);
        }
        // save properties only if there's something new
        if (property == null || (property.isDynamic() && !property.getValue().equals(dynamicProperty.getValue()))) {
            needToBeSaved = true;
        }
    }
    if (needToBeSaved) {
        // Add previous user-defined properties
        updatedProperties.addAll(userDefinedProperties);
        // Sort properties alphabetically to avoid redeploy if just the order has changed.
        List<Property> sortedUpdatedProperties = updatedProperties.stream().sorted(Comparator.comparing(Property::getKey)).collect(Collectors.toList());
        // Create properties container
        Properties apiProperties = new Properties();
        try {
            apiProperties.setProperties(sortedUpdatedProperties);
        } catch (RuntimeException e) {
            logger.error(e.getMessage(), e);
        }
        latestApi.setProperties(apiProperties);
        boolean isSync = apiService.isSynchronized(api.getId());
        // Update API
        apiService.update(latestApi.getId(), convert(latestApi));
        // Do not deploy if there are manual changes to push
        if (isSync) {
            // Publish API only in case of changes
            if (!updatedProperties.containsAll(properties) || !properties.containsAll(updatedProperties)) {
                apiService.deploy(latestApi.getId(), "dynamic-property-updater", EventType.PUBLISH_API);
            }
        }
    }
}
Also used : DynamicProperty(io.gravitee.management.services.dynamicproperties.model.DynamicProperty) java.util(java.util) Logger(org.slf4j.Logger) Properties(io.gravitee.definition.model.Properties) ApiEntity(io.gravitee.management.model.ApiEntity) ApiService(io.gravitee.management.service.ApiService) Property(io.gravitee.definition.model.Property) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) UpdateApiEntity(io.gravitee.management.model.UpdateApiEntity) Provider(io.gravitee.management.services.dynamicproperties.provider.Provider) EventType(io.gravitee.management.model.EventType) Handler(io.vertx.core.Handler) DynamicProperty(io.gravitee.management.services.dynamicproperties.model.DynamicProperty) ApiEntity(io.gravitee.management.model.ApiEntity) UpdateApiEntity(io.gravitee.management.model.UpdateApiEntity) Properties(io.gravitee.definition.model.Properties) DynamicProperty(io.gravitee.management.services.dynamicproperties.model.DynamicProperty) Property(io.gravitee.definition.model.Property)

Aggregations

Properties (io.gravitee.definition.model.Properties)1 Property (io.gravitee.definition.model.Property)1 ApiEntity (io.gravitee.management.model.ApiEntity)1 EventType (io.gravitee.management.model.EventType)1 UpdateApiEntity (io.gravitee.management.model.UpdateApiEntity)1 ApiService (io.gravitee.management.service.ApiService)1 DynamicProperty (io.gravitee.management.services.dynamicproperties.model.DynamicProperty)1 Provider (io.gravitee.management.services.dynamicproperties.provider.Provider)1 Handler (io.vertx.core.Handler)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1