Search in sources :

Example 1 with CollectionReferenceDescriptionProperty

use of io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty in project ARLAS-server by gisaia.

the class XmlUtils method parsePropertiesXml.

public static void parsePropertiesXml(Map<String, CollectionReferenceDescriptionProperty> properties, XMLStreamWriter writer, Stack<String> namespace, String uri, Object source, String prefix, ArrayList<Pattern> excludeFields) throws XMLStreamException, ArlasException {
    if (properties != null) {
        for (String key : properties.keySet()) {
            CollectionReferenceDescriptionProperty property = properties.get(key);
            namespace.push(key);
            String path = String.join(".", new ArrayList<>(namespace));
            boolean excludePath = excludeFields.stream().anyMatch(pattern -> pattern.matcher(path).matches());
            if (!excludePath && property.indexed) {
                if (property.type == FieldType.OBJECT) {
                    parsePropertiesXml(property.properties, writer, namespace, uri, source, prefix, excludeFields);
                } else {
                    Object valueObject = MapExplorer.getObjectFromPath(String.join(".", new ArrayList<>(namespace)), source);
                    if (valueObject != null && property.type != FieldType.DATE && property.type != FieldType.GEO_POINT && property.type != FieldType.GEO_SHAPE) {
                        String value = valueObject.toString();
                        writeElement(writer, String.join(".", new ArrayList<>(namespace)), value, uri, prefix);
                    } else if (valueObject != null && property.type == FieldType.DATE) {
                        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX");
                        f.setTimeZone(TimeZone.getTimeZone("UTC"));
                        if (property.format == null) {
                            property.format = CollectionReference.DEFAULT_TIMESTAMP_FORMAT;
                        }
                        Long timestamp = TimestampTypeMapper.getTimestamp(valueObject, property.format);
                        writeElement(writer, String.join(".", new ArrayList<>(namespace)), f.format(new Date(timestamp)), uri, prefix);
                    }
                }
            }
            namespace.pop();
        }
    } else {
        writeElement(writer, String.join(".", new ArrayList<>(namespace)), null, uri, prefix);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) CollectionReferenceDescriptionProperty(io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty)

Example 2 with CollectionReferenceDescriptionProperty

use of io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty in project ARLAS-server by gisaia.

the class CollectionReferenceManager method getType.

public FieldType getType(CollectionReference collectionReference, String field, boolean throwException) throws ArlasException {
    FieldType fieldType = cacheManager.getFieldType(collectionReference.collectionName, field);
    if (fieldType == null) {
        String[] props = field.split("\\.");
        CollectionReferenceDescriptionProperty esField = collectionReferenceService.describeCollection(collectionReference).properties.get(props[0]);
        if (esField == null) {
            return getUnknownType(field, collectionReference.collectionName, throwException);
        }
        for (int i = 1; i < props.length; i++) {
            esField = esField.properties.get(props[i]);
            if (esField == null) {
                return getUnknownType(field, collectionReference.collectionName, throwException);
            }
        }
        if (esField != null) {
            fieldType = esField.type;
            cacheManager.putFieldType(collectionReference.collectionName, field, fieldType);
        } else {
            return getUnknownType(field, collectionReference.collectionName, throwException);
        }
    }
    return fieldType;
}
Also used : FieldType(io.arlas.server.core.model.response.FieldType) CollectionReferenceDescriptionProperty(io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty)

Example 3 with CollectionReferenceDescriptionProperty

use of io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty in project ARLAS-server by gisaia.

the class CollectionReferenceService method getFromSource.

private Map<String, CollectionReferenceDescriptionProperty> getFromSource(CollectionReference collectionReference, Map source, Stack<String> namespace, ArrayList<Pattern> excludeFields, Optional<Set<String>> columnFilterPredicates, boolean parentIsIndexed) {
    Map<String, CollectionReferenceDescriptionProperty> ret = new HashMap<>();
    for (Object key : source.keySet()) {
        namespace.push(key.toString());
        String path = String.join(".", namespace);
        boolean excludePath = excludeFields.stream().anyMatch(pattern -> pattern.matcher(path).matches());
        if (!excludePath) {
            if (source.get(key) instanceof Map) {
                Map property = (Map) source.get(key);
                CollectionReferenceDescriptionProperty collectionProperty = new CollectionReferenceDescriptionProperty();
                if (property.containsKey("type")) {
                    collectionProperty.type = FieldType.getType(property.get("type"));
                } else {
                    collectionProperty.type = FieldType.OBJECT;
                }
                if (FilterMatcherUtil.matchesOrWithin(columnFilterPredicates, path, collectionProperty.type == FieldType.OBJECT)) {
                    // check whether the field is declared in the mapping but not index
                    if (property.containsKey("enabled")) {
                        collectionProperty.indexed = (boolean) property.get("enabled") && parentIsIndexed;
                    } else if (property.containsKey("index")) {
                        collectionProperty.indexed = (boolean) property.get("index") && parentIsIndexed;
                    } else {
                        collectionProperty.indexed = parentIsIndexed;
                    }
                    if (property.containsKey("format")) {
                        String format = property.get("format").toString();
                        if (format == null && collectionProperty.type.equals(FieldType.DATE)) {
                            format = CollectionReference.DEFAULT_TIMESTAMP_FORMAT;
                        }
                        collectionProperty.format = format;
                    }
                    if (property.containsKey("properties") && property.get("properties") instanceof Map) {
                        collectionProperty.properties = getFromSource(collectionReference, (Map) property.get("properties"), namespace, excludeFields, columnFilterPredicates, collectionProperty.indexed);
                    }
                    if (collectionReference.params.taggableFields != null) {
                        collectionProperty.taggable = Arrays.asList(collectionReference.params.taggableFields.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList()).contains(path);
                    }
                    ret.put(key.toString(), collectionProperty);
                }
            }
        }
        namespace.pop();
    }
    return ret;
}
Also used : CollectionReference(io.arlas.server.core.model.CollectionReference) FieldType(io.arlas.server.core.model.response.FieldType) java.util(java.util) Logger(org.slf4j.Logger) CollectionReferenceParameters(io.arlas.server.core.model.CollectionReferenceParameters) NotFoundException(io.arlas.server.core.exceptions.NotFoundException) CacheManager(io.arlas.server.core.managers.CacheManager) LoggerFactory(org.slf4j.LoggerFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ArlasException(io.arlas.server.core.exceptions.ArlasException) Stream(java.util.stream.Stream) io.arlas.server.core.utils(io.arlas.server.core.utils) CollectionReferenceDescriptionProperty(io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty) Pattern(java.util.regex.Pattern) CollectionReferenceDescription(io.arlas.server.core.model.response.CollectionReferenceDescription) CollectionReferenceDescriptionProperty(io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty)

Example 4 with CollectionReferenceDescriptionProperty

use of io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty in project ARLAS-server by gisaia.

the class CollectionReferenceService method getType.

public FieldType getType(CollectionReference collectionReference, String field, boolean throwException) throws ArlasException {
    FieldType fieldType = cacheManager.getFieldType(collectionReference.collectionName, field);
    if (fieldType == null) {
        String[] props = field.split("\\.");
        CollectionReferenceDescriptionProperty esField = describeCollection(collectionReference).properties.get(props[0]);
        if (esField == null) {
            return getUnknownType(field, collectionReference.collectionName, throwException);
        }
        for (int i = 1; i < props.length; i++) {
            esField = esField.properties.get(props[i]);
            if (esField == null) {
                return getUnknownType(field, collectionReference.collectionName, throwException);
            }
        }
        if (esField != null) {
            fieldType = esField.type;
            cacheManager.putFieldType(collectionReference.collectionName, field, fieldType);
        } else {
            return getUnknownType(field, collectionReference.collectionName, throwException);
        }
    }
    return fieldType;
}
Also used : FieldType(io.arlas.server.core.model.response.FieldType) CollectionReferenceDescriptionProperty(io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty)

Example 5 with CollectionReferenceDescriptionProperty

use of io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty in project ARLAS-server by gisaia.

the class CollectionReferenceService method describeCollection.

public CollectionReferenceDescription describeCollection(CollectionReference collectionReference, Optional<String> columnFilter) throws ArlasException {
    ArrayList<Pattern> excludeFields = new ArrayList<>();
    if (collectionReference.params.excludeFields != null) {
        Arrays.asList(collectionReference.params.excludeFields.split(",")).forEach(field -> excludeFields.add(Pattern.compile("^" + field.replace(".", "\\.").replace("*", ".*") + "$")));
    }
    CollectionReferenceDescription collectionReferenceDescription = new CollectionReferenceDescription();
    collectionReferenceDescription.params = collectionReference.params;
    collectionReferenceDescription.collectionName = collectionReference.collectionName;
    Map<String, LinkedHashMap> mappings = getMapping(collectionReferenceDescription.params.indexName);
    Iterator<String> indices = mappings.keySet().iterator();
    Map<String, CollectionReferenceDescriptionProperty> properties = new HashMap<>();
    Optional<Set<String>> columnFilterPredicates = ColumnFilterUtil.getColumnFilterPredicates(columnFilter, collectionReference);
    while (indices.hasNext()) {
        String index = indices.next();
        LinkedHashMap fields = mappings.get(index);
        properties = union(properties, getFromSource(collectionReference, fields, new Stack<>(), excludeFields, columnFilterPredicates, true));
    }
    collectionReferenceDescription.properties = properties;
    if (properties.isEmpty()) {
        throw new ArlasException("This collection can not be described. Check if index or template ".concat(collectionReferenceDescription.params.indexName).concat(" exist in Elasticsearch"));
    }
    return collectionReferenceDescription;
}
Also used : ArlasException(io.arlas.server.core.exceptions.ArlasException) Pattern(java.util.regex.Pattern) CollectionReferenceDescription(io.arlas.server.core.model.response.CollectionReferenceDescription) CollectionReferenceDescriptionProperty(io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty)

Aggregations

CollectionReferenceDescriptionProperty (io.arlas.server.core.model.response.CollectionReferenceDescriptionProperty)6 FieldType (io.arlas.server.core.model.response.FieldType)3 ArlasException (io.arlas.server.core.exceptions.ArlasException)2 CollectionReferenceDescription (io.arlas.server.core.model.response.CollectionReferenceDescription)2 Pattern (java.util.regex.Pattern)2 NotFoundException (io.arlas.server.core.exceptions.NotFoundException)1 CacheManager (io.arlas.server.core.managers.CacheManager)1 CollectionReference (io.arlas.server.core.model.CollectionReference)1 CollectionReferenceParameters (io.arlas.server.core.model.CollectionReferenceParameters)1 io.arlas.server.core.utils (io.arlas.server.core.utils)1 SimpleDateFormat (java.text.SimpleDateFormat)1 java.util (java.util)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1