Alejandro Acuña
2024-08-12 831c7bd85763b5eb5ef46664c65f0181824f9e13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package art.servers.transactionsserver.model.video;
 
import art.library.interop.InteropParameters;
import art.library.interop.InteropResponse;
import art.library.interop.serialization.SerializationException;
import art.library.model.devices.cctv.camera.information.CameraInformationPreset;
import art.library.model.devices.user.User;
import art.library.model.devices.user.UserConfiguration;
import art.library.model.devices.user.configuration.UserConfigurationVideo;
import art.servers.transactionsserver.Shared;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
 
public class ModelVideo
{
    public ModelVideo() throws Exception
    {
    }
    
    
    public InteropResponse setUserConfigurationVideo(InteropParameters parameters) throws SerializationException
    {
        AtomicBoolean success = new AtomicBoolean(true);
        try
        {
            String language = parameters.hasParameter("language") ? (String) parameters.getParameterValue("language") : Shared.getLanguage(); 
            String[] luserIdentification = parameters.getBodyContentValue(String[].class);
            UserConfigurationVideo userConfigurationVideo = parameters.getParameterValue(UserConfigurationVideo.class);
            Arrays.stream(luserIdentification).forEach(i ->
            {
                if (setUserConfigurationVideo(i, userConfigurationVideo) != true) success.set(false);
            });
            return new InteropResponse(true);
        } catch (Exception e)
        {
            throw new SerializationException("");
        }
    }
    
    private boolean setUserConfigurationVideo(String userIdentification, UserConfigurationVideo userConfigurationVideo)
    {
        try
        {
            User user = (User) Shared.getModel().getDevice(userIdentification);
            if (user == null) throw new SerializationException(Shared.getMessage("user not found"));
           
            User userclone = (User) Shared.model.cloneDevice(user);
            if (userclone.configuration == null) userclone.configuration = new UserConfiguration();
            userclone.getDeviceConfiguration().video = userConfigurationVideo;
            Shared.getModel().updateDevice(userclone);
            return true;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
    
    public InteropResponse addCameraInformationPresetOfLoggedUsers(InteropParameters parameters) throws SerializationException
    {
        AtomicBoolean success = new AtomicBoolean(true);
        try
        {
            String language = parameters.hasParameter("language") ? (String) parameters.getParameterValue("language") : Shared.getLanguage();
            CameraInformationPreset preset = parameters.getParameterValue(CameraInformationPreset.class);
            String[] luserIdentifier = parameters.getBodyContentValue(String[].class);
            Arrays.stream(luserIdentifier).forEach(i ->
            {
                if (addCameraInformationPresetOf(i, preset) != true) success.set(false);
            });
            if (!success.get()) throw new SerializationException(Shared.getMessage("error adding presets into some users"));
            return new InteropResponse(success.get());
        } catch (Exception e)
        {
            throw new SerializationException(Shared.getMessage("error adding user preset"), e);
        }
    }
 
    private boolean addCameraInformationPresetOf(String userIdentifier, CameraInformationPreset preset) 
    {
        try
        {
            User user = (User) Shared.getModel().getDevice(userIdentifier);
            if (user == null) throw new SerializationException(Shared.getMessage("user not found"));
            
            User userclone = (User) Shared.model.cloneDevice(user);
            if (userclone.configuration == null) userclone.configuration = new UserConfiguration();
            if (userclone.getDeviceConfiguration().video == null) userclone.getDeviceConfiguration().video = new UserConfigurationVideo();
            if (userclone.getDeviceConfiguration().video.lpresets == null) userclone.getDeviceConfiguration().video.lpresets = new ArrayList<>();
            userclone.getDeviceConfiguration().video.lpresets.add(preset);
            Shared.getModel().updateDevice(user, userclone);
            return true;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
    
    public InteropResponse deleteCameraInformationPresetOfLoggedUsers(InteropParameters parameters) throws SerializationException
    {
        AtomicBoolean success = new AtomicBoolean(true);
        try
        {
            String language = parameters.hasParameter("language") ? (String) parameters.getParameterValue("language") : Shared.getLanguage();
            String presetIdentifier = parameters.getParameterValue("presetIdentifier");
            String[] luserIdentifier = parameters.getBodyContentValue(String[].class);
            Arrays.stream(luserIdentifier).forEach(i ->
            {
                if (deleteCameraInformationPresetOf(i, Integer.valueOf(presetIdentifier)) != true) success.set(false);
            });
            return new InteropResponse(success.get());
        } catch (Exception e)
        {
            throw new SerializationException(Shared.getMessage("error deleting user preset"), e);
        }
    }
    
    private boolean deleteCameraInformationPresetOf(String userIdentifier, int presetIdentifier)
    {
        int initialSize = 0;
        int finalSize = 0;
        try
        {
            User user = (User) Shared.getModel().getDevice(userIdentifier);
            User userclone = (User) Shared.model.cloneDevice(user);
            if ((userclone.getDeviceConfiguration() != null)
                    && (userclone.getDeviceConfiguration().video != null)
                    && (userclone.getDeviceConfiguration().video.lpresets != null))
            {
                initialSize = userclone.getDeviceConfiguration().video.lpresets.size();
////////////////// TODO                userclone.getDeviceConfiguration().video.lpresets.removeIf(p -> p.name == presetIdentifier);
                finalSize = userclone.getDeviceConfiguration().video.lpresets.size();
            }
            
            if (initialSize > finalSize)
            {
                Shared.getModel().updateDevice(userclone);
                return true;
            } else
            {
                return false;
            }
        } catch (Exception e)
        {
        }
        return false;
    }
    
}