Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Jun 24, 2024
1 parent 155959a commit 6d79896
Show file tree
Hide file tree
Showing 27 changed files with 497 additions and 127 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-dataverse-client-lib</artifactId>
<version>0.29.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
</dependency>
</dependencies>

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package nl.knaw.dans.dvcli;

import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvcli.command.CollectionAssignRole;
import nl.knaw.dans.dvcli.command.CollectionAssignRole2;
import nl.knaw.dans.dvcli.command.CollectionCmd;
import nl.knaw.dans.dvcli.command.CollectionCmd2;
import nl.knaw.dans.dvcli.command.CollectionCreateDataset;
import nl.knaw.dans.dvcli.command.CollectionDelete;
import nl.knaw.dans.dvcli.command.CollectionGetContents;
Expand Down Expand Up @@ -59,8 +60,6 @@ public void configureCommandLine(CommandLine commandLine, DdDataverseCliConfig c
var dataverseClient = config.getDataverse().build();

commandLine.addSubcommand(new CommandLine(new CollectionCmd(dataverseClient))
.addSubcommand(new CollectionAssignRole())
.addSubcommand(new CollectionCreateDataset())
.addSubcommand(new CollectionDelete())
.addSubcommand(new CollectionGetContents())
.addSubcommand(new CollectionGetStorageSize())
Expand All @@ -74,7 +73,9 @@ public void configureCommandLine(CommandLine commandLine, DdDataverseCliConfig c
.addSubcommand(new CollectionView())
).addSubcommand(new CommandLine(new DatasetCmd(dataverseClient))
.addSubcommand(new DeleteDraft())
);
).addSubcommand(new CommandLine(new CollectionCmd2(dataverseClient))
.addSubcommand(new CollectionCreateDataset())
.addSubcommand(new CollectionAssignRole2()));
log.debug("Configuring command line");

}
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/nl/knaw/dans/dvcli/action/BatchProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class BatchProcessor<I, R> {
/**
* The labeled items to process.
*/
private final Iterable<Pair<String, I>> labeledItems;
private final List<Pair<String, I>> labeledItems;

/**
* The action to apply to each item.
Expand All @@ -57,7 +57,7 @@ public void process() {
int i = 0;
for (var labeledItem : labeledItems) {
delayIfNeeded(i);
logStartAction(++i);
log.info("Processing item {} of {}", ++i, labeledItems.size());
callAction(labeledItem.getFirst(), labeledItem.getSecond());
}
log.info("Finished batch processing");
Expand Down Expand Up @@ -86,12 +86,4 @@ private void delayIfNeeded(int i) {
}
}

private void logStartAction(int i) {
if (labeledItems instanceof List) {
log.info("Processing item {} of {}", i, ((List<?>) labeledItems).size());
}
else {
log.info("Processing item {}", i);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/nl/knaw/dans/dvcli/action/ConsoleReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public class ConsoleReport<I, R> implements Report<I, R> {

@Override
public void reportSuccess(String label, I i, R r) {
System.err.println(label + ": OK");
System.err.println(label + ": OK. " + r);
}

@Override
public void reportFailure(String label, I i, Exception e) {
System.err.println(label + ": FAILED: " + e.getMessage());
System.err.println(label + ": FAILED: Exception type = " + e.getClass().getSimpleName() + ", message = " + e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.dvcli.action;

import nl.knaw.dans.lib.dataverse.DataverseApi;
import nl.knaw.dans.lib.dataverse.DataverseClient;

import java.io.IOException;
import java.util.stream.Stream;

public class SingleCollectionOrCollectionsFile {
private final SingleIdOrIdsFile singleIdOrIdsFile;
private final DataverseClient dataverseClient;

public SingleCollectionOrCollectionsFile(String singleCollectionOrCollectionsFile, DataverseClient dataverseClient) {
this.singleIdOrIdsFile = new SingleIdOrIdsFile(singleCollectionOrCollectionsFile, "root");
this.dataverseClient = dataverseClient;
}

public Stream<Pair<String, DataverseApi>> getCollections() throws IOException {
return singleIdOrIdsFile.getPids().map(alias -> new Pair<>(alias, dataverseClient.dataverse(alias)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class SingleDatasetOrDatasetsFile {
private final DataverseClient dataverseClient;

public SingleDatasetOrDatasetsFile(String singleDatasetOrDatasetsFile, DataverseClient dataverseClient) {
this.singleIdOrIdsFile = new SingleIdOrIdsFile(singleDatasetOrDatasetsFile);
this.singleIdOrIdsFile = new SingleIdOrIdsFile(singleDatasetOrDatasetsFile, "-");
this.dataverseClient = dataverseClient;
}

Expand Down
33 changes: 29 additions & 4 deletions src/main/java/nl/knaw/dans/dvcli/action/SingleIdOrIdsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,45 @@

import lombok.AllArgsConstructor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;

@AllArgsConstructor
public class SingleIdOrIdsFile {
public static final String DEFAULT_TARGET_PLACEHOLDER = "__DEFAULT_TARGET_PLACEHOLDER__";

private final String singleIdOrIdFile;
private final String defaultId;

@SuppressWarnings("resource")
public Stream<String> getPids() throws IOException {
var pidFile = Paths.get(singleIdOrIdFile);
if (Files.exists(pidFile)) {
return Files.readAllLines(pidFile).stream().map(String::trim);
if (singleIdOrIdFile.equals(DEFAULT_TARGET_PLACEHOLDER)) {
return Stream.of(defaultId);
}

Stream<String> lines = null;

if ("-".equals(singleIdOrIdFile)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
lines = reader.lines();
}
else {
var pidFile = Paths.get(singleIdOrIdFile);
if (Files.exists(pidFile)) {
lines = Files.lines(pidFile)
.flatMap(line -> Arrays.stream(line.trim().split("\\s+")));
}
else {
lines = Stream.of(singleIdOrIdFile);
}

}
return Stream.of(singleIdOrIdFile);
// Split lines further by whitespace
return lines.flatMap(line -> Arrays.stream(line.trim().split("\\s+")));
}
}
2 changes: 0 additions & 2 deletions src/main/java/nl/knaw/dans/dvcli/command/AbstractCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package nl.knaw.dans.dvcli.command;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import nl.knaw.dans.lib.dataverse.DataverseClient;
import nl.knaw.dans.lib.dataverse.DataverseException;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

public abstract class AbstractSubcommandContainer extends AbstractCmd {
protected DataverseClient dataverseClient;

public AbstractSubcommandContainer(@NonNull DataverseClient dataverseClient) {
this.dataverseClient = dataverseClient;
}


@Override
public void doCall() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.dvcli.command;

import lombok.NonNull;
import nl.knaw.dans.dvcli.action.BatchProcessor;
import nl.knaw.dans.dvcli.action.Pair;
import nl.knaw.dans.dvcli.action.SingleIdOrIdsFile;
import nl.knaw.dans.lib.dataverse.DataverseClient;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

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

public abstract class AbstractSubcommandContainer2<T> extends AbstractCmd {
private static final long DEFAULT_DELAY = 1000;

protected DataverseClient dataverseClient;

public AbstractSubcommandContainer2(@NonNull DataverseClient dataverseClient) {
this.dataverseClient = dataverseClient;
}

@Parameters(index = "0", description = "The target(s) of the operation; this is either an ID a file with a with a list of IDs, or - if the subcommand supports it - a parameters file.",
paramLabel = "targets", defaultValue = SingleIdOrIdsFile.DEFAULT_TARGET_PLACEHOLDER)

protected String targets;

@Option(names = { "-d", "--delay" }, description = "Delay in milliseconds between requests to the server (default: ${DEFAULT-VALUE}).", defaultValue = "" + DEFAULT_DELAY)
protected long delay;

protected BatchProcessor.BatchProcessorBuilder<T, String> batchProcessorBuilder() throws IOException {
return BatchProcessor.<T, String> builder()
.labeledItems(getItems())
.delay(delay);
}

protected <P> BatchProcessor.BatchProcessorBuilder<P, String> paramsBatchProcessorBuilder() throws IOException {
return BatchProcessor.<P, String> builder()
.delay(delay);
}

protected abstract List<Pair<String, T>> getItems() throws IOException;

@Override
public void doCall() {
}
}
48 changes: 0 additions & 48 deletions src/main/java/nl/knaw/dans/dvcli/command/CollectionAssignRole.java

This file was deleted.

Loading

0 comments on commit 6d79896

Please sign in to comment.