[SECARSP-147] *save only last device settings
[platform/core/security/suspicious-activity-monitor.git] / server / src / main / java / com / samsung / samserver / service / impl / LockServiceImpl.java
1 /*
2  * In Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 package com.samsung.samserver.service.impl;
7
8 import com.samsung.samserver.domain.*;
9 import com.samsung.samserver.service.*;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.stereotype.Service;
12 import org.springframework.transaction.annotation.Transactional;
13 import org.slf4j.*;
14
15 import java.time.Instant;
16 import java.util.Optional;
17 import static com.samsung.samserver.config.ApplicationProperties.DeviceSettings;
18
19 /**
20  * Service Implementation for managing device lock.
21  *
22  * @author <A HREF="mailto:m.dalakov@samsung.com">Mykhailo Dalakov</A>
23  * @version 1.0
24  */
25 @Service
26 @Transactional
27 public class LockServiceImpl implements LockService {
28
29     private final Logger log = LoggerFactory.getLogger(LockServiceImpl.class);
30
31     @Autowired
32     private UpdatesService updatesService;
33
34     @Autowired
35     private DeviceSettings deviceSettings;
36
37     /**
38      * Update settings on device lock.
39      *
40      * @param device the device
41      */
42     @Override
43     public void update(Device device) {
44         log.debug("managing device lock"); //NOSONAR
45         String type = "settings";
46         String out;
47         DeviceSettings.WhenLocked locked =  deviceSettings.getWhenLocked();
48         DeviceSettings.WhenUnlocked unlocked =  deviceSettings.getWhenUnlocked();
49         if (device.getLocked().equals("1")) {
50             out = "{\n" +
51                     "  \"timeout\": " + locked.getTimeout() + ",\n" +
52                     "  \"lock\": " + locked.getLock() + "\n" +
53                     "}\n";
54         } else {
55             out = "{\n" +
56                     "  \"timeout\": " + unlocked.getTimeout() + ",\n" +
57                     "  \"lock\": " + unlocked.getLock() + "\n" +
58                     "}\n";
59         }
60
61         String uuid = updatesService.getUpdateUID(type, out);
62         Optional<Updates> u = updatesService.find(device, type);
63         if (!u.isPresent()) {
64             u = Optional.of(
65                 new Updates()
66                     .type(type)
67                     .descr("action:settings updates")
68                     .device(device)
69                     .su("n")
70             );
71         }
72
73         updatesService.save(
74             u.get()
75                 .uuid(uuid)
76                 .uri( "/api/device-service/get-udata?duid="+device.getDuid()+"&uuid="+uuid)
77                 .rdate(Instant.now())
78                 .ctime(Instant.now())
79                 .data(out)
80         );
81     }
82
83 }