All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManager Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright (c) 2017 Otávio Santana and others
 *   All rights reserved. This program and the accompanying materials
 *   are made available under the terms of the Eclipse Public License v1.0
 *   and Apache License v2.0 which accompanies this distribution.
 *   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 *   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
 *
 *   You may elect to redistribute this code under either of these licenses.
 *
 *   Contributors:
 *
 *   Otavio Santana
 */

package org.jnosql.diana.mongodb.document;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.result.DeleteResult;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.jnosql.diana.api.Sort;
import org.jnosql.diana.api.document.DocumentCollectionManager;
import org.jnosql.diana.api.document.DocumentDeleteQuery;
import org.jnosql.diana.api.document.DocumentEntity;
import org.jnosql.diana.api.document.DocumentQuery;
import org.jnosql.diana.api.document.Documents;

import java.time.Duration;
import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
import static org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD;
import static org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument;

/**
 * The mongodb implementation to {@link DocumentCollectionManager} that does not support TTL methods
 * 

{@link MongoDBDocumentCollectionManager#insert(DocumentEntity, Duration)}

*/ public class MongoDBDocumentCollectionManager implements DocumentCollectionManager { private static final BsonDocument EMPTY = new BsonDocument(); private final MongoDatabase mongoDatabase; MongoDBDocumentCollectionManager(MongoDatabase mongoDatabase) { this.mongoDatabase = mongoDatabase; } @Override public DocumentEntity insert(DocumentEntity entity) { Objects.requireNonNull(entity, "entity is required"); String collectionName = entity.getName(); MongoCollection collection = mongoDatabase.getCollection(collectionName); Document document = getDocument(entity); collection.insertOne(document); boolean hasNotId = entity.getDocuments().stream() .map(org.jnosql.diana.api.document.Document::getName).noneMatch(k -> k.equals(ID_FIELD)); if (hasNotId) { entity.add(Documents.of(ID_FIELD, document.get(ID_FIELD))); } return entity; } @Override public DocumentEntity insert(DocumentEntity entity, Duration ttl) { throw new UnsupportedOperationException("MongoDB does not support save with TTL"); } @Override public DocumentEntity update(DocumentEntity entity) { Objects.requireNonNull(entity, "entity is required"); DocumentEntity copy = entity.copy(); String collectionName = entity.getName(); MongoCollection collection = mongoDatabase.getCollection(collectionName); Document id = copy.find(ID_FIELD) .map(d -> new Document(d.getName(), d.getValue().get())) .orElseThrow(() -> new UnsupportedOperationException("To update this DocumentEntity " + "the field `id` is required")); copy.remove(ID_FIELD); collection.findOneAndReplace(id, getDocument(entity)); return entity; } @Override public void delete(DocumentDeleteQuery query) { Objects.requireNonNull(query, "query is required"); String collectionName = query.getDocumentCollection(); MongoCollection collection = mongoDatabase.getCollection(collectionName); Bson mongoDBQuery = DocumentQueryConversor.convert(query.getCondition() .orElseThrow(() -> new IllegalArgumentException("condition is required"))); DeleteResult deleteResult = collection.deleteMany(mongoDBQuery); } @Override public List select(DocumentQuery query) { Objects.requireNonNull(query, "query is required"); String collectionName = query.getDocumentCollection(); MongoCollection collection = mongoDatabase.getCollection(collectionName); Bson mongoDBQuery = query.getCondition().map(DocumentQueryConversor::convert).orElse(EMPTY); FindIterable documents = collection.find(mongoDBQuery); if (query.getSkip() > 0) { documents.skip((int) query.getSkip()); } if (query.getLimit() > 0) { documents.limit((int) query.getLimit()); } query.getSorts().stream().map(this::getSort).forEach(documents::sort); return stream(documents.spliterator(), false).map(MongoDBUtils::of) .map(ds -> DocumentEntity.of(collectionName, ds)).collect(toList()); } @Override public long count(String documentCollection) { Objects.requireNonNull(documentCollection, "documentCollection is required"); MongoCollection collection = mongoDatabase.getCollection(documentCollection); return collection.count(); } private Bson getSort(Sort sort) { boolean isAscending = Sort.SortType.ASC.equals(sort.getType()); return isAscending?Sorts.ascending(sort.getName()): Sorts.descending(sort.getName()); } @Override public void close() { } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy