Skip to content

Commit

Permalink
generic AbstractAssignmentRole
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Sep 10, 2024
1 parent c83e29c commit aa913d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package nl.knaw.dans.dvcli.command;

import nl.knaw.dans.dvcli.action.Pair;
import nl.knaw.dans.lib.dataverse.DatasetApi;
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.RoleAssignment;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
Expand All @@ -36,7 +34,7 @@
import java.util.Optional;

@Command
public abstract class AbstractAssignmentRole extends AbstractCmd {
public abstract class AbstractAssignmentRole<CMD extends AbstractSubcommandContainer<?>, API> extends AbstractCmd {
/* @ParentCommand
protected DatasetCmd datasetCmd;*/

Expand Down Expand Up @@ -64,44 +62,48 @@ private Optional<RoleAssignment> readFromCommandLine() {
return Optional.empty();
}

private List<Pair<String, RoleAssignmentParams>> readFromFile(DatasetCmd datasetCmd) throws IOException {
private List<Pair<String, RoleAssignmentParams<API>>> readFromFile(CMD cmd) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(commandParameter.parameterFile);
CSVParser csvParser = new CSVParser(reader, CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setHeader("PID", "ASSIGNEE", "ROLE")
.setSkipHeaderRecord(true)
.build())) {

List<Pair<String, RoleAssignmentParams>> result = new ArrayList<>();
List<Pair<String, RoleAssignmentParams<API>>> result = new ArrayList<>();

for (CSVRecord csvRecord : csvParser) {
var pid = csvRecord.get("PID");
DatasetApi datasetApi = datasetCmd.dataverseClient.dataset(pid);
API api = getItem(pid);
RoleAssignment roleAssignment = new RoleAssignment();
roleAssignment.setAssignee(csvRecord.get("ASSIGNEE"));
roleAssignment.setRole(csvRecord.get("ROLE"));

RoleAssignmentParams params = new RoleAssignmentParams(datasetApi, Optional.of(roleAssignment));
RoleAssignmentParams<API> params = new RoleAssignmentParams<>(api, Optional.of(roleAssignment));
result.add(new Pair<>(pid, params));
}

return result;
}
}

protected List<Pair<String, RoleAssignmentParams>> getRoleAssignmentParams(DatasetCmd datasetCmd) throws IOException {
protected abstract API getItem(String pid);

protected List<Pair<String, RoleAssignmentParams<API>>> getRoleAssignmentParams(CMD cmd) throws IOException {
if (commandParameter.parameterFile != null) {
return readFromFile(datasetCmd);
return readFromFile(cmd);
}
else if (commandParameter.assignment != null) {
return datasetCmd.getItems().stream()
.map(p -> new Pair<>(p.getFirst(), new RoleAssignmentParams(p.getSecond(), readFromCommandLine())))
var items = cmd.getItems();
return items.stream()
.map(p -> {
var second = new RoleAssignmentParams<API>((API) p.getSecond(), readFromCommandLine());
return new Pair<>(p.getFirst(), second);
})
.toList();
}
return List.of();
}

protected record RoleAssignmentParams(DatasetApi pid, Optional<RoleAssignment> roleAssignment) {
protected record RoleAssignmentParams<A>(A pid, Optional<RoleAssignment> roleAssignment) {
}


}
25 changes: 16 additions & 9 deletions src/main/java/nl/knaw/dans/dvcli/command/DatasetAssignRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,48 @@
*/
package nl.knaw.dans.dvcli.command;

import nl.knaw.dans.dvcli.action.ConsoleReport;
import nl.knaw.dans.dvcli.action.Pair;
import nl.knaw.dans.dvcli.action.ThrowingFunction;
import nl.knaw.dans.lib.dataverse.DatasetApi;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Command(name = "assign-role",
mixinStandardHelpOptions = true,
description = "Manage role assignments on one or more datasets.")

public class DatasetAssignRole extends AbstractAssignmentRole {
public class DatasetAssignRole extends AbstractAssignmentRole<DatasetCmd, DatasetApi> {
@ParentCommand
DatasetCmd datasetCmd;

private static class RoleAssignmentAction implements ThrowingFunction<RoleAssignmentParams, String, Exception> {
@Override
protected DatasetApi getItem(String pid) {
return datasetCmd.dataverseClient.dataset(pid);
}

private static class RoleAssignmentAction implements ThrowingFunction<RoleAssignmentParams<DatasetApi>, String, Exception> {
@Override
public String apply(RoleAssignmentParams roleAssignmentParams) throws IOException, DataverseException {
public String apply(RoleAssignmentParams<DatasetApi> roleAssignmentParams) throws IOException, DataverseException {
if (roleAssignmentParams.roleAssignment().isPresent()) {
var r = roleAssignmentParams.pid().assignRole(roleAssignmentParams.roleAssignment().get());
return r.getEnvelopeAsString();
var api = roleAssignmentParams.pid();
var assignment = roleAssignmentParams.roleAssignment().get();
return api.assignRole(assignment).getEnvelopeAsString();
}
return "There was no assignment-role to assign.";
}
}

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

}

0 comments on commit aa913d0

Please sign in to comment.