Search in sources :

Example 1 with OnLocationResourceChangeEvent

use of alien4cloud.orchestrators.locations.events.OnLocationResourceChangeEvent in project alien4cloud by alien4cloud.

the class ManagedServiceResourceEventService method updateLocations.

/**
 * add if not yet present the given locations ids into the serviceResource
 *
 * @param serviceResource
 * @param locationsToAdd
 */
private void updateLocations(ServiceResource serviceResource, String[] locationsToAdd) {
    if (serviceResource.getLocationIds() == null) {
        serviceResource.setLocationIds(locationsToAdd);
        for (String locationId : serviceResource.getLocationIds()) {
            publisher.publishEvent(new OnLocationResourceChangeEvent(this, locationId));
        }
    } else {
        Set<String> locations = Sets.newHashSet(serviceResource.getLocationIds());
        locations.addAll(Sets.newHashSet(locationsToAdd));
        serviceResource.setLocationIds(locations.toArray(new String[locations.size()]));
    }
    for (String locationId : locationsToAdd) {
        publisher.publishEvent(new OnLocationResourceChangeEvent(this, locationId));
    }
}
Also used : OnLocationResourceChangeEvent(alien4cloud.orchestrators.locations.events.OnLocationResourceChangeEvent)

Example 2 with OnLocationResourceChangeEvent

use of alien4cloud.orchestrators.locations.events.OnLocationResourceChangeEvent in project alien4cloud by alien4cloud.

the class ServiceResourceService method updateLocations.

private void updateLocations(ServiceResource serviceResource, String[] locations) {
    if (locations == null) {
        return;
    }
    // Check what elements have changed.
    Set<String> removedLocations = CollectionUtils.safeNewHashSet(serviceResource.getLocationIds());
    Set<String> addedLocations = Sets.newHashSet();
    Set<String> newLocations = Sets.newHashSet();
    for (String locationId : locations) {
        if (removedLocations.contains(locationId)) {
            // This location was already affected
            removedLocations.remove(locationId);
            newLocations.add(locationId);
        } else {
            // This is an added element.
            if (!alienDAO.exist(Location.class, locationId)) {
                throw new NotFoundException("Location with id <" + locationId + "> does not exist.");
            }
            addedLocations.add(locationId);
            newLocations.add(locationId);
        }
    }
    serviceResource.setLocationIds(newLocations.toArray(new String[newLocations.size()]));
    // Dispatch location changed events (not a big deal if the save is actually canceled as it just changed the update date).
    for (String locationId : addedLocations) {
        publisher.publishEvent(new OnLocationResourceChangeEvent(this, locationId));
    }
    for (String locationId : removedLocations) {
        publisher.publishEvent(new OnLocationResourceChangeEvent(this, locationId));
    }
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) OnLocationResourceChangeEvent(alien4cloud.orchestrators.locations.events.OnLocationResourceChangeEvent) Location(alien4cloud.model.orchestrators.locations.Location)

Aggregations

OnLocationResourceChangeEvent (alien4cloud.orchestrators.locations.events.OnLocationResourceChangeEvent)2 NotFoundException (alien4cloud.exception.NotFoundException)1 Location (alien4cloud.model.orchestrators.locations.Location)1