2020-12-17 18:27:45 +01:00
|
|
|
package org.gcube.application.geoportal.service.engine.mongo;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.bson.Document;
|
|
|
|
import org.bson.types.ObjectId;
|
|
|
|
import org.gcube.application.geoportal.common.model.legacy.Concessione;
|
|
|
|
import org.gcube.application.geoportal.common.model.legacy.report.PublicationReport;
|
|
|
|
import org.gcube.application.geoportal.model.fault.ConfigurationException;
|
|
|
|
import org.gcube.application.geoportal.service.utils.Serialization;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
|
|
|
|
|
import lombok.Synchronized;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class ConcessioniMongoManager extends MongoManager{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ConcessioniMongoManager() throws ConfigurationException {
|
|
|
|
super();
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
}
|
2020-12-18 12:40:46 +01:00
|
|
|
private static final String collectionName="legacyConcessioni";
|
2020-12-17 18:27:45 +01:00
|
|
|
private static final String DB_NAME="gna_dev";
|
|
|
|
|
|
|
|
|
|
|
|
private MongoDatabase db=null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Synchronized
|
|
|
|
protected MongoDatabase getDatabase() {
|
|
|
|
if(db==null) {
|
|
|
|
db=client.getDatabase(DB_NAME);
|
|
|
|
}
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static Document asDocument (Concessione c) throws JsonProcessingException {
|
2020-12-18 12:40:46 +01:00
|
|
|
Document toReturn=Document.parse(Serialization.write(c));
|
|
|
|
if(c.getMongo_id()!=null&&!c.getMongo_id().isEmpty())
|
|
|
|
toReturn.append(ID, new ObjectId(c.getMongo_id()));
|
|
|
|
return toReturn;
|
2020-12-17 18:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static Concessione asConcessione (Document d) throws JsonProcessingException, IOException {
|
|
|
|
return Serialization.read(d.toJson(), Concessione.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Concessione registerNew(Concessione toRegister) throws IOException {
|
|
|
|
ObjectId id=insert(asDocument(toRegister), collectionName);
|
2020-12-18 12:40:46 +01:00
|
|
|
|
2020-12-17 18:27:45 +01:00
|
|
|
Concessione toReturn=asConcessione(getById(id,collectionName));
|
|
|
|
toReturn.setMongo_id(id.toHexString());
|
2020-12-18 12:40:46 +01:00
|
|
|
|
|
|
|
return asConcessione(update(asDocument(toReturn),collectionName));
|
2020-12-17 18:27:45 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 12:40:46 +01:00
|
|
|
public Concessione update(Concessione toRegister) throws IOException {
|
|
|
|
return asConcessione(update(asDocument(toRegister),collectionName));
|
2020-12-17 18:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<Concessione> list(){
|
|
|
|
ArrayList<Concessione> toReturn=new ArrayList<>();
|
|
|
|
iterate(null, collectionName).forEach((Document d)->{
|
|
|
|
try {
|
|
|
|
toReturn.add(asConcessione(d));
|
|
|
|
}catch(Throwable t) {
|
|
|
|
log.error("Unable to read Document as concessione ",t);
|
|
|
|
log.debug("Document was "+d.toJson());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Concessione getById(String id) throws JsonProcessingException, IOException {
|
2020-12-18 12:40:46 +01:00
|
|
|
log.debug("Loading by ID "+id);
|
2020-12-17 18:27:45 +01:00
|
|
|
return asConcessione(getById(new ObjectId(id),collectionName));
|
|
|
|
}
|
|
|
|
public void deleteById(String id) {
|
|
|
|
delete(new ObjectId(id), collectionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Concessione publish(String id) throws JsonProcessingException, IOException{
|
|
|
|
Concessione toReturn=asConcessione(getById(new ObjectId(id),collectionName));
|
|
|
|
toReturn.setDefaults();
|
|
|
|
toReturn.validate();
|
|
|
|
publish(toReturn);
|
2020-12-18 12:40:46 +01:00
|
|
|
return asConcessione(update(asDocument(toReturn),collectionName));
|
2020-12-17 18:27:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static PublicationReport publish(Concessione c) {
|
|
|
|
//TODO implement
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|