Skip to content

Commit

Permalink
Merge pull request #2627 from SCADA-LTS/fix/#2625_Fixed_validate_Even…
Browse files Browse the repository at this point in the history
…t_Detectors

#2625 Fixed validate Event Detectors - extends key cache to eventDete…
  • Loading branch information
Limraj authored Aug 4, 2023
2 parents fb3c76d + 8220375 commit 0086fa1
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/org/scada_lts/dao/IPointEventDetectorDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public interface IPointEventDetectorDAO {
void delete(int dataPointId, PointEventDetectorVO pointEventDetector);
void deleteWithId(String dataPointIds);
PointEventDetectorVO getPointEventDetector(int pointEventDetectorId);
PointEventDetectorVO getPointEventDetector(String pointEventDetectorXid);
PointEventDetectorVO getPointEventDetector(String pointEventDetectorXid, int dataPointId);
int getDataPointId(int pointEventDetectorId);
}
12 changes: 6 additions & 6 deletions src/org/scada_lts/dao/PointEventDetectorDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,22 +424,22 @@ public PointEventDetectorVO getPointEventDetector(int pointEventDetectorId) {
}

@Override
public PointEventDetectorVO getPointEventDetector(String pointEventDetectorXid) {
public PointEventDetectorVO getPointEventDetector(String pointEventDetectorXid, int dataPointId) {

if (LOG.isTraceEnabled()) {
LOG.trace("getPointEventDetector(String pointEventDetectorXid) pointEventDetectorXid:" +pointEventDetectorXid);
LOG.trace("getPointEventDetector(String pointEventDetectorXid, int dataPointId) pointEventDetectorXid:" +pointEventDetectorXid + ", dataPointId:" + dataPointId);
}

String templateSelectWhereIdOrderBy = POINT_EVENT_DETECTOR_SELECT + "where " + COLUMN_NAME_XID + "=? "
+ "order by " + COLUMN_NAME_ID;
String templateSelectWhereIdOrderBy = POINT_EVENT_DETECTOR_SELECT + "where " + COLUMN_NAME_XID + "=? and " + COLUMN_NAME_DATA_POINT_ID + "=?"
+ " order by " + COLUMN_NAME_ID;

try {
return DAO.getInstance().getJdbcTemp().queryForObject(templateSelectWhereIdOrderBy,
new Object[]{pointEventDetectorXid}, new PointEventDetectorRowMapper(null));
new Object[]{pointEventDetectorXid, dataPointId}, new PointEventDetectorRowMapper(null));
} catch (EmptyResultDataAccessException ex) {
return null;
} catch (IncorrectResultSizeDataAccessException ex) {
LOG.warn("There is more than one detector with xid:" + pointEventDetectorXid + ", msg: " +ex.getMessage(), ex);
LOG.warn("There is more than one detector with xid:" + pointEventDetectorXid + ", dataPointId:" + dataPointId + ", msg: " +ex.getMessage(), ex);
return new PointEventDetectorVO(Common.NEW_ID, null);
} catch (Exception ex) {
LOG.warn(ex.getMessage(), ex);
Expand Down
4 changes: 2 additions & 2 deletions src/org/scada_lts/dao/cache/PointEventDetectorCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public PointEventDetectorVO selectPointEventDetector(int pointEventDetectorId) {
}

@Override
public PointEventDetectorVO selectPointEventDetector(String pointEventDetectorXid) {
return pointEventDetectorDAO.getPointEventDetector(pointEventDetectorXid);
public PointEventDetectorVO selectPointEventDetector(String pointEventDetectorXid, int dataPointId) {
return pointEventDetectorDAO.getPointEventDetector(pointEventDetectorXid, dataPointId);
}

@Override
Expand Down
14 changes: 7 additions & 7 deletions src/org/scada_lts/dao/cache/PointEventDetectorCacheable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public interface PointEventDetectorCacheable {
PointEventDetectorVO selectPointEventDetector(int pointEventDetectorId);

@Caching(cacheable = {
@Cacheable(cacheNames = "point_event_detector_by_xid", key = "#p0", unless = "#result == null || #result.id == -1",
@Cacheable(cacheNames = "point_event_detector_by_xid_and_data_point_id", key = "#p0 + '-' + #p1", unless = "#result == null || #result.id == -1",
condition = "#p0 != null")
})
PointEventDetectorVO selectPointEventDetector(String pointEventDetectorXid);
PointEventDetectorVO selectPointEventDetector(String pointEventDetectorXid, int dataPointId);

@Caching(cacheable = {
@Cacheable(cacheNames = "data_point_id_by_point_event_detector_id", key = "#p0", unless = "#result == 0")
Expand All @@ -38,7 +38,7 @@ public interface PointEventDetectorCacheable {
@CacheEvict(cacheNames = "point_event_detector_list_by_data_point_id", key = "#p0"),
@CacheEvict(cacheNames = "point_event_detector", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "point_event_detector_by_xid", key = "#pointEventDetector.xid",
@CacheEvict(cacheNames = "point_event_detector_by_xid_and_data_point_id", key = "#pointEventDetector.xid + '-' + #p0",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "data_point_id_by_point_event_detector_id", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null")
Expand All @@ -49,7 +49,7 @@ public interface PointEventDetectorCacheable {
@CacheEvict(cacheNames = "point_event_detector_list_by_data_point_id", key = "#p0"),
@CacheEvict(cacheNames = "point_event_detector", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "point_event_detector_by_xid", key = "#pointEventDetector.xid",
@CacheEvict(cacheNames = "point_event_detector_by_xid_and_data_point_id", key = "#pointEventDetector.xid + '-' + #p0",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "data_point_id_by_point_event_detector_id", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null")
Expand All @@ -60,7 +60,7 @@ public interface PointEventDetectorCacheable {
@CacheEvict(cacheNames = "point_event_detector_list_by_data_point_id", key = "#p0"),
@CacheEvict(cacheNames = "point_event_detector", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "point_event_detector_by_xid", key = "#pointEventDetector.xid",
@CacheEvict(cacheNames = "point_event_detector_by_xid_and_data_point_id", key = "#pointEventDetector.xid + '-' + #p0",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "data_point_id_by_point_event_detector_id", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null")
Expand All @@ -71,7 +71,7 @@ public interface PointEventDetectorCacheable {
@CacheEvict(cacheNames = "point_event_detector_list_by_data_point_id", key = "#p0"),
@CacheEvict(cacheNames = "point_event_detector", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "point_event_detector_by_xid", key = "#pointEventDetector.xid",
@CacheEvict(cacheNames = "point_event_detector_by_xid_and_data_point_id", key = "#pointEventDetector.xid + '-' + #p0",
condition = "#pointEventDetector != null"),
@CacheEvict(cacheNames = "data_point_id_by_point_event_detector_id", key = "#pointEventDetector.id",
condition = "#pointEventDetector != null")
Expand All @@ -81,7 +81,7 @@ public interface PointEventDetectorCacheable {
@Caching(evict = {
@CacheEvict(cacheNames = "point_event_detector_list_by_data_point_id", allEntries = true),
@CacheEvict(cacheNames = "point_event_detector", allEntries = true),
@CacheEvict(cacheNames = "point_event_detector_by_xid", allEntries = true),
@CacheEvict(cacheNames = "point_event_detector_by_xid_and_data_point_id", allEntries = true),
@CacheEvict(cacheNames = "data_point_id_by_point_event_detector_id", allEntries = true)
})
default void deleteWithId(String dataPointIds) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public List<PointEventDetectorVO> getPointEventDetectors(DataPointVO dataPoint)

@Override
public boolean isEventDetectorXidUnique(int dataPointId, String xid, int excludeId) {
PointEventDetectorVO pointEventDetectorVO = pointEventDetectorCache.selectPointEventDetector(xid);
PointEventDetectorVO pointEventDetectorVO = pointEventDetectorCache.selectPointEventDetector(xid, dataPointId);
if(pointEventDetectorVO == null)
return true;
if(pointEventDetectorVO.getId() == Common.NEW_ID)
Expand Down Expand Up @@ -65,8 +65,8 @@ public PointEventDetectorVO getPointEventDetector(int pointEventDetectorId) {
}

@Override
public PointEventDetectorVO getPointEventDetector(String pointEventDetectorXid) {
return pointEventDetectorCache.selectPointEventDetector(pointEventDetectorXid);
public PointEventDetectorVO getPointEventDetector(String pointEventDetectorXid, int dataPointId) {
return pointEventDetectorCache.selectPointEventDetector(pointEventDetectorXid, dataPointId);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/org/scada_lts/mango/service/DataPointService.java
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public String getDetectorXid(int pointEventDetectorId) {

@Override
public int getDetectorId(String pointEventDetectorXid, int dataPointId) {
PointEventDetectorVO pointEventDetectorVO = pointEventDetectorDAO.getPointEventDetector(pointEventDetectorXid);
PointEventDetectorVO pointEventDetectorVO = pointEventDetectorDAO.getPointEventDetector(pointEventDetectorXid, dataPointId);
if(pointEventDetectorVO == null)
return 0;
return pointEventDetectorVO.getId();
Expand Down
3 changes: 2 additions & 1 deletion src/org/scada_lts/mango/service/DataSourceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,15 @@ public int copyDataSource(final int dataSourceId, final ResourceBundle bundle) {
MqttPointLocatorVO pointLocator = dataPointCopy.getPointLocator();
pointLocator.setClientId(MqttUtils.generateUniqueClientId());
}
dataPointService.saveDataPoint(dataPointCopy);

//Copy event detectors
for (PointEventDetectorVO pointEventDetector: dataPointCopy.getEventDetectors()) {
pointEventDetector.setId(Common.NEW_ID);
pointEventDetector.setXid(new DataPointService().generateEventDetectorUniqueXid(dataPointCopy.getId()));
pointEventDetector.njbSetDataPoint(dataPointCopy);
}
dataPointService.saveDataPoint(dataPointCopy);

//Copy permissions
dataPointService.copyPermissions(dataPoint.getId(), dataPointCopy.getId());
}
Expand Down
25 changes: 20 additions & 5 deletions test/org/scada_lts/dao/IsEventDetectorXidUniqueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_126",
0,
true
Expand All @@ -30,6 +31,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_126",
0,
true
Expand All @@ -38,6 +40,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_125",
0,
false
Expand All @@ -46,6 +49,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_125",
0,
false
Expand All @@ -54,6 +58,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_126",
1,
true
Expand All @@ -62,6 +67,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_126",
1,
true
Expand All @@ -70,6 +76,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_123",
1,
true
Expand All @@ -78,6 +85,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_123",
1,
true
Expand All @@ -86,6 +94,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_123",
2,
false
Expand All @@ -94,6 +103,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, "PED_124"),
new PointEventDetectorVO(3, "PED_125"),
1,
"PED_123",
2,
false
Expand All @@ -102,6 +112,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, null),
new PointEventDetectorVO(2, null),
new PointEventDetectorVO(3, null),
1,
"PED_126",
1,
true
Expand All @@ -110,6 +121,7 @@ public static Object[][] primeNumbers() {
new PointEventDetectorVO(1, "PED_123"),
new PointEventDetectorVO(2, null),
new PointEventDetectorVO(3, null),
1,
"PED_123",
2,
false
Expand All @@ -121,15 +133,18 @@ public static Object[][] primeNumbers() {
private final PointEventDetectorVO pointEventDetector1;
private final PointEventDetectorVO pointEventDetector2;
private final PointEventDetectorVO pointEventDetector3;
private final int dataPointId;
private final String xid;
private final int exclusiveId;
private final boolean expected;

public IsEventDetectorXidUniqueTest(PointEventDetectorVO pointEventDetector1, PointEventDetectorVO pointEventDetector2,
PointEventDetectorVO pointEventDetector3, String xid, int exclusiveId, boolean expected) {
PointEventDetectorVO pointEventDetector3, int dataPointId,
String xid, int exclusiveId, boolean expected) {
this.pointEventDetector1 = pointEventDetector1;
this.pointEventDetector2 = pointEventDetector2;
this.pointEventDetector3 = pointEventDetector3;
this.dataPointId = dataPointId;
this.xid = xid;
this.exclusiveId = exclusiveId;
this.expected = expected;
Expand All @@ -143,16 +158,16 @@ public void config() {
PointEventDetectorCache pointEventDetectorCache = new PointEventDetectorCache(pointEventDetectorDaoMock);
subject = new PointEventDetectorDaoWithCache(pointEventDetectorCache);

when(pointEventDetectorDaoMock.getPointEventDetector(eq(pointEventDetector1.getXid()))).thenReturn(pointEventDetector1);
when(pointEventDetectorDaoMock.getPointEventDetector(eq(pointEventDetector2.getXid()))).thenReturn(pointEventDetector2);
when(pointEventDetectorDaoMock.getPointEventDetector(eq(pointEventDetector3.getXid()))).thenReturn(pointEventDetector3);
when(pointEventDetectorDaoMock.getPointEventDetector(eq(pointEventDetector1.getXid()), eq(dataPointId))).thenReturn(pointEventDetector1);
when(pointEventDetectorDaoMock.getPointEventDetector(eq(pointEventDetector2.getXid()), eq(dataPointId))).thenReturn(pointEventDetector2);
when(pointEventDetectorDaoMock.getPointEventDetector(eq(pointEventDetector3.getXid()), eq(dataPointId))).thenReturn(pointEventDetector3);
}

@Test
public void when_isEventDetectorXidUnique() {

//when
boolean result = subject.isEventDetectorXidUnique(-1, xid, exclusiveId);
boolean result = subject.isEventDetectorXidUnique(dataPointId, xid, exclusiveId);

//then:
assertEquals(expected, result);
Expand Down
2 changes: 1 addition & 1 deletion webapp-resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,6 @@

<cache alias="point_event_detector_list_by_data_point_id" uses-template="integer_point_event_detector_list_template"/>
<cache alias="point_event_detector" uses-template="integer_point_event_detector_template"/>
<cache alias="point_event_detector_by_xid" uses-template="string_point_event_detector_template"/>
<cache alias="point_event_detector_by_xid_and_data_point_id" uses-template="string_point_event_detector_template"/>
<cache alias="data_point_id_by_point_event_detector_id" uses-template="integer_integer_cache_template"/>
</config>

0 comments on commit 0086fa1

Please sign in to comment.