Skip to content

Commit

Permalink
GeoNetwork harvester / Check if a resource exists to save it, instead…
Browse files Browse the repository at this point in the history
… of trying to retrieve the file details, to avoid confusing NoSuchFileException exception (#7577)

* GeoNetwork harvester / Check if a resource exists to save it, instead of trying to retrieve the file details, to avoid confusing NoSuchFileException exception

* GeoNetwork harvester / Optimise save file check
  • Loading branch information
josegar74 authored Mar 7, 2024
1 parent 43e127f commit ae54e18
Showing 1 changed file with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.fao.geonet.GeonetContext;
import org.fao.geonet.Logger;
import org.fao.geonet.MetadataResourceDatabaseMigration;
import org.fao.geonet.api.exception.ResourceNotFoundException;
import org.fao.geonet.api.records.attachments.Store;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.domain.*;
Expand Down Expand Up @@ -77,9 +78,9 @@ public class Aligner extends BaseAligner<GeonetParams> {
private UUIDMapper localUuids;
private String processName;
private String preferredSchema;
private Map<String, Object> processParams = new HashMap<String, Object>();
private Map<String, Object> processParams = new HashMap<>();
private MetadataRepository metadataRepository;
private Map<String, Map<String, String>> hmRemoteGroups = new HashMap<String, Map<String, String>>();
private Map<String, Map<String, String>> hmRemoteGroups = new HashMap<>();
private SettingManager settingManager;

public Aligner(AtomicBoolean cancelMonitor, Logger log, ServiceContext context, XmlRequest req,
Expand Down Expand Up @@ -119,7 +120,7 @@ private void setupLocEntity(List<Element> list, Map<String, Map<String, String>>
for (Element entity : list) {
String name = entity.getChildText("name");

Map<String, String> hm = new HashMap<String, String>();
Map<String, String> hm = new HashMap<>();
hmEntity.put(name, hm);

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -163,7 +164,7 @@ public HarvestResult align(SortedSet<RecordInfo> records, List<HarvestError> err

result.locallyRemoved++;
}
} catch (Throwable t) {
} catch (Exception t) {
log.error("Couldn't remove metadata with uuid " + uuid);
log.error(t);
result.unchangedMetadata++;
Expand Down Expand Up @@ -197,7 +198,6 @@ public HarvestResult align(SortedSet<RecordInfo> records, List<HarvestError> err
String id = dataMan.getMetadataId(ri.uuid);

// look up value of localrating/enable
SettingManager settingManager = context.getBean(SettingManager.class);
String localRating = settingManager.getValue(Settings.SYSTEM_LOCALRATING_ENABLE);

if (id == null) {
Expand Down Expand Up @@ -230,6 +230,7 @@ public HarvestResult align(SortedSet<RecordInfo> records, List<HarvestError> err
case SKIP:
log.debug("Skipping record with uuid " + ri.uuid);
result.uuidSkipped++;
break;
default:
break;
}
Expand All @@ -248,7 +249,7 @@ public HarvestResult align(SortedSet<RecordInfo> records, List<HarvestError> err
}

}
} catch (Throwable t) {
} catch (Exception t) {
log.error("Couldn't insert or update metadata with uuid " + ri.uuid);
log.error(t);
result.unchangedMetadata++;
Expand Down Expand Up @@ -282,7 +283,7 @@ private Element extractValidMetadataForImport(DirectoryStream<Path> files, Eleme
Log.debug(Geonet.MEF, "Multiple metadata files");

Map<String, Pair<String, Element>> mdFiles =
new HashMap<String, Pair<String, Element>>();
new HashMap<>();
for (Path file : files) {
if (Files.isRegularFile(file)) {
Element metadata = Xml.loadFile(file);
Expand Down Expand Up @@ -353,8 +354,8 @@ private Element extractValidMetadataForImport(DirectoryStream<Path> files, Eleme
}

private void addMetadata(final RecordInfo ri, final boolean localRating, String uuid) throws Exception {
final String id[] = {null};
final Element md[] = {null};
final String[] id = {null};
final Element[] md = {null};

//--- import metadata from MEF file

Expand Down Expand Up @@ -595,13 +596,13 @@ private void addPrivilegesFromGroupPolicy(String id, Element privil) throws Exce
}

private Map<String, Set<String>> buildPrivileges(Element privil) {
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
Map<String, Set<String>> map = new HashMap<>();

for (Object o : privil.getChildren("group")) {
Element group = (Element) o;
String name = group.getAttributeValue("name");

Set<String> set = new HashSet<String>();
Set<String> set = new HashSet<>();
map.put(name, set);

for (Object op : group.getChildren("operation")) {
Expand Down Expand Up @@ -662,9 +663,9 @@ private String createGroup(String name) throws Exception {
*/
private void updateMetadata(final RecordInfo ri, final String id, final boolean localRating,
final boolean useChangeDate, String localChangeDate, Boolean force) throws Exception {
final Element md[] = {null};
final Element publicFiles[] = {null};
final Element privateFiles[] = {null};
final Element[] md = {null};
final Element[] publicFiles = {null};
final Element[] privateFiles = {null};

if (localUuids.getID(ri.uuid) == null && !force) {
if (log.isDebugEnabled())
Expand Down Expand Up @@ -756,7 +757,6 @@ private void updateMetadata(RecordInfo ri, String id, Element md,
return;
}

final IMetadataManager metadataManager = context.getBean(IMetadataManager.class);
Metadata metadata;
if (!force && !ri.isMoreRecentThan(date)) {
if (log.isDebugEnabled())
Expand Down Expand Up @@ -883,12 +883,18 @@ private void saveFile(final Store store, String metadataUuid, String file,
ISODate remIsoDate = new ISODate(changeDate);
boolean saveFile;

final MetadataResource description = store.getResourceDescription(context, metadataUuid, visibility, file, true);
if (description == null) {
saveFile = true;
} else {
ISODate locIsoDate = new ISODate(description.getLastModification().getTime(), false);
Store.ResourceHolder resourceHolder;
try {
resourceHolder = store.getResource(context, metadataUuid, visibility, file, true);
} catch (ResourceNotFoundException ex) {
resourceHolder = null;
}

if ((resourceHolder != null) && (resourceHolder.getMetadata() != null)) {
ISODate locIsoDate = new ISODate(resourceHolder.getMetadata().getLastModification().getTime(), false);
saveFile = (remIsoDate.timeDifferenceInSeconds(locIsoDate) > 0);
} else {
saveFile = true;
}

if (saveFile) {
Expand Down

0 comments on commit ae54e18

Please sign in to comment.