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

253 lines
8.1 KiB
Java
Raw Normal View History

2022-03-01 12:19:37 +01:00
package org.gcube.application.geoportalcommon;
import java.util.ArrayList;
2022-03-11 12:01:27 +01:00
import java.util.LinkedHashMap;
2022-03-01 12:19:37 +01:00
import java.util.List;
2022-03-11 12:01:27 +01:00
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;
2022-03-01 12:19:37 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class ConvertToDataValueObjectModel.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
2022-03-04 14:39:44 +01:00
* Mar 4, 2022
2022-03-01 12:19:37 +01:00
*/
public class ConvertToDataValueObjectModel {
private static Logger LOG = LoggerFactory.getLogger(ConvertToDataValueObjectModel.class);
/**
2022-03-04 14:39:44 +01:00
* To document config DV.
2022-03-01 12:19:37 +01:00
*
* @param dc the dc
2022-03-04 14:39:44 +01:00
* @return the document config DV
2022-03-01 12:19:37 +01:00
*/
2022-03-04 14:39:44 +01:00
public static DocumentConfigDV toDocumentConfigDV(DocumentConfig dc) {
LOG.trace("toDocumentConfigDV called");
2022-03-01 12:19:37 +01:00
if (dc == null) {
LOG.warn(DocumentConfig.class.getSimpleName() + " is null");
return null;
}
2022-03-04 14:39:44 +01:00
DocumentConfigDV dcVO = new DocumentConfigDV();
2022-03-01 12:19:37 +01:00
dcVO.setId(dc.getId());
dcVO.setType(dc.getType());
2022-03-04 14:39:44 +01:00
dcVO.setItemType(dc.getItem_type());
2022-03-01 12:19:37 +01:00
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;
}
2022-03-04 14:39:44 +01:00
ConfigurationDV configVO = new ConfigurationDV();
List<GcubeProfileDV> gCubeProfilesVO = new ArrayList<GcubeProfileDV>(gCubeProfiles.size());
2022-03-01 12:19:37 +01:00
for (GcubeProfile gCubeProfile : gCubeProfiles) {
2022-03-04 14:39:44 +01:00
gCubeProfilesVO.add(toGcubeProfileDV(gCubeProfile));
2022-03-01 12:19:37 +01:00
}
configVO.setGcubeProfiles(gCubeProfilesVO);
dcVO.setConfiguration(configVO);
LOG.debug("returning: " + dcVO);
return dcVO;
}
/**
2022-03-04 14:39:44 +01:00
* To gcube profile DV.
2022-03-01 12:19:37 +01:00
*
* @param gCubeProfile the g cube profile
2022-03-04 14:39:44 +01:00
* @return the gcube profile DV
2022-03-01 12:19:37 +01:00
*/
2022-03-04 14:39:44 +01:00
public static GcubeProfileDV toGcubeProfileDV(GcubeProfile gCubeProfile) {
LOG.trace("toGcubeProfileDV called");
2022-03-01 12:19:37 +01:00
if (gCubeProfile == null) {
LOG.warn(GcubeProfile.class.getSimpleName() + " is null");
return null;
}
2022-03-04 14:39:44 +01:00
GcubeProfileDV gpVO = new GcubeProfileDV();
2022-03-01 12:19:37 +01:00
gpVO.setGcubeName(gCubeProfile.getGcubeName());
gpVO.setGcubeSecondaryType(gCubeProfile.getGcubeSecondaryType());
gpVO.setMinOccurs(gCubeProfile.getMinOccurs());
gpVO.setMaxOccurs(gCubeProfile.getMaxOccurs());
gpVO.setSectionName(gCubeProfile.getSectionName());
gpVO.setSectionTitle(gCubeProfile.getSectionTitle());
2022-03-07 12:01:28 +01:00
gpVO.setParentName(gCubeProfile.getParentName());
2022-03-01 12:19:37 +01:00
List<FilePath> filePaths = gCubeProfile.getFilePaths();
if (filePaths != null) {
LOG.warn("List of " + FilePath.class.getSimpleName() + " is null");
2022-03-04 14:39:44 +01:00
List<FilePathDV> filePathsVO = new ArrayList<FilePathDV>(filePaths.size());
2022-03-01 12:19:37 +01:00
for (FilePath filePath : filePaths) {
2022-03-04 14:39:44 +01:00
filePathsVO.add(toFilePathDV(filePath));
2022-03-01 12:19:37 +01:00
}
gpVO.setFilePaths(filePathsVO);
}
LOG.info("returning: " + gpVO);
return gpVO;
}
/**
2022-03-04 14:39:44 +01:00
* To file path DV.
2022-03-01 12:19:37 +01:00
*
* @param filePath the file path
2022-03-04 14:39:44 +01:00
* @return the file path DV
2022-03-01 12:19:37 +01:00
*/
2022-03-04 14:39:44 +01:00
public static FilePathDV toFilePathDV(FilePath filePath) {
LOG.trace("toFilePathDV called");
2022-03-01 12:19:37 +01:00
if (filePath == null) {
LOG.warn("List of " + FilePath.class.getSimpleName() + " is null");
return null;
}
2022-03-04 14:39:44 +01:00
FilePathDV fpVO = new FilePathDV();
2022-03-01 12:19:37 +01:00
fpVO.setFieldName(filePath.getFieldName());
fpVO.setFieldDefinition(filePath.getFieldDefinition());
LOG.info("returning: " + fpVO);
return fpVO;
}
2022-03-11 12:01:27 +01:00
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());
}
2022-03-01 12:19:37 +01:00
}