2020-11-11 18:17:06 +01:00
|
|
|
package org.gcube.application.geoportal.service.engine.mongo;
|
|
|
|
|
2020-12-04 18:37:15 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
import static com.mongodb.client.model.Filters.eq;
|
|
|
|
|
2020-11-18 18:06:38 +01:00
|
|
|
import org.bson.Document;
|
2020-12-17 18:27:45 +01:00
|
|
|
import org.bson.types.ObjectId;
|
2020-11-18 18:06:38 +01:00
|
|
|
import org.gcube.application.geoportal.model.fault.ConfigurationException;
|
2020-12-17 18:27:45 +01:00
|
|
|
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
|
2020-11-11 18:17:06 +01:00
|
|
|
|
|
|
|
import com.mongodb.MongoClient;
|
2020-11-18 18:06:38 +01:00
|
|
|
import com.mongodb.client.FindIterable;
|
|
|
|
import com.mongodb.client.MongoCollection;
|
|
|
|
import com.mongodb.client.MongoDatabase;
|
2020-11-11 18:17:06 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Slf4j
|
2020-12-04 18:37:15 +01:00
|
|
|
public abstract class MongoManager {
|
2020-11-11 18:17:06 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
protected MongoClient client=null;
|
2020-11-11 18:17:06 +01:00
|
|
|
|
2020-11-19 17:38:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-18 18:06:38 +01:00
|
|
|
public MongoManager() throws ConfigurationException {
|
2020-12-17 18:27:45 +01:00
|
|
|
client=ImplementationProvider.get().getMongoClientProvider().getObject();
|
2020-11-18 18:06:38 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
log.info("Got Mongo Client at "+client.getConnectPoint());
|
|
|
|
// NOT AUTHORIZED
|
|
|
|
// log.debug("Existing databases "+client.getDatabaseNames());
|
2020-11-11 18:17:06 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:37:15 +01:00
|
|
|
// private abstract MongoDatabase getDatabase() {
|
|
|
|
// return client.getDatabase("gna-db");
|
|
|
|
// }
|
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
// TODO check if existing DB
|
2020-12-04 18:37:15 +01:00
|
|
|
protected abstract MongoDatabase getDatabase();
|
2020-11-11 18:17:06 +01:00
|
|
|
|
|
|
|
//*********** PROJECTS
|
2020-12-17 18:27:45 +01:00
|
|
|
// NB BsonId
|
|
|
|
protected ObjectId insert(Document proj, String collectionName) {
|
|
|
|
MongoDatabase database=getDatabase();
|
|
|
|
MongoCollection<Document> collection = database.getCollection(collectionName);
|
|
|
|
collection.insertOne(Document.parse(proj.toJson()));
|
|
|
|
return proj.getObjectId("_id");
|
2020-11-18 18:06:38 +01:00
|
|
|
|
|
|
|
}
|
2020-11-11 18:17:06 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
public void delete(ObjectId id, String collectionName) {
|
|
|
|
MongoDatabase database=getDatabase();
|
|
|
|
MongoCollection<Document> collection = database.getCollection(collectionName);
|
|
|
|
collection.deleteOne(eq("_id",id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Document getById(ObjectId id,String collectionName) {
|
|
|
|
MongoDatabase database=getDatabase();
|
|
|
|
MongoCollection<Document> coll=database.getCollection(collectionName);
|
|
|
|
return coll.find(new Document("_id",id)).first();
|
2020-11-18 18:06:38 +01:00
|
|
|
}
|
2020-12-17 18:27:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
public FindIterable<Document> iterate(Document filter,String collectionName) {
|
2020-11-18 18:06:38 +01:00
|
|
|
MongoDatabase database=getDatabase();
|
2020-12-17 18:27:45 +01:00
|
|
|
MongoCollection<Document> coll=database.getCollection(collectionName);
|
|
|
|
if(filter==null)
|
|
|
|
return coll.find();
|
|
|
|
else
|
|
|
|
return coll.find(filter);
|
2020-11-18 18:06:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
|
|
|
|
public <T> FindIterable<T> iterateForClass(Document filter,String collectionName,Class<T> clazz) {
|
2020-11-18 18:06:38 +01:00
|
|
|
MongoDatabase database=getDatabase();
|
2020-12-17 18:27:45 +01:00
|
|
|
MongoCollection<Document> coll=database.getCollection(collectionName);
|
|
|
|
if(filter==null)
|
|
|
|
return coll.find(clazz);
|
|
|
|
else
|
|
|
|
return coll.find(filter,clazz);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void update(Document toUpdate,String collectionName) {
|
|
|
|
MongoDatabase database=getDatabase();
|
|
|
|
MongoCollection<Document> coll=database.getCollection(collectionName);
|
|
|
|
coll.findOneAndReplace(eq("_id",toUpdate.getObjectId("_id")), toUpdate);
|
2020-11-18 18:06:38 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 18:17:06 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
|
2020-11-11 18:17:06 +01:00
|
|
|
//********** PROFILES
|
|
|
|
|
2020-11-18 18:06:38 +01:00
|
|
|
|
2020-11-11 18:17:06 +01:00
|
|
|
}
|