Skip to content

Commit

Permalink
DD-1605 Implement several dd-dataverse-cli dataset commands - VIII - …
Browse files Browse the repository at this point in the history
…subcommand remove for RoleAssignmnet command (collection and dataset)
  • Loading branch information
aliassheikh committed Sep 17, 2024
1 parent f34aa8a commit 84049d0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-dataverse-client-lib</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@

@Command
public abstract class AbstractAssignmentRole<CMD extends AbstractSubcommandContainer<?>, API> extends AbstractCmd {

Check warning on line 37 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L37

Added line #L37 was not covered by tests
/* @ParentCommand
protected DatasetCmd datasetCmd;*/

static class CommandParameter {
@Parameters(description = "alias and role assignee (example: @dataverseAdmin=contributor)")
@Parameters(description = "Alias and role assignee (example: @dataverseAdmin=contributor)")

Check warning on line 40 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L39-L40

Added lines #L39 - L40 were not covered by tests
String assignment = "";

@Option(names = { "-f",
"--parameter-file" }, description = "CSV file to read parameters from. The file should have a header row with columns 'PID', 'ROLE', and 'ASSIGNMENT'.")
"--parameter-file" }, description = "CSV file to read parameters from. The file should have a header row with columns 'PID', 'ASSIGNEE' and 'ROLE'.")
Path parameterFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@
import nl.knaw.dans.dvcli.action.ThrowingFunction;
import nl.knaw.dans.lib.dataverse.DataverseApi;
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.RoleAssignment;
import nl.knaw.dans.lib.dataverse.model.RoleAssignmentReadOnly;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;
import java.util.Optional;

@Command(name = "role-assignment",
mixinStandardHelpOptions = true,
description = "Manage role assignments.",
subcommands = { CollectionRoleAssignment.CollectionListRoleAssignments.class, CollectionRoleAssignment.CollectionAssignRole.class })
subcommands = { CollectionRoleAssignment.CollectionListRoleAssignments.class, CollectionRoleAssignment.CollectionAssignRole.class, CollectionRoleAssignment.CollectionDeleteRole.class })
public class CollectionRoleAssignment extends AbstractCmd {

Check warning on line 35 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L35

Added line #L35 was not covered by tests
@ParentCommand
static private CollectionCmd collectionCmd;

@Command(name = "list", mixinStandardHelpOptions = true, description = "List the role assignments of a Dataverse collection.")
@Command(name = "list",
mixinStandardHelpOptions = true,
description = "List the role assignments of a Dataverse collection.")
static class CollectionListRoleAssignments extends AbstractCmd {

Check warning on line 42 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L42

Added line #L42 was not covered by tests

@Override
Expand Down Expand Up @@ -73,6 +78,43 @@ collectionCmd.<RoleAssignmentParams<DataverseApi>> paramsBatchProcessorBuilder()
}

Check warning on line 78 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L72-L78

Added lines #L72 - L78 were not covered by tests
}

@Command(name = "remove",
mixinStandardHelpOptions = true,
description = "remove a role assignment from the specified dataverse collection")
static class CollectionDeleteRole extends AbstractAssignmentRole<CollectionCmd, DataverseApi> {

Check warning on line 84 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L84

Added line #L84 was not covered by tests

@Override
protected DataverseApi getItem(String pid) {
return collectionCmd.dataverseClient.dataverse(pid);

Check warning on line 88 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L88

Added line #L88 was not covered by tests
}

private static class RoleAssignmentAction implements ThrowingFunction<RoleAssignmentParams<DataverseApi>, String, Exception> {
@Override
public String apply(RoleAssignmentParams<DataverseApi> roleAssignmentParams) throws IOException, DataverseException {
if (roleAssignmentParams.roleAssignment().isPresent()) {
RoleAssignment roleAssignment = roleAssignmentParams.roleAssignment().get();
Optional<RoleAssignmentReadOnly> role = roleAssignmentParams.pid().listRoleAssignments().getData().stream()

Check warning on line 96 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L95-L96

Added lines #L95 - L96 were not covered by tests
.filter(r -> r.get_roleAlias().equals(roleAssignment.getRole()) && r.getAssignee().equals(roleAssignment.getAssignee())).findFirst();
if (role.isPresent()) {
var r = roleAssignmentParams.pid().deleteRoleAssignment(role.get().getId());
return r.getEnvelopeAsString();

Check warning on line 100 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L99-L100

Added lines #L99 - L100 were not covered by tests
}
}
return "There was no assignment-role to assign.";

Check warning on line 103 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L103

Added line #L103 was not covered by tests
}
}

@Override
public void doCall() throws IOException, DataverseException {
collectionCmd.<RoleAssignmentParams<DataverseApi>> paramsBatchProcessorBuilder()
.labeledItems(getRoleAssignmentParams(collectionCmd))
.action(new CollectionRoleAssignment.CollectionDeleteRole.RoleAssignmentAction())
.report(new ConsoleReport<>())
.build()
.process();
}

Check warning on line 115 in src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionRoleAssignment.java#L109-L115

Added lines #L109 - L115 were not covered by tests
}

@Override
public void doCall() throws IOException, DataverseException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@
import nl.knaw.dans.dvcli.action.ThrowingFunction;
import nl.knaw.dans.lib.dataverse.DatasetApi;
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.RoleAssignment;
import nl.knaw.dans.lib.dataverse.model.RoleAssignmentReadOnly;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;
import java.util.Optional;

@Command(name = "role-assignment",
mixinStandardHelpOptions = true,
description = "Manage role assignments.",
subcommands = { DatasetRoleAssignment.DatasetListRoleAssignments.class, DatasetRoleAssignment.DatasetAssignRole.class })
subcommands = { DatasetRoleAssignment.DatasetListRoleAssignments.class, DatasetRoleAssignment.DatasetAssignRole.class, DatasetRoleAssignment.DatasetDeleteRole.class })
public class DatasetRoleAssignment extends AbstractCmd {

Check warning on line 35 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L35

Added line #L35 was not covered by tests
@ParentCommand
static private DatasetCmd datasetCmd;

@Command(name = "list", mixinStandardHelpOptions = true, description = "List role assignments for the specified dataset.")
@Command(name = "list",
mixinStandardHelpOptions = true,
description = "List role assignments for the specified dataset.")
static class DatasetListRoleAssignments extends AbstractCmd {

Check warning on line 42 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L42

Added line #L42 was not covered by tests

@Override
Expand Down Expand Up @@ -74,6 +79,43 @@ datasetCmd.<RoleAssignmentParams<DatasetApi>> paramsBatchProcessorBuilder()

}

@Command(name = "remove",
mixinStandardHelpOptions = true,
description = "remove role assignment from specified dataset(s)")
static class DatasetDeleteRole extends AbstractAssignmentRole<DatasetCmd, DatasetApi> {

Check warning on line 85 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L85

Added line #L85 was not covered by tests

@Override
protected DatasetApi getItem(String pid) {
return datasetCmd.dataverseClient.dataset(pid);

Check warning on line 89 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L89

Added line #L89 was not covered by tests
}

private static class RoleAssignmentAction implements ThrowingFunction<RoleAssignmentParams<DatasetApi>, String, Exception> {
@Override
public String apply(RoleAssignmentParams<DatasetApi> roleAssignmentParams) throws IOException, DataverseException {
if (roleAssignmentParams.roleAssignment().isPresent()) {
RoleAssignment roleAssignment = roleAssignmentParams.roleAssignment().get();
Optional<RoleAssignmentReadOnly> role = roleAssignmentParams.pid().listRoleAssignments().getData().stream()

Check warning on line 97 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L96-L97

Added lines #L96 - L97 were not covered by tests
.filter(r -> r.get_roleAlias().equals(roleAssignment.getRole()) && r.getAssignee().equals(roleAssignment.getAssignee())).findFirst();
if (role.isPresent()) {
var r = roleAssignmentParams.pid().deleteRoleAssignment(role.get().getId());
return r.getEnvelopeAsString();

Check warning on line 101 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L100-L101

Added lines #L100 - L101 were not covered by tests
}
}
return "There was no assignment-role to assign.";

Check warning on line 104 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L104

Added line #L104 was not covered by tests
}
}

@Override
public void doCall() throws IOException, DataverseException {
datasetCmd.<RoleAssignmentParams<DatasetApi>> paramsBatchProcessorBuilder()
.labeledItems(getRoleAssignmentParams(datasetCmd))
.action(new RoleAssignmentAction())
.report(new ConsoleReport<>())
.build()
.process();
}

Check warning on line 116 in src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/DatasetRoleAssignment.java#L110-L116

Added lines #L110 - L116 were not covered by tests
}

@Override
public void doCall() throws IOException, DataverseException {

Expand Down

0 comments on commit 84049d0

Please sign in to comment.