[SECARSP-115] *add update devices func (lock)
authorm.dalakov <m.dalakov@samsung.com>
Wed, 28 Feb 2018 13:57:16 +0000 (15:57 +0200)
committerDmytro Lomtiev <d.lomtev@samsung.com>
Fri, 16 Mar 2018 14:16:49 +0000 (16:16 +0200)
Change-Id: I8754c4584e7b650b1c22a521b432a021a21a6dd1

server/src/main/java/com/samsung/samserver/config/Constants.java
server/src/main/java/com/samsung/samserver/domain/Device.java
server/src/main/java/com/samsung/samserver/web/rest/controller/IRestDashboard.java
server/src/main/java/com/samsung/samserver/web/rest/controller/impl/RestDashboard.java
server/src/main/java/com/samsung/samserver/web/rest/service/ui/GetDevicesRestService.java [moved from server/src/main/java/com/samsung/samserver/web/rest/service/GetDevicesUIRestService.java with 93% similarity]
server/src/main/java/com/samsung/samserver/web/rest/service/ui/UpdateDevicesRestService.java [new file with mode: 0644]

index fab45ad..84478e5 100644 (file)
@@ -12,6 +12,7 @@ public final class Constants {
 
     // Regex for acceptable logins
     public static final String LOGIN_REGEX = "^[_'.@A-Za-z0-9-]*$";
+    public static final String DEVICE_LOCKED_REGEX = "[0-1]";
 
     public static final String SYSTEM_ACCOUNT = "system";
     public static final String ANONYMOUS_USER = "anonymoususer";
index e978a23..686aa77 100644 (file)
@@ -7,9 +7,13 @@ package com.samsung.samserver.domain;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import javax.persistence.*;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
 import java.io.Serializable;
 import java.time.Instant;
 import java.util.*;
+
+import com.samsung.samserver.config.Constants;
 import lombok.*;
 
 /**
@@ -55,6 +59,8 @@ public class Device implements Serializable {
     @Column(name = "descr")
     private String descr;
 
+    //@Pattern(regexp = Constants.DEVICE_LOCKED_REGEX)
+    //@Size(min = 1, max = 1)
     @Column(name = "jhi_locked")
     private String locked;
 
index 3d779b5..0c9c3de 100644 (file)
@@ -7,6 +7,8 @@ package com.samsung.samserver.web.rest.controller;
 
 import com.samsung.samserver.web.rest.service.*;
 import com.samsung.samserver.web.rest.errors.UserServiceError;
+import com.samsung.samserver.web.rest.service.ui.GetDevicesRestService;
+import com.samsung.samserver.web.rest.service.ui.UpdateDevicesRestService;
 import org.springframework.data.domain.Pageable;
 import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
@@ -36,7 +38,7 @@ public interface IRestDashboard {
     })
     @RequestMapping(value ="/devices", method = RequestMethod.GET)
     @ResponseStatus(HttpStatus.OK)
-    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices(Pageable pageable);
+    public ResponseEntity<List<GetDevicesRestService.UIDevice>> getAllDevices(Pageable pageable);
 
 
     /**
@@ -92,4 +94,20 @@ public interface IRestDashboard {
     public ResponseEntity<Void> logout();
 
 
+    /**
+     * PUT  /devices/update : Updates an existing devices.
+     *
+     * @param uiDeviceUpdate list the device info to update
+     * @return the ResponseEntity with status 200 (OK)
+     * or with status 400 (Bad Request) if the uiDeviceUpdate is not valid
+     */
+    @ApiOperation(value = "devices/update", notes = "The “devices update” operation is used to update devices.")
+    @ApiResponses(value = {
+            @ApiResponse(code = 200, message = "OK. Devices was updated successfully."),
+            @ApiResponse(code = 400, message = "Bad Request. The reason is contained in the tags “errorKey”, “message”, “title”"),
+            @ApiResponse(code = 500, message = "Internal server error. The user has to repeat action.")
+    })
+    @RequestMapping(value ="/devices/update", method = RequestMethod.PUT)
+    public ResponseEntity<Void> updateDevice(@Valid @RequestBody List<UpdateDevicesRestService.UIDeviceUpdate> uiDeviceUpdate);
+
 }
index 8b02663..2ba6a4a 100644 (file)
@@ -8,6 +8,8 @@ package com.samsung.samserver.web.rest.controller.impl;
 import com.samsung.samserver.service.DeviceService;
 import com.samsung.samserver.web.rest.controller.IRestDashboard;
 import com.samsung.samserver.web.rest.service.*;
+import com.samsung.samserver.web.rest.service.ui.GetDevicesRestService;
+import com.samsung.samserver.web.rest.service.ui.UpdateDevicesRestService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Pageable;
 import org.springframework.http.*;
@@ -34,7 +36,10 @@ public class RestDashboard  implements IRestDashboard {
     private AccountService accountService;
 
     @Autowired
-    private GetDevicesUIRestService getDevicesUIRestService;
+    private GetDevicesRestService getDevicesRestService;
+
+    @Autowired
+    private UpdateDevicesRestService updateDevicesRestService;
 
     /**
      * GET  /devices : get all the devices.
@@ -42,8 +47,8 @@ public class RestDashboard  implements IRestDashboard {
      * @return the ResponseEntity with status 200 (OK) and the list of devices in body
      */
     @Override
-    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices(Pageable pageable) {
-        return getDevicesUIRestService.getAllDevices(pageable);
+    public ResponseEntity<List<GetDevicesRestService.UIDevice>> getAllDevices(Pageable pageable) {
+        return getDevicesRestService.getAllDevices(pageable);
     }
 
     /**
@@ -76,4 +81,16 @@ public class RestDashboard  implements IRestDashboard {
         return ResponseEntity.ok().headers(new HttpHeaders()).build();
     }
 
+    /**
+     * PUT  /devices/update : Updates an existing devices.
+     *
+     * @param uiDeviceUpdate list the device info to update
+     * @return the ResponseEntity with status 200 (OK)
+     * or with status 400 (Bad Request) if the uiDeviceUpdate is not valid
+     */
+    @Override
+    public ResponseEntity<Void> updateDevice(@Valid @RequestBody List<UpdateDevicesRestService.UIDeviceUpdate> uiDeviceUpdate) {
+        return updateDevicesRestService.updateDevice(uiDeviceUpdate);
+    }
+
 }
@@ -3,7 +3,7 @@
  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  */
-package com.samsung.samserver.web.rest.service;
+package com.samsung.samserver.web.rest.service.ui;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.samsung.samserver.domain.Device;
@@ -28,9 +28,9 @@ import static com.samsung.samserver.service.impl.GeoIPService.GeoIP;
  * @version 1.0
  */
 @Service
-public class GetDevicesUIRestService {
+public class GetDevicesRestService {
 
-    private final Logger log = LoggerFactory.getLogger(GetDevicesUIRestService.class);
+    private final Logger log = LoggerFactory.getLogger(GetDevicesRestService.class);
 
     @Autowired
     private DeviceService deviceService;
@@ -38,10 +38,10 @@ public class GetDevicesUIRestService {
     @Autowired
     private GeoIPService geoIPService;
 
-    public ResponseEntity<List<GetDevicesUIRestService.UIDevice>> getAllDevices(Pageable pageable) {
+    public ResponseEntity<List<GetDevicesRestService.UIDevice>> getAllDevices(Pageable pageable) {
         log.debug("UI request to get all devices"); //NOSONAR
         Page<Device> devices = deviceService.findAll(pageable);
-        List<GetDevicesUIRestService.UIDevice> uiDevices = new ArrayList<>();
+        List<GetDevicesRestService.UIDevice> uiDevices = new ArrayList<>();
         for (Device device: devices) {
             UIDevice uiDevice = new UIDevice();
             uiDevice.setId(device.getId());
diff --git a/server/src/main/java/com/samsung/samserver/web/rest/service/ui/UpdateDevicesRestService.java b/server/src/main/java/com/samsung/samserver/web/rest/service/ui/UpdateDevicesRestService.java
new file mode 100644 (file)
index 0000000..7d71931
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * In Samsung Ukraine R&D Center (SRK under a contract between)
+ * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ */
+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 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.*;
+
+/**
+ * REST service implementation for get updates.
+ *
+ * @author <A HREF="mailto:m.dalakov@samsung.com">Mykhailo Dalakov</A>
+ * @version 1.0
+ */
+@Service
+public class UpdateDevicesRestService {
+
+    private final Logger log = LoggerFactory.getLogger(UpdateDevicesRestService.class);
+
+    @Autowired
+    private DeviceService deviceService;
+
+    public ResponseEntity<Void> updateDevice(@Valid @RequestBody List<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);
+            }
+        }
+        return ResponseEntity.ok().headers(new HttpHeaders()).build();
+    }
+
+    @Getter @Setter @ToString
+    public static class UIDeviceUpdate {
+        private Long id;
+        @Pattern(regexp = Constants.DEVICE_LOCKED_REGEX)
+        @Size(min = 1, max = 1)
+        private Integer locked;
+    }
+
+}
\ No newline at end of file