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

590 lines
20 KiB
Java

package org.gcube.application.geoportalcommon;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
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.identification.IdentificationReference;
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
import org.gcube.application.geoportal.common.model.document.relationships.Relationship;
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.IdentificationReferenceDV;
import org.gcube.application.geoportalcommon.shared.geoportal.LifecycleInformationDV;
import org.gcube.application.geoportalcommon.shared.geoportal.ProjectDV;
import org.gcube.application.geoportalcommon.shared.geoportal.RelationshipDV;
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);
/**
* To use case descriptor DV.
*
* @param ucd the ucd
* @param handlersIds the handlers ids. If not null only returns (so filtering
* for) the 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, ucd.getName());
if (hdDV != null) {
LOG.debug("handler converted to {}", hdDV);
listHandlersDV.add(hdDV);
} else {
LOG.info("Skipping {} as null for {}", HandlerDeclarationDV.class.getSimpleName(),
handlerDeclaration);
}
}
}
}
} else {
List<HandlerDeclaration> listHandlerDeclaration = ucd.getHandlers();
for (HandlerDeclaration handlerDeclaration : listHandlerDeclaration) {
LOG.debug("converting handler {}", handlerDeclaration);
HandlerDeclarationDV hdDV = toHandlerDeclarationDV(handlerDeclaration, ucd.getName());
if (hdDV != null) {
LOG.debug("handler converted to {}", hdDV);
listHandlersDV.add(hdDV);
} else {
LOG.info("Skipping {} as null for {}", HandlerDeclarationDV.class.getSimpleName(),
handlerDeclaration);
}
}
}
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
* @param profileName the profile name
* @param readConfigs the read configs
* @return the handler declaration DV
* @throws Exception the exception
*/
public static HandlerDeclarationDV toHandlerDeclarationDV(HandlerDeclaration handlerDeclaration, String profileName,
GEOPORTAL_CONFIGURATION_TYPE... readConfigs) 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.setItemType(profileName);
hdDV.setDataHandlerType(to_GEOPORTAL_DATA_HANDLER(handlerDeclaration.getId()));
if (configuration == null) {
LOG.warn("Configuration is null in the handler with id: " + handlerDeclaration.getId());
return null;
}
try {
if (readConfigs == null || readConfigs.length == 0) {
readConfigs = GEOPORTAL_CONFIGURATION_TYPE.values();
}
GEOPORTAL_CONFIGURATION_TYPE geoportalConfigType = null;
ArrayList<String> jsonConfigurations = null;
for (GEOPORTAL_CONFIGURATION_TYPE configManaged : readConfigs) {
try {
LOG.debug("searching '" + configManaged.getId() + "' in the configuration " + configuration);
LOG.trace("contains " + configManaged.getId() + ": "
+ configuration.containsKey(configManaged.getId()));
ArrayList<LinkedHashMap<String, Object>> listHashMapConfig = configuration
.get(configManaged.getId(), ArrayList.class);
LOG.debug("hashMapConfig found is: {}", listHashMapConfig);
if (listHashMapConfig != null) {
jsonConfigurations = new ArrayList<String>();
for (LinkedHashMap<String, Object> config : listHashMapConfig) {
Document document = new Document(config);
String jsonValue = document.toJson();
LOG.debug("config is: {}", jsonValue);
jsonConfigurations.add(jsonValue);
}
LOG.debug("configurations found are: {}", jsonConfigurations);
geoportalConfigType = configManaged;
break;
}
} catch (Exception e) {
LOG.info(configManaged.getId() + " not found in the configuration of the handler "
+ handlerDeclaration.getId(), e);
}
}
if (jsonConfigurations == null) {
String error = "No managed configurations as '"
+ Arrays.asList(GEOPORTAL_CONFIGURATION_TYPE.values()).toString() + "' found in the handler "
+ handlerDeclaration.getId();
LOG.warn(error);
return null;
}
switch (geoportalConfigType) {
case gcube_profiles: {
List<GcubeProfileDV> listGcubeProfiles = new ArrayList<GcubeProfileDV>(jsonConfigurations.size());
int i = 0;
for (String asJSONString : jsonConfigurations) {
LOG.debug(++i + ") the gCubeProfile is: " + asJSONString);
GcubeProfile profile = org.gcube.application.geoportal.client.utils.Serialization.read(asJSONString,
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>(jsonConfigurations.size());
int i = 0;
for (String asJSONString : jsonConfigurations) {
LOG.debug(++i + ") the itemField is: " + asJSONString);
ItemField profile = org.gcube.application.geoportal.client.utils.Serialization.read(asJSONString,
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(ItemField.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());
fpVO.setGcubeProfileFieldName(filePath.getGcubeProfileFieldName());
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: {}, 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() : "");
theProject.setVersion(project.getVersion()!=null? project.getVersion().getValue():"");
theProject.setTheDocument(toDocumentDV(project.getTheDocument(), DocumentDV.class, projectReader.getListDocumentKeys(),
projectReader.isIncludeFullDocumentMap()));
List<Relationship> relations = project.getRelationships();
if (relations != null && projectReader.isIncludeRelationships()) {
List<RelationshipDV> listRelations = new ArrayList<RelationshipDV>(relations.size());
for (Relationship relationship : relations) {
listRelations.add(toRelationshipDV(relationship));
}
theProject.setRelationships(listRelations);
}
List<IdentificationReference> identificationReferences = project.getIdentificationReferences();
if(identificationReferences!=null) {
Map<String, IdentificationReferenceDV> mapIdentReferenceDV = new HashMap<String, IdentificationReferenceDV>(identificationReferences.size());
for (IdentificationReference identificationReference : identificationReferences) {
IdentificationReferenceDV idv = toIdentificationReferenceDV(identificationReference, projectReader.getListDocumentKeys(),
projectReader.isIncludeFullDocumentMap());
mapIdentReferenceDV.put(idv.getType(), idv);
}
theProject.setMapIdentReferenceDV(mapIdentReferenceDV);
}
if (projectReader.isIncludeLifecycleInformation()) {
if (project.getLifecycleInformation() != null)
theProject.setLifecycleInformationDV(toLifecycleInformationDV(project.getLifecycleInformation()));
}
// 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()));
// }
//
// LOG.info("Returning concessioneDV with id: " + theConcessione.getItemId());
//
if (LOG.isDebugEnabled())
LOG.trace("Returning: " + theProject);
LOG.info("Returning project with id: " + theProject.getId());
return theProject;
} catch (Exception e) {
LOG.error("Error on converting project: " + project, e);
return null;
}
}
private static IdentificationReferenceDV toIdentificationReferenceDV(
IdentificationReference identificationReference, List<String> listDocumentKeys, boolean getFullMap) {
if (identificationReference == null)
return null;
IdentificationReferenceDV idv = (IdentificationReferenceDV) toDocumentDV(identificationReference,
IdentificationReferenceDV.class, listDocumentKeys, getFullMap);
idv.setType(identificationReference.getType());
return idv;
}
// /**
// * 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> targetClass,
List<String> listDocumentKeys, boolean getFullMap) {
if (document == null)
return null;
T documentDV;
if (targetClass == null)
documentDV = (T) new DocumentDV();
else {
try {
documentDV = targetClass.newInstance();
} catch (InstantiationException e) {
LOG.warn("InstantiationException: " + e.getMessage() + ". Instancing default "
+ DocumentDV.class.getSimpleName());
documentDV = (T) new DocumentDV();
} catch (IllegalAccessException e) {
LOG.warn("IllegalAccessException: " + e.getMessage() + ". Instancing default "
+ DocumentDV.class.getSimpleName());
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());
}
/**
* To validation report.
*
* @param validationReport the validation report
* @return the validation report DV
* @throws Exception the exception
*/
public static LifecycleInformationDV toLifecycleInformationDV(LifecycleInformation li) throws Exception {
LOG.info("toLifecycleInformationDV called");
if (li == null)
return null;
LOG.debug("toLifecycleInformationDV called for: " + li);
LifecycleInformationDV liDV = new LifecycleInformationDV();
liDV.setErrorMessages(li.getErrorMessages());
liDV.setLastInvokedStep(li.getLastInvokedStep());
liDV.setLastOperationStatus(toLifecycleInformationDVStatus(li.getLastOperationStatus()));
liDV.setWarningMessages(li.getWarningMessages());
liDV.setAsJSONString(toJSON(li));
LOG.debug("Returning: " + liDV);
return liDV;
}
public static LifecycleInformationDV.Status toLifecycleInformationDVStatus(LifecycleInformation.Status status) {
if (status == null)
return null;
try {
return LifecycleInformationDV.Status.valueOf(status.name());
} catch (Exception e) {
LOG.error("Error on converting " + status, e);
return LifecycleInformationDV.Status.NOT_SPECIFIED;
}
}
/**
* To JSON.
*
* @param theObj the the obj
* @return the string
*/
public static String toJSON(Object theObj) {
LOG.debug("toJSON called");
try {
// if (theObj instanceof Serializable) {
return org.gcube.application.geoportal.client.utils.Serialization.write(theObj);
// }
// throw new Exception("The input object is not serializable");
} catch (Exception e) {
LOG.warn("Error on deserializing: ", e);
return null;
}
}
}