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;
|
}
|
|
}
|