Example usage for org.springframework.data.mongodb.core.convert MongoConverter getTypeMapper

List of usage examples for org.springframework.data.mongodb.core.convert MongoConverter getTypeMapper

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.convert MongoConverter getTypeMapper.

Prototype

MongoTypeMapper getTypeMapper();

Source Link

Document

Returns thw TypeMapper being used to write type information into Document s created with that converter.

Usage

From source file:piecework.repository.concrete.ProcessInstanceRepositoryCustomImpl.java

private void include(Update update, Map<String, List<Value>> data, String id) {
    if (data != null && !data.isEmpty()) {
        MongoConverter converter = mongoOperations.getConverter();
        MongoTypeMapper typeMapper = converter.getTypeMapper();

        Set<String> keywords = new HashSet<String>();
        for (Map.Entry<String, List<Value>> entry : data.entrySet()) {
            String key = "data." + entry.getKey();
            List<Value> values = entry.getValue();
            List<Object> dbObjects = new ArrayList<Object>();

            for (Value value : values) {
                if (value != null) {
                    Object dbObject = converter.convertToMongoType(value);
                    Class<?> clz = null;
                    if (value instanceof File)
                        clz = File.class;
                    else if (value instanceof User)
                        clz = User.class;
                    else if (value instanceof Secret)
                        clz = Secret.class;
                    else if (value instanceof DateValue)
                        clz = DateValue.class;

                    keywords.addAll(ProcessInstanceUtility.keywords(value));

                    if (clz != null)
                        typeMapper.writeType(clz, DBObject.class.cast(dbObject));

                    dbObjects.add(dbObject);
                }/* www .j av  a 2s  . c om*/
            }

            update.set(key, dbObjects);
        }
        if (!keywords.isEmpty()) {
            BasicDBList eachList = new BasicDBList();
            for (String keyword : keywords) {
                eachList.add(keyword);
            }
            if (StringUtils.isNotEmpty(id))
                eachList.add(id);
            update.addToSet("keywords", BasicDBObjectBuilder.start("$each", eachList).get());
        }

    }
}