Example usage for org.springframework.data.solr.core.mapping SolrPersistentProperty isCollectionLike

List of usage examples for org.springframework.data.solr.core.mapping SolrPersistentProperty isCollectionLike

Introduction

In this page you can find the example usage for org.springframework.data.solr.core.mapping SolrPersistentProperty isCollectionLike.

Prototype

boolean isCollectionLike();

Source Link

Document

Returns whether the property is a Collection , Iterable or an array.

Usage

From source file:org.springframework.data.solr.core.convert.MappingSolrConverter.java

@SuppressWarnings("rawtypes")
protected void write(Object source, final Map target, SolrPersistentEntity<?> entity) {

    final PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(
            entity.getPropertyAccessor(source), getConversionService());

    entity.doWithProperties(new PropertyHandler<SolrPersistentProperty>() {

        @SuppressWarnings({ "unchecked" })
        @Override/* ww  w  .ja  v  a 2 s  .com*/
        public void doWithPersistentProperty(SolrPersistentProperty persistentProperty) {

            Object value = accessor.getProperty(persistentProperty);
            if (value == null || persistentProperty.isReadonly()) {
                return;
            }

            if (persistentProperty.containsWildcard() && !persistentProperty.isMap()) {
                throw new IllegalArgumentException("Field '" + persistentProperty.getFieldName()
                        + "' must not contain wildcards. Consider excluding Field from beeing indexed.");
            }

            Object fieldValue = value;
            if (persistentProperty.isMap() && persistentProperty.containsWildcard()) {
                TypeInformation<?> mapTypeInformation = persistentProperty.getTypeInformation()
                        .getMapValueType();
                Class<?> rawMapType = mapTypeInformation.getType();

                Map<?, ?> map = (Map<?, ?>) fieldValue;
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    String mappedFieldName = entry.getKey().toString();
                    SolrInputField field = new SolrInputField(mappedFieldName);
                    if (entry.getValue() instanceof Iterable) {
                        for (Object o : (Iterable<?>) entry.getValue()) {
                            field.addValue(convertToSolrType(rawMapType, o), 1f);
                        }
                    } else {
                        if (rawMapType.isArray()) {
                            for (Object o : (Object[]) entry.getValue()) {
                                field.addValue(convertToSolrType(rawMapType, o), 1f);
                            }
                        } else {
                            field.addValue(convertToSolrType(rawMapType, entry.getValue()), 1f);
                        }
                    }
                    target.put(mappedFieldName, field);
                }
                return;
            }

            SolrInputField field = new SolrInputField(persistentProperty.getFieldName());
            if (persistentProperty.isCollectionLike()) {
                Collection<?> collection = asCollection(fieldValue);
                for (Object o : collection) {
                    if (o != null) {
                        field.addValue(convertToSolrType(persistentProperty.getType(), o), 1f);
                    }
                }
            } else {
                field.setValue(convertToSolrType(persistentProperty.getType(), fieldValue), 1f);
            }
            target.put(persistentProperty.getFieldName(), field);

            if (persistentProperty.isBoosted()) {
                field.setBoost(persistentProperty.getBoost());
            }

        }
    });

    if (entity.isBoosted() && target instanceof SolrInputDocument) {
        ((SolrInputDocument) target).setDocumentBoost(entity.getBoost());
    }

}