[SECARSP-147] *save only last device settings
authorm.dalakov <m.dalakov@samsung.com>
Fri, 9 Mar 2018 07:10:04 +0000 (09:10 +0200)
committerDmytro Lomtiev <d.lomtev@samsung.com>
Fri, 16 Mar 2018 14:18:57 +0000 (16:18 +0200)
Change-Id: Iad6f063fabb5f83b379d553204882e3c0d359bd6

server/src/main/java/com/samsung/samserver/repository/UpdatesRepository.java
server/src/main/java/com/samsung/samserver/service/UpdatesService.java
server/src/main/java/com/samsung/samserver/service/impl/LockServiceImpl.java
server/src/main/java/com/samsung/samserver/service/impl/UpdatesServiceImpl.java

index 815774d..b988fd0 100644 (file)
@@ -26,4 +26,6 @@ public interface UpdatesRepository extends JpaRepository<Updates, Long> {
 
     Optional<Updates> findOneBySuAndUuidAndDevice(String su, String uuid, Device device);
 
+    Optional<Updates> findOneBySuAndDeviceAndType(String su, Device device, String type);
+
 }
index fb40b55..0b7b048 100644 (file)
@@ -79,4 +79,13 @@ public interface UpdatesService {
      */
     void confirm(String uuid, Device device);
 
+    /**
+     * Get updates data by update type
+     *
+     * @param device the Device entity
+     * @param type the update type
+     * @return the update data entity
+     */
+    Optional<Updates> find(Device device, String type);
+
 }
index 483e661..b4cec01 100644 (file)
@@ -11,6 +11,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.slf4j.*;
+
+import java.time.Instant;
 import java.util.Optional;
 import static com.samsung.samserver.config.ApplicationProperties.DeviceSettings;
 
@@ -55,12 +57,27 @@ public class LockServiceImpl implements LockService {
                     "  \"lock\": " + unlocked.getLock() + "\n" +
                     "}\n";
         }
+
         String uuid = updatesService.getUpdateUID(type, out);
-        Optional<Updates> u = updatesService.find(uuid, device);
+        Optional<Updates> u = updatesService.find(device, type);
         if (!u.isPresent()) {
-            String uri = "/api/device-service/get-udata?duid="+device.getDuid()+"&uuid="+uuid;
-            updatesService.save(new Updates(uuid, type, uri, null, "action:settings updates", out, device));
+            u = Optional.of(
+                new Updates()
+                    .type(type)
+                    .descr("action:settings updates")
+                    .device(device)
+                    .su("n")
+            );
         }
+
+        updatesService.save(
+            u.get()
+                .uuid(uuid)
+                .uri( "/api/device-service/get-udata?duid="+device.getDuid()+"&uuid="+uuid)
+                .rdate(Instant.now())
+                .ctime(Instant.now())
+                .data(out)
+        );
     }
 
 }
index 3e17389..b1f1de8 100644 (file)
@@ -102,6 +102,7 @@ public class UpdatesServiceImpl implements UpdatesService {
      * @param data update data
      * @return the String
      */
+    @Override
     public String getUpdateUID(String type, String data){
         return DigestUtils.sha256Hex(type + data);
     }
@@ -138,4 +139,18 @@ public class UpdatesServiceImpl implements UpdatesService {
         }
     }
 
+    /**
+     * Get updates data by update type
+     *
+     * @param device the Device entity
+     * @param type the update type
+     * @return the update data entity
+     */
+    @Override
+    @Transactional
+    public Optional<Updates> find(Device device, String type){
+        log.debug("Request to get updates Data by update type: {}, {}", device, type);
+        return updatesRepository.findOneBySuAndDeviceAndType("n", device, type);
+    }
+
 }