Skip to content

Commit

Permalink
Added database configuration for truncate-notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBoon committed Aug 27, 2024
1 parent 2e7d21c commit 2faf995
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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");
}
}
75 changes: 75 additions & 0 deletions src/main/java/nl/knaw/dans/dvcli/action/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.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() );
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/nl/knaw/dans/dvcli/command/NotificationTruncate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -23,6 +25,11 @@
public class NotificationTruncate extends AbstractCmd {
// dataverse truncate-notifications {--user <uid>|--all-users } <number-of-records-to-keep>

DdDataverseDatabaseConfig dbcfg;
public NotificationTruncate(DdDataverseDatabaseConfig dbcfg) {
this.dbcfg = dbcfg;
}

@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
UserOptions users;

Expand All @@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.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";
}

0 comments on commit 2faf995

Please sign in to comment.