/*
 * IoT - Industrial Internet Framework
 * Apache License Version 2.0, January 2004
 * Released as a part of Helsinki University
 * Software Engineering Lab in summer 2015
 */
package fi.iot.iiframework.restapi;

import fi.iot.iiframework.application.ApplicationSettings;
import fi.iot.iiframework.domain.InformationSource;
import fi.iot.iiframework.domain.Sensor;
import fi.iot.iiframework.restapi.exceptions.InvalidParametersException;
import fi.iot.iiframework.restapi.exceptions.ResourceNotFoundException;
import fi.iot.iiframework.services.domain.InformationSourceService;
import fi.iot.iiframework.services.domain.SensorService;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("1.0/sensors")
public class SensorController {

    @Autowired
    private InformationSourceService sourceService;

    @Autowired
    private SensorService sensorservice;

    @Autowired
    private RestAPIHelper helper;

    @Autowired
    private ApplicationSettings settings;

    @Secured({"ROLE_VIEWER", "ROLE_MODERATOR"})
    @RequestMapping(value = "/{sensorid}/view", produces = "application/json")
    @ResponseBody
    public Sensor getSensor(
            @PathVariable String sensorid,
            @RequestParam(required = false) Map<String, String> params
    ) throws ResourceNotFoundException {
        return (Sensor) helper.returnOrException(sensorservice.get(sensorid));
    }

    @Secured({"ROLE_VIEWER", "ROLE_MODERATOR"})
    @RequestMapping(value = "/{sourceid}/list", produces = "application/json")
    @ResponseBody
    public Set<Sensor> listSensors(
            @PathVariable String sourceid,
            @RequestParam(required = false) Map<String, String> params
    ) throws ResourceNotFoundException {
        InformationSource source = (InformationSource) helper.returnOrException(sourceService.get(sourceid));
        return source.getSensors();
    }

    @Secured({"ROLE_VIEWER", "ROLE_MODERATOR"})
    @RequestMapping(value = "/{sourceid}/list/{amount}", produces = "application/json")
    @ResponseBody
    public List<Sensor> listSensorsAmount(
            @PathVariable String sourceid,
            @PathVariable int amount,
            @RequestParam(required = false) Map<String, String> params
    ) throws ResourceNotFoundException, InvalidParametersException {
        InformationSource source
                = (InformationSource) helper.returnOrException(sourceService.get(sourceid));
        helper.exceptionIfWrongLimits(0, amount);
        return sensorservice.getBy(0, amount, source);
    }

    @Secured({"ROLE_VIEWER", "ROLE_MODERATOR"})
    @RequestMapping(value = "/{sourceid}/list/{from}/{to}", produces = "application/json")
    @ResponseBody
    public List<Sensor> listSensorsFromTo(
            @PathVariable String sourceid,
            @PathVariable int from,
            @PathVariable int to,
            @RequestParam(required = false) Map<String, String> params
    ) throws InvalidParametersException, ResourceNotFoundException {
        InformationSource source
                = (InformationSource) helper.returnOrException(sourceService.get(sourceid));
        helper.exceptionIfWrongLimits(to, from);
        return sensorservice.getBy(from, to, source);
    }
}

@@ -360,16 +360,85 @@
 Sensor;%0A
+import fi.iot.iiframework.restapi.exceptions.InvalidObjectException;%0A
 import f
@@ -494,32 +494,32 @@
 etersException;%0A
-
 import fi.iot.ii
@@ -825,24 +825,116 @@
 .Autowired;%0A
+import org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0A
 import org.s
@@ -1226,25 +1226,25 @@
 rvice sensor
-s
+S
 ervice;%0A%0A   
@@ -1294,66 +1294,8 @@
 r;%0A%0A
-    @Autowired%0A    private ApplicationSettings settings;%0A%0A
     
@@ -1678,29 +1678,682 @@
 nsor
-service.get(sensorid)
+Service.get(sensorid));%0A    %7D%0A    %0A    @Secured(%22ROLE_MODERATOR%22)%0A    @RequestMapping(%0A            value = %22/%7Bsensorid%7D/edit%22,%0A            method = RequestMethod.POST,%0A            produces = %22application/json%22,%0A            consumes = %22application/json%22%0A    )%0A    @ResponseBody%0A    public ResponseEntity%3CSensor%3E editSensorConfiguration(%0A            @PathVariable String sensorid,%0A            @RequestBody Sensor sensor%0A    ) throws InvalidParametersException, ResourceNotFoundException, InvalidObjectException %7B%0A        helper.returnOrException(sourceService.get(sensorid));%0A        sensorService.update(sensor);%0A        return new ResponseEntity%3C%3E(sensor, HttpStatus.CREATED
 );%0A 
@@ -3454,33 +3454,33 @@
    return sensor
-s
+S
 ervice.getBy(0, 
@@ -4152,17 +4152,17 @@
 n sensor
-s
+S
 ervice.g

edit sensor with RestAPI