private Object getPropertyValue()

in plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/IncrementPropertyAction.java [64:142]


    private Object getPropertyValue(Action action, Event event, String propertyName, Map<String, Object> properties)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        String propertyTarget = (String) action.getParameterValues().get("propertyTarget");
        String rootPropertyName = propertyName.split("\\.")[0];
        Object propertyValue = 1;

        Object propertyTargetValue = null;

        if (StringUtils.isNotEmpty(propertyTarget)) {
            propertyTargetValue = PropertyUtils.getNestedProperty(((CustomItem) event.getTarget()).getProperties(), propertyTarget);
        }

        if (propertyTargetValue != null) {
            if (propertyTargetValue instanceof Integer) {
                if (properties.containsKey(rootPropertyName)) {
                    Object nestedProperty = PropertyUtils.getNestedProperty(properties, propertyName);
                    if (nestedProperty == null) {
                        propertyValue = propertyTargetValue;
                    } else if (nestedProperty instanceof Integer) {
                        propertyValue = (int) propertyTargetValue + (int) nestedProperty;
                    } else {
                        throw new IllegalStateException("Property: " + propertyName + " already exist, can not increment the property because the exiting property is not integer");
                    }
                } else {
                    propertyValue = propertyTargetValue;
                }
            } else if (propertyTargetValue instanceof Map) {
                if (properties.containsKey(rootPropertyName)) {
                    Object nestedPropertyValue = PropertyUtils.getNestedProperty(properties, propertyName);
                    if (nestedPropertyValue == null) {
                        propertyValue = propertyTargetValue;
                    } else if (nestedPropertyValue instanceof Map) {
                        // Create a new map to avoid modifying the original Object
                        Map<String, Object> newPropertyValue = new HashMap<>();
                        Map<String, Object> nestedProperty = (Map<String, Object>) nestedPropertyValue;

                        // increment with target
                        ((Map<String, Object>) propertyTargetValue).forEach((key, targetValue) -> {
                            if ((targetValue instanceof Integer && (nestedProperty.containsKey(key) && nestedProperty.get(key) instanceof Integer)) ||
                                    (targetValue instanceof Integer && !nestedProperty.containsKey(key))) {
                                newPropertyValue.put(key, nestedProperty.containsKey(key) ? (int) nestedProperty.get(key) + (int) targetValue : targetValue);
                            }
                        });

                        // add original props that was not incremented
                        nestedProperty.forEach((key, nestedValue) -> {
                            if (!newPropertyValue.containsKey(key)) {
                                newPropertyValue.put(key, nestedValue);
                            }
                        });
                        propertyValue = newPropertyValue;
                    } else {
                        throw new IllegalStateException("Property: " + propertyName + " already exist, can not increment the properties from the map because the exiting property is not map");
                    }
                } else {
                    propertyValue = propertyTargetValue;
                }
            }
        } else {
            if (properties.containsKey(rootPropertyName)) {
                Object nestedPropertyValue = PropertyUtils.getNestedProperty(properties, propertyName);
                if (nestedPropertyValue == null) {
                    propertyValue = 1;
                } else if (nestedPropertyValue instanceof Integer) {
                    propertyValue = (int) nestedPropertyValue + 1;
                } else if (nestedPropertyValue instanceof Map) {
                    // Create a new map to avoid modifying the original object
                    Map<String, Object> newPropertyValue = new HashMap<>();
                    Map<String, Object> nestedProperty = (Map<String, Object>) nestedPropertyValue;
                    nestedProperty.forEach((key, propValue) -> newPropertyValue.put(key, propValue instanceof Integer ? (int) propValue + 1 : propValue));
                    propertyValue = newPropertyValue;
                } else {
                    throw new IllegalStateException("Property: " + propertyName + " already exist, can not increment the property because the exiting property is not integer or map");
                }
            }
        }

        return propertyValue;
    }