Skip to content

Commit

Permalink
Delete draft resource at the end of draft util transaction. (#8100)
Browse files Browse the repository at this point in the history
* Delete draft resource at the end of draft util transaction.

* marking deprecating

* update info logs

* Update datastorages/cmis/src/main/java/org/fao/geonet/api/records/attachments/CMISStore.java

Co-authored-by: Jose García <[email protected]>

* Update datastorages/cmis/src/main/java/org/fao/geonet/api/records/attachments/CMISStore.java

Co-authored-by: Jose García <[email protected]>

* Update datastorages/cmis/src/main/java/org/fao/geonet/api/records/attachments/CMISStore.java

Co-authored-by: Jose García <[email protected]>

* Update datastorages/cmis/src/main/java/org/fao/geonet/api/records/attachments/CMISStore.java

Co-authored-by: Jose García <[email protected]>

* Update datastorages/cmis/src/main/java/org/fao/geonet/api/records/attachments/CMISStore.java

Co-authored-by: Jose García <[email protected]>

---------

Co-authored-by: Jose García <[email protected]>
  • Loading branch information
wangf1122 and josegar74 authored Jun 4, 2024
1 parent 5f448a5 commit aab6dfe
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ public String delResources(final ServiceContext context, final String metadataUu
return delResources(context, metadataUuid, true);
}

@Override
public String delResources(final ServiceContext context, final String metadataUuid, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
return delResources(context, metadataId);
}

@Override
public String delResource(final ServiceContext context, final String metadataUuid, final String resourceId) throws Exception {
return delResource(context, metadataUuid, resourceId, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ private Path getPath(ServiceContext context, int metadataId, MetadataResourceVis
}

@Override
public String delResources(ServiceContext context, String metadataUuid, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
public String delResources(ServiceContext context, int metadataId) throws Exception {
Path metadataDir = Lib.resource.getMetadataDir(getDataDirectory(context), metadataId);
try {
IO.deleteFileOrDirectory(metadataDir, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ public String delResources(ServiceContext context, String metadataUuid, Boolean
return null;
}

public String delResources(ServiceContext context, int metadataId) throws Exception {
if (decoratedStore != null) {
return decoratedStore.delResources(context, metadataId);
}
return null;
}

@Override
public String delResource(ServiceContext context, String metadataUuid, String resourceId, Boolean approved) throws Exception {
if (decoratedStore != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,24 @@ MetadataResource putResource(ServiceContext context, String metadataUuid, String
/**
* Delete all resources for a metadata
*
* @deprecated it is possible that the metadata draft was deleted during the transaction. Use
* String delResources(ServiceContext context, int metadataId) throws Exception; instead.
*
* @param context
* @param metadataUuid The metadata UUID
* @param approved Return the approved version or not
*/
@Deprecated
String delResources(ServiceContext context, String metadataUuid, Boolean approved) throws Exception;

/**
* Delete all resources for a metadata
*
* @param context
* @param metadataId The metadata ID
*/
String delResources(ServiceContext context, int metadataId) throws Exception;

/**
* Delete a resource from the metadata store
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ public MetadataResource patchResourceStatus(final ServiceContext context, final
}

@Override
public String delResources(final ServiceContext context, final String metadataUuid, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
public String delResources(final ServiceContext context, final int metadataId) throws Exception {
String folderKey = null;
try {
folderKey = getMetadataDir(context, metadataId);
Expand All @@ -396,24 +395,24 @@ public String delResources(final ServiceContext context, final String metadataUu
cmisUtils.invalidateFolderCache(folderKey);

Log.info(Geonet.RESOURCES,
String.format("Metadata '%s(%s)' directory '%s' removed.", metadataUuid, metadataId, folderKey));
return String.format("Metadata '%s(%s)' directory '%s' removed.", metadataUuid, metadataId, folderKey);
String.format("Metadata '%d' directory '%s' removed.", metadataId, folderKey));
return String.format("Metadata '%d' directory '%s' removed.", metadataId, folderKey);
} catch (CmisObjectNotFoundException e) {
Log.warning(Geonet.RESOURCES,
String.format("Unable to located metadata '%s(%s)' directory '%s' to be removed.", metadataUuid, metadataId, folderKey));
return String.format("Unable to located metadata '%s(%s)' directory '%s' to be removed.", metadataUuid, metadataId, folderKey);
String.format("Unable to located metadata '%d' directory '%s' to be removed.", metadataId, folderKey));
return String.format("Unable to located metadata '%d' directory '%s' to be removed.", metadataId, folderKey);
} catch (ResourceNotFoundException e) {
Log.warning(Geonet.RESOURCES,
String.format("Unable to located metadata '%s(%s)' directory '%s' to be removed.", metadataUuid, metadataId, folderKey));
return String.format("Unable to located metadata '%s(%s)' directory '%s' to be removed.", metadataUuid, metadataId, folderKey);
String.format("Unable to located metadata '%d' directory '%s' to be removed.", metadataId, folderKey));
return String.format("Unable to located metadata '%d' directory '%s' to be removed.", metadataId, folderKey);
} catch (CmisPermissionDeniedException e) {
Log.warning(Geonet.RESOURCES,
String.format("Insufficient privileges, unable to remove metadata '%s(%s)' directory '%s'.", metadataUuid, metadataId, folderKey));
return String.format("Insufficient privileges, unable to remove metadata '%s(%s)' directory '%s'.", metadataUuid, metadataId, folderKey);
String.format("Insufficient privileges, unable to remove metadata '%d' directory '%s'.", metadataId, folderKey));
return String.format("Insufficient privileges, unable to remove metadata '%d' directory '%s'.", metadataId, folderKey);
} catch (CmisConstraintException e) {
Log.warning(Geonet.RESOURCES,
String.format("Unable to remove metadata '%s(%s)' directory '%s' due so constraint violation or locks.", metadataUuid, metadataId, folderKey));
return String.format("Unable to remove metadata '%s(%s)' directory '%s' due so constraint violation or locks.", metadataUuid, metadataId, folderKey);
String.format("Unable to remove metadata '%d' directory '%s' due so constraint violation or locks.", metadataId, folderKey));
return String.format("Unable to remove metadata '%d' directory '%s' due so constraint violation or locks.", metadataId, folderKey);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,7 @@ public MetadataResource patchResourceStatus(final ServiceContext context, final
}

@Override
public String delResources(final ServiceContext context, final String metadataUuid, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
public String delResources(final ServiceContext context, final int metadataId) throws Exception {
try {
ListContainerOptions opts = new ListContainerOptions();
opts.prefix(getMetadataDir(context, metadataId) + jCloudConfiguration.getFolderDelimiter()).recursive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ public MetadataResource patchResourceStatus(final ServiceContext context, final
}

@Override
public String delResources(final ServiceContext context, final String metadataUuid, Boolean approved) throws Exception {
int metadataId = canEdit(context, metadataUuid, approved);
public String delResources(final ServiceContext context, final int metadataId) throws Exception {
try {
final ListObjectsV2Result objects = s3.getClient().listObjectsV2(
s3.getBucket(), getMetadataDir(metadataId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public AbstractMetadata replaceMetadataWithDraft(AbstractMetadata md, AbstractMe
Element xmlData = draft.getXmlData(false);
String changeDate = draft.getDataInfo().getChangeDate().getDateAndTime();

store.delResources(context, draft.getUuid(), false);
removeDraft((MetadataDraft) draft);

// Copy contents
Expand All @@ -155,8 +154,10 @@ public AbstractMetadata replaceMetadataWithDraft(AbstractMetadata md, AbstractMe
xmlData, false, false,
context.getLanguage(), changeDate, true, IndexingMode.full);

Log.info(Geonet.DATA_MANAGER, "Record updated with draft contents: " + md.getId());
Log.info(Geonet.DATA_MANAGER, "Record '" + md.getUuid() + "(" +md.getId() +")' update with draft contents from metadata id '" + draft.getId() +"'.");

Log.info(Geonet.DATA_MANAGER, "Cleaning up draft record resources for metadata '" + draft.getUuid() + "(" +draft.getId() +")'");
store.delResources(context, draft.getId());
} catch (Exception e) {
Log.error(Geonet.DATA_MANAGER, "Error upgrading from draft record with id " + md.getId(), e);
}
Expand Down

0 comments on commit aab6dfe

Please sign in to comment.