Skip to content

Commit

Permalink
Update db search and replace to support working copies (geonetwork#8514)
Browse files Browse the repository at this point in the history
* Update db search and replace API to allow replacements on working copies

* Remove param

* retrigger checks

* retrigger checks

* Update db search and replace preview API to allow replacements on working copies

* Update search and replace logic to use the correct repository

* Remove unused import

* Update selectOneWithSearchAndReplace to fallback to non-draft repository

* Fix calling wrong method
  • Loading branch information
tylerjmchugh authored Dec 10, 2024
1 parent c2b4558 commit cd74d3b
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,31 @@ public void setListOfStatusCreatingDraft(Set<String> listOfStatusCreatingDraft)
public Set<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -68,4 +70,51 @@ public interface MetadataDraftRepository
*/
@Nonnull
List<MetadataDraft> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ public interface MetadataRepository extends GeonetRepository<Metadata, Integer>,
@Nonnull
List<Metadata> 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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public class DatabaseProcessApi {
@Autowired
DataManager dataMan;

@Autowired
IMetadataUtils metadataUtils;

@Autowired
SchemaManager schemaMan;

Expand Down Expand Up @@ -192,7 +195,7 @@ public ResponseEntity<Object> 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);

Expand All @@ -203,7 +206,6 @@ public ResponseEntity<Object> 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));
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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.
Expand Down

0 comments on commit cd74d3b

Please sign in to comment.