-
-
Notifications
You must be signed in to change notification settings - Fork 499
Add support to get metatada resource json object into the index. #9036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//============================================================================= | ||
//=== Copyright (C) 2001-2011 Food and Agriculture Organization of the | ||
//=== Copyright (C) 2001-2025 Food and Agriculture Organization of the | ||
//=== United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
//=== and United Nations Environment Programme (UNEP) | ||
//=== | ||
|
@@ -23,6 +23,14 @@ | |
|
||
package org.fao.geonet.kernel.datamanager.base; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyName; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.introspect.Annotated; | ||
import com.fasterxml.jackson.databind.introspect.AnnotatedMember; | ||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; | ||
import com.google.common.collect.ArrayListMultimap; | ||
import com.google.common.collect.Multimap; | ||
import com.yammer.metrics.core.TimerContext; | ||
|
@@ -34,6 +42,7 @@ | |
import jeeves.xlink.Processor; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.fao.geonet.ApplicationContextHolder; | ||
import org.fao.geonet.annotations.IndexIgnore; | ||
import org.fao.geonet.api.records.attachments.Store; | ||
import org.fao.geonet.constants.Geonet; | ||
import org.fao.geonet.domain.*; | ||
|
@@ -532,6 +541,9 @@ public void indexMetadata(final String metadataId, | |
int savedCount = userSavedSelectionRepository.countTimesUserSavedMetadata(uuid, 0); | ||
fields.put(Geonet.IndexFieldNames.USER_SAVED_COUNT, savedCount); | ||
|
||
// Add metadata file store information | ||
fields.putAll(indexMetadataFileStore(fullMd)); | ||
|
||
fields.putAll(addExtraFields(fullMd)); | ||
|
||
if (fullMd != null) { | ||
|
@@ -660,4 +672,50 @@ private ServiceContext getServiceContext() { | |
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { | ||
this.publisher = publisher; | ||
} | ||
|
||
/** | ||
* Get file store properties to be used in the index. | ||
* | ||
* @return multimap object representing the file store resources to be added to the index. | ||
*/ | ||
public Multimap<String, Object> indexMetadataFileStore(AbstractMetadata fullMd) { | ||
Multimap<String, Object> indexMetadataFileStoreFields = ArrayListMultimap.create(); | ||
try { | ||
List<MetadataResource> metadataResources = store.getResources( | ||
ServiceContext.get(), | ||
fullMd.getUuid(), | ||
(org.fao.geonet.api.records.attachments.Sort) null, | ||
null, | ||
!(fullMd instanceof MetadataDraft)); | ||
|
||
if (metadataResources != null && !metadataResources.isEmpty()) { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
|
||
// Exclude fields with the index ignore annotation | ||
objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() { | ||
@Override | ||
public boolean hasIgnoreMarker(AnnotatedMember member) { | ||
return member.hasAnnotation(IndexIgnore.class) || super.hasIgnoreMarker(member); | ||
} | ||
|
||
@Override | ||
public PropertyName findNameForSerialization(Annotated annotated) { | ||
if (annotated.hasAnnotation(IndexIgnore.class)) return null; | ||
return super.findNameForSerialization(annotated); | ||
} | ||
}); | ||
Comment on lines
+693
to
+709
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Creating an |
||
|
||
JsonNode jsonNode = objectMapper.valueToTree(metadataResources); | ||
indexMetadataFileStoreFields.put("fileStore", jsonNode); | ||
} | ||
} catch (Exception e) { | ||
Log.warning(Geonet.INDEX_ENGINE, String.format( | ||
"Resource for metadata '%s'(%d) is not accessible or cannot be found. Skipping index. Error is: %s", | ||
fullMd.getUuid(), fullMd.getId(), e.getMessage())); | ||
} | ||
return indexMetadataFileStoreFields; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (C) 2001-2025 Food and Agriculture Organization of the | ||
* United Nations (FAO-UN), United Nations World Food Programme (WFP) | ||
* and United Nations Environment Programme (UNEP) | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA | ||
* | ||
* Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, | ||
* Rome - Italy. email: [email protected] | ||
*/ | ||
|
||
package org.fao.geonet.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation to indicate that a field/method should be excluded in the index. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD, ElementType.METHOD}) | ||
public @interface IndexIgnore { | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -30,6 +30,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||
import org.fao.geonet.utils.Xml; | ||||||||||||||||||||||||||||||||||||||||||||||
import org.jdom.Element; | ||||||||||||||||||||||||||||||||||||||||||||||
import static org.junit.Assert.assertFalse; | ||||||||||||||||||||||||||||||||||||||||||||||
import static org.junit.Assert.assertNull; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import org.junit.Test; | ||||||||||||||||||||||||||||||||||||||||||||||
import org.xmlunit.builder.DiffBuilder; | ||||||||||||||||||||||||||||||||||||||||||||||
import org.xmlunit.builder.Input; | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -72,4 +74,22 @@ public void testOdsConversion() throws Exception { | |||||||||||||||||||||||||||||||||||||||||||||
String.format("Differences: %s", diff.toString()), | ||||||||||||||||||||||||||||||||||||||||||||||
diff.hasDifferences()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||||||||||||
public void testArrayConversion() throws Exception { | ||||||||||||||||||||||||||||||||||||||||||||||
String jsonString = "[{\"Test\":\"value1\"},{\"Test\":\"value2\"}]"; | ||||||||||||||||||||||||||||||||||||||||||||||
Element xmlFromJSON = Xml.getXmlFromJSON(jsonString); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
String expectedElement = "<root><array><Test>value1</Test></array><array><Test>value2</Test></array></root>"; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Diff diff = DiffBuilder | ||||||||||||||||||||||||||||||||||||||||||||||
.compare(Input.fromString(expectedElement)) | ||||||||||||||||||||||||||||||||||||||||||||||
.withTest(Input.fromString(Xml.getString(xmlFromJSON))) | ||||||||||||||||||||||||||||||||||||||||||||||
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)) | ||||||||||||||||||||||||||||||||||||||||||||||
.normalizeWhitespace() | ||||||||||||||||||||||||||||||||||||||||||||||
.ignoreComments() | ||||||||||||||||||||||||||||||||||||||||||||||
.checkForSimilar() | ||||||||||||||||||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||||||||||||||||
assertFalse(diff.toString(), diff.hasDifferences()); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm adding here a new test for JSON object.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it isn't a good idea from the point of view of the performance to create a new
TransformerFactory
every time the method is called. Shouldn't we useTransformerFactoryFactory.getTransformerFactory()
instead that uses a singleton?