diff --git a/core/src/main/java/org/fao/geonet/kernel/datamanager/IMetadataUtils.java b/core/src/main/java/org/fao/geonet/kernel/datamanager/IMetadataUtils.java index 63c3dadb559..e875d9fc5d6 100644 --- a/core/src/main/java/org/fao/geonet/kernel/datamanager/IMetadataUtils.java +++ b/core/src/main/java/org/fao/geonet/kernel/datamanager/IMetadataUtils.java @@ -549,4 +549,31 @@ void setCreativeCommons(ServiceContext context, String id, String licenseurl, St * @param dest */ void replaceFiles(AbstractMetadata original, AbstractMetadata dest); + + /** + * Get the metadata after preforming a search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ + String selectOneWithSearchAndReplace(String uuid, String search, String replace); + + /** + * Get the metadata after preforming a regex search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ + String selectOneWithRegexSearchAndReplaceWithFlags(String uuid, String search, String replace, String flags); + + /** + * Get the metadata after preforming a regex search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ + String selectOneWithRegexSearchAndReplace(String uuid, String search, String replace); } diff --git a/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataUtils.java b/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataUtils.java index ad4dde213b4..bbcbe009e6d 100644 --- a/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataUtils.java +++ b/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataUtils.java @@ -1028,4 +1028,19 @@ public void cloneFiles(AbstractMetadata original, AbstractMetadata dest) { public void replaceFiles(AbstractMetadata original, AbstractMetadata dest) { // Empty implementation for non-draft mode as not used } + + @Override + public String selectOneWithSearchAndReplace(String uuid, String search, String replace) { + return metadataRepository.selectOneWithSearchAndReplace(uuid, search, replace); + } + + @Override + public String selectOneWithRegexSearchAndReplaceWithFlags(String uuid, String search, String replace, String flags) { + return metadataRepository.selectOneWithRegexSearchAndReplaceWithFlags(uuid, search, replace, flags); + } + + @Override + public String selectOneWithRegexSearchAndReplace(String uuid, String search, String replace) { + return metadataRepository.selectOneWithRegexSearchAndReplace(uuid, search, replace); + } } diff --git a/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java b/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java index 21cf26e85bd..39b163ded5b 100644 --- a/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java +++ b/core/src/main/java/org/fao/geonet/kernel/datamanager/draft/DraftMetadataUtils.java @@ -690,4 +690,31 @@ public void setListOfStatusCreatingDraft(Set listOfStatusCreatingDraft) public Set getListOfStatusCreatingDraft() { return listOfStatusToTriggerDraftCreation; } + + @Override + public String selectOneWithSearchAndReplace(String uuid, String search, String replace) { + String updatedXml = metadataDraftRepository.selectOneWithSearchAndReplace(uuid, search, replace); + if (updatedXml == null) { + updatedXml = super.selectOneWithSearchAndReplace(uuid, search, replace); + } + return updatedXml; + } + + @Override + public String selectOneWithRegexSearchAndReplaceWithFlags(String uuid, String search, String replace, String flags) { + String updatedXml = metadataDraftRepository.selectOneWithRegexSearchAndReplaceWithFlags(uuid, search, replace, flags); + if (updatedXml == null) { + updatedXml = super.selectOneWithRegexSearchAndReplaceWithFlags(uuid, search, replace, flags); + } + return updatedXml; + } + + @Override + public String selectOneWithRegexSearchAndReplace(String uuid, String search, String replace) { + String updatedXml = metadataDraftRepository.selectOneWithRegexSearchAndReplace(uuid, search, replace); + if (updatedXml == null) { + updatedXml = super.selectOneWithRegexSearchAndReplace(uuid, search, replace); + } + return updatedXml; + } } diff --git a/domain/src/main/java/org/fao/geonet/repository/MetadataDraftRepository.java b/domain/src/main/java/org/fao/geonet/repository/MetadataDraftRepository.java index b382a4dd876..9e226a86a32 100644 --- a/domain/src/main/java/org/fao/geonet/repository/MetadataDraftRepository.java +++ b/domain/src/main/java/org/fao/geonet/repository/MetadataDraftRepository.java @@ -30,6 +30,8 @@ import org.fao.geonet.domain.MetadataDraft; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; /** * Data Access object for the {@link MetadataDraft} entities. @@ -68,4 +70,51 @@ public interface MetadataDraftRepository */ @Nonnull List findAllByHarvestInfo_Uuid(@Nonnull String uuid); + + /** + * Get the metadata after preforming a search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ + @Query(value = "SELECT replace(data, :search, :replace) FROM MetadataDraft m " + + "WHERE uuid = :uuid", + nativeQuery = true) + String selectOneWithSearchAndReplace( + @Param("uuid") String uuid, + @Param("search") String search, + @Param("replace") String replace); + + /** + * Get the metadata after preforming a regex search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ + @Query(value = "SELECT regexp_replace(data, :pattern, :replace) FROM MetadataDraft m " + + "WHERE uuid = :uuid", + nativeQuery = true) + String selectOneWithRegexSearchAndReplace( + @Param("uuid") String uuid, + @Param("pattern") String search, + @Param("replace") String replace); + + /** + * Get the metadata after preforming a regex search and replace on it with regex flags. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @param flags The regex flags to use. + * @return The metadata with the search and replace applied. + */ + @Query(value = "SELECT regexp_replace(data, :pattern, :replace, :flags) FROM MetadataDraft m " + + "WHERE uuid = :uuid", + nativeQuery = true) + String selectOneWithRegexSearchAndReplaceWithFlags( + @Param("uuid") String uuid, + @Param("pattern") String search, + @Param("replace") String replace, + @Param("flags") String flags); } diff --git a/domain/src/main/java/org/fao/geonet/repository/MetadataRepository.java b/domain/src/main/java/org/fao/geonet/repository/MetadataRepository.java index 3a8fefb4955..eba9c43f0ee 100644 --- a/domain/src/main/java/org/fao/geonet/repository/MetadataRepository.java +++ b/domain/src/main/java/org/fao/geonet/repository/MetadataRepository.java @@ -76,8 +76,13 @@ public interface MetadataRepository extends GeonetRepository, @Nonnull List findAllByHarvestInfo_Uuid(@Nonnull String uuid); - - + /** + * Get the metadata after preforming a search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ @Query(value = "SELECT replace(data, :search, :replace) FROM metadata m " + "WHERE uuid = :uuid", nativeQuery = true) @@ -86,6 +91,13 @@ String selectOneWithSearchAndReplace( @Param("search") String search, @Param("replace") String replace); + /** + * Get the metadata after preforming a regex search and replace on it. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @return The metadata with the search and replace applied. + */ @Query(value = "SELECT regexp_replace(data, :pattern, :replace) FROM metadata m " + "WHERE uuid = :uuid", nativeQuery = true) @@ -94,6 +106,14 @@ String selectOneWithRegexSearchAndReplace( @Param("pattern") String search, @Param("replace") String replace); + /** + * Get the metadata after preforming a regex search and replace on it with regex flags. + * @param uuid The UUID of the metadata to search for. + * @param search The string to search for. + * @param replace The string to replace the search string with. + * @param flags The regex flags to use. + * @return The metadata with the search and replace applied. + */ @Query(value = "SELECT regexp_replace(data, :pattern, :replace, :flags) FROM metadata m " + "WHERE uuid = :uuid", nativeQuery = true) diff --git a/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessApi.java b/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessApi.java index afd1f15d037..6b709470b44 100644 --- a/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessApi.java +++ b/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessApi.java @@ -92,6 +92,9 @@ public class DatabaseProcessApi { @Autowired DataManager dataMan; + @Autowired + IMetadataUtils metadataUtils; + @Autowired SchemaManager schemaMan; @@ -192,7 +195,7 @@ public ResponseEntity previewProcessSearchAndReplace( final String siteURL = request.getRequestURL().toString() + "?" + request.getQueryString(); for (String uuid : records) { - String id = dataMan.getMetadataId(uuid); + String id = String.valueOf(metadataUtils.findOneByUuid(uuid).getId()); Log.info("org.fao.geonet.services.metadata", "Processing metadata for preview with id:" + id); @@ -203,7 +206,6 @@ public ResponseEntity previewProcessSearchAndReplace( false, processingReport); if (record != null) { if (diffType != null) { - IMetadataUtils metadataUtils = serviceContext.getBean(IMetadataUtils.class); AbstractMetadata metadata = metadataUtils.findOne(id); preview.addContent( Diff.diff(metadata.getData(), Xml.getString(record), diffType)); @@ -393,7 +395,7 @@ public void process(String catalogueId) throws Exception { DataManager dataMan = context.getBean(DataManager.class); ApplicationContext appContext = ApplicationContextHolder.get(); for (String uuid : this.records) { - String id = getDataManager().getMetadataId(uuid); + String id = String.valueOf(context.getBean(IMetadataUtils.class).findOneByUuid(uuid).getId()); Log.info("org.fao.geonet.services.metadata", "Processing metadata with id:" + id); diff --git a/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessUtils.java b/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessUtils.java index 1387a2c8e87..70808011c86 100644 --- a/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessUtils.java +++ b/services/src/main/java/org/fao/geonet/api/processing/DatabaseProcessUtils.java @@ -38,7 +38,6 @@ import org.fao.geonet.kernel.search.IndexingMode; import org.fao.geonet.kernel.setting.SettingManager; import org.fao.geonet.lib.Lib; -import org.fao.geonet.repository.MetadataRepository; import org.fao.geonet.repository.MetadataValidationRepository; import org.fao.geonet.utils.Xml; import org.jdom.Element; @@ -61,7 +60,6 @@ public static Element process(ServiceContext context, String id, AccessManager accessMan = context.getBean(AccessManager.class); DataManager dataMan = context.getBean(DataManager.class); IMetadataUtils metadataUtils = context.getBean(IMetadataUtils.class); - MetadataRepository metadataRepository = context.getBean(MetadataRepository.class); report.incrementProcessedRecords(); @@ -88,11 +86,11 @@ public static Element process(ServiceContext context, String id, String updatedXml = useRegexp ? (StringUtils.isNotEmpty(flags) - ? metadataRepository.selectOneWithRegexSearchAndReplaceWithFlags( + ? metadataUtils.selectOneWithRegexSearchAndReplaceWithFlags( info.getUuid(), search, replace, flags) - : metadataRepository.selectOneWithRegexSearchAndReplace( + : metadataUtils.selectOneWithRegexSearchAndReplace( info.getUuid(), search, replace)) - : metadataRepository.selectOneWithSearchAndReplace( + : metadataUtils.selectOneWithSearchAndReplace( info.getUuid(), search, replace); // Check XML is still well formed.