Example usage for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getField

List of usage examples for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getField

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.mapping MongoPersistentProperty getField.

Prototype

@Nullable
    Field getField();

Source Link

Usage

From source file:things.mongo.MongoConnector.java

private Object extractId(MongoPersistentProperty id, Object value) {

    try {//from  w ww  .j  a va 2  s  . co  m
        Field idField = id.getField();
        String idValue = (String) idField.get(value);
        return idValue;
    } catch (IllegalAccessException e) {
        throw new TypeRuntimeException("Can't extract id from type " + typeRegistry.getType(value),
                typeRegistry.getType(value), e);
    } catch (ClassCastException cce) {
        throw new TypeRuntimeException(
                "Can't extract id for type " + typeRegistry.getType(value) + ": id is not of String type",
                typeRegistry.getType(value));
    }
}

From source file:org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.java

protected void checkForIndexes(final MongoPersistentEntity<?> entity) {
    final Class<?> type = entity.getType();
    if (!classesSeen.containsKey(type)) {
        if (log.isDebugEnabled()) {
            log.debug("Analyzing class " + type + " for index information.");
        }// www  .j  a v a 2s . c  om

        // Make sure indexes get created
        if (type.isAnnotationPresent(CompoundIndexes.class)) {
            CompoundIndexes indexes = type.getAnnotation(CompoundIndexes.class);
            for (CompoundIndex index : indexes.value()) {

                String indexColl = StringUtils.hasText(index.collection()) ? index.collection()
                        : entity.getCollection();
                DBObject definition = (DBObject) JSON.parse(index.def());

                ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(),
                        index.sparse());

                if (log.isDebugEnabled()) {
                    log.debug("Created compound index " + index);
                }
            }
        }

        entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
            public void doWithPersistentProperty(MongoPersistentProperty persistentProperty) {

                Field field = persistentProperty.getField();

                if (field.isAnnotationPresent(Indexed.class)) {

                    Indexed index = field.getAnnotation(Indexed.class);
                    String name = index.name();

                    if (!StringUtils.hasText(name)) {
                        name = persistentProperty.getFieldName();
                    } else {
                        if (!name.equals(field.getName()) && index.unique() && !index.sparse()) {
                            // Names don't match, and sparse is not true. This situation will generate an error on the server.
                            if (log.isWarnEnabled()) {
                                log.warn("The index name " + name + " doesn't match this property name: "
                                        + field.getName()
                                        + ". Setting sparse=true on this index will prevent errors when inserting documents.");
                            }
                        }
                    }

                    String collection = StringUtils.hasText(index.collection()) ? index.collection()
                            : entity.getCollection();
                    int direction = index.direction() == IndexDirection.ASCENDING ? 1 : -1;
                    DBObject definition = new BasicDBObject(persistentProperty.getFieldName(), direction);

                    ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse());

                    if (log.isDebugEnabled()) {
                        log.debug("Created property index " + index);
                    }

                } else if (field.isAnnotationPresent(GeoSpatialIndexed.class)) {

                    GeoSpatialIndexed index = field.getAnnotation(GeoSpatialIndexed.class);

                    GeospatialIndex indexObject = new GeospatialIndex(persistentProperty.getFieldName());
                    indexObject.withMin(index.min()).withMax(index.max());
                    indexObject.named(StringUtils.hasText(index.name()) ? index.name() : field.getName());

                    String collection = StringUtils.hasText(index.collection()) ? index.collection()
                            : entity.getCollection();
                    mongoDbFactory.getDb().getCollection(collection).ensureIndex(indexObject.getIndexKeys(),
                            indexObject.getIndexOptions());

                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Created %s for entity %s in collection %s! ", indexObject,
                                entity.getType(), collection));
                    }
                }
            }
        });

        classesSeen.put(type, true);
    }
}