Skip to content

Commit

Permalink
Merge pull request #2818 from SCADA-LTS/feature/#2815_Update_Point_Pr…
Browse files Browse the repository at this point in the history
…operties_Examples

Feature/#2815 update point properties examples
  • Loading branch information
Limraj authored Feb 15, 2024
2 parents 1c3ad18 + b53c14c commit 9e4f44d
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 68 deletions.
6 changes: 6 additions & 0 deletions WebContent/WEB-INF/spring-security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@
<intercept-url pattern="/api/datapoint/getAll" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER')" method="GET" />
<intercept-url pattern="/api/datapoint" access="@guard.hasDataPointReadPermission(request)" method="GET" />

<!-- DataPoint POST/PUT -->
<intercept-url pattern="/api/datapoint" access="hasRole('ROLE_ADMIN')" method="POST" />
<intercept-url pattern="/api/datapoint" access="hasRole('ROLE_ADMIN')" method="PUT" />
<intercept-url pattern="/api/datapoint/disabled" access="hasRole('ROLE_ADMIN')" method="PUT" />
<intercept-url pattern="/api/datapoint/enabled" access="hasRole('ROLE_ADMIN')" method="PUT" />

<!-- DataSource GET -->
<intercept-url pattern="/api/datasource" access="@guard.hasDataSourceReadPermission(request)" method="GET" />

Expand Down
4 changes: 3 additions & 1 deletion src/com/serotonin/mango/vo/DataPointVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
import static org.scada_lts.utils.XidUtils.validateXid;

@JsonRemoteEntity
public class DataPointVO implements Serializable, Cloneable, JsonSerializable, ChangeComparable<DataPointVO> {
public class DataPointVO implements Serializable, Cloneable, JsonSerializable, ChangeComparable<DataPointVO>,
ScadaValidation {
private static final long serialVersionUID = -1;
public static final String XID_PREFIX = "DP_";

Expand Down Expand Up @@ -656,6 +657,7 @@ public String toString() {
+ ", purgeStrategy=" + purgeStrategy + ", purgeValuesLimit=" + purgeValuesLimit + "]";
}

@Override
public void validate(DwrResponseI18n response) {

DataPointService dataPointService = new DataPointService();
Expand Down
7 changes: 7 additions & 0 deletions src/com/serotonin/mango/vo/ScadaValidation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.serotonin.mango.vo;

import com.serotonin.web.dwr.DwrResponseI18n;

public interface ScadaValidation {
void validate(DwrResponseI18n response);
}
4 changes: 3 additions & 1 deletion src/com/serotonin/mango/vo/dataSource/DataSourceVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.serotonin.mango.util.ChangeComparable;
import com.serotonin.mango.util.ExportCodes;
import com.serotonin.mango.util.LocalizableJsonException;
import com.serotonin.mango.vo.ScadaValidation;
import com.serotonin.mango.vo.dataSource.bacnet.BACnetIPDataSourceVO;
import com.serotonin.mango.vo.dataSource.ebro.EBI25DataSourceVO;
import com.serotonin.mango.vo.dataSource.galil.GalilDataSourceVO;
Expand Down Expand Up @@ -86,7 +87,7 @@
import static org.scada_lts.utils.XidUtils.validateXid;

abstract public class DataSourceVO<T extends DataSourceVO<?>> extends ChangeStatus implements
Serializable, Cloneable, JsonSerializable, ChangeComparable<T> {
Serializable, Cloneable, JsonSerializable, ChangeComparable<T>, ScadaValidation {
public enum Type {
EBI25(16, "dsEdit.ebi25", false) {
@Override
Expand Down Expand Up @@ -492,6 +493,7 @@ eventId, message, getAlarmLevel(eventId, defaultAlarmLevel),
duplicateHandling);
}

@Override
public void validate(DwrResponseI18n response) {

DataSourceService dataSourceService = new DataSourceService();
Expand Down
16 changes: 16 additions & 0 deletions src/org/scada_lts/utils/ApiUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scada_lts.utils;

import com.serotonin.mango.Common;
import com.serotonin.mango.vo.ScadaValidation;
import com.serotonin.mango.vo.User;
import com.serotonin.mango.vo.mailingList.EmailRecipient;
import com.serotonin.mango.vo.mailingList.UserEntry;
Expand All @@ -12,9 +13,11 @@
import org.apache.commons.logging.LogFactory;
import org.scada_lts.mango.service.SystemSettingsService;
import org.scada_lts.mango.service.UserService;
import org.scada_lts.web.mvc.api.exceptions.BadRequestException;
import org.scada_lts.web.mvc.api.json.JsonSettingsMisc;
import org.scada_lts.web.mvc.api.user.UserInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
Expand Down Expand Up @@ -145,4 +148,17 @@ public static <T, R> R convertList(List<T> list, Function<List<T>, R> converter)
public static <T, R> R convertList(T list, Function<T, R> converter) {
return converter.apply(list);
}

public static void validateObject(HttpServletRequest request, ScadaValidation toUpdate) {
DwrResponseI18n responseI18n = new DwrResponseI18n();
toUpdate.validate(responseI18n);
if(responseI18n.getHasMessages()) {
throw new BadRequestException(toMapMessages(responseI18n),
request.getRequestURI());
}
}

public static boolean idExists(Integer id) {
return id != null && id > 0;
}
}
4 changes: 2 additions & 2 deletions src/org/scada_lts/utils/UpdateValueUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import java.util.function.Consumer;
import java.util.function.Predicate;

final class UpdateValueUtils {
public final class UpdateValueUtils {

private UpdateValueUtils() {}

static <T> void setIf(T value, Consumer<T> setter, Predicate<T> setIf) {
public static <T> void setIf(T value, Consumer<T> setter, Predicate<T> setIf) {
if(setIf.test(value))
setter.accept(value);
}
Expand Down
20 changes: 20 additions & 0 deletions src/org/scada_lts/web/mvc/api/DataPointAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,25 @@ public ResponseEntity<List<DataPointIdentifier>> getDataPointIdentifiersPlcByDat
List<DataPointIdentifier> response = dataPointApiService.getDataPointIdentifiersPlcByDataSourceId(request, datasourceId);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PutMapping(value = "/api/datapoint/enabled")
public ResponseEntity<DataPointJson> enableDataPoint(@RequestParam(required = false) String xid,
@RequestParam(required = false) Integer id,
HttpServletRequest request) {
LOG.debug(request.getRequestURI());

DataPointJson response = dataPointApiService.enableDataPoint(request, xid, id, true);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PutMapping(value = "/api/datapoint/disabled")
public ResponseEntity<DataPointJson> disableDataPoint(@RequestParam(required = false) String xid,
@RequestParam(required = false) Integer id,
HttpServletRequest request) {
LOG.debug(request.getRequestURI());

DataPointJson response = dataPointApiService.enableDataPoint(request, xid, id, false);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

59 changes: 41 additions & 18 deletions src/org/scada_lts/web/mvc/api/DataPointApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.User;
import com.serotonin.mango.web.dwr.EmportDwr;
import com.serotonin.web.dwr.DwrResponseI18n;
import org.scada_lts.dao.model.DataPointIdentifier;
import org.scada_lts.mango.service.DataPointService;
import org.scada_lts.permissions.service.GetDataPointsWithAccess;
Expand All @@ -17,15 +16,15 @@

import javax.servlet.http.HttpServletRequest;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.scada_lts.permissions.service.GetDataPointsWithAccess.filteringByAccess;
import static org.scada_lts.utils.ApiUtils.toMapMessages;
import static org.scada_lts.utils.ApiUtils.idExists;
import static org.scada_lts.utils.ApiUtils.validateObject;
import static org.scada_lts.utils.DataSourcePointApiUtils.toObject;
import static org.scada_lts.utils.UpdateValueUtils.setIf;
import static org.scada_lts.utils.ValidationUtils.*;

@Service
Expand Down Expand Up @@ -82,15 +81,35 @@ public DataPointJson create(HttpServletRequest request, DataPointJson datapoint)
public DataPointJson update(HttpServletRequest request, DataPointJson datapoint) {
checkIfNonAdminThenUnauthorized(request);
checkArgsIfEmptyThenBadRequest(request, "Data Point cannot be null.", datapoint);
getDataPointFromDatabase(request, datapoint.getXid(), datapoint.getId());
DataPointVO fromRequest = toDataPointVO(request, datapoint);
dataSourceApiService.read(request, fromRequest.getDataSourceXid(), fromRequest.getDataSourceId());
DataPointVO toUpdate = getDataPointFromDatabase(request, datapoint.getXid(), datapoint.getId());
updateObjectDataPointVO(toUpdate, datapoint);
validateObject(request, toUpdate);
DataPointJson response;
try {
dataPointService.updateDataPointConfiguration(fromRequest);
dataPointService.updateDataPointConfiguration(toUpdate);
response = DataSourcePointJsonFactory.getDataPointJson(toUpdate);
} catch (Exception ex) {
throw new InternalServerErrorException(ex, request.getRequestURI());
}
return datapoint;
return response;
}

public DataPointJson enableDataPoint(HttpServletRequest request, String xid, Integer id, Boolean enabled) {
checkIfNonAdminThenUnauthorized(request);
checkArgsIfEmptyThenBadRequest(request, "Enabled is required.", enabled);
checkArgsIfTwoEmptyThenBadRequest(request, "Id or xid cannot be null.", id, xid);
DataPointVO toUpdate = getDataPointFromDatabase(request, xid, id);
DataPointJson response;
if(toUpdate.isEnabled() == enabled)
throw new BadRequestException("Data point already is " + (enabled ? "enabled" : "disabled"), request.getRequestURI());
try {
toUpdate.setEnabled(enabled);
Common.ctx.getRuntimeManager().saveDataPoint(toUpdate);
response = DataSourcePointJsonFactory.getDataPointJson(toUpdate);
} catch (Exception ex) {
throw new InternalServerErrorException(ex, request.getRequestURI());
}
return response;
}

@Override
Expand Down Expand Up @@ -158,7 +177,7 @@ public DataPointVO getDataPointFromDatabase(HttpServletRequest request, String x
User user = Common.getUser(request);

DataPointVO response;
if(id != null) {
if(idExists(id)) {
response = toObject(id, user, request, dataPointService::getDataPoint,
GetDataPointsWithAccess::hasDataPointReadPermission, a -> a);
} else {
Expand Down Expand Up @@ -243,20 +262,15 @@ private static DataPointVO toDataPointVO(HttpServletRequest request, DataPointJs
} catch (Exception ex) {
throw new InternalServerErrorException(ex, request.getRequestURI());
}
DwrResponseI18n responseI18n = new DwrResponseI18n();
dataPointVO.validate(responseI18n);
if(responseI18n.getHasMessages()) {
throw new BadRequestException(toMapMessages(responseI18n),
request.getRequestURI());
}
validateObject(request, dataPointVO);
return dataPointVO;
}

private List<DataPointVO> getDataPointsBy(HttpServletRequest request, String dataSourceXid, Integer dataSourceId) {
checkArgsIfTwoEmptyThenBadRequest(request, "Data Point id or xid cannot be null.", dataSourceId, dataSourceXid);
List<DataPointVO> dataPoints;
try {
if (dataSourceId != null) {
if (idExists(dataSourceId)) {
dataPoints = dataPointService.getDataPoints(dataSourceId,
Comparator.comparing(DataPointVO::getName));
} else {
Expand All @@ -275,4 +289,13 @@ private List<DataPointVO> getDataPointsBy(HttpServletRequest request, String dat
}
return response;
}

private static void updateObjectDataPointVO(DataPointVO toUpdate, DataPointJson fromRequest) {
setIf(fromRequest.getXid(), toUpdate::setXid, a -> !StringUtils.isEmpty(a));
setIf(fromRequest.getPointLocator(), a -> toUpdate.setPointLocator(a.parsePointLocatorData()), Objects::nonNull);
setIf(fromRequest.getDescription(), toUpdate::setDescription, a -> !StringUtils.isEmpty(a));
setIf(fromRequest.getName(), toUpdate::setName, a -> !StringUtils.isEmpty(a));
setIf(fromRequest.isEnabled(), toUpdate::setEnabled, Objects::nonNull);
setIf(fromRequest.isSettable(), toUpdate::setSettable, Objects::nonNull);
}
}
25 changes: 9 additions & 16 deletions src/org/scada_lts/web/mvc/api/DataSourceApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import com.serotonin.mango.Common;
import com.serotonin.mango.vo.User;
import com.serotonin.mango.vo.dataSource.DataSourceVO;
import com.serotonin.web.dwr.DwrResponseI18n;
import org.scada_lts.dao.model.DataSourceIdentifier;
import org.scada_lts.mango.service.DataSourceService;
import org.scada_lts.web.mvc.api.datasources.DataPointJson;
import org.scada_lts.web.mvc.api.datasources.DataSourceJson;
import org.scada_lts.web.mvc.api.datasources.DataSourcePointJsonFactory;
import org.scada_lts.web.mvc.api.exceptions.BadRequestException;
import org.scada_lts.web.mvc.api.exceptions.InternalServerErrorException;
import org.springframework.stereotype.Service;

Expand All @@ -19,7 +17,8 @@
import java.util.Map;
import java.util.stream.Collectors;

import static org.scada_lts.utils.ApiUtils.toMapMessages;
import static org.scada_lts.utils.ApiUtils.idExists;
import static org.scada_lts.utils.ApiUtils.validateObject;
import static org.scada_lts.utils.DataSourcePointApiUtils.toObject;
import static org.scada_lts.utils.ValidationUtils.*;

Expand Down Expand Up @@ -147,7 +146,7 @@ public Map<String, Object> toggleDataSource(HttpServletRequest request, String x
Map<String, Object> response = new HashMap<>();
try {
boolean state;
if(id != null) {
if(idExists(id)) {
state = dataSourceService.toggleDataSource(id);
} else {
state = dataSourceService.toggleDataSource(xid);
Expand All @@ -168,7 +167,7 @@ public List<DataPointJson> enableAllPointsInDataSource(HttpServletRequest reques

List<DataPointJson> response;
try {
if(id != null) {
if(idExists(id)) {
response = dataSourceService.enableAllDataPointsInDS(id, user)
.stream().map(DataPointJson::new)
.collect(Collectors.toList());
Expand Down Expand Up @@ -200,7 +199,7 @@ private DataSourceVO<?> getDataSourceFromDatabase(HttpServletRequest request, St
checkArgsIfTwoEmptyThenBadRequest(request, "Id or xid cannot be null.", id, xid);
User user = Common.getUser(request);
DataSourceVO<?> response;
if(id != null) {
if(idExists(id)) {
response = toObject(id, user, request, dataSourceService::getDataSource,
dataSourceService::hasDataSourceReadPermission,
a -> a);
Expand All @@ -213,19 +212,13 @@ private DataSourceVO<?> getDataSourceFromDatabase(HttpServletRequest request, St
}

private static DataSourceVO<?> toDataSourceVO(HttpServletRequest request, DataSourceJson dataSource) {
DataSourceVO<?> vo;
DataSourceVO<?> dataSourceVO;
try {
vo = dataSource.createDataSourceVO();
dataSourceVO = dataSource.createDataSourceVO();
} catch (Exception ex) {
throw new InternalServerErrorException(ex, request.getRequestURI());
}

DwrResponseI18n responseI18n = new DwrResponseI18n();
vo.validate(responseI18n);
if(responseI18n.getHasMessages()) {
throw new BadRequestException(toMapMessages(responseI18n),
request.getRequestURI());
}
return vo;
validateObject(request, dataSourceVO);
return dataSourceVO;
}
}
Loading

0 comments on commit 9e4f44d

Please sign in to comment.