Skip to content

Commit

Permalink
add permission doc deletes, check/delete per-version perm docs via api
Browse files Browse the repository at this point in the history
  • Loading branch information
qqmyers committed Jun 3, 2024
1 parent 5f791f5 commit a7839b5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.DatasetVersion.VersionState;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.dataaccess.DataAccess;
import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter;
Expand Down Expand Up @@ -759,6 +760,13 @@ public List<DataFile> findAll() {
return em.createQuery("select object(o) from DataFile as o order by o.id", DataFile.class).getResultList();
}

public List<VersionState> findVersionStates(Long fileId) {
Query query = em.createQuery(
"select distinct dv.versionState from DatasetVersion dv where dv.id in (select fm.datasetVersion.id from FileMetadata fm where fm.dataFile.id=:fileId)");
query.setParameter("fileId", fileId);
return query.getResultList();
}

public DataFile save(DataFile dataFile) {

if (dataFile.isMergeable()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.harvard.iq.dataverse.search;

import edu.harvard.iq.dataverse.*;
import edu.harvard.iq.dataverse.DatasetVersion.VersionState;
import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean;
import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUserServiceBean;
import edu.harvard.iq.dataverse.batch.util.LoggingUtil;
Expand Down Expand Up @@ -503,7 +504,7 @@ private void doIndexDataset(Dataset dataset, boolean doNormalSolrDocCleanUp) thr
solrIdsOfFilesToDelete = findFilesOfParentDataset(dataset.getId());
logger.fine("Existing file docs: " + String.join(", ", solrIdsOfFilesToDelete));
//We keep the latest version's docs unless it is deaccessioned and there is no published/released version
//So skip the loop removing those docs from the delete list in that case
//So skip the loop removing those docs from the delete list except in that case
if ((!latestVersion.isDeaccessioned() || atLeastOnePublishedVersion)) {
List<FileMetadata> latestFileMetadatas = latestVersion.getFileMetadatas();
String suffix = (new IndexableDataset(latestVersion)).getDatasetState().getSuffix();
Expand Down Expand Up @@ -588,6 +589,10 @@ private void doIndexDataset(Dataset dataset, boolean doNormalSolrDocCleanUp) thr
debug.append("numPublishedVersions: " + numPublishedVersions + "\n");
if (doNormalSolrDocCleanUp) {
if(!solrIdsOfFilesToDelete.isEmpty()) {
for(String file: solrIdsOfFilesToDelete) {
//Also remove associated permission docs
solrIdsOfFilesToDelete.add(file+"_permission");
}
IndexResponse resultOfAttemptToPremptivelyDeletePublishedFiles = solrIndexService.deleteMultipleSolrIds(solrIdsOfFilesToDelete);
debug.append("result of attempt to premptively deleted published files before reindexing: " + resultOfAttemptToPremptivelyDeletePublishedFiles + "\n");
}
Expand Down Expand Up @@ -2088,8 +2093,48 @@ public List<String> findPermissionsInSolrOnly() throws SearchException {
SolrDocumentList list = rsp.getResults();
for (SolrDocument doc: list) {
long id = Long.parseLong((String) doc.getFieldValue(SearchFields.DEFINITION_POINT_DVOBJECT_ID));
String docId = (String)doc.getFieldValue(SearchFields.ID);
if(!dvObjectService.checkExists(id)) {
permissionInSolrOnly.add((String)doc.getFieldValue(SearchFields.ID));
permissionInSolrOnly.add(docId);
} else {
DvObject obj = dvObjectService.findDvObject(id);
if (obj instanceof Dataset d) {
DatasetVersion dv = d.getLatestVersion();
if (docId.endsWith("draft_permission")) {
if (!dv.isDraft()) {
permissionInSolrOnly.add(docId);
}
} else if (docId.endsWith("deaccessioned_permission")) {
if (!dv.isDeaccessioned()) {
permissionInSolrOnly.add(docId);
}
} else {
if (d.getReleasedVersion() != null) {
permissionInSolrOnly.add(docId);
}
}
} else if (obj instanceof DataFile f) {
List<VersionState> states = dataFileService.findVersionStates(f.getId());
Set<String> strings = states.stream().map(VersionState::toString).collect(Collectors.toSet());
logger.info("for " + docId + " states: " + String.join(", ", strings));
if (docId.endsWith("draft_permission")) {
if (!states.contains(VersionState.DRAFT)) {
permissionInSolrOnly.add(docId);
}
} else if (docId.endsWith("deaccessioned_permission")) {
if (!states.contains(VersionState.DEACCESSIONED) && states.size() == 1) {
permissionInSolrOnly.add(docId);
}
} else {
if (!states.contains(VersionState.RELEASED)) {
permissionInSolrOnly.add(docId);
} else if (!dataFileService.findMostRecentVersionFileIsIn(f).getDatasetVersion()
.equals(f.getOwner().getReleasedVersion())) {
permissionInSolrOnly.add(docId);
}

}
}
}
}
if (cursorMark.equals(nextCursorMark)) {
Expand Down

0 comments on commit a7839b5

Please sign in to comment.