gcube-cms-suite/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java

406 lines
18 KiB
Java
Raw Normal View History

2021-09-20 16:47:35 +02:00
package org.gcube.application.geoportal.service.engine.mongo;
2021-12-01 11:13:34 +01:00
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mongodb.client.MongoDatabase;
2022-01-17 18:19:40 +01:00
import com.vdurmont.semver4j.Semver;
2022-01-17 13:30:21 +01:00
import lombok.Getter;
2021-12-01 11:13:34 +01:00
import lombok.extern.slf4j.Slf4j;
2022-01-17 18:19:40 +01:00
import org.apache.commons.io.IOUtils;
2021-12-01 11:13:34 +01:00
import org.bson.Document;
import org.bson.types.ObjectId;
2021-12-07 16:12:43 +01:00
import org.gcube.application.cms.plugins.LifecycleManager;
2021-12-07 16:42:39 +01:00
import org.gcube.application.cms.plugins.faults.StepException;
2022-01-12 18:42:22 +01:00
import org.gcube.application.cms.plugins.model.PluginDescriptor;
2021-12-15 19:10:19 +01:00
import org.gcube.application.cms.plugins.reports.ExecutionReport;
2021-12-07 16:42:39 +01:00
import org.gcube.application.cms.plugins.requests.StepExecutionRequest;
2022-01-17 18:19:40 +01:00
import org.gcube.application.geoportal.common.faults.StorageException;
2021-12-07 16:42:39 +01:00
import org.gcube.application.geoportal.common.model.document.*;
2021-12-01 11:13:34 +01:00
import org.gcube.application.geoportal.common.model.legacy.Concessione;
2022-01-17 18:19:40 +01:00
import org.gcube.application.geoportal.common.model.profile.Field;
2022-01-12 18:42:22 +01:00
import org.gcube.application.geoportal.common.model.profile.HandlerDeclaration;
2021-12-07 13:05:08 +01:00
import org.gcube.application.geoportal.common.model.profile.Profile;
2021-12-01 11:13:34 +01:00
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
2022-01-27 15:02:53 +01:00
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.TempFile;
2022-01-17 18:19:40 +01:00
import org.gcube.application.geoportal.common.utils.JSONPathWrapper;
import org.gcube.application.geoportal.common.utils.StorageUtils;
2021-12-07 13:05:08 +01:00
import org.gcube.application.geoportal.service.engine.ImplementationProvider;
2022-01-17 18:19:40 +01:00
import org.gcube.application.geoportal.service.engine.WorkspaceManager;
2021-12-01 11:13:34 +01:00
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
import org.gcube.application.geoportal.service.model.internal.faults.DeletionException;
2022-01-17 13:30:21 +01:00
import org.gcube.application.cms.Serialization;
2021-12-07 16:12:43 +01:00
import org.gcube.application.geoportal.service.utils.UserUtils;
2022-01-17 18:19:40 +01:00
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
2021-09-20 16:47:35 +02:00
2021-12-07 11:16:26 +01:00
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
2021-12-01 11:13:34 +01:00
import java.io.IOException;
2022-01-17 18:19:40 +01:00
import java.io.InputStream;
import java.net.URL;
2021-12-07 11:16:26 +01:00
import java.security.InvalidParameterException;
2022-01-17 18:19:40 +01:00
import java.util.ArrayList;
2022-01-12 18:42:22 +01:00
import java.util.List;
2021-12-07 13:05:08 +01:00
import java.util.Map;
2021-12-01 11:13:34 +01:00
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Consumer;
2022-01-17 13:30:21 +01:00
import static org.gcube.application.cms.Serialization.*;
2021-12-01 11:13:34 +01:00
@Slf4j
public class ProfiledMongoManager extends MongoManager implements MongoManagerI<ProfiledDocument>{
2022-01-18 12:01:33 +01:00
@Getter
2021-12-07 13:05:08 +01:00
Profile profile;
2021-12-07 16:12:43 +01:00
MongoDatabase db=null;
2022-01-17 13:30:21 +01:00
2021-12-07 16:12:43 +01:00
2021-12-07 11:16:26 +01:00
public ProfiledMongoManager(String profileId) throws ConfigurationException {
2022-01-14 12:31:11 +01:00
// Check Profile ID
2021-12-07 16:12:43 +01:00
log.info("Loading profile ID {} ",profileId);
2021-12-07 11:16:26 +01:00
if(profileId==null) throw new InvalidParameterException("Profile ID cannot be null");
2021-12-07 13:05:08 +01:00
Map<String,Profile> profiles=ImplementationProvider.get().getProfiles().getObject();
2021-12-07 16:12:43 +01:00
if(!profiles.containsKey(profileId)) {
log.debug("Asked profile {} not found. Available ones are {} ",profileId,profiles.keySet());
throw new WebApplicationException("Profile " + profileId + " not registered", Response.Status.NOT_FOUND);
}
profile=profiles.get(profileId);
log.debug("Loaded Profile {} ",profile);
2022-01-14 12:31:11 +01:00
// Connect to DB
String toUseDB=super.client.getConnection().getDatabase();
log.info("Connecting to DB {} ",toUseDB);
// TODO MAP OF DATABASES?
db=client.getTheClient().getDatabase(toUseDB);
2021-12-01 11:13:34 +01:00
}
2022-01-17 13:30:21 +01:00
@Getter(lazy = true)
private final LifecycleManager manager=getLCManager();
2021-12-07 16:12:43 +01:00
2022-01-17 13:30:21 +01:00
private LifecycleManager getLCManager() {
try{
LifecycleManager toReturn=null;
//Getting Lifecycle Manager declaration from Profile
List<HandlerDeclaration> handlerDeclarations= profile.getHandlersMap().get(PluginDescriptor.BaseTypes.LIFECYCLE_MANAGER);
if(handlerDeclarations==null || handlerDeclarations.isEmpty()) throw new ConfigurationException("No Lifecycle Handler defined for profile ID "+profile.getId());
if(handlerDeclarations.size()>1) throw new ConfigurationException("Too many Lifecycle Handlers defined ("+handlerDeclarations+") in profile ID "+profile.getId());
HandlerDeclaration lcHandlerDeclaration=handlerDeclarations.get(0);
// Loading Lifecycle Manager
log.debug("Looking for handler {} ",lcHandlerDeclaration);
toReturn=(LifecycleManager) ImplementationProvider.get().getPluginManager().getObject().get(lcHandlerDeclaration.getId());
if(toReturn==null) throw new ConfigurationException("Unable to find Lifecycle Manager Plugin. ID "+lcHandlerDeclaration.getId());
return toReturn;
} catch(Throwable t){
log.warn("Unable to load LC Manager ",t);
return null;
}
}
2021-12-07 16:12:43 +01:00
2021-12-01 11:13:34 +01:00
private String getCollectionName(){
2021-12-07 16:42:39 +01:00
// TODO Profile can directly specify, use ID only as default
2022-01-14 12:31:11 +01:00
2021-12-07 16:12:43 +01:00
return profile.getId();
2021-12-01 11:13:34 +01:00
}
@Override
2021-12-07 16:12:43 +01:00
public MongoDatabase getDatabase(){
return db;
2021-12-01 11:13:34 +01:00
}
@Override
2021-12-07 16:42:39 +01:00
public ProfiledDocument registerNew(Document toRegisterDoc) throws IOException, StepException {
log.info("Registering new document in {} ",profile.getId());
log.debug("Going to register {}",toRegisterDoc.toJson());
2021-12-07 11:16:26 +01:00
ProfiledDocument toRegister = new ProfiledDocument();
toRegister.setTheDocument(toRegisterDoc);
2021-12-07 16:42:39 +01:00
PublicationInfo pubInfo=new PublicationInfo();
pubInfo.setCreationInfo(UserUtils.getCurrent().asInfo());
// TODO Set Access From Profile
Access access=new Access();
access.setLicense("");
access.setPolicy(AccessPolicy.OPEN);
pubInfo.setAccess(access);
toRegister.setInfo(pubInfo);
2021-12-01 11:13:34 +01:00
2021-12-07 16:42:39 +01:00
toRegister.setProfileID(profile.getId());
toRegister.setProfileVersion(profile.getVersion());
2022-01-17 18:19:40 +01:00
toRegister.setVersion(new Semver("1.0.0"));
2021-12-01 11:13:34 +01:00
2021-12-15 19:10:19 +01:00
// Apply Lifecycle
2021-12-01 11:13:34 +01:00
2022-01-17 18:19:40 +01:00
toRegister=step(toRegister,StepExecutionRequest.Steps.ON_INIT_DOCUMENT,null).getResult();
2021-12-07 16:42:39 +01:00
log.debug("Going to register {} ",toRegister);
2021-12-01 11:13:34 +01:00
2021-12-15 19:10:19 +01:00
// Insert object
2022-01-27 15:02:53 +01:00
ObjectId id =insert(asDocumentWithId(toRegister),getCollectionName());
2021-12-07 16:12:43 +01:00
2021-12-07 16:42:39 +01:00
log.info("Obtained id {} ",id);
2021-12-07 16:12:43 +01:00
return getByID(id.toHexString());
2021-12-01 11:13:34 +01:00
}
@Override
2022-01-17 18:19:40 +01:00
public ProfiledDocument update(String id, Document toSet) throws IOException, StepException {
2021-12-01 11:13:34 +01:00
log.trace("Replacing {} ",toSet);
2022-01-17 18:19:40 +01:00
ProfiledDocument toUpdate=getByID(id);
toUpdate.setTheDocument(toSet);
toUpdate=onUpdate(toUpdate);
2022-01-27 15:02:53 +01:00
ProfiledDocument toReturn =convert(replace(asDocumentWithId(toUpdate),new ObjectId(id),getCollectionName()),ProfiledDocument.class);
log.debug("Updated ProfiledDocument is {}",toReturn);
return toReturn;
2022-01-17 18:19:40 +01:00
}
private ProfiledDocument onUpdate(ProfiledDocument toUpdate) throws StepException {
UserUtils.AuthenticatedUser u = UserUtils.getCurrent();
toUpdate.getInfo().setLastEditInfo(u.asInfo());
toUpdate.setVersion(toUpdate.getVersion().withIncPatch());
return step(toUpdate,StepExecutionRequest.Steps.ON_UPDATE_DOCUMENT,null).getResult();
2021-12-01 11:13:34 +01:00
}
@Override
public void delete(String id,boolean force) throws DeletionException {
log.debug("Deleting by ID {}, force {}",id,force);
try{
ProfiledDocument doc =getByID(id);
2021-12-07 16:12:43 +01:00
// TODO INVOKE LIFECYCLE
2021-12-01 11:13:34 +01:00
//if(!force&&isPublished(id)) throw new Exception("Cannot delete published documents. Unpublish it or use force = true");
try{
// TODO CHECK PHASE AND STATUS
// DEINDEX
// DEMATERIALIZE
// DELETE CONTENT
// DELETE ENTRY
throw new DeletionException("IMPLEMENT THIS");
// delete(asId(id), getCollectionName());
}catch(DeletionException e) {
//storing updated - partially deleted
// concessione=onUpdate(concessione);
2022-01-27 15:02:53 +01:00
// replace(asDocumentWithId(concessione), collectionName);
2021-12-01 11:13:34 +01:00
throw e;
}
}catch(Throwable t){
throw new DeletionException("Unable to delete "+id,t);
}
}
@Override
2022-01-17 18:19:40 +01:00
public ProfiledDocument getByID(String id){
Document doc=super.getById(asId(id),getCollectionName());
if(doc==null) throw new WebApplicationException("No document with ID "+id);
return convert(doc,ProfiledDocument.class);
2021-12-01 11:13:34 +01:00
}
@Override
2021-12-07 11:16:26 +01:00
public Iterable<Document> query(QueryRequest queryRequest) {
log.info("Querying {} ",queryRequest);
LinkedBlockingQueue queue=new LinkedBlockingQueue<Concessione>();
query(queryRequest,getCollectionName()).forEach(
(Consumer<? super Document>) (Document d)->{try{
queue.put(d);
}catch(Throwable t){log.warn("Unable to translate "+d);}});
log.info("Returned {} elements ",queue.size());
return queue;
}
@Override
public Iterable<ProfiledDocument> filter(QueryRequest queryRequest) {
2021-12-01 11:13:34 +01:00
log.info("Searching concessione for filter {} ",queryRequest);
LinkedBlockingQueue queue=new LinkedBlockingQueue<Concessione>();
query(queryRequest,getCollectionName()).forEach(
(Consumer<? super Document>) (Document d)->{try{
queue.put(d);
}catch(Throwable t){log.warn("Unable to translate "+d);}});
log.info("Returned {} elements ",queue.size());
return queue;
}
@Override
2022-01-17 18:19:40 +01:00
public ProfiledDocument performStep(String id, String step, Document options) throws StepException, JsonProcessingException {
ExecutionReport report = step(getByID(id), step, options);
2022-01-27 15:02:53 +01:00
return convert(replace(asDocumentWithId(report.getResult()),new ObjectId(id),getCollectionName()),ProfiledDocument.class);
2021-12-01 11:13:34 +01:00
}
2022-01-17 18:19:40 +01:00
/**
* NB Put at path :
*
2022-01-27 15:02:53 +01:00
* Path Examples
* artifact
* images
* images[1]
* layers[?(@.name = 'myName')]
*
2022-01-17 18:19:40 +01:00
*
*
*/
2021-12-01 11:13:34 +01:00
@Override
2022-01-27 15:02:53 +01:00
public ProfiledDocument registerFileSet(String id,RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException {
List<TempFile> files=request.getStreams();
Document attributes =request.getAttributes();
log.info("Registering Fileset for {}, Request is {} ",id,request);
2022-01-17 18:19:40 +01:00
ProfiledDocument doc=getByID(id);
WorkspaceManager ws=new WorkspaceManager();
StorageUtils storage=ImplementationProvider.get().getStorageProvider().getObject();
2022-01-27 15:02:53 +01:00
log.debug("Checking {}  path against profile {}",request.getFieldPath(),profile.getId());
2022-01-17 18:19:40 +01:00
JSONPathWrapper schemaWrapper= new JSONPathWrapper(profile.getSchema().toJson());
2022-01-27 15:02:53 +01:00
List<Field> fieldDefinitions=schemaWrapper.getByPath(request.getFieldPath(),Field.class);
if(fieldDefinitions==null || fieldDefinitions.isEmpty())
throw new WebApplicationException("No Field found in schema "+profile.getId()+" at "+request.getFieldPath(), Response.Status.BAD_REQUEST);
if(fieldDefinitions.size()>1)
throw new WebApplicationException("Multiple field definitions ("+fieldDefinitions.size()+") found in "+profile.getId()+" for "+request.getFieldPath(),Response.Status.BAD_REQUEST);
Field fieldDefinition=fieldDefinitions.get(0);
2022-01-17 18:19:40 +01:00
log.debug("Field definition is {}",fieldDefinition);
JSONPathWrapper docWrapper=new JSONPathWrapper(doc.getTheDocument().toJson());
2022-01-27 15:02:53 +01:00
List<RegisteredFileSet> found=docWrapper.getByPath(request.getDestinationPath(),RegisteredFileSet.class);
2022-01-17 18:19:40 +01:00
if(fieldDefinition.getMaxCardinality()==1 && (!found.isEmpty())){
2022-01-27 15:02:53 +01:00
throw new WebApplicationException("Cannot add registered fileset at "+request.getFieldPath()+" : field is not collection.",Response.Status.BAD_REQUEST);
2022-01-17 18:19:40 +01:00
}
2022-01-27 15:02:53 +01:00
Object toSet=null;
2022-01-17 18:19:40 +01:00
2022-01-27 15:02:53 +01:00
if(fieldDefinition.isCollection()){
2022-01-17 18:19:40 +01:00
}
2022-01-27 15:02:53 +01:00
// Manage clash options
if(!found.isEmpty())
switch (request.getClashOption()){
case REPLACE_EXISTING: {
if(found.size()>1)
throw new WebApplicationException("Cannot replace multiple items at "+request.getDestinationPath()+".",Response.Status.BAD_REQUEST);
deleteFileSet(doc.get_id(),request.getDestinationPath(),false);
toSet = prepareRegisteredFileSet(doc,profile,request.getDestinationPath(),attributes,files,storage,ws);
break;
}case MERGE_EXISTING: {
if(found.size()>1)
throw new WebApplicationException("Cannot merge multiple items at "+request.getDestinationPath()+".",Response.Status.BAD_REQUEST);
attributes.putAll(Serialization.asDocument(found.get(0)));
deleteFileSet(doc.get_id(),request.getDestinationPath(),false);
toSet = prepareRegisteredFileSet(doc,profile,request.getDestinationPath(),attributes,files,storage,ws);
break;
}case APPEND: {
if(!fieldDefinition.isCollection())
throw new WebApplicationException("Cannot append to "+request.getDestinationPath()+" : field "+request.getFieldPath()+" is not collection.",
Response.Status.BAD_REQUEST);
RegisteredFileSet registeredFileSet=prepareRegisteredFileSet(doc,profile,request.getDestinationPath(),attributes,files,storage,ws);
found.add(registeredFileSet);
toSet=found;
break;
}
}
docWrapper.set(request.getDestinationPath(),toSet);
// if(fieldDefinition.getMaxCardinality()>1){
// // Field is collection
// found.add(registeredFileSet);
// docWrapper.set(destination,found);
// }
// else {
// docWrapper.set(destination,registeredFileSet);
// }
2022-01-17 18:19:40 +01:00
log.debug("Setting result on profiled document");
doc.setTheDocument(Document.parse(docWrapper.getCtx().jsonString()));
doc=onUpdate(doc);
2022-01-27 15:02:53 +01:00
return convert(replace(asDocumentWithId(doc),new ObjectId(id),getCollectionName()),ProfiledDocument.class);
2021-12-01 11:13:34 +01:00
}
2021-12-15 19:10:19 +01:00
2022-01-18 12:01:33 +01:00
@Override
public ProfiledDocument deleteFileSet(String id, String destination, Boolean force) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException {
throw new RuntimeException("Implement this");
}
2022-01-17 18:19:40 +01:00
// @Override
// public ProfiledDocument deleteRegisteredFileSet(String id, String destination, List<TempFile> files) {
// throw new RuntimeException("TO IMPLEMENT");
// }
private ExecutionReport step(ProfiledDocument theDocument,String step,Document callParameters) throws StepException {
log.info("[Profile {} ] Invoking Step {} on {}" ,profile.getId(),step,getManager().getDescriptor());
2021-12-15 19:10:19 +01:00
StepExecutionRequest request=new StepExecutionRequest();
2022-01-17 18:19:40 +01:00
request.setCallParameters(callParameters);
2021-12-15 19:10:19 +01:00
request.setDocument(theDocument);
request.setProfile(profile);
2022-01-17 18:19:40 +01:00
request.setStep(step);
2021-12-15 19:10:19 +01:00
log.debug("Requesting Step Execution {} ",request);
2022-01-17 13:30:21 +01:00
ExecutionReport report= getManager().performStep(request);
log.debug("Report is {}",report);
if(report.getResult()==null) throw new StepException("Report result is null");
return report;
2021-12-15 19:10:19 +01:00
}
2022-01-17 18:19:40 +01:00
private static final RegisteredFileSet prepareRegisteredFileSet(ProfiledDocument doc, Profile profile,String destination,
Document attributes,List<TempFile> files, StorageUtils storage,WorkspaceManager ws) throws StorageHubException, StorageException {
log.debug("Preparing Registered FileSet..");
attributes.putIfAbsent(RegisteredFileSet.CREATION_INFO,UserUtils.getCurrent().asInfo());
attributes.putIfAbsent(RegisteredFileSet.ACCESS,doc.getInfo().getAccess());
FolderContainer base=ws.createFolder(new WorkspaceManager.FolderOptions(
doc.get_id(),"Base Folder for profiled document. Profile "+profile.getId(),null));
FolderContainer sectionFolder=ws.createFolder(new WorkspaceManager.FolderOptions(
doc.get_id()+destination,"Registered Fileset at path "+destination,base));
attributes.putIfAbsent(RegisteredFileSet.FOLDER_ID,sectionFolder.getId());
ArrayList<RegisteredFile> registeredFiles=new ArrayList<>();
for (TempFile f : files) {
InputStream is=null;
try{
log.debug("Opening temp file {}",f);
String fileUrl=storage.getURL(f.getId());
log.debug("Got URL {} from ID {}",fileUrl,f.getId());
is=new URL(fileUrl).openStream();
RegisteredFile registered=ws.registerFile(new WorkspaceManager.FileOptions(f.getFilename(),is,
"Imported via gcube CMS service ", sectionFolder));
log.debug("Registered "+registered);
registeredFiles.add(registered);
}catch(StorageHubException | IOException e){
throw new StorageException("Unable to store "+f,e);
}finally{
if(is!=null)
IOUtils.closeQuietly(is);
}
}
attributes.putIfAbsent(RegisteredFileSet.PAYLOADS,registeredFiles);
return Serialization.convert(attributes,RegisteredFileSet.class);
}
2021-09-20 16:47:35 +02:00
}