Example usage for org.springframework.data.mongodb.core.convert MongoTypeMapper writeType

List of usage examples for org.springframework.data.mongodb.core.convert MongoTypeMapper writeType

Introduction

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

Prototype

void writeType(Class<?> type, S dbObject);

Source Link

Document

Writes type information for the given type into the given sink.

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);
                }/* ww  w .  j av  a  2 s.  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());
        }

    }
}