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

com.memority.domino.shared.api.AttributeValueConverterUtils Maven / Gradle / Ivy

Go to download

This artifact provides the API classes that are necessary to implement synchronization configuration Rules on the Memority IM platform.

There is a newer version: 3.43.1
Show newest version
/*
 * Copyright (c) 2016-2023 Memority. All Rights Reserved.
 *
 * This file is part of Memority Domino API , a Memority project.
 *
 * This file is released under the Memority Public Artifacts End-User License Agreement,
 * see 
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 */
package com.memority.domino.shared.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.memority.toolkit.core.api.domain.ValueObject;
import com.memority.toolkit.core.api.util.KitObjectMapperFactory;
import com.memority.citadel.shared.api.im.AttributeValue;

import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author Pierre Sion <[email protected]>
 * Date: 2019/09/12
 */
public class AttributeValueConverterUtils {

    private static final ObjectMapper objectMapper = KitObjectMapperFactory.createStandardObjectMapper();

    public static AttributeValue convertFromJavaToApi(AttributeValue javaAttributeValue) {
        if (javaAttributeValue.getValues().isEmpty()) {
            return javaAttributeValue;
        }

        List values = javaAttributeValue.getValues().stream()
                .map(javaValue -> {
                    if (javaValue instanceof ValueObject) {
                        return javaValue.toString();
                    } else {
                        return javaValue;
                    }
                })
                .collect(toList());

        return AttributeValue.from(javaAttributeValue.getId(), values, javaAttributeValue.getMultiValuedState());
    }

    public static List> convertFromJavaToApi(List> attributeValues) {
        return attributeValues.stream()
                .map(AttributeValueConverterUtils::convertFromJavaToApi)
                .collect(toList());
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static AttributeDelta convertFromJavaToApi(AttributeDelta attributeDelta) {
        return new AttributeDelta<>(attributeDelta.getOperation(),
                                    attributeDelta.getId(),
                                    convertFromJavaToApi((AttributeValue)attributeDelta).getValues(),
                                    attributeDelta.getMultiValuedState())
                .setValuesToRemove((List)attributeDelta.valuesToRemove())
                .setValuesToAdd(attributeDelta.valuesToAdd());
    }

    // [SYNC-681] 2 byte array objects cannot be compared using "equals"
    public static AttributeValue convertFromJavaToComparable(AttributeValue javaAttributeValue) {
        if (javaAttributeValue.getValues().isEmpty()) {
            return javaAttributeValue;
        }

        List values = javaAttributeValue.getValues().stream()
                .map(javaValue -> {
                    JsonNode jsonNode;
                    if (javaValue instanceof ValueObject) {
                        // Otherwise the id object is serialized as { "value": "id", "kind": ... }
                        jsonNode = objectMapper.valueToTree(javaValue.toString());
                    } else {
                        jsonNode = objectMapper.valueToTree(javaValue);
                    }
                    if (jsonNode.isNull()) {
                        return null;
                    } else if (jsonNode.isValueNode()) {
                        return jsonNode.asText();
                    } else {
                        try {
                            return objectMapper.writeValueAsString(jsonNode);
                        } catch (JsonProcessingException e) {
                            throw new RuntimeException(e); // Should not happen
                        }
                    }
                })
                .collect(toList());

        return AttributeValue.from(javaAttributeValue.getId(), values, javaAttributeValue.getMultiValuedState());
    }

    public static List> convertFromJavaToComparable(List> attributeValues) {
        return attributeValues.stream()
                .map(AttributeValueConverterUtils::convertFromJavaToComparable)
                .collect(toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy