diff --git a/src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java b/src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java index e22b89f..c576284 100644 --- a/src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java +++ b/src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java @@ -58,6 +58,7 @@ public String getName() { public void configureCommandLine(CommandLine commandLine, DdDataverseCliConfig config) { log.debug("Building Dataverse client"); var dataverseClient = config.getDataverse().build(); + var databaseConfig = config.getDb(); commandLine.addSubcommand(new CommandLine(new CollectionCmd(dataverseClient)) .addSubcommand(new CollectionAssignRole()) .addSubcommand(new CollectionCreateDataset()) @@ -75,7 +76,7 @@ public void configureCommandLine(CommandLine commandLine, DdDataverseCliConfig c .addSubcommand(new CommandLine(new DatasetCmd(dataverseClient)) .addSubcommand(new DeleteDraft()) ) - .addSubcommand(new CommandLine(new NotificationTruncate())); + .addSubcommand(new CommandLine(new NotificationTruncate(databaseConfig))); log.debug("Configuring command line"); } } diff --git a/src/main/java/nl/knaw/dans/dvcli/action/Database.java b/src/main/java/nl/knaw/dans/dvcli/action/Database.java new file mode 100644 index 0000000..17768d9 --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvcli/action/Database.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * 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.dvcli.config.DdDataverseDatabaseConfig; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +/** + * Provides access to the Dataverse Database (Postgres). + * Some actions are not supported by the Dataverse API (yet) + * and must be done by direct access to the database. + */ +public class Database { + + public Database(DdDataverseDatabaseConfig config) { + this.host = config.getHost(); + this.database = config.getDatabase(); + this.user = config.getUser(); + this.password = config.getPassword(); + } + + Connection connection = null; + + String port = "5432"; // Fixed port for Postgres + // TODO should come from config + String host = "localhost"; + String database = "dvndb"; + String user = "dvnapp"; + String password = "secret"; + + public void connect() { + try { + if (connection == null) { + connection = DriverManager + .getConnection("jdbc:postgresql://" + host + ":" + port + "/" + database, + user, + password); + } + if (connection != null) { + System.out.println("Connected to the database!"); + } else { + System.out.println("Failed to make connection!"); + } + } catch (SQLException e) { + System.err.println( "Database error: " + e.getClass().getName() + " " + e.getMessage() ); + } + } + + public void close() { + try { + if (connection != null) { + connection.close(); + connection = null; + } + } catch (SQLException e) { + System.err.println( "Database error: " + e.getClass().getName() + " " + e.getMessage() ); + } + } +} diff --git a/src/main/java/nl/knaw/dans/dvcli/command/NotificationTruncate.java b/src/main/java/nl/knaw/dans/dvcli/command/NotificationTruncate.java index 0f395d3..ccadceb 100644 --- a/src/main/java/nl/knaw/dans/dvcli/command/NotificationTruncate.java +++ b/src/main/java/nl/knaw/dans/dvcli/command/NotificationTruncate.java @@ -15,6 +15,8 @@ */ package nl.knaw.dans.dvcli.command; +import nl.knaw.dans.dvcli.action.Database; +import nl.knaw.dans.dvcli.config.DdDataverseDatabaseConfig; import picocli.CommandLine; @CommandLine.Command(name = "truncate-notifications", @@ -23,6 +25,11 @@ public class NotificationTruncate extends AbstractCmd { // dataverse truncate-notifications {--user |--all-users } + DdDataverseDatabaseConfig dbcfg; + public NotificationTruncate(DdDataverseDatabaseConfig dbcfg) { + this.dbcfg = dbcfg; + } + @CommandLine.ArgGroup(exclusive = true, multiplicity = "1") UserOptions users; @@ -43,6 +50,17 @@ public void doCall() { System.out.println("Number of records to keep: " + numberOfRecordsToKeep); System.out.println("User: " + (users.allUser ? "all users" : users.user)); + // show database config + System.out.println("Database config - host: " + dbcfg.getHost()); + System.out.println("Database config - database: " + dbcfg.getDatabase()); + System.out.println("Database config - user: " + dbcfg.getUser()); + System.out.println("Database config - password: " + dbcfg.getPassword()); + + // Connect to database + Database db = new Database(dbcfg); + //db.connect(); + // do someting with the database + //db.close(); throw new UnsupportedOperationException("Not yet implemented."); } } diff --git a/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseCliConfig.java b/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseCliConfig.java index 3c31fee..1b668fd 100644 --- a/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseCliConfig.java +++ b/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseCliConfig.java @@ -19,10 +19,14 @@ import io.dropwizard.core.Configuration; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NonNull; import nl.knaw.dans.lib.util.DataverseClientFactory; @Data @EqualsAndHashCode(callSuper = true) public class DdDataverseCliConfig extends Configuration { private DataverseClientFactory dataverse; + + @NonNull + private DdDataverseDatabaseConfig db = new DdDataverseDatabaseConfig(); } diff --git a/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseDatabaseConfig.java b/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseDatabaseConfig.java new file mode 100644 index 0000000..e0cfa9d --- /dev/null +++ b/src/main/java/nl/knaw/dans/dvcli/config/DdDataverseDatabaseConfig.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) + * + * 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.config; + +import lombok.Data; + +import javax.validation.constraints.NotEmpty; + +@Data +public class DdDataverseDatabaseConfig { + + @NotEmpty + private String host = "localhost"; + + @NotEmpty + private String database = "dvndb"; + + @NotEmpty + private String user = "dvnapp"; + + @NotEmpty + private String password = "secret"; +} +