Skip to content

Commit

Permalink
Merge pull request #2800 from SCADA-LTS/fix/#2798_Corrected_History_A…
Browse files Browse the repository at this point in the history
…nnotation_Change_Point_Value_By_User

#2798 Corrected History Annotation Change Point Value By User
  • Loading branch information
Limraj authored Feb 29, 2024
2 parents 9ca6d33 + 4cbfaca commit e86b3b2
Show file tree
Hide file tree
Showing 79 changed files with 1,408 additions and 1,018 deletions.
5 changes: 0 additions & 5 deletions src/com/serotonin/mango/rt/RuntimeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,6 @@ public DataPointListener getDataPointListeners(int dataPointId) {
// Point values
public void setDataPointValue(int dataPointId, MangoValue value,
SetPointSource source) {
if(source instanceof User){
setDataPointValue(dataPointId,
new PointValueTime(value, System.currentTimeMillis(),((User)source).getUsername()), source);
}
else
setDataPointValue(dataPointId,
new PointValueTime(value, System.currentTimeMillis()), source);
}
Expand Down
64 changes: 22 additions & 42 deletions src/com/serotonin/mango/rt/dataImage/AnnotatedPointValueTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
*/
package com.serotonin.mango.rt.dataImage;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.ResourceBundle;

import com.serotonin.mango.Common;
import com.serotonin.mango.rt.dataImage.types.MangoValue;
import com.serotonin.web.i18n.I18NUtils;
import com.serotonin.mango.util.AnnotatedPointValueUtils;
import com.serotonin.web.taglib.DateFunctions;

/**
* This class provides a way of arbitrarily annotating a PointValue. Point value annotations should not be confused with
Expand Down Expand Up @@ -56,24 +58,11 @@ public class AnnotatedPointValueTime extends PointValueTime {
* @see SetPointSource
*/
private String sourceDescriptionArgument;
public AnnotatedPointValueTime(String valueChanger,MangoValue value, long time, int sourceType, int sourceId) {
super(value, time);
this.sourceType = sourceType;
this.sourceId = sourceId;
this.sourceDescriptionArgument = valueChanger;
}

public AnnotatedPointValueTime(MangoValue value, long time, int sourceType, int sourceId) {
super(value, time);
this.sourceType = sourceType;
this.sourceId = sourceId;
this.sourceDescriptionArgument = getWhoChangedValue();
}
public AnnotatedPointValueTime(MangoValue value, long time, int sourceType, int sourceId,String sourceDescription) {
super(value, time);
this.sourceType = sourceType;
this.sourceId = sourceId;
this.sourceDescriptionArgument = sourceDescription;
this.setWhoChangedValue(sourceDescription);
}

@Override
Expand All @@ -90,24 +79,10 @@ public int getSourceType() {
}

public String getSourceDescriptionKey() {
switch (sourceType) {
case SetPointSource.Types.ANONYMOUS:
return "annotation.anonymous";
case SetPointSource.Types.EVENT_HANDLER:
return "annotation.eventHandler";
case SetPointSource.Types.USER:
return "annotation.user";
case SetPointSource.Types.POINT_LINK:
return "annotation.pointLink";
default:
return "annotation.unknown";
}
return AnnotatedPointValueUtils.getSourceDescriptionKey(sourceType);
}

public String getSourceDescriptionArgument() {
String EMPTY_STRING = "";
if(sourceDescriptionArgument.equals( EMPTY_STRING ) )
setSourceDescriptionArgument(getWhoChangedValue());
return sourceDescriptionArgument;
}

Expand All @@ -116,20 +91,25 @@ public void setSourceDescriptionArgument(String sourceDescriptionArgument) {
}

public String getAnnotation(ResourceBundle bundle) {
String pattern = I18NUtils.getMessage(bundle, getSourceDescriptionKey());
if (sourceDescriptionArgument == null)
return MessageFormat.format(pattern, I18NUtils.getMessage(bundle, "common.deleted"));
return MessageFormat.format(pattern, sourceDescriptionArgument);
return AnnotatedPointValueUtils.getAnnotation(bundle, sourceType, sourceDescriptionArgument);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AnnotatedPointValueTime)) return false;
if (!super.equals(o)) return false;
AnnotatedPointValueTime that = (AnnotatedPointValueTime) o;
return getSourceType() == that.getSourceType() && getSourceId() == that.getSourceId() && Objects.equals(getSourceDescriptionArgument(), that.getSourceDescriptionArgument());
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getSourceType(), getSourceId(), getSourceDescriptionArgument());
}

@Override
public String toString() {
return new StringBuilder()
.append("AnnotatedPointValueTime{")
.append(" sourceType= ").append(getSourceType())
.append(" sourceId= ").append(getSourceId())
.append(" whoChangedValue= ").append(getWhoChangedValue())
.append(" sourceDescriptionArgument= ").append(getSourceDescriptionArgument())
.append("}").toString();
return "AnnotatedPointValueTime(" + (sourceDescriptionArgument == null ? getAnnotation(Common.getBundle()) : sourceDescriptionArgument) + " -- " + getValue() + "@" + DateFunctions.getTime(getTime()) + ")";
}
}
7 changes: 0 additions & 7 deletions src/com/serotonin/mango/rt/dataImage/DataPointNonSyncRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ public class DataPointNonSyncRT extends DataPointRT implements IDataPointRT {
public DataPointNonSyncRT(DataPointVO vo, PointLocatorRT pointLocator) {
super(vo, pointLocator);
}
public DataPointNonSyncRT(DataPointVO vo, PointLocatorRT pointLocator, int cacheSize, int maxSize) {
super(vo, pointLocator, cacheSize, maxSize);
}

public DataPointNonSyncRT(DataPointVO vo) {
super(vo);
}

@Override
protected void savePointValue(PointValueTime newValue, SetPointSource source,
Expand Down
23 changes: 4 additions & 19 deletions src/com/serotonin/mango/rt/dataImage/DataPointRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import static org.scada_lts.utils.PointValueStateUtils.isSetPointHandler;
import static org.scada_lts.utils.PointValueStateUtils.isSetPoint;

public class DataPointRT implements IDataPoint, ILifecycle, TimeoutClient, ScadaWebSockets<MangoValue> {
private static final Log LOG = LogFactory.getLog(DataPointRT.class);
Expand Down Expand Up @@ -89,22 +89,7 @@ public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator) {
pointValueService = new PointValueService();
dataPointServiceWebSocket = ApplicationBeans.getDataPointServiceWebSocketBean();
}
public DataPointRT(DataPointVO vo, PointLocatorRT pointLocator,int cacheSize,int maxSize) {
this.vo = vo;
this.pointLocator = pointLocator;
valueCache = new PointValueCache(cacheSize);
valueCache.setMaxSize(maxSize);
pointValueService = new PointValueService();
dataPointServiceWebSocket = ApplicationBeans.getDataPointServiceWebSocketBean();
}

public DataPointRT(DataPointVO vo) {
this.vo = vo;
this.pointLocator = null;
valueCache = new PointValueCache();
pointValueService = new PointValueService();
dataPointServiceWebSocket = ApplicationBeans.getDataPointServiceWebSocketBean();
}
public PointValueCache getPointValueCache(){
return this.valueCache;
}
Expand Down Expand Up @@ -252,10 +237,10 @@ protected void savePointValue(PointValueTime newValue, SetPointSource source,
return;
}

boolean isSetPointHandler = isSetPointHandler(source);
boolean isSetPoint = isSetPoint(source);
boolean backdated = pointValue != null
&& newValue.getTime() < pointValue.getTime()
&& !isSetPointHandler;
&& !isSetPoint;

// Determine whether the new value qualifies for logging.
boolean logValue;
Expand Down Expand Up @@ -318,7 +303,7 @@ else if (backdated)


// Ignore historical values.
if (pointValue == null || newValue.getTime() >= pointValue.getTime() || isSetPointHandler) {
if (pointValue == null || newValue.getTime() >= pointValue.getTime() || isSetPoint) {
PointValueTime oldValue = pointValue;
pointValue = newValue;
fireEvents(oldValue, newValue, source != null, false);
Expand Down
16 changes: 4 additions & 12 deletions src/com/serotonin/mango/rt/dataImage/DataPointSynchronizedRT.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ public DataPointSynchronizedRT(DataPointVO vo, PointLocatorRT pointLocator) {
this.pointValueState = PointValueState.empty();
}

public DataPointSynchronizedRT(DataPointVO vo, PointLocatorRT pointLocator, int cacheSize, int maxSize) {
super(vo, pointLocator, cacheSize, maxSize);
this.pointValueState = PointValueState.empty();
}

public DataPointSynchronizedRT(DataPointVO vo) {
super(vo);
this.pointValueState = PointValueState.empty();
}

@Override
protected void savePointValue(PointValueTime newValue, SetPointSource source,
boolean async) {
Expand Down Expand Up @@ -198,8 +188,10 @@ public String toString() {
private Optional<PointValueState> createAndUpdateState(PointValueTime newValue, DataPointVO vo, SetPointSource source) {
lock.writeLock().lock();
try {
pointValueState = PointValueState.newState(newValue, pointValueState, vo, source);
return Optional.of(pointValueState);
PointValueState state = PointValueState.newState(newValue, pointValueState, vo, source);
if(!state.isBackdated())
pointValueState = state;
return Optional.of(state);
} catch(Exception ex) {
LOG.error(ex.getMessage(), ex);
return Optional.empty();
Expand Down
70 changes: 18 additions & 52 deletions src/com/serotonin/mango/rt/dataImage/PointValueCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import java.util.ArrayList;
import java.util.List;

import com.serotonin.mango.db.dao.PointValueDao;
import com.serotonin.mango.rt.link.PointLinkRT;
import com.serotonin.mango.vo.User;
import org.scada_lts.mango.service.PointValueService;

/**
* This class maintains an ordered list of the most recent values for a data point. It will mirror values in the
Expand All @@ -37,7 +36,7 @@
public class PointValueCache {
private final int dataPointId;
private final int defaultSize;
private final PointValueDao dao;
private final PointValueService service;
private int maxSize = 0;

/**
Expand All @@ -46,71 +45,38 @@ public class PointValueCache {
* time, always use a local copy of the variable for read purposes.
*/
private List<PointValueTime> cache = new ArrayList<PointValueTime>();
public PointValueCache(){
this.dataPointId = -1;
this.defaultSize = -1;
this.dao=null;
}

public PointValueCache(int defaultSize) {
this.defaultSize = defaultSize;
this.dataPointId = -1;
this.dao=null;
}

public PointValueCache(int dataPointId, int defaultSize) {
this.dataPointId = dataPointId;
this.defaultSize = defaultSize;
dao = new PointValueDao();
service = new PointValueService();

if (defaultSize > 0)
refreshCache(defaultSize);
}

/*
those method will proprably used to get information - annotations - from database but.....
*/
/*public String getValuesFromCacheForHistoryTable__(String givenpointValue){
final String EMPTY_STRING="";
for(PointValueTime pointValueTime:cache){
if(((MangoValue)pointValueTime.getValue()).getStringValue().equals(givenpointValue) ) return pointValueTime.getWhoChangedValue();
}
return EMPTY_STRING;
}*/
/*public boolean getValuesFromCacheForHistoryTable(String time__,String givenPointValue){
for(PointValueTime pointValueTime:cache){
if( ((MangoValue)pointValueTime.getValue()).getStringValue().equals(givenPointValue) ) return true;
}
return false;
}*/

public void savePointValueIntoCache(PointValueTime pvt, SetPointSource source, boolean logValue) {
setChangeOwnerIfSourceIsNotEmpty(pvt,source);
}
public void savePointValueIntoDaoAndCacheUpdate(PointValueTime pvt, SetPointSource source, boolean logValue, boolean async) {
if (logValue) {
if (async)
dao.savePointValueAsync(dataPointId, pvt, source);
service.savePointValueAsync(dataPointId, pvt, source);
else
pvt = dao.savePointValueSync(dataPointId, pvt, source);

pvt = service.savePointValueSync(dataPointId, pvt, source);
}
setChangeOwnerIfSourceIsNotEmpty(pvt,source);

insertPointValueTimeIntoCache(pvt);
}
private void setChangeOwnerIfSourceIsNotEmpty(PointValueTime pvt, SetPointSource source) {

if(source!=null)
{
if(source instanceof User)
{

pvt.setWhoChangedValue(((User)source).getUsername());
if((!logValue || async) && !pvt.isAnnotated() && source != null) {
AnnotatedPointValueTime annotatedPointValueTime = new AnnotatedPointValueTime(pvt.getValue(), pvt.getTime(),
source.getSetPointSourceType(), source.getSetPointSourceId());
if(source instanceof User) {
User user = (User) source;
annotatedPointValueTime.setSourceDescriptionArgument(user.getUsername());
}
insertPointValueTimeIntoCache(annotatedPointValueTime);
return;
}

insertPointValueTimeIntoCache(pvt);
}

private void insertPointValueTimeIntoCache(PointValueTime pvt){
List<PointValueTime> c = cache;
List<PointValueTime> newCache = new ArrayList<PointValueTime>(c.size() + 1);
Expand Down Expand Up @@ -141,7 +107,7 @@ private void insertPointValueTimeIntoCache(PointValueTime pvt){
*/
void logPointValueAsync(PointValueTime pointValue, SetPointSource source) {
// Save the new value and get a point value time back that has the id and annotations set, as appropriate.
dao.savePointValueAsync(dataPointId, pointValue, source);
service.savePointValueAsync(dataPointId, pointValue, source);
}

public PointValueTime getLatestPointValue() {
Expand Down Expand Up @@ -178,15 +144,15 @@ private void refreshCache(int size) {
maxSize = size;
if (size == 1) {
// Performance thingy
PointValueTime pvt = dao.getLatestPointValue(dataPointId);
PointValueTime pvt = service.getLatestPointValue(dataPointId);
if (pvt != null) {
List<PointValueTime> c = new ArrayList<PointValueTime>();
c.add(pvt);
cache = c;
}
}
else
cache = dao.getLatestPointValues(dataPointId, size);
cache = service.getLatestPointValues(dataPointId, size);
}
}

Expand Down
Loading

0 comments on commit e86b3b2

Please sign in to comment.