package alien4cloud.rest.orchestrator;

import alien4cloud.audit.annotation.Audit;
import alien4cloud.model.orchestrators.locations.Location;
import alien4cloud.model.orchestrators.locations.LocationModifierReference;
import alien4cloud.orchestrators.locations.services.LocationModifierService;
import alien4cloud.orchestrators.locations.services.LocationService;
import alien4cloud.rest.model.RestResponse;
import alien4cloud.rest.model.RestResponseBuilder;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * Manages the location modifiers associated with the location.
 */
@Slf4j
@RestController
@RequestMapping(value = { "/rest/orchestrators/{orchestratorId}/locations/{locationId}/modifiers",
        "/rest/v1/orchestrators/{orchestratorId}/locations/{locationId}/modifiers",
        "/rest/latest/orchestrators/{orchestratorId}/locations/{locationId}/modifiers" }, produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "Location topology modifiers.", description = "Advanced feature that allows configuration of modifiers associated with this location.", authorizations = {
        @Authorization("ADMIN") })
public class LocationModifiersController {
    @Resource
    private LocationService locationService;
    @Resource
    private LocationModifierService locationModifierService;

    @ApiOperation(value = "Add a modifier to a location.", authorizations = { @Authorization("ADMIN") })
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize("hasAuthority('ADMIN')")
    @Audit
    public RestResponse<Boolean> add(@ApiParam(value = "Id of the location", required = true) @PathVariable String locationId,
                                     @ApiParam(value = "The location modifier to add", required = true) @RequestBody LocationModifierReference locationModifierReference) {
        Location location = locationService.getOrFail(locationId);
        boolean added = locationModifierService.add(location, locationModifierReference);
        return RestResponseBuilder.<Boolean> builder().data(added).build();
    }

    @ApiOperation(value = "Update the order of a modifier.", authorizations = { @Authorization("ADMIN") })
    @RequestMapping(value = "/from/{from}/to/{to}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse<Boolean> move(@ApiParam(value = "Id of the location", required = true) @PathVariable String locationId,
                                        @ApiParam(value = "From index", required = true) @PathVariable int from, @ApiParam(value = "To index", required = true) @PathVariable int to) {
        Location location = locationService.getOrFail(locationId);
        boolean moved = locationModifierService.move(location, from, to);
        return RestResponseBuilder.<Boolean> builder().data(moved).build();
    }

    @ApiOperation(value = "Delete a location modifier at the given index.", authorizations = { @Authorization("ADMIN") })
    @RequestMapping(value = "/{index}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public RestResponse<Boolean> delete(@ApiParam(value = "Id of the location", required = true) @PathVariable String locationId, @ApiParam(value = "Index of the location modifier to delete", required = true) @PathVariable int index) {
        Location location = locationService.getOrFail(locationId);
        boolean removed = locationModifierService.remove(location, index);
        return RestResponseBuilder.<Boolean> builder().data(removed).build();
    }

    @ApiOperation(value = "Get all modifiers for a given location.")
    @RequestMapping(method = RequestMethod.GET)
    public RestResponse<List<LocationModifierReference>> getAllModifiers(
            @ApiParam(value = "Id of the location for which to get all modifiers.") @PathVariable String locationId) {
        Location location = locationService.getOrFail(locationId);
        List<LocationModifierReference> modifiers = location.getModifiers();
        if (modifiers == null) {
            modifiers = Lists.newArrayList();
        }
        return RestResponseBuilder.<List<LocationModifierReference>> builder().data(modifiers).build();
    }
}
@@ -29,24 +29,542 @@
 hestrator;%0A%0A
+import java.util.List;%0A%0Aimport javax.annotation.Resource;%0A%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.security.access.prepost.PreAuthorize;%0Aimport org.springframework.web.bind.annotation.PathVariable;%0Aimport org.springframework.web.bind.annotation.RequestBody;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.RestController;%0A%0Aimport com.google.common.collect.Lists;%0A%0A
 import alien
@@ -974,48 +974,8 @@
 er;%0A
-import com.google.common.collect.Lists;%0A
 impo
@@ -1171,505 +1171,8 @@
 f4j;
-%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.security.access.prepost.PreAuthorize;%0Aimport org.springframework.web.bind.annotation.PathVariable;%0Aimport org.springframework.web.bind.annotation.RequestBody;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.RestController;%0A%0Aimport javax.annotation.Resource;%0Aimport java.util.List;%0Aimport java.util.Set;
 %0A%0A/*
@@ -2285,23 +2285,20 @@
 esponse%3C
-Boolean
+Void
 %3E add(@A
@@ -2389,33 +2389,8 @@
 Id,%0A
-                         
     
@@ -2610,24 +2610,8 @@
     
- boolean added =
  loc
@@ -2701,39 +2701,36 @@
 esponseBuilder.%3C
-Boolean
+Void
 %3E builder().data
@@ -2729,20 +2729,8 @@
 r().
-data(added).
 buil
@@ -2997,23 +2997,20 @@
 esponse%3C
-Boolean
+Void
 %3E move(@
@@ -3102,36 +3102,8 @@
 Id,%0A
-                            
     
@@ -3332,24 +3332,8 @@
     
- boolean moved =
  loc
@@ -3407,39 +3407,36 @@
 esponseBuilder.%3C
-Boolean
+Void
 %3E builder().data
@@ -3435,20 +3435,8 @@
 r().
-data(moved).
 buil
@@ -3709,23 +3709,20 @@
 esponse%3C
-Boolean
+Void
 %3E delete
@@ -3811,16 +3811,28 @@
 ationId,
+%0A           
  @ApiPar
@@ -4008,26 +4008,8 @@
     
- boolean removed =
  loc
@@ -4094,15 +4094,12 @@
 er.%3C
-Boolean
+Void
 %3E bu
@@ -4110,22 +4110,8 @@
 r().
-data(removed).
 buil

Fix controller.