FTPC 多客户端同时操作通一个ATrow对象处理,列表刷新,时间戳对比工具类

1.解决ATRow对象被同时操作引发的数据问题,避免重复数据写入和旧数据覆盖等问题

/**
 * AT表(对象)工具类
 *
 * @author xuweijun
 * @date 2022/5/23
 */
public class ATUtils extends ATRowUtility{

    //private constructor
    private ATUtils() {
    }
 /**
     * ATrow是否被删除
     * @author xuweijun
     * @date 2022/11/22 10:34
     * @param atRow
     * @return boolean
     */
    public static boolean isATRowDeleted(ATRow atRow){
        if (atRow.isNew() || atRow.isHistorical()) {
            return true;
        }
        ATDefinition var1 = atRow.getATDefinition();
        ATRowFilter var2 = PCContext.getFunctions().createATRowFilter(var1.getName());
        var2.forATRowKeyEqualTo(atRow.getKey());

        Vector var3;
        try {
            var3 = var2.exec(false);
        } catch (DatasweepException var6) {
            throw new MESRuntimeException("Error when refreshing ATRow", var6);
        }

        if (var3.size() != 1) {
           return true;
        } else {
            return false;
        }
    }
    /**
     * 对象是否被从数据库删除
     * @author xuweijun
     * @date 2022/11/22 10:29
     * @param mesatObject
     * @return boolean
     */
    public static boolean isATObjetDeleted(MESATObject mesatObject){
        ATRow atRow = mesatObject.getATRow();
        if (atRow.isNew() || atRow.isHistorical()) {
            return true;
        }
        return isATRowDeleted(atRow);
    }
    /**
     * atRow是否被数据库更改,只能校验一次,后续缓存被更新
     * @author xuweijun
     * @date 2022/11/22 10:21
     * @param atRow
     * @return boolean
     */
    public static boolean isATRowModified(ATRow atRow) {
        if (atRow.isNew() || atRow.isHistorical()) {
            return false;
        }
        Time lastModifiedTime = atRow.getLastModifiedTime();
        ATDefinition var1 = atRow.getATDefinition();
        ATRowFilter var2 = PCContext.getFunctions().createATRowFilter(var1.getName());
        var2.forATRowKeyEqualTo(atRow.getKey());
        Vector var3;
        try {
            var3 = var2.exec(false);
        } catch (DatasweepException var6) {
            throw new MESRuntimeException("Error when refreshing ATRow", var6);
        }
        if (var3.size() != 1) {
            return false;
        } else {
            ATRow atRowNew = (ATRow) var3.get(0);
            Time lastModifiedTimeNew = atRowNew.getLastModifiedTime();
            if (lastModifiedTimeNew == null && lastModifiedTime == null) {
                return false;
            }
            if (lastModifiedTimeNew != null && lastModifiedTime != null) {

                return !lastModifiedTimeNew.equals(lastModifiedTime);
            }
            return true;
        }
    }
    /**
     * 对象是否被数据库更改,该方法只能校验一次,查询后首次缓存被更新了
     * @author xuweijun
     * @date 2022/11/22 10:20
     * @param mesatObject
     * @return boolean
     */
    public static boolean isATObjetModified(MESATObject mesatObject){
        ATRow atRow = mesatObject.getATRow();
        if (atRow.isNew() || atRow.isHistorical()) {
            return false;
        }
        return isATRowModified(atRow);
    }

    /**
     * 客户端需要保存当前时间戳
     * @author xuweijun
     * @date 2022/11/22 15:53
     * @param mesatObject
     * @param originalTime
     * @return boolean
     */
    public static boolean isATObjetLastModified(MESATObject mesatObject,Time originalTime)
    {
        ATRow atRow = mesatObject.getATRow();
        if (atRow.isNew() || atRow.isHistorical()) {
            return false;
        }
        return isATRowLastModified(atRow,originalTime);
    }
    /**
     * 客户端需要保存当前时间戳
     * @author xuweijun
     * @date 2022/11/22 15:55
     * @param atRow
     * @param originalTime
     * @return boolean
     */
    public static boolean isATRowLastModified(ATRow atRow,Time originalTime)
    {
        if (atRow.isNew() || atRow.isHistorical()) {
            return false;
        }
        Time lastModifiedTime = originalTime;
        ATDefinition var1 = atRow.getATDefinition();
        ATRowFilter var2 = PCContext.getFunctions().createATRowFilter(var1.getName());
        var2.forATRowKeyEqualTo(atRow.getKey());
        Vector var3;
        try {
            var3 = var2.exec(false);
        } catch (DatasweepException var6) {
            throw new MESRuntimeException("Error when refreshing ATRow", var6);
        }
        if (var3.size() != 1) {
            return false;
        } else {
            ATRow atRowNew = (ATRow) var3.get(0);
            Time lastModifiedTimeNew = atRowNew.getLastModifiedTime();
            if (lastModifiedTimeNew == null && lastModifiedTime == null) {
                return false;
            }
            if (lastModifiedTimeNew != null && lastModifiedTime != null) {

                return !lastModifiedTimeNew.equals(lastModifiedTime);
            }
            return true;
        }
    }

    /**
     * 过滤掉数据库已经被删除的AT对象
     * @author xuweijun
     * @date 2022/11/22 12:49
     * @param atObjects 对象列表
     * @param isUpdate 是否对当前对象进行更新
     * @return java.util.List
     */
    public static List<? extends MESATObject> filterATObjetNotInDb(List<? extends MESATObject> atObjects,boolean isUpdate)
    {
        if(CollectionUtility.isEmpty(atObjects))
        {
            return atObjects;
        }
        ATRow atRow = atObjects.get(0).getATRow();
        ATDefinition var1 = atRow.getATDefinition();
        ATRowFilter var2 = PCContext.getFunctions().createATRowFilter(var1.getName());
        for (int i = 0; i < atObjects.size(); i++) {
            MESATObject mesatObject = atObjects.get(i);
            ATRow atRow1 = mesatObject.getATRow();
            var2.forATRowKeyEqualTo(atRow1.getKey());
        }
        Vector<ATRow> var3;
        try {
            var3 = var2.exec(false);
        } catch (DatasweepException var6) {
            throw new MESRuntimeException("Error when refreshing ATRow", var6);
        }
        Iterator<? extends MESATObject> iterator = atObjects.iterator();
        if (CollectionUtility.isEmpty(var3)) {
            atObjects.clear();
        } else {
            while (iterator.hasNext()) {
                MESATObject next = iterator.next();
                ATRow atRow1 = next.getATRow();
                ATRow atRow2 = var3.stream().filter(item -> item.getKey() == atRow1.getKey()).findAny().orElse(null);
                if (atRow2 == null) {
                    iterator.remove();
                } else {
                    if (isUpdate) {
						//这里不用强制更新com.datasweep.compatibility.manager.ATManager#updateObject会更新缓存中的引用
                        ReflectUtilsEx.setObjFieldValue(next,next.getClass(),"dgtATRow",atRow2);
                    }
                }
            }
        }
        return atObjects;
    }
 }

你可能感兴趣的:(FTPC,java,数据库)