From 310817e425daa0890e76add3988b9192d5c86071 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 31 Aug 2022 12:43:41 +0200 Subject: [PATCH] migration in progress --- .../ConvertToDataValueObjectModel.java | 174 ++++++++++++++++-- .../geoportal/ProjectsCaller.java | 30 +-- .../shared/SearchingFilter.java | 16 +- .../LoadDocumentConfiguration.java | 21 ++- 4 files changed, 183 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java index 5985b64..41eacfb 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java +++ b/src/main/java/org/gcube/application/geoportalcommon/ConvertToDataValueObjectModel.java @@ -1,5 +1,7 @@ package org.gcube.application.geoportalcommon; +import java.io.IOException; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -45,6 +47,15 @@ import org.gcube.application.geoportalcommon.shared.geoportal.ucd.UseCaseDescrip import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + /** * The Class ConvertToDataValueObjectModel. * @@ -63,6 +74,10 @@ public class ConvertToDataValueObjectModel { public static final String HOURS_MINUTES_SEPARATOR = ":"; public static final String TIME_FORMAT = "HH" + HOURS_MINUTES_SEPARATOR + "mm"; + + public static List KEYSET_POSSIBLE_DATE = Arrays.asList("start", "end", "created", "updated", "inizio", + "fine", "creato", "aggiornato"); + /** * To use case descriptor DV. * @@ -129,6 +144,12 @@ public class ConvertToDataValueObjectModel { } + /** + * To GEOPORTA L DAT A HANDLER. + * + * @param id the id + * @return the geoportal data handler + */ public static GEOPORTAL_DATA_HANDLER to_GEOPORTAL_DATA_HANDLER(String id) { if (id == null) @@ -364,7 +385,6 @@ public class ConvertToDataValueObjectModel { * @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) { LOG.info("toProjectDV called"); @@ -385,7 +405,7 @@ public class ConvertToDataValueObjectModel { project.getProfileVersion() != null ? project.getProfileVersion().getValue() : ""); theProject.setVersion(project.getVersion() != null ? project.getVersion().getValue() : ""); - theProject.setTheDocument(toDocumentDV(project.getTheDocument(), DocumentDV.class, + theProject.setTheDocument(toGenericDocumentDV(project.getTheDocument(), DocumentDV.class, projectReader.getListDocumentKeys(), projectReader.isIncludeFullDocumentMap())); List relations = project.getRelationships(); @@ -451,7 +471,6 @@ public class ConvertToDataValueObjectModel { * * @param project the project * @return the result document DV - * @throws Exception the exception */ public static ResultDocumentDV toResultDocumentDV(Project project) { LOG.info("toResultDocumentDV called"); @@ -465,8 +484,8 @@ public class ConvertToDataValueObjectModel { try { - ResultDocumentDV rd = (ResultDocumentDV) toDocumentDV(project.getTheDocument(), ResultDocumentDV.class, - null, true); + ResultDocumentDV rd = (ResultDocumentDV) toGenericDocumentDV(project.getTheDocument(), + ResultDocumentDV.class, null, true); rd.setId(project.getId()); rd.setProfileID(project.getProfileID()); @@ -492,6 +511,12 @@ public class ConvertToDataValueObjectModel { } + /** + * To publication info DV. + * + * @param info the info + * @return the publication info DV + */ private static PublicationInfoDV toPublicationInfoDV(PublicationInfo info) { if (info == null) return null; @@ -505,6 +530,12 @@ public class ConvertToDataValueObjectModel { } + /** + * To access DV. + * + * @param access the access + * @return the access DV + */ private static AccessDV toAccessDV(Access access) { if (access == null) return null; @@ -516,6 +547,12 @@ public class ConvertToDataValueObjectModel { return acDV; } + /** + * To accounting info DV. + * + * @param creationInfo the creation info + * @return the accounting info DV + */ private static AccountingInfoDV toAccountingInfoDV(AccountingInfo creationInfo) { if (creationInfo == null) return null; @@ -531,18 +568,26 @@ public class ConvertToDataValueObjectModel { return aidv; } + /** + * To identification reference DV. + * + * @param identificationReference the identification reference + * @param listDocumentKeys the list document keys + * @param getFullMap the get full map + * @return the identification reference DV + */ private static IdentificationReferenceDV toIdentificationReferenceDV( IdentificationReference identificationReference, List listDocumentKeys, boolean getFullMap) { if (identificationReference == null) return null; - IdentificationReferenceDV idv = (IdentificationReferenceDV) toDocumentDV(identificationReference, + IdentificationReferenceDV idv = (IdentificationReferenceDV) toGenericDocumentDV(identificationReference, IdentificationReferenceDV.class, listDocumentKeys, getFullMap); idv.setType(identificationReference.getType()); return idv; } - + /** * To date format string. * @@ -597,24 +642,24 @@ public class ConvertToDataValueObjectModel { // } /** - * To document DV. + * To generic document DV. * * @param the generic type * @param document the document - * @param toType the to type + * @param targetClass the target class * @param listDocumentKeys the list document keys * @param getFullMap the get full map - * @return the t + * @return the document DV */ - public static DocumentDV toDocumentDV(Document document, Class targetClass, + public static DocumentDV toGenericDocumentDV(Document document, Class targetClass, List listDocumentKeys, boolean getFullMap) { if (document == null) return null; T documentDV; - if (targetClass == null) + if (targetClass == null) { documentDV = (T) new DocumentDV(); - else { + } else { try { documentDV = targetClass.newInstance(); } catch (InstantiationException e) { @@ -631,7 +676,8 @@ public class ConvertToDataValueObjectModel { if (listDocumentKeys != null && !getFullMap) { LinkedHashMap documentAsMap = new LinkedHashMap(listDocumentKeys.size()); for (String key : listDocumentKeys) { - documentAsMap.put(key, document.get(key)); + + documentAsMap = fillMapValue(document, key, documentAsMap); } documentDV.setDocumentAsMap(documentAsMap); } @@ -640,7 +686,8 @@ public class ConvertToDataValueObjectModel { Set keySet = document.keySet(); LinkedHashMap documentAsMap = new LinkedHashMap(keySet.size()); for (String key : keySet) { - documentAsMap.put(key, document.get(key)); + // documentAsMap.put(key, document.get(key)); + documentAsMap = fillMapValue(document, key, documentAsMap); } documentDV.setDocumentAsMap(documentAsMap); } @@ -649,6 +696,95 @@ public class ConvertToDataValueObjectModel { return documentDV; } + + public static final DateTimeFormatter FULL_FORMATTER=DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss"); + + public static class MyLocalDateSerializer extends JsonSerializer { + @Override + public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { + gen.writeString(value.format(FULL_FORMATTER)); + } + } + + public static class ParseDeserializer extends StdDeserializer { + public ParseDeserializer() { + super(LocalDateTime.class); + } + + @Override + public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { + return LocalDateTime.parse(p.getValueAsString()); // or overloaded with an appropriate format + } + } + + public static class MyLocalDateDeserializer extends JsonDeserializer { + + @Override + public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + return LocalDate.parse(p.getValueAsString(),FULL_FORMATTER); + } + } + + /** + * Fill map. + * + * @param document the document + * @param key the key + * @param documentAsMap the document as map + * @return the linked hash map + */ + public static LinkedHashMap fillMapValue(Document document, String key, + LinkedHashMap documentAsMap) { + + Object value = document.get(key); + String keyLower = key.toLowerCase(); + + /* + ObjectMapper objectMapper = new ObjectMapper(); + JavaTimeModule javaTimeModule = new JavaTimeModule(); + javaTimeModule.addDeserializer(LocalDateTime.class, new ParseDeserializer()); +// javaTimeModule.addSerializer(LocalDate.class, new MyLocalDateSerializer()); + objectMapper.registerModule(javaTimeModule); + + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); + */ + + + //checking if the key is a Date + for (String possibleDate : KEYSET_POSSIBLE_DATE) { + if (keyLower.contains(possibleDate)) { + try { + LOG.debug("value "+value+" is instance of: "+value.getClass()); + Document documentDate = new Document((Map) value); + String jsonValue = documentDate.toJson(); + LOG.debug("jsonValue "+value); + + //LocalDate asDate = objectMapper.readerFor(LocalDate.class).readValue(jsonValue); + + LocalDateTime asDate = org.gcube.application.geoportal.client.utils.Serialization.read(jsonValue, LocalDateTime.class); + LOG.info("Casted as date: " + asDate); + value = asDate; + break; + } catch (Exception e) { + LOG.warn("### MUST BE FIXED ### the field {} with value {} is not a date", key, value); + } + } + } + + documentAsMap.put(key, value); + return documentAsMap; + } + + public class LocalDateDeserializer extends JsonDeserializer { + + @Override + public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + return LocalDate.parse(p.getValueAsString()); + } + } + /** * To relationship DV. @@ -667,7 +803,7 @@ public class ConvertToDataValueObjectModel { /** * To validation report. * - * @param validationReport the validation report + * @param li the li * @return the validation report DV * @throws Exception the exception */ @@ -689,6 +825,12 @@ public class ConvertToDataValueObjectModel { return liDV; } + /** + * To lifecycle information DV status. + * + * @param status the status + * @return the lifecycle information D v. status + */ public static LifecycleInformationDV.Status toLifecycleInformationDVStatus(LifecycleInformation.Status status) { if (status == null) diff --git a/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java b/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java index 44a5457..315b2e1 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java +++ b/src/main/java/org/gcube/application/geoportalcommon/geoportal/ProjectsCaller.java @@ -446,41 +446,13 @@ public class ProjectsCaller { LOG.info("Paging offset: " + offsetIndex + ", limit: " + limitIndex); LOG.info("Direction: " + sDirection); LOG.info("Projection: " + projectionDocument); - LOG.info("Order by Fields: " + orderingFields); + LOG.info("Order by Fields: " + ordering); LOG.info("Search for conditions: " + filter.getConditions()); if (query != null) { LOG.info("Search query to JSON: " + query.toJson()); } return geoportalClient.query(request); -// int i = 0; -// while (projects.hasNext()) { -// Project project = projects.next(); -// ProjectDV projectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectDVBuilder); -// toReturnList.add(projectDV); -// i++; -// LOG.trace(i + ") converted: " + projectDV); -// } -// LOG.debug("read " + toReturnList + " project/s"); -// -// searchedData.setData(toReturnList); -// -// // TODO WORKAROUND MUST BE REMOVE AFTER THE QUERY COUNT -// // AND LIST.SIZE WILL BE AVAILABLE IN THE SERVICE -// if (filter.getConditions() != null) { -// searchedData.setTotalItems(toReturnList.size()); -// totalItems = toReturnList.size(); -// } -// -// if (totalItems == limit || totalItems == 0) { -// LOG.debug("Page completed returning " + totalItems + " items"); -// int newOffset = offsetIndex + limitIndex; -// searchedData.setServerSearchFinished(newOffset > totalItems || totalItems == 0); -// LOG.debug("is Search finished: " + searchedData.isServerSearchFinished()); -// -// } -// -// return searchedData; } catch (Exception e) { LOG.error("Error on loading paginated and filtered list of Project: ", e); diff --git a/src/main/java/org/gcube/application/geoportalcommon/shared/SearchingFilter.java b/src/main/java/org/gcube/application/geoportalcommon/shared/SearchingFilter.java index e4a829a..bf66e33 100644 --- a/src/main/java/org/gcube/application/geoportalcommon/shared/SearchingFilter.java +++ b/src/main/java/org/gcube/application/geoportalcommon/shared/SearchingFilter.java @@ -4,8 +4,6 @@ import java.io.Serializable; import java.util.LinkedHashMap; import java.util.List; -import org.gcube.application.geoportalcommon.shared.geoportal.DocumentDV; -import org.gcube.application.geoportalcommon.shared.geoportal.ResultDocumentDV; import org.gcube.application.geoportalcommon.shared.geoportal.config.ItemFieldDV; /** @@ -99,16 +97,14 @@ public class SearchingFilter implements Serializable { } - private void addProjectionBaseInfo(Class theClass) { + private void addProjectionBaseInfo() { if (projection != null) { projection.put("_profileID", 1); projection.put("_profileVersion", 1); projection.put("_version", 1); - if (theClass.isAssignableFrom(ResultDocumentDV.class)) { - projection.put("_lifecycleInformation", 1); - projection.put("_info", 1); - } + projection.put("_lifecycleInformation", 1); + projection.put("_info", 1); } } @@ -122,6 +118,8 @@ public class SearchingFilter implements Serializable { public SearchingFilter(List orderByFields, ORDER order) { this.orderByFields = orderByFields; this.order = order; + if (this.order == null) + order = ORDER.ASC; } /** @@ -145,9 +143,9 @@ public class SearchingFilter implements Serializable { this.conditions = conditions; } - public void setProjection(LinkedHashMap projection, Class theClass) { + public void setProjection(LinkedHashMap projection) { this.projection = projection; - addProjectionBaseInfo(theClass); + addProjectionBaseInfo(); } public LinkedHashMap getProjection() { diff --git a/src/test/java/org/gcube/application/LoadDocumentConfiguration.java b/src/test/java/org/gcube/application/LoadDocumentConfiguration.java index fb082ed..c493dd9 100644 --- a/src/test/java/org/gcube/application/LoadDocumentConfiguration.java +++ b/src/test/java/org/gcube/application/LoadDocumentConfiguration.java @@ -3,6 +3,8 @@ package org.gcube.application; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -19,6 +21,17 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + public class LoadDocumentConfiguration { private static String TOKEN = ""; @@ -36,7 +49,7 @@ public class LoadDocumentConfiguration { // MockDocumentConfigurationReader mock = new MockDocumentConfigurationReader(); // System.out.println(mock.getListDocumentConfig()); - + toHandlerDeclarationDV(null); } @@ -80,10 +93,10 @@ public class LoadDocumentConfiguration { LOG.debug("File as JSON: " + jsonConfig); Document document = org.gcube.application.geoportal.client.utils.Serialization.read(jsonConfig, Document.class); - + LinkedHashMap configuration = (LinkedHashMap) document.get("_configuration"); - - //configuration.values(); + + // configuration.values(); try { List gcubeProfiles = new ArrayList(configuration.values());