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

253 lines
8.1 KiB
Java

package org.gcube.application.geoportalcommon;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
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.geoportalcommon.geoportal.config.Configuration;
import org.gcube.application.geoportalcommon.geoportal.config.DocumentConfig;
import org.gcube.application.geoportalcommon.geoportal.config.FilePath;
import org.gcube.application.geoportalcommon.geoportal.config.GcubeProfile;
import org.gcube.application.geoportalcommon.shared.geoportal.DocumentDV;
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.config.ConfigurationDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.DocumentConfigDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.FilePathDV;
import org.gcube.application.geoportalcommon.shared.geoportal.config.GcubeProfileDV;
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 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 dcVO = new DocumentConfigDV();
dcVO.setId(dc.getId());
dcVO.setType(dc.getType());
dcVO.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;
}
ConfigurationDV configVO = new ConfigurationDV();
List<GcubeProfileDV> gCubeProfilesVO = new ArrayList<GcubeProfileDV>(gCubeProfiles.size());
for (GcubeProfile gCubeProfile : gCubeProfiles) {
gCubeProfilesVO.add(toGcubeProfileDV(gCubeProfile));
}
configVO.setGcubeProfiles(gCubeProfilesVO);
dcVO.setConfiguration(configVO);
LOG.debug("returning: " + dcVO);
return dcVO;
}
/**
* 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 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;
}
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;
}
}
public static TemporalReferenceDV toTemporalReferenceDV(TemporalReference temporalReference,
List<String> listDocumentKeys, boolean getFullMap) {
if (temporalReference == null)
return null;
TemporalReferenceDV trDV = toDocumentDV(temporalReference, TemporalReferenceDV.class, listDocumentKeys,
getFullMap);
trDV.setField(temporalReference.getField());
return trDV;
}
public static <T extends DocumentDV> T 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;
}
public static RelationshipDV toRelationshipDV(Relationship relationship) {
if (relationship == null)
return null;
return new RelationshipDV(relationship.getRelationshipName(), relationship.getTargetID());
}
}