All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.pl.entity.spi.helpers.ChangeResultInspector Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity.spi.helpers;

import com.google.common.collect.Sets;
import com.kenshoo.pl.entity.ChangeEntityCommand;
import com.kenshoo.pl.entity.ChangeResult;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityChangeResult;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.FieldsValueMap;
import com.kenshoo.pl.entity.Identifier;
import com.kenshoo.pl.entity.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class ChangeResultInspector> {

    private static final Logger logger = LoggerFactory.getLogger(ChangeResultInspector.class);
    public static final String VALUE_NOT_FOUND = "not found";
    public static final ValidationError UNEXPECTED_ERROR = new ValidationError("Unexpected error");

    private final Set> inspectedFields;
    private final String inspectedFlow;

    public ChangeResultInspector(Set> inspectedFields, String inspectedFlow) {
        this.inspectedFields = inspectedFields;
        this.inspectedFlow = inspectedFlow;
    }

    public void inspect(Map, CurrentEntityState> originalState, ChangeResult results, List> observedResults) {
        Iterator> resultIterator = results.iterator();
        Iterator> observedResultIterator = observedResults.iterator();
        while(resultIterator.hasNext() && observedResultIterator.hasNext()) {
            EntityChangeResult result = resultIterator.next();
            ObservedResult observedResult = observedResultIterator.next();
            if(result.isSuccess() && observedResult.isSuccess()) {
                inspectResult(originalState.get(result.getIdentifier()), result, observedResult);
            } else if(result.isSuccess()){
                logResultWarning(observedResult.getIdentifier(), observedResult.getErrorCode().orElse(""), true, false);
                observedResult.setInspectedStatus(ObservedResult.InspectedStatus.LEGACY_ERROR_MISMATCH);
            } else if(observedResult.isSuccess()) {
                Optional firstError = result.getErrors().stream().findAny();
                logResultWarning(observedResult.getIdentifier(), firstError.orElse(UNEXPECTED_ERROR).getErrorCode(), false, true);
                observedResult.setInspectedStatus(ObservedResult.InspectedStatus.PERSISTENCE_ERROR_MISMATCH);
            }
        }
    }

    public static void logException(String inspectedFlow, Throwable t) {
        logger.warn("Change result inspector can't inspect keyword changes for flow " + inspectedFlow, t);
    }

    private void inspectResult(CurrentEntityState originalEntity, EntityChangeResult result, ObservedResult observedResult) {
        ChangeEntityCommand command = result.getCommand();
        inspectedFields.forEach(field -> {
            boolean fieldChanged = command.isFieldChanged(field);
            boolean containsObservedField = observedResult.containsField(field);
            if(fieldChanged && containsObservedField) {
                Object value = getValue(command, field);
                Object observedValue = getValue(observedResult, field);
                if(!Objects.equals(value, observedValue)) {
                    logFieldWarning(observedResult.getIdentifier(), field.toString(), value, observedValue);
                    observedResult.setInspectedStatus(ObservedResult.InspectedStatus.VALUE_MISMATCH);
                }
            } else if(fieldChanged) {
                Object value = getValue(command, field);
                logFieldWarning(observedResult.getIdentifier(), field.toString(), value, VALUE_NOT_FOUND);
                observedResult.setInspectedStatus(ObservedResult.InspectedStatus.VALUE_MISMATCH);
            } else if(containsObservedField) {
                Object originalValue = originalEntity.containsField(field) ? getValue(originalEntity, field) : null;
                Object observedValue = getValue(observedResult, field);
                if(!Objects.equals(originalValue, observedValue)) {
                    logFieldWarning(observedResult.getIdentifier(), field.toString(), VALUE_NOT_FOUND, observedValue);
                    observedResult.setInspectedStatus(ObservedResult.InspectedStatus.VALUE_MISMATCH);
                }
            }
        });
    }


    private  Object getValue(CurrentEntityState fieldsValueMap, EntityField field) {
        return field.getDbAdapter().getDbValues(fieldsValueMap.get(field)).collect(Collectors.toList()).get(0);
    }

    private  Object getValue(FieldsValueMap fieldsValueMap, EntityField field) {
        return field.getDbAdapter().getDbValues(fieldsValueMap.get(field)).collect(Collectors.toList()).get(0);
    }

    private void logFieldWarning(Identifier identifier, String fieldName, Object value, Object observedValue) {
        logger.warn("Change result inspector reports different value result for flow {} and identifier {} for field {} : legacy {}, persistent {}", inspectedFlow, identifier.toString(), fieldName, observedValue, value);
    }

    private void logResultWarning(Identifier identifier, String errorCode, boolean success, boolean inspectedSuccess) {
        logger.warn("Change result inspector reports validation mismatch for flow {} and identifier {} : legacy result [{}], persistent result [{}], error {}", inspectedFlow, identifier.toString(), inspectedSuccess, success, errorCode);
    }

    public static class Builder> {
        private Set> inspectedFields = Sets.newHashSet();
        private String inspectedFlow;


        public final Builder withInspectedFields(Collection> fields) {
            inspectedFields.addAll(fields);
            return this;
        }

        public Builder inspectedFlow(String inspectedFlow) {
            this.inspectedFlow = inspectedFlow;
            return this;
        }

        public ChangeResultInspector build() {
            return new ChangeResultInspector<>(inspectedFields, inspectedFlow);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy