Search in sources :

Example 1 with KeyPathElement

use of io.s4.dispatcher.partitioner.KeyInfo.KeyPathElement in project core by s4.

the class AbstractPE method setKeyValue.

private void setKeyValue(Object event, CompoundKeyInfo compoundKeyInfo) {
    if (compoundKeyInfo == null) {
        return;
    }
    keyValue = new ArrayList<Object>();
    Schema schema = schemaContainer.getSchema(event.getClass());
    // get the value for each keyInfo
    for (KeyInfo keyInfo : compoundKeyInfo.getKeyInfoList()) {
        Object value = null;
        Object record = event;
        List<?> list = null;
        Property property = null;
        for (KeyPathElement keyPathElement : keyInfo.getKeyPath()) {
            if (keyPathElement instanceof KeyPathElementIndex) {
                record = list.get(((KeyPathElementIndex) keyPathElement).getIndex());
                schema = property.getComponentProperty().getSchema();
            } else {
                String keyPathElementName = ((KeyPathElementName) keyPathElement).getKeyName();
                property = schema.getProperties().get(keyPathElementName);
                value = null;
                try {
                    value = property.getGetterMethod().invoke(record);
                } catch (Exception e) {
                    Logger.getLogger("s4").error(e);
                    return;
                }
                if (value == null) {
                    Logger.getLogger("s4").error("Value for " + keyPathElementName + " is null!");
                    return;
                }
                if (property.getType().isPrimitive() || property.isNumber() || property.getType().equals(String.class)) {
                    keyValue.add(value);
                    if (saveKeyRecord) {
                        if (keyRecord == null) {
                            keyRecord = new ArrayList<Object>();
                        }
                        keyRecord.add(record);
                    }
                    continue;
                } else if (property.isList()) {
                    try {
                        list = (List) property.getGetterMethod().invoke(record);
                    } catch (Exception e) {
                        Logger.getLogger("s4").error(e);
                        return;
                    }
                } else {
                    try {
                        record = property.getGetterMethod().invoke(record);
                    } catch (Exception e) {
                        Logger.getLogger("s4").error(e);
                        return;
                    }
                    schema = property.getSchema();
                }
            }
        }
    }
}
Also used : KeyInfo(io.s4.dispatcher.partitioner.KeyInfo) CompoundKeyInfo(io.s4.dispatcher.partitioner.CompoundKeyInfo) Schema(io.s4.schema.Schema) KeyPathElementIndex(io.s4.dispatcher.partitioner.KeyInfo.KeyPathElementIndex) KeyPathElementName(io.s4.dispatcher.partitioner.KeyInfo.KeyPathElementName) ArrayList(java.util.ArrayList) List(java.util.List) Property(io.s4.schema.Schema.Property) KeyPathElement(io.s4.dispatcher.partitioner.KeyInfo.KeyPathElement)

Aggregations

CompoundKeyInfo (io.s4.dispatcher.partitioner.CompoundKeyInfo)1 KeyInfo (io.s4.dispatcher.partitioner.KeyInfo)1 KeyPathElement (io.s4.dispatcher.partitioner.KeyInfo.KeyPathElement)1 KeyPathElementIndex (io.s4.dispatcher.partitioner.KeyInfo.KeyPathElementIndex)1 KeyPathElementName (io.s4.dispatcher.partitioner.KeyInfo.KeyPathElementName)1 Schema (io.s4.schema.Schema)1 Property (io.s4.schema.Schema.Property)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1