geoportal-data-common/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectMod...

529 lines
17 KiB
Java

package org.gcube.application.geoportalcommon;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bson.Document;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.Relationship;
import org.gcube.application.geoportal.common.model.document.temporal.TemporalReference;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.application.geoportalcommon.geoportal.config.FilePath;
import org.gcube.application.geoportalcommon.geoportal.config.GcubeProfile;
import org.gcube.application.geoportalcommon.geoportal.config.ItemField;
import org.gcube.application.geoportalcommon.shared.geoportal.ConfigurationDV;
import org.gcube.application.geoportalcommon.shared.geoportal.DocumentDV;
import org.gcube.application.geoportalcommon.shared.geoportal.GEOPORTAL_CONFIGURATION_TYPE;
import org.gcube.application.geoportalcommon.shared.geoportal.GEOPORTAL_DATA_HANDLER;
import org.gcube.application.geoportalcommon.shared.geoportal.HandlerDeclarationDV;
import org.gcube.application.geoportalcommon.shared.geoportal.ProjectDV;
import org.gcube.application.geoportalcommon.shared.geoportal.RelationshipDV;
import org.gcube.application.geoportalcommon.shared.geoportal.TemporalReferenceDV;
import org.gcube.application.geoportalcommon.shared.geoportal.UseCaseDescriptorDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.ItemFieldDV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class ConvertToDataValueObjectModel.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 4, 2022
*/
public class ConvertToDataValueObjectModel {
private static Logger LOG = LoggerFactory.getLogger(ConvertToDataValueObjectModel.class);
/**
* The Enum GEOPORTAL_CONFIGURATION_TYPE.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 16, 2022
*/
// /**
// * To document config DV.
// *
// * @param dc the dc
// * @return the document config DV
// */
// public static DocumentConfigDV toDocumentConfigDV(DocumentConfig dc) {
// LOG.trace("toDocumentConfigDV called");
//
// if (dc == null) {
// LOG.warn(DocumentConfig.class.getSimpleName() + " is null");
// return null;
// }
//
// DocumentConfigDV dcDV = new DocumentConfigDV();
// dcDV.setId(dc.getId());
// dcDV.setType(dc.getType());
// dcDV.setItemType(dc.getItem_type());
//
// Configuration config = dc.getConfiguration();
// if (config == null) {
// LOG.warn(Configuration.class.getSimpleName() + " is null");
// return null;
// }
//
// List<GcubeProfile> gCubeProfiles = config.getGcubeProfiles();
// if (gCubeProfiles == null) {
// LOG.warn("List of " + GcubeProfile.class.getSimpleName() + " is null");
// return null;
// }
//
// ConfigGcubeProfileDV configGcubeProfile = new ConfigGcubeProfileDV();
// List<GcubeProfileDV> gCubeProfilesVO = new ArrayList<GcubeProfileDV>(gCubeProfiles.size());
// for (GcubeProfile gCubeProfile : gCubeProfiles) {
// gCubeProfilesVO.add(toGcubeProfileDV(gCubeProfile));
// }
// configGcubeProfile.setGcubeProfiles(gCubeProfilesVO);
// dcDV.setConfiguration(configGcubeProfile);
//
// LOG.debug("returning: " + dcDV);
// return dcDV;
// }
// /**
// * To document DV.
// *
// * @param <T> the generic type
// * @param config the config
// * @param type the type
// * @return the document DV
// */
// public static <T> DocumentDV<T> toDocumentDV(Configuration<T> config, Class<T> type) {
// LOG.trace("toDocumentConfigDV called");
//
// if (config == null) {
// LOG.warn(Configuration.class.getSimpleName() + " is null");
// return null;
// }
//
// List<T> listConfigs = config.getListConfigs();
// if (listConfigs == null) {
// LOG.warn("List of Configs is null");
// return null;
// }
//
// DocumentDV<T> doc = null;
// if (type == ConfigGcubeProfileDV.class) {
// doc = (DocumentDV<T>) new DocumentDV<ConfigGcubeProfileDV>();
// ConfigGcubeProfileDV configVO = new ConfigGcubeProfileDV();
// List<GcubeProfileDV> listConfigsDV = new ArrayList<GcubeProfileDV>(listConfigs.size());
// for (GcubeProfile gCubeProfile : listConfigsDV) {
// gCubeProfilesVO.add(toGcubeProfileDV(gCubeProfile));
// }
// configVO.setGcubeProfiles(gCubeProfilesVO);
// doc.setConfiguration(configVO);
// }
// LOG.debug("returning: " + doc);
// return doc;
// }
/**
* To use case descriptor DV.
*
* @param ucd the ucd
* @param handlersIds the handlers ids. If not null only returns (so filtering
* for) handler ids. Otherwise returns all handlers
* @return the document config DV
* @throws Exception the exception
*/
public static UseCaseDescriptorDV toUseCaseDescriptorDV(UseCaseDescriptor ucd, String... handlersIds)
throws Exception {
LOG.trace("toUseCaseDescriptorDV called");
if (ucd == null) {
LOG.warn(UseCaseDescriptor.class.getSimpleName() + " is null");
return null;
}
UseCaseDescriptorDV ucdVO = new UseCaseDescriptorDV();
ucdVO.setId(ucd.getId());
ucdVO.setName(ucd.getName());
ucdVO.setDescription(ucd.getDescription());
ucdVO.setVersion(ucd.getVersion() != null ? ucd.getVersion().toString() : "");
List<HandlerDeclarationDV> listHandlersDV = new ArrayList<HandlerDeclarationDV>();
if (handlersIds != null) {
Map<String, List<HandlerDeclaration>> handlersMapByID = ucd.getHandlersMapByID();
if (handlersMapByID != null) {
for (String handlersId : handlersIds) {
List<HandlerDeclaration> listHandlerDeclaration = handlersMapByID.get(handlersId);
for (HandlerDeclaration handlerDeclaration : listHandlerDeclaration) {
LOG.debug("converting handler {}", handlerDeclaration);
HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration);
LOG.debug("handler converted to {}", hdDV);
listHandlersDV.add(hdDV);
}
}
}
} else {
List<HandlerDeclaration> listHandlerDeclaration = ucd.getHandlers();
for (HandlerDeclaration handlerDeclaration : listHandlerDeclaration) {
LOG.debug("converting handler {}", handlerDeclaration);
HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration);
LOG.debug("handler converted to {}", hdDV);
listHandlersDV.add(hdDV);
}
}
ucdVO.setHandlers(listHandlersDV);
LOG.info("returning {}", ucdVO);
return ucdVO;
}
public static GEOPORTAL_DATA_HANDLER to_GEOPORTAL_DATA_HANDLER(String id) {
if (id == null)
return null;
for (GEOPORTAL_DATA_HANDLER gdh : GEOPORTAL_DATA_HANDLER.values()) {
if (gdh.getId().equals(id)) {
return gdh;
}
}
return null;
}
/**
* To handler declaration DV.
*
* @param handlerDeclaration the handler declaration
* @return the handler declaration DV
* @throws Exception the exception
*/
public static HandlerDeclarationDV toHandlerDeclarationDV(HandlerDeclaration handlerDeclaration) throws Exception {
if (handlerDeclaration == null) {
LOG.warn(HandlerDeclaration.class.getSimpleName() + " is null");
return null;
}
Document configuration = handlerDeclaration.getConfiguration();
HandlerDeclarationDV hdDV = new HandlerDeclarationDV();
hdDV.setId(handlerDeclaration.getId());
hdDV.setType(handlerDeclaration.getType());
hdDV.setHandlerType(to_GEOPORTAL_DATA_HANDLER(handlerDeclaration.getId()));
if (configuration == null) {
LOG.warn("Configuration is null");
return null;
}
try {
GEOPORTAL_CONFIGURATION_TYPE foundConfig = null;
ArrayList<String> configurations = null;
for (GEOPORTAL_CONFIGURATION_TYPE configManaged : GEOPORTAL_CONFIGURATION_TYPE.values()) {
try {
configurations = configuration.get(configManaged.getId(), ArrayList.class);
foundConfig = configManaged;
break;
} catch (Exception e) {
LOG.info(configManaged.getId() + "not found in the handler");
}
}
if (configurations == null) {
String error = "No managed configurations as '" + GEOPORTAL_CONFIGURATION_TYPE.values()
+ "' found in the handler";
LOG.warn(error);
throw new Exception(error);
}
switch (foundConfig) {
case gcube_profiles: {
List<GcubeProfileDV> listGcubeProfiles = new ArrayList<GcubeProfileDV>(configurations.size());
int i = 0;
for (String gCubeProfile : configurations) {
LOG.debug(i++ + ") the gCubeProfile is: " + gCubeProfile);
GcubeProfile profile = org.gcube.application.geoportal.client.utils.Serialization.read(gCubeProfile,
GcubeProfile.class);
listGcubeProfiles.add(toGcubeProfileDV(profile));
}
ConfigurationDV<List<GcubeProfileDV>> dDV = new ConfigurationDV<List<GcubeProfileDV>>(
listGcubeProfiles);
dDV.setConfiguration(listGcubeProfiles);
dDV.setConfigurationType(GEOPORTAL_CONFIGURATION_TYPE.gcube_profiles);
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
return hdDV;
}
case item_fields: {
List<ItemFieldDV> listItemFields = new ArrayList<ItemFieldDV>(configurations.size());
int i = 0;
for (String itemField : configurations) {
LOG.debug(i++ + ") the itemField is: " + itemField);
ItemField profile = org.gcube.application.geoportal.client.utils.Serialization.read(itemField,
ItemField.class);
listItemFields.add(toItemFieldDV(profile));
}
ConfigurationDV<List<ItemFieldDV>> dDV = new ConfigurationDV<List<ItemFieldDV>>(listItemFields);
dDV.setConfiguration(listItemFields);
dDV.setConfigurationType(GEOPORTAL_CONFIGURATION_TYPE.item_fields);
hdDV.setConfiguration(dDV);
LOG.info("returning {}", hdDV);
return hdDV;
}
default:
break;
}
} catch (Exception e) {
LOG.error("Error on getting " + HandlerDeclaration.class.getSimpleName(), e);
throw e;
}
return null;
}
/**
* To gcube profile DV.
*
* @param gCubeProfile the g cube profile
* @return the gcube profile DV
*/
public static GcubeProfileDV toGcubeProfileDV(GcubeProfile gCubeProfile) {
LOG.trace("toGcubeProfileDV called");
if (gCubeProfile == null) {
LOG.warn(GcubeProfile.class.getSimpleName() + " is null");
return null;
}
GcubeProfileDV gpVO = new GcubeProfileDV();
gpVO.setGcubeName(gCubeProfile.getGcubeName());
gpVO.setGcubeSecondaryType(gCubeProfile.getGcubeSecondaryType());
gpVO.setMinOccurs(gCubeProfile.getMinOccurs());
gpVO.setMaxOccurs(gCubeProfile.getMaxOccurs());
gpVO.setSectionName(gCubeProfile.getSectionName());
gpVO.setSectionTitle(gCubeProfile.getSectionTitle());
gpVO.setParentName(gCubeProfile.getParentName());
List<FilePath> filePaths = gCubeProfile.getFilePaths();
if (filePaths != null) {
LOG.warn("List of " + FilePath.class.getSimpleName() + " is null");
List<FilePathDV> filePathsVO = new ArrayList<FilePathDV>(filePaths.size());
for (FilePath filePath : filePaths) {
filePathsVO.add(toFilePathDV(filePath));
}
gpVO.setFilePaths(filePathsVO);
}
LOG.info("returning: " + gpVO);
return gpVO;
}
/**
* To item field DV.
*
* @param itemField the item field
* @return the item field DV
*/
public static ItemFieldDV toItemFieldDV(ItemField itemField) {
LOG.trace("toItemFieldDV called");
if (itemField == null) {
LOG.warn(GcubeProfile.class.getSimpleName() + " is null");
return null;
}
ItemFieldDV ifDV = new ItemFieldDV();
ifDV.setDisplayAsResult(itemField.isAsResult());
ifDV.setDisplayName(itemField.getLabel());
ifDV.setJsonFields(itemField.getPaths());
ifDV.setOperator(itemField.getOperator());
ifDV.setSearchable(itemField.isSearchable());
ifDV.setSortable(itemField.isSortable());
LOG.info("returning: " + ifDV);
return ifDV;
}
/**
* To file path DV.
*
* @param filePath the file path
* @return the file path DV
*/
public static FilePathDV toFilePathDV(FilePath filePath) {
LOG.trace("toFilePathDV called");
if (filePath == null) {
LOG.warn("List of " + FilePath.class.getSimpleName() + " is null");
return null;
}
FilePathDV fpVO = new FilePathDV();
fpVO.setFieldName(filePath.getFieldName());
fpVO.setFieldDefinition(filePath.getFieldDefinition());
LOG.info("returning: " + fpVO);
return fpVO;
}
/**
* To project DV.
*
* @param project the project
* @param projectReader the project reader
* @return the project DV
* @throws Exception the exception
*/
public static ProjectDV toProjectDV(Project project, ProjectDVBuilder projectReader) throws Exception {
LOG.info("toProjectDV called");
if (project == null)
return null;
LOG.info("toProjectDV called for project id=%s with ", project.getId(), projectReader);
if (LOG.isTraceEnabled())
LOG.trace("Source project is: " + project);
try {
ProjectDV theProject = new ProjectDV();
theProject.setId(project.getId());
theProject.setProfileID(project.getProfileID());
theProject.setProfileVersion(
project.getProfileVersion() != null ? project.getProfileVersion().getValue() : "");
Relationship[] relations = project.getRelationships();
if (relations != null && projectReader.isIncludeRelationships()) {
List<RelationshipDV> listRelations = new ArrayList<RelationshipDV>(relations.length);
for (Relationship relationship : relations) {
listRelations.add(toRelationshipDV(relationship));
}
theProject.setRelationships(listRelations);
}
if (projectReader.isIncludeSpatialReference()) {
theProject.setSpatialReference(toDocumentDV(project.getSpatialReference(), DocumentDV.class,
projectReader.getListDocumentKeys(), projectReader.isIncludeFullDocumentMap()));
}
if (projectReader.isIncludeTemporalReference()) {
theProject.setTemporalReference(toTemporalReferenceDV(project.getTemporalReference(),
projectReader.getListDocumentKeys(), projectReader.isIncludeFullDocumentMap()));
}
// if (projectReader.isIncludeValidationReport()) {
// theProject.setValidationReport(toValidationReport(concessione.getReport()));
// if (theProject.getValidationReport() != null)
// theProject.setValidationStatus(theConcessione.getValidationReport().getStatus());
// }
//
// LOG.info("Returning concessioneDV with id: " + theConcessione.getItemId());
//
// if (LOG.isTraceEnabled())
// LOG.trace("Returning: " + theConcessione);
return theProject;
} catch (Exception e) {
LOG.error("Error on converting project: " + project, e);
return null;
}
}
/**
* To temporal reference DV.
*
* @param temporalReference the temporal reference
* @param listDocumentKeys the list document keys
* @param getFullMap the get full map
* @return the temporal reference DV
*/
public static TemporalReferenceDV toTemporalReferenceDV(TemporalReference temporalReference,
List<String> listDocumentKeys, boolean getFullMap) {
if (temporalReference == null)
return null;
TemporalReferenceDV trDV = (TemporalReferenceDV) toDocumentDV(temporalReference, TemporalReferenceDV.class,
listDocumentKeys, getFullMap);
trDV.setField(temporalReference.getField());
return trDV;
}
/**
* To document DV.
*
* @param <T> the generic type
* @param document the document
* @param toType the to type
* @param listDocumentKeys the list document keys
* @param getFullMap the get full map
* @return the t
*/
public static <T extends DocumentDV> DocumentDV toDocumentDV(Document document, Class<T> toType,
List<String> listDocumentKeys, boolean getFullMap) {
if (document == null)
return null;
T documentDV;
if (toType == TemporalReferenceDV.class) {
documentDV = (T) new TemporalReferenceDV();
} else {
documentDV = (T) new DocumentDV();
}
if (listDocumentKeys != null && !getFullMap) {
LinkedHashMap<String, Object> documentAsMap = new LinkedHashMap<String, Object>(listDocumentKeys.size());
for (String key : listDocumentKeys) {
documentAsMap.put(key, document.get(key));
}
documentDV.setDocumentAsMap(documentAsMap);
}
if (getFullMap) {
Set<String> keySet = document.keySet();
LinkedHashMap<String, Object> documentAsMap = new LinkedHashMap<String, Object>(keySet.size());
for (String key : keySet) {
documentAsMap.put(key, document.get(key));
}
documentDV.setDocumentAsMap(documentAsMap);
}
documentDV.setDocumentAsJSON(document.toJson());
return documentDV;
}
/**
* To relationship DV.
*
* @param relationship the relationship
* @return the relationship DV
*/
public static RelationshipDV toRelationshipDV(Relationship relationship) {
if (relationship == null)
return null;
return new RelationshipDV(relationship.getRelationshipName(), relationship.getTargetID());
}
}