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

io.elsci.signals.mock.assets.AssetsDao Maven / Gradle / Ivy

There is a newer version: 20231025.1145-36
Show newest version
package io.elsci.signals.mock.assets;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import io.elsci.signals.sdk.assets.Asset;
import org.bson.Document;
import org.bson.conversions.Bson;

public class AssetsDao {
    private final MongoClient mongo;
    private final String dbName;
    private final String entitiesCollectionName;

    public AssetsDao(MongoClient mongo, String dbName, String entitiesCollectionName) {
        this.mongo = mongo;
        this.dbName = dbName;
        this.entitiesCollectionName = entitiesCollectionName;
    }

    public Asset save(Asset asset) {
        MongoCollection collection = entityCollection();
        Document d = new Document("_id", asset.getId())
                .append("eid", asset.getId())
                .append("type", "asset")
                .append("name", asset.getName())
                .append("description", asset.getName())
                .append("createdAt", asset.getCreatedAt())
                .append("editedAt", asset.getEditedAt());
        collection.insertOne(d);
        return asset;
    }

    public FindIterable find(Bson filter) {
        return entityCollection().find(filter);
    }

    private MongoCollection entityCollection() {
        return mongo.getDatabase(dbName).getCollection(entitiesCollectionName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy