Skip to content

Commit

Permalink
Closes #2505: Allow to move multiple datasets at once
Browse files Browse the repository at this point in the history
  • Loading branch information
diasf committed Sep 4, 2024
1 parent d49edbe commit 7a5cb20
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2973,6 +2973,7 @@ dashboard.datamove.confirm.dialog=Are you sure you want to move this dataset?
dashboard.datamove.confirm.yes=Yes, Move Dataset
dashboard.datamove.confirm.force=Force Move
dashboard.datamove.dataset.message.success=The dataset "{0}" ({1}) has been successfully moved to "{2}" dataverse.
dashboard.datamove.dataset.message.success.multiple=The datasets ({0}) have been successfully moved to "{1}" dataverse.<br>
dashboard.datamove.dataset.message.failure.summary=Failed to move dataset.
dashboard.datamove.dataset.message.failure.details=The dataset "{0}" ({1}) could not be moved to "{2}" dataverse.<br>{3}<br>{4}<br>
dashboard.datamove.dataverse.placeholder=Enter Dataverse Identifier\u2026
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2926,6 +2926,7 @@ dashboard.datamove.confirm.yes=Tak, przenie\u015B zbi\u00F3r danych
dashboard.datamove.confirm.no=Nie, anuluj
dashboard.datamove.confirm.force=Wymu\u015B przeniesienie
dashboard.datamove.dataset.message.success=Zbi\u00F3r danych "{0}" ({1}) zosta\u0142 pomy\u015Blnie przeniesiony do kolekcji "{2}".
dashboard.datamove.dataset.message.success.multiple=Zbior\u00F3w danych ({0}) zosta\u0142y pomy\u015Blnie przeniesiony do kolekcji "{1}".<br>
dashboard.datamove.dataset.message.failure.summary=Przeniesienie zbioru danych nie powiod\u0142o si\u0119.
dashboard.datamove.dataset.message.failure.details=Zbi\u00F3r danych "{0}" ({1}) nie m\u00F3g\u0142 zosta\u0107 przeniesiony do kolekcji "{2}". <br>{3}<br>{4}<br>
dashboard.datamove.dataverse.placeholder=Wprowad\u017A identyfikator kolekcji\u2026
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,30 @@ public class DashboardDatamovePage implements Serializable {
private boolean forceMove = false;

// Following two are for dataset move
private Dataset sourceDataset;
private List<Dataset> sourceDatasets;

private Dataverse targetDataverse;

// And following two for dataverse move

private Dataverse source;

private Dataverse target;
private Dataverse sourceDataverse;

// -------------------- GETTERS --------------------

public boolean isForceMove() {
return forceMove;
}

public Dataset getSourceDataset() {
return sourceDataset;
public List<Dataset> getSourceDatasets() {
return sourceDatasets;
}

public Dataverse getTargetDataverse() {
return targetDataverse;
}

public Dataverse getSource() {
return source;
}

public Dataverse getTarget() {
return target;
public Dataverse getSourceDataverse() {
return sourceDataverse;
}

// -------------------- LOGIC --------------------
Expand All @@ -108,6 +102,7 @@ public String init() {
if (!user.isSuperuser() || systemConfig.isReadonlyMode()) {
return permissionsWrapper.notAuthorized();
}
sourceDatasets = new ArrayList<>();
return StringUtils.EMPTY;
}

Expand All @@ -124,48 +119,53 @@ public List<Dataverse> completeDataverse(String query) {
}

public void moveDataset() {
if (sourceDataset == null || targetDataverse == null) {
if (sourceDatasets == null || sourceDatasets.isEmpty() || targetDataverse == null) {
// We should never get here, but in case of some unexpected failure we should be prepared nevertheless
JsfHelper.addErrorMessage(getStringFromBundle("dashboard.datamove.empty.fields"));
return;
}

Summary summary = new Summary(Summary.Mode.DATASET)
.addParameter(sourceDataset.getDisplayName())
.addParameter(extractSourcePersistentId())
.addParameter(targetDataverse.getName());

try {
DataverseRequest dataverseRequest = requestService.getDataverseRequest();
String previousSourceAlias = extractSourceAlias();
commandEngine.submit(new MoveDatasetCommand(dataverseRequest, sourceDataset, targetDataverse, forceMove));
logger.info(createMessageWithDatasetMoveInfo("Moved", previousSourceAlias));
resetDatasetMoveFields();
summary.showSuccessMessage();
} catch (MoveException me) {
logger.log(Level.WARNING, createMessageWithDatasetMoveInfo("Unable to move"), me);
summary.addParameter(me)
.addParameter(createForceInfoIfApplicable(me))
.showFailureMessage();
} catch (CommandException ce) {
logger.log(Level.WARNING, createMessageWithDatasetMoveInfo("Unable to move"), ce);
JsfHelper.addErrorMessage(getStringFromBundle("dashboard.datamove.dataset.message.failure.summary"), StringUtils.EMPTY);
List<String> successfulIds = new ArrayList<>();
List<String> failureMessages = new ArrayList<>();
for (Dataset source : sourceDatasets) {
Summary summary = new Summary(Summary.Mode.DATASET);
try {
summary.addParameter(source.getDisplayName())
.addParameter(extractSourcePersistentId(source))
.addParameter(targetDataverse.getName());

DataverseRequest dataverseRequest = requestService.getDataverseRequest();
String previousSourceAlias = extractSourceAlias(source);
commandEngine.submit(new MoveDatasetCommand(dataverseRequest, source, targetDataverse, forceMove));
logger.info(createMessageWithDatasetMoveInfo(source, "Moved", previousSourceAlias));
successfulIds.add(extractSourcePersistentId(source));
} catch (MoveException me) {
logger.log(Level.WARNING, createMessageWithDatasetMoveInfo(source, "Unable to move"), me);
summary.addParameter(me).addParameter(createForceInfoIfApplicable(me));
failureMessages.add(summary.getFailureMessageDetail());
} catch (CommandException ce) {
logger.log(Level.WARNING, createMessageWithDatasetMoveInfo(source, "Unable to move"), ce);
summary.addParameter(getStringFromBundle("dashboard.datamove.dataset.message.failure.summary"));
failureMessages.add(summary.getFailureMessageDetail());
}
}

showDatasetsMovedMessage(successfulIds, failureMessages, targetDataverse.getName());
}

public void moveDataverse() {
if (source == null || target == null) {
if (sourceDataverse == null || targetDataverse == null) {
JsfHelper.addErrorMessage(getStringFromBundle("dashboard.datamove.empty.fields"));
return;
}

Summary summary = new Summary(Summary.Mode.DATAVERSE)
.addParameter(extractDataverseAlias(source))
.addParameter(extractDataverseAlias(target));
.addParameter(extractDataverseAlias(sourceDataverse))
.addParameter(extractDataverseAlias(targetDataverse));

try {
DataverseRequest dataverseRequest = requestService.getDataverseRequest();
commandEngine.submit(new MoveDataverseCommand(dataverseRequest, source, target, forceMove));
commandEngine.submit(new MoveDataverseCommand(dataverseRequest, sourceDataverse, targetDataverse, forceMove));
logger.info(createMessageWithDataverseMoveInfo("Moved"));
resetDataverseMoveFields();
summary.showSuccessMessage();
Expand All @@ -186,38 +186,56 @@ public String getMessageDetails() {

// -------------------- PRIVATE --------------------

private String extractSourcePersistentId() {
return Optional.ofNullable(sourceDataset)
private static void showDatasetsMovedMessage(List<String> successfulIds, List<String> failureMessages, String dataverseName) {
StringBuilder sb = new StringBuilder();

if (!successfulIds.isEmpty()) {
sb.append(getStringFromBundle("dashboard.datamove.dataset.message.success.multiple",
StringUtils.join(successfulIds, ", "), dataverseName));
}

if (!failureMessages.isEmpty()) {
if (!successfulIds.isEmpty()) {
sb.append("<hr>");
}
sb.append(String.join("<hr>", failureMessages));
}

JsfHelper.addInfoMessage(sb.toString().replaceAll("<br><br>", "<br>"));
}

private static String extractSourcePersistentId(Dataset source) {
return Optional.ofNullable(source)
.map(Dataset::getGlobalId)
.map(GlobalId::asString)
.orElse(StringUtils.EMPTY);
}

private String extractSourceAlias() {
return Optional.ofNullable(sourceDataset)
private static String extractSourceAlias(Dataset source) {
return Optional.ofNullable(source)
.map(Dataset::getOwner)
.map(Dataverse::getAlias)
.orElse(StringUtils.EMPTY);
}

private String extractDataverseAlias(Dataverse dataverse) {
private static String extractDataverseAlias(Dataverse dataverse) {
return Optional.ofNullable(dataverse)
.map(Dataverse::getAlias)
.orElse(StringUtils.EMPTY);
}

private String createMessageWithDatasetMoveInfo(String message, String source) {
private String createMessageWithDatasetMoveInfo(Dataset sourceDs, String message, String source) {
return String.format("%s %s from %s to %s",
message, extractSourcePersistentId(), source, extractDataverseAlias(targetDataverse));
message, extractSourcePersistentId(sourceDs), source, extractDataverseAlias(targetDataverse));
}

private String createMessageWithDatasetMoveInfo(String message) {
return createMessageWithDatasetMoveInfo(message, extractSourceAlias());
private String createMessageWithDatasetMoveInfo(Dataset sourceDs, String message) {
return createMessageWithDatasetMoveInfo(sourceDs, message, extractSourceAlias(sourceDs));
}

private String createMessageWithDataverseMoveInfo(String message) {
return String.format("%s %s to %s",
message, extractDataverseAlias(source), extractDataverseAlias(target));
message, extractDataverseAlias(sourceDataverse), extractDataverseAlias(targetDataverse));
}

private String createForceInfoIfApplicable(MoveException mde) {
Expand All @@ -233,13 +251,13 @@ private boolean isForcingPossible(MoveException mde) {
}

private void resetDatasetMoveFields() {
sourceDataset = null;
sourceDatasets = new ArrayList<>();
targetDataverse = null;
}

private void resetDataverseMoveFields() {
source = null;
target = null;
sourceDataverse = null;
targetDataverse = null;
}

// -------------------- SETTERS --------------------
Expand All @@ -248,20 +266,16 @@ public void setForceMove(boolean forceMove) {
this.forceMove = forceMove;
}

public void setSourceDataset(Dataset sourceDataset) {
this.sourceDataset = sourceDataset;
public void setSourceDatasets(List<Dataset> sourceDatasets) {
this.sourceDatasets = sourceDatasets;
}

public void setTargetDataverse(Dataverse targetDataverse) {
this.targetDataverse = targetDataverse;
}

public void setSource(Dataverse source) {
this.source = source;
}

public void setTarget(Dataverse target) {
this.target = target;
public void setSourceDataverse(Dataverse sourceDataverse) {
this.sourceDataverse = sourceDataverse;
}

// -------------------- INNER CLASSES ---------------------
Expand Down Expand Up @@ -311,10 +325,14 @@ public void showSuccessMessage() {
JsfHelper.addFlashSuccessMessage(getStringFromBundle(buildKey("message.success"), summaryParameters.toArray()));
}

public String getFailureMessageDetail() {
return getStringFromBundle(buildKey("message.failure.details"), summaryParameters.toArray());
}

public void showFailureMessage() {
JsfHelper.addErrorMessage(
getStringFromBundle(buildKey("message.failure.summary")),
getStringFromBundle(buildKey("message.failure.details"), summaryParameters.toArray()));
getFailureMessageDetail());
}

// -------------------- PRIVATE --------------------
Expand Down
8 changes: 4 additions & 4 deletions dataverse-webapp/src/main/webapp/dashboard-datamove.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
required="true"
requiredMessage="#{bundle['requiredField']}"
placeholder="#{bundle['dashboard.datamove.dataset.placeholder']}"
multiple="false" scrollHeight="180"
multiple="true" unique="true" scrollHeight="180"
forceSelection="true"
minQueryLength="10" queryDelay="1000" size="36"
emptyMessage="#{bundle['dashboard.datamove.dataset.menu.invalidMsg']}"
value="#{DashboardDatamovePage.sourceDataset}"
value="#{DashboardDatamovePage.sourceDatasets}"
completeMethod="#{DashboardDatamovePage.completeSourceDataset}"
var="srcDS" styleClass="DropdownPopup"
panelStyleClass="DropdownPopupPanel"
Expand Down Expand Up @@ -199,7 +199,7 @@
forceSelection="true"
minQueryLength="2" queryDelay="1000" size="36"
emptyMessage="#{bundle['dashboard.datamove.dataverse.menu.invalidMsg']}"
value="#{DashboardDatamovePage.source}"
value="#{DashboardDatamovePage.sourceDataverse}"
completeMethod="#{DashboardDatamovePage.completeDataverse}"
var="srcDV" styleClass="DropdownPopup"
panelStyleClass="DropdownPopupPanel"
Expand Down Expand Up @@ -249,7 +249,7 @@
forceSelection="true"
minQueryLength="2" queryDelay="1000" size="36"
emptyMessage="#{bundle['dashboard.datamove.dataverse.menu.invalidMsg']}"
value="#{DashboardDatamovePage.target}"
value="#{DashboardDatamovePage.targetDataverse}"
completeMethod="#{DashboardDatamovePage.completeDataverse}"
var="targetDV" styleClass="DropdownPopup"
panelStyleClass="DropdownPopupPanel"
Expand Down

0 comments on commit 7a5cb20

Please sign in to comment.