Search in sources :

Example 1 with ProxyTransform

use of cz.o2.proxima.transform.ProxyTransform in project proxima-platform by O2-Czech-Republic.

the class ConfigRepository method addProxyAttributeAsymmetric.

@SuppressWarnings("unchecked")
private void addProxyAttributeAsymmetric(String attrName, Map<String, Object> proxyMap, EntityDescriptor.Builder entityBuilder) {
    final AttributeDescriptor<Object> readTarget;
    final AttributeDescriptor<Object> writeTarget;
    final ProxyTransform readTransform;
    final ProxyTransform writeTransform;
    Map<String, Object> write = toMapOrNull(proxyMap.get(WRITE));
    Map<String, Object> read = toMapOrNull(proxyMap.get(READ));
    AttributeDescriptor<Object> original = null;
    if (write == null || read == null) {
        // we need to load the original attribute, which must have been
        // loaded (must contain `scheme`)
        original = (AttributeDescriptor<Object>) entityBuilder.getAttribute(attrName);
    }
    if (read != null) {
        readTarget = Optional.ofNullable(read.get("from")).map(Object::toString).map(entityBuilder::getAttribute).map(a -> (AttributeDescriptor<Object>) a).orElseThrow(() -> new IllegalStateException("Invalid state: `read.from` must not be null"));
    } else {
        readTarget = original;
    }
    if (write != null) {
        writeTarget = Optional.ofNullable(write.get("into")).map(Object::toString).map(entityBuilder::getAttribute).map(a -> (AttributeDescriptor<Object>) a).orElseThrow(() -> new IllegalStateException("Invalid state: `write.into` must not be null"));
    } else {
        writeTarget = original;
    }
    readTransform = readTarget == original ? ElementWiseProxyTransform.identity() : getProxyTransform(read);
    writeTransform = writeTarget == original ? ElementWiseProxyTransform.identity() : getProxyTransform(write);
    URI schemeURI = readProxySchemeOptional(attrName, proxyMap, writeTarget, readTarget);
    entityBuilder.addAttribute(AttributeDescriptor.newProxy(attrName, readTarget, readTransform, writeTarget, writeTransform, schemeURI, requireValueSerializerFactory(schemeURI).getValueSerializer(schemeURI)));
}
Also used : ElementWiseProxyTransform(cz.o2.proxima.transform.ElementWiseProxyTransform) ProxyTransform(cz.o2.proxima.transform.ProxyTransform) ConfigObject(com.typesafe.config.ConfigObject) URI(java.net.URI)

Example 2 with ProxyTransform

use of cz.o2.proxima.transform.ProxyTransform in project proxima-platform by O2-Czech-Republic.

the class ConfigRepository method loadProxyAttribute.

@SuppressWarnings("unchecked")
private void loadProxyAttribute(String attrName, Map<String, Object> settings, EntityDescriptor.Builder entityBuilder) {
    if (settings.get(PROXY) instanceof Map) {
        Map<String, Object> proxyMap = (Map) settings.get(PROXY);
        // copy scheme from upper to inner map, if needed
        proxyMap.computeIfAbsent(SCHEME, settings::get);
        addProxyAttributeAsymmetric(attrName, proxyMap, entityBuilder);
    } else {
        final AttributeDescriptor<Object> writeTarget;
        final ProxyTransform readTransform;
        final ProxyTransform writeTransform;
        final AttributeDescriptor<Object> readTarget = writeTarget = Optional.ofNullable(settings.get(PROXY)).map(Object::toString).map(entityBuilder::getAttribute).map(a -> (AttributeDescriptor<Object>) a).orElseThrow(() -> new IllegalStateException("Invalid state: `proxy` must not be null"));
        if (loadClasses) {
            readTransform = writeTransform = getProxyTransform(settings);
        } else {
            readTransform = writeTransform = null;
        }
        URI schemeURI = readProxySchemeOptional(attrName, settings, writeTarget, readTarget);
        entityBuilder.addAttribute(AttributeDescriptor.newProxy(attrName, readTarget, readTransform, writeTarget, writeTransform, schemeURI, requireValueSerializerFactory(schemeURI).getValueSerializer(schemeURI)));
    }
}
Also used : ElementWiseProxyTransform(cz.o2.proxima.transform.ElementWiseProxyTransform) ProxyTransform(cz.o2.proxima.transform.ProxyTransform) ConfigObject(com.typesafe.config.ConfigObject) Map(java.util.Map) HashMap(java.util.HashMap) URI(java.net.URI)

Example 3 with ProxyTransform

use of cz.o2.proxima.transform.ProxyTransform in project proxima-platform by O2-Czech-Republic.

the class AttributeFamilyProxyDataAccessor method transformSingleRead.

private StreamElement transformSingleRead(StreamElement input) {
    AttributeProxyDescriptor<?> attr = lookupTarget.get(input.getAttributeDescriptor());
    if (attr != null) {
        ProxyTransform transform = attr.getReadTransform();
        String attribute = transform.asElementWise().toProxy(input.getAttribute());
        return StreamElement.upsert(input.getEntityDescriptor(), attr, input.getUuid(), input.getKey(), attribute, input.getStamp(), input.getValue());
    }
    log.warn("Received unknown attribute {}. Letting though, but this " + "might cause other issues.", input.getAttributeDescriptor());
    return input;
}
Also used : ProxyTransform(cz.o2.proxima.transform.ProxyTransform)

Aggregations

ProxyTransform (cz.o2.proxima.transform.ProxyTransform)3 ConfigObject (com.typesafe.config.ConfigObject)2 ElementWiseProxyTransform (cz.o2.proxima.transform.ElementWiseProxyTransform)2 URI (java.net.URI)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1