Search in sources :

Example 1 with EncoderContext

use of org.bson.codecs.EncoderContext in project morphia by mongodb.

the class Filters method expr.

/**
 * Allows use of aggregation expressions within the query language.
 *
 * @param expression the expression to evaluate
 * @return the filter
 * @query.filter $expr
 */
public static Filter expr(Expression expression) {
    return new Filter("$expr", null, expression) {

        @Override
        public void encode(Datastore datastore, BsonWriter writer, EncoderContext context) {
            writer.writeName("$expr");
            Expression value = getValue();
            if (value != null) {
                wrapExpression(datastore, writer, value, context);
            } else {
                writer.writeNull();
            }
        }

        @Override
        public Expression getValue() {
            return (Expression) super.getValue();
        }
    };
}
Also used : Datastore(dev.morphia.Datastore) EncoderContext(org.bson.codecs.EncoderContext) Expression(dev.morphia.aggregation.experimental.expressions.impls.Expression) ExpressionHelper.wrapExpression(dev.morphia.aggregation.experimental.codecs.ExpressionHelper.wrapExpression) BsonWriter(org.bson.BsonWriter)

Example 2 with EncoderContext

use of org.bson.codecs.EncoderContext in project engine by Lumeer.

the class UserCodec method encode.

@Override
public void encode(final BsonWriter bsonWriter, final User user, final EncoderContext encoderContext) {
    Document bson = user.getId() != null ? new Document(ID, new ObjectId(user.getId())) : new Document();
    bson.append(NAME, user.getName()).append(EMAIL, user.getEmail());
    if (user.getGroups() != null) {
        List<Document> groupsArray = user.getGroups().entrySet().stream().map(entry -> new Document(ORGANIZATION_ID, entry.getKey()).append(GROUPS, entry.getValue())).collect(Collectors.toList());
        bson.append(ALL_GROUPS, groupsArray);
    }
    documentCodec.encode(bsonWriter, bson, encoderContext);
}
Also used : Document(org.bson.Document) EncoderContext(org.bson.codecs.EncoderContext) BsonObjectId(org.bson.BsonObjectId) User(io.lumeer.api.model.User) Set(java.util.Set) HashMap(java.util.HashMap) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Collectors(java.util.stream.Collectors) BsonReader(org.bson.BsonReader) BsonValue(org.bson.BsonValue) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) Codec(org.bson.codecs.Codec) ObjectId(org.bson.types.ObjectId) BsonWriter(org.bson.BsonWriter) CollectibleCodec(org.bson.codecs.CollectibleCodec) Collections(java.util.Collections) DecoderContext(org.bson.codecs.DecoderContext) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document)

Example 3 with EncoderContext

use of org.bson.codecs.EncoderContext in project morphia by mongodb.

the class MorphiaQuery method getQueryDocument.

Document getQueryDocument() {
    DocumentWriter writer = new DocumentWriter(mapper, seedQuery);
    document(writer, () -> {
        EncoderContext context = EncoderContext.builder().build();
        for (Filter filter : filters) {
            filter.encode(datastore, writer, context);
        }
    });
    Document query = writer.getDocument();
    if (mapper.isMappable(getEntityClass())) {
        mapper.updateQueryWithDiscriminators(mapper.getEntityModel(getEntityClass()), query);
    }
    return query;
}
Also used : EncoderContext(org.bson.codecs.EncoderContext) Filter(dev.morphia.query.experimental.filters.Filter) NearFilter(dev.morphia.query.experimental.filters.NearFilter) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Aggregations

EncoderContext (org.bson.codecs.EncoderContext)3 BsonWriter (org.bson.BsonWriter)2 Document (org.bson.Document)2 Datastore (dev.morphia.Datastore)1 ExpressionHelper.wrapExpression (dev.morphia.aggregation.experimental.codecs.ExpressionHelper.wrapExpression)1 Expression (dev.morphia.aggregation.experimental.expressions.impls.Expression)1 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)1 Filter (dev.morphia.query.experimental.filters.Filter)1 NearFilter (dev.morphia.query.experimental.filters.NearFilter)1 User (io.lumeer.api.model.User)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 BsonObjectId (org.bson.BsonObjectId)1 BsonReader (org.bson.BsonReader)1 BsonValue (org.bson.BsonValue)1