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

com.memority.domino.shared.api.sync.Account 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.sync;

import com.memority.citadel.shared.api.im.*;
import com.memority.toolkit.core.api.groovy.ReadWriteMapLikeGroovyObject;

import org.apache.commons.lang3.Validate;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

import static com.memority.domino.shared.api.DominoBuiltinAttributeId.Values.*;


/**
 * Application account representation suitable for Groovy scripts (rules, actions,...).
 * All application accounts exposed in Groovy scripts are represented as Accounts.
 */
public class Account implements ReadWriteMapLikeGroovyObject, HasMutableValues {

    private final Map map;

    public Account() {
        this.map = new HashMap<>();
    }
    public Account(List> attributeValues) {
        this.map = new HashMap<>();
        this.init(attributeValues);
    }
    private void init(List> attributeValues) {
        for (AttributeValue attributeValue:attributeValues) {
            if (attributeValue.isMultiValued()) {
                this.map.put(attributeValue.getId(), new ArrayList<>(attributeValue.getValues()));
            } else {
                if (attributeValue.hasValue()) {
                    this.map.put(attributeValue.getId(), attributeValue.getValue());
                } else {
                    this.map.put(attributeValue.getId(), null);
                }
            }
        }
    }

    public List> attributeValues() {
        return this.map.entrySet().stream()
                .map(e -> this.attributeValue(e.getKey(), e.getValue()))
                .collect(Collectors.toList());
    }

    public AttributeValue attributeValue(String id) {
        return attributeValue(id, this.map.get(id));
    }

    public Map> attributeValueMap() {
        return map.entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> this.attributeValue(e.getKey(), e.getValue())));
    }

    private AttributeValue attributeValue(String id, Object value) {
        return AttributeValue.from(id, value);
    }

    @Override
    public Object getProperty(String propertyName) {
        return this.map.get(propertyName);
    }

    @Override
    public void setProperty(String propertyName, Object newValue) {
        if (newValue instanceof Iterable) {
            List values = new ArrayList<>();
            ((Iterable) newValue).forEach(values::add); // will mark list as changed
            this.map.put(propertyName, values);
        } else {
            this.map.put(propertyName, newValue);
        }
    }

    /**
     * Deprecated since {@link ApiObject} does not implement {@code Map} anymore,
     * kept for backward compatibility.
     *
     * @param propertyName the attribute id
     * @return the attribute value
     */
    @Deprecated
    public Object get(String propertyName) {
        return getProperty(propertyName);
    }

    /**
     * Deprecated since {@link ApiObject} does not implement {@code Map} anymore,
     * kept for backward compatibility.
     *
     * @param propertyName the id of the attribute to set
     * @param newValue the value to set
     * @return the old attribute value
     */
    @Deprecated
    public Object put(String propertyName, Object newValue) {
        Object oldValue = getProperty(propertyName);
        setProperty(propertyName, newValue);
        return oldValue;
    }

    @Override
    public List values(String attributeId) {
        if (! this.has(attributeId)) {
            // No property by that name, initialize an empty list
            setProperty(attributeId, Collections.emptyList());
        }

        Object value = getProperty(attributeId);
        if (value instanceof List) {
            return (List) value;
        } else {
            // As per the HasValue interface, we should return a list anyway. However, this
            // should not be expected to be mutable to change the attribute value
            // so we return an unmodifiable list.
            return Collections.singletonList(value);
        }
    }

    @Override
    public Object value(String attributeId) {
        Object value = getProperty(attributeId);
        if (value == null) {
            return null;
        } else if (value instanceof List) {
            return ((List) value).isEmpty() ? null : ((List) value).get(0);
        } else {
            return value;
        }
    }

    @Override
    public boolean hasValue(String attributeId) {
        return ! values(attributeId).isEmpty();
    }

    @Override
    public boolean has(String attributeId) {
        return this.map.containsKey(attributeId);
    }

    @Override
    public Set names() {
        return this.map.keySet();
    }

    @Override
    public  void setValue(String name, T newValue) {
        setProperty(name, newValue);
    }

    @Override
    public  void setValues(String name, List newValues) {
        Validate.notNull(newValues);
        setProperty(name, newValues);
    }

    @Override
    public  boolean addValue(String name, T additionalValue, boolean distinct, boolean sorted) {
        return addValues(name, Collections.singletonList(additionalValue), distinct, sorted);
    }

    @Override
    @SuppressWarnings("unchecked")
    public  boolean addValues(String name, List additionalValues, boolean distinct, boolean sorted) {
        List values = (List) values(name);
        boolean changed = false;
        for (T additionalValue:additionalValues) {
            if (distinct && values.contains(additionalValue)) {
                continue;
            }
            changed |= values.add(additionalValue);
        }
        if (sorted) {
            values.sort((Comparator)Comparator.naturalOrder());
            changed = true;
        }
        return changed;
    }

    @Override
    public  boolean removeValue(String name, T valueToRemove) {
        return removeValues(name, Collections.singletonList(valueToRemove));
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public  boolean removeValues(String name, List valuesToRemove) {
        Object val = getProperty(name);
        if (val == null) {
            return false;
        } else if (val instanceof List) {
            return ((List) val).removeAll(valuesToRemove);
        } else {
            if (valuesToRemove.contains(val)) {
                setProperty(name, null);
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public boolean remove(String name) {
        // If no value, nothing to do
        if (!this.map.containsKey(name)) {
            return false;
        }

        // Get value and...
        Object value = this.map.get(name);

        // Value is null, won't do much with that
        if (value == null) {
            return false;
        }

        if (value instanceof List) {
            @SuppressWarnings("rawtypes")
            List list = (List) value;
            boolean changed = !list.isEmpty();
            list.clear();
            return changed;
        } else {
            this.map.put(name, null);
            return true;
        }
    }

    @Override
    public boolean delete(String name) {
        return this.map.remove(name) != null;
    }

    /*
     * Declare built-in attributes so that their type is explicit in Groovy scripts.
     */
    @SuppressWarnings("unused")
    public ShadowKind get__kind__() {
        return (ShadowKind) getProperty(KIND);
    }

    @SuppressWarnings("unused")
    public String get__name__() {
        return toString(getProperty(NAME));
    }

    @SuppressWarnings("unused")
    public String get__type__() {
        return toString(getProperty(TYPE));
    }

    @SuppressWarnings("unused")
    public String get__applicationId__() {
        return toString(getProperty(APPLICATION_ID));
    }

    @SuppressWarnings("unused")
    public String get__objectId__() {
        return toString(getProperty(OBJECT_ID));
    }

    @SuppressWarnings("unused")
    public String get__objectClass__() {
        return toString(getProperty(OBJECT_CLASS));
    }

    @SuppressWarnings("unused")
    public String get__owner__() {
        return toString(getProperty(OWNER));
    }

    @SuppressWarnings("unused")
    public SyncSituation get__syncSituation__() {
        return (SyncSituation)getProperty(SYNC_SITUATION);
    }

    @SuppressWarnings("unused")
    public Instant get__lastSyncDate__() {
        return (Instant) getProperty(LAST_SYNC_DATE);
    }

    @SuppressWarnings("unused")
    public AccountStatus get__accountStatus__() {
        return (AccountStatus) getProperty(ACCOUNT_STATUS);
    }

    private String toString(Object object) {
        return object != null ? object.toString() : null;
    }

}