2022-02-14 12:23:13 +01:00
|
|
|
|
package org.gcube.application.cms.concessioni.plugins;
|
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
import com.mongodb.util.JSON;
|
|
|
|
|
import lombok.Data;
|
2022-02-14 12:23:13 +01:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.bson.Document;
|
|
|
|
|
import org.gcube.application.cms.concessioni.sdi.SDIManager;
|
2022-02-14 15:00:59 +01:00
|
|
|
|
import org.gcube.application.cms.concessioni.sdi.faults.SDIInteractionException;
|
2022-02-14 12:23:13 +01:00
|
|
|
|
import org.gcube.application.cms.plugins.MaterializationPlugin;
|
|
|
|
|
import org.gcube.application.cms.plugins.faults.InitializationException;
|
|
|
|
|
import org.gcube.application.cms.plugins.faults.MaterializationException;
|
|
|
|
|
import org.gcube.application.cms.plugins.faults.ShutDownException;
|
|
|
|
|
import org.gcube.application.cms.plugins.model.PluginDescriptor;
|
|
|
|
|
import org.gcube.application.cms.plugins.reports.InitializationReport;
|
|
|
|
|
import org.gcube.application.cms.plugins.reports.MaterializationReport;
|
2022-02-14 15:00:59 +01:00
|
|
|
|
import org.gcube.application.cms.plugins.reports.Report;
|
2022-02-14 12:23:13 +01:00
|
|
|
|
import org.gcube.application.cms.plugins.requests.MaterializationRequest;
|
2022-02-14 15:00:59 +01:00
|
|
|
|
import org.gcube.application.cms.serialization.Serialization;
|
|
|
|
|
import org.gcube.application.geoportal.common.model.JSONPathWrapper;
|
|
|
|
|
import org.gcube.application.geoportal.common.model.document.filesets.Materialization;
|
2022-02-14 12:23:13 +01:00
|
|
|
|
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
|
|
|
|
|
import org.gcube.application.geoportal.common.model.document.ProfiledDocument;
|
|
|
|
|
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
|
2022-02-14 15:00:59 +01:00
|
|
|
|
import org.gcube.application.geoportal.common.model.profile.Field;
|
2022-02-14 12:23:13 +01:00
|
|
|
|
import org.gcube.application.geoportal.common.model.profile.HandlerDeclaration;
|
|
|
|
|
import org.gcube.application.geoportal.common.model.profile.Profile;
|
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
|
@Slf4j
|
|
|
|
|
public class SDIMaterializerPlugin implements MaterializationPlugin {
|
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
@Data
|
|
|
|
|
public static class MaterializationConfig{
|
|
|
|
|
private String schemaField;
|
|
|
|
|
private String documentPath;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
|
SDIManager sdiManager;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public InitializationReport initInContext() throws InitializationException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public InitializationReport init() throws InitializationException {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void shutdown() throws ShutDownException {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
/**
|
|
|
|
|
* Expected params :
|
|
|
|
|
* -workspace MANDATORY
|
|
|
|
|
* -titleField
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
* @return
|
|
|
|
|
* @throws MaterializationException
|
|
|
|
|
*/
|
2022-02-14 12:23:13 +01:00
|
|
|
|
@Override
|
|
|
|
|
public MaterializationReport materialize(MaterializationRequest request) throws MaterializationException {
|
|
|
|
|
log.info("Materializer {} : Performing {} ",this.getDescriptor().getId(),request);
|
2022-02-14 15:00:59 +01:00
|
|
|
|
ProfiledDocument profiledDocument=request.getDocument();
|
2022-02-14 12:23:13 +01:00
|
|
|
|
Profile profile = request.getProfile();
|
|
|
|
|
Document configuration=request.getParameters();
|
|
|
|
|
Document profileConfiguration =null;
|
|
|
|
|
for (HandlerDeclaration h : profile.getHandlers()) {
|
|
|
|
|
if (h.getId().equals("SDI-Default-Materializer")) {
|
|
|
|
|
profileConfiguration = h.getConfiguration();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
MaterializationReport report= new MaterializationReport();
|
|
|
|
|
|
2022-02-14 12:23:13 +01:00
|
|
|
|
log.debug("Profile Configuration is {} ",profileConfiguration);
|
|
|
|
|
try{
|
2022-02-14 15:00:59 +01:00
|
|
|
|
JSONPathWrapper documentNavigator=new JSONPathWrapper(profiledDocument.getTheDocument().toJson());
|
|
|
|
|
JSONPathWrapper profileNavigator=new JSONPathWrapper(profile.getSchema().toJson());
|
2022-02-14 12:23:13 +01:00
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
for(Object fsConfigObj : configuration.get("registeredFileSetPaths", List.class)){
|
|
|
|
|
log.debug("Managing {} ",fsConfigObj);
|
|
|
|
|
MaterializationConfig fsConfig=Serialization.convert(fsConfigObj,MaterializationConfig.class);
|
|
|
|
|
Field f = profileNavigator.getByPath(fsConfig.getSchemaField(), Field.class).get(0);
|
2022-02-14 12:23:13 +01:00
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
// TODO Check if schema points to RegisteredFileSet
|
2022-02-14 12:23:13 +01:00
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
for(RegisteredFileSet fs : documentNavigator.getByPath(fsConfig.getDocumentPath(),RegisteredFileSet.class)){
|
|
|
|
|
log.debug("Found {} ",fs);
|
|
|
|
|
configuration.putIfAbsent("basePersistencePath",profile.getId());
|
|
|
|
|
configuration.putIfAbsent("documentID",profiledDocument.get_id());
|
|
|
|
|
if(configuration.containsKey("titleField"))
|
|
|
|
|
configuration.putIfAbsent("layerTitle",fs.getString(configuration.getString("titleField")));
|
2022-02-14 17:06:32 +01:00
|
|
|
|
else configuration.putIfAbsent("layerTitle",fs.getUUID());
|
|
|
|
|
|
2022-02-14 15:00:59 +01:00
|
|
|
|
RegisteredFileSet obtained = sdiManager.materializeLayer(fs,configuration);
|
|
|
|
|
log.debug("Obtained {} ",obtained);
|
|
|
|
|
documentNavigator.set("$..[?(@.uuid == "+fs.getUUID()+")]",obtained);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
profiledDocument.setTheDocument(Document.parse(documentNavigator.getCtx().json()));
|
|
|
|
|
report.setResultDocument(profiledDocument);
|
|
|
|
|
report.setStatus(Report.Status.OK);
|
|
|
|
|
}catch (SDIInteractionException e){
|
|
|
|
|
log.error("Unable to materialize "+request,e);
|
|
|
|
|
report.setStatus(Report.Status.ERROR);
|
|
|
|
|
report.putMessage(e.getMessage());
|
2022-02-14 12:23:13 +01:00
|
|
|
|
}catch (Throwable t){
|
2022-02-14 15:00:59 +01:00
|
|
|
|
log.error("Unable to materialize "+request,t);
|
|
|
|
|
report.setStatus(Report.Status.ERROR);
|
|
|
|
|
report.putMessage(t.getMessage());
|
2022-02-14 12:23:13 +01:00
|
|
|
|
}finally{
|
2022-02-14 15:00:59 +01:00
|
|
|
|
return report;
|
2022-02-14 12:23:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PluginDescriptor getDescriptor() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|