[SECARSP-115] *update devices func: return UIDevice list, validate input values
[platform/core/security/suspicious-activity-monitor.git] / server / src / main / java / com / samsung / samserver / web / rest / service / ui / UpdateDevicesRestService.java
index 7d71931..0b9de9a 100644 (file)
@@ -5,20 +5,18 @@
  */
 package com.samsung.samserver.web.rest.service.ui;
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.samsung.samserver.config.Constants;
 import com.samsung.samserver.domain.Device;
 import com.samsung.samserver.service.DeviceService;
+import com.samsung.samserver.service.impl.GeoIPService;
 import org.springframework.stereotype.Service;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.*;
 import org.slf4j.*;
-import java.util.List;
-import lombok.*;
-
 import javax.validation.Valid;
 import javax.validation.constraints.*;
+import java.util.*;
+import lombok.*;
 
 /**
  * REST service implementation for get updates.
@@ -34,23 +32,57 @@ public class UpdateDevicesRestService {
     @Autowired
     private DeviceService deviceService;
 
-    public ResponseEntity<Void> updateDevice(@Valid @RequestBody List<UpdateDevicesRestService.UIDeviceUpdate> uiDeviceUpdate) {
+    @Autowired
+    private GeoIPService geoIPService;
+
+    public ResponseEntity<List<GetDevicesRestService.UIDevice>> updateDevice(@Valid @RequestBody UpdateDevicesRestService.UIDeviceUpdate uiDeviceUpdate) {
         log.debug("UI request to update devices : {}", uiDeviceUpdate);
-        for (UIDeviceUpdate update: uiDeviceUpdate) {
-            Device d =  deviceService.findOne(update.getId());
-            if (d!=null) {
-                d.setLocked(update.getLocked().toString());
-                deviceService.save(d);
+        int c = 0;
+        ArrayList<GetDevicesRestService.UIDevice> uiDevices = new ArrayList<>();
+        List<UIDeviceUpdateOne> updates = uiDeviceUpdate.getUpdates();
+        for (UIDeviceUpdateOne update: updates) {
+            Device device =  deviceService.findOne(update.getId());
+            if (device!=null) {
+                device.setLocked(update.getLocked().toString());
+                deviceService.save(device);
+                GetDevicesRestService.UIDevice uiDevice = new GetDevicesRestService.UIDevice();
+                uiDevice.setId(device.getId());
+                uiDevice.setDuid(device.getDuid());
+                uiDevice.setCtime(device.getCtime());
+                uiDevice.setModel(device.getModel());
+                uiDevice.setIpaddr(device.getIpaddr());
+                uiDevice.setSn(device.getSn());
+                uiDevice.setDescr(device.getDescr());
+                uiDevice.setLocked(device.getLocked()!=null && !device.getLocked().equals("0") ? 1:0);
+                uiDevice.setUiDeviceOS(new GetDevicesRestService.UIDeviceOS(device.getSw(), device.getOsname(), device.getOsver()));
+                uiDevice.setUiDeviceType(new GetDevicesRestService.UIDeviceType(device.getDtype().getName(), device.getDtype().getDescr()));
+                uiDevice.setGeoIP(geoIPService.getGeo(device.getIpaddr()));
+                uiDevices.add(uiDevice);
+                c++;
             }
         }
-        return ResponseEntity.ok().headers(new HttpHeaders()).build();
+        final HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+        HttpStatus hs = c == updates.size() ? HttpStatus.OK : (c!=0 ? HttpStatus.PARTIAL_CONTENT : HttpStatus.NO_CONTENT);
+        return new ResponseEntity<>(uiDevices , httpHeaders, hs);
     }
 
+    /**
+     * View Model for UI Device update.
+     */
     @Getter @Setter @ToString
     public static class UIDeviceUpdate {
+        @Valid
+        @NotNull
+        private List<UIDeviceUpdateOne> updates;
+    }
+
+    @Getter @Setter @ToString
+    static class UIDeviceUpdateOne {
+        @NotNull
         private Long id;
-        @Pattern(regexp = Constants.DEVICE_LOCKED_REGEX)
-        @Size(min = 1, max = 1)
+        @NotNull
+        @Max(1) @Min(0)
         private Integer locked;
     }