Skip to content

Commit

Permalink
Merge branch 'dev' into ps/#554-HandlingNoWeatherData
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippSchmelter authored Nov 5, 2024
2 parents cd5354d + d1fcf67 commit 6723cba
Show file tree
Hide file tree
Showing 21 changed files with 1,412 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id 'signing'
id 'pmd' // code check, working on source code
id 'com.diffplug.spotless' version '6.25.0' //code format
id 'com.github.spotbugs' version '6.0.25' // code check, working on byte code
id 'com.github.spotbugs' version '6.0.26' // code check, working on byte code
id 'de.undercouch.download' version '5.6.0'
id 'kr.motd.sphinx' version '2.10.1' // documentation generation
id 'jacoco' // java code coverage plugin
Expand Down Expand Up @@ -73,7 +73,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.3'
testImplementation "org.spockframework:spock-core:2.3-groovy-$groovyVersion"
testImplementation 'org.objenesis:objenesis:3.4' // Mock creation with constructor parameters
testImplementation 'net.bytebuddy:byte-buddy:1.15.7' // Mocks of classes
testImplementation 'net.bytebuddy:byte-buddy:1.15.10' // Mocks of classes

// testcontainers (docker framework for testing)
testImplementation "org.testcontainers:testcontainers:$testcontainersVersion"
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/edu/ie3/datamodel/io/DbGridMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io;

import static edu.ie3.datamodel.io.SqlUtils.quote;

import java.util.UUID;
import java.util.stream.Stream;

/** Class for identification of entities and results from grids in SQL databases. */
public record DbGridMetadata(String gridName, UUID uuid) {

public static final String GRID_TABLE_COLUMN = "grids";
public static final String GRID_NAME_COLUMN = "grid_name";
public static final String GRID_UUID_COLUMN = "grid_uuid";

public String toString() {
return GRID_NAME_COLUMN + "=" + gridName + ", " + GRID_UUID_COLUMN + "=" + uuid.toString();
}

/** @return Stream with grid uuid */
public Stream<String> getStreamForQuery() {
return Stream.of(quote(uuid.toString(), "'"));
}
}
45 changes: 45 additions & 0 deletions src/main/java/edu/ie3/datamodel/io/SqlUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* © 2023. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io;

import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SqlUtils {

protected static final Logger log = LoggerFactory.getLogger(SqlUtils.class);
private static final String END_QUERY_CREATE_TABLE =
")\n \t WITHOUT OIDS\n \t TABLESPACE pg_default;";

private SqlUtils() {
throw new IllegalStateException("Utility classes cannot be instantiated");
}

private static String beginQueryCreateTable(String schemaName, String tableName) {
return "CREATE TABLE " + schemaName + "." + tableName + "\n(\n";
}

/** @return query to create a SQL table for a grid */
public static String queryCreateGridTable(String schemaName) {
return beginQueryCreateTable(schemaName, DbGridMetadata.GRID_TABLE_COLUMN)
+ "\tuuid uuid PRIMARY KEY,\n\tname TEXT NOT NULL\n"
+ END_QUERY_CREATE_TABLE;
}

/**
* To avoid data type conflicts while insertion into a SQL table all columns should be quoted.
*
* @return input with quoteSymbol
*/
public static String quote(String input, String quoteSymbol) {
if (Objects.equals(input, "") || Objects.equals(input, "null")) {
return "NULL";
} else {
return input.matches("^\".*\"$") ? input : quoteSymbol + input + quoteSymbol;
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/edu/ie3/datamodel/io/connectors/SqlConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ public ResultSet executeQuery(Statement stmt, String query) throws SQLException
/**
* Executes an update query
*
* @param updateQuery the query to execute
* @param query the query to execute
* @return The number of updates or a negative number if the execution failed
*/
public int executeUpdate(String updateQuery) {
try (Statement stmt = getConnection().createStatement()) {
return stmt.executeUpdate(updateQuery);
public int executeUpdate(String query) throws SQLException {
try (Statement statement = getConnection().createStatement()) {
return statement.executeUpdate(query);
} catch (SQLException e) {
log.error(String.format("Error at execution of query \"%1.127s\": ", updateQuery), e);
return -1;
throw new SQLException(
String.format("Error at execution of query, SQLReason: '%s'", e.getMessage()), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
*/
package edu.ie3.datamodel.io.naming;

import static edu.ie3.datamodel.io.naming.EntityPersistenceNamingStrategy.logger;

import edu.ie3.datamodel.io.naming.timeseries.ColumnScheme;
import edu.ie3.datamodel.models.Entity;
import edu.ie3.datamodel.models.timeseries.TimeSeries;
import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
import edu.ie3.datamodel.models.timeseries.repetitive.LoadProfileInput;
import edu.ie3.datamodel.models.value.Value;
import java.util.Optional;

/** A naming strategy for database entities */
public class DatabaseNamingStrategy {

private static final String TIME_SERIES_PREFIX = "time_series_";

private static final String LOAD_PROFILE_PREFIX = "load_profile_";

private final EntityPersistenceNamingStrategy entityPersistenceNamingStrategy;

public DatabaseNamingStrategy(EntityPersistenceNamingStrategy entityPersistenceNamingStrategy) {
Expand Down Expand Up @@ -42,7 +52,48 @@ public String getTimeSeriesEntityName(ColumnScheme columnScheme) {
return TIME_SERIES_PREFIX + columnScheme.getScheme();
}

/**
* Provides the name of a load profile given by the load profile key
*
* @param lpKey Load profile key
* @return the table name
*/
private String getLoadProfileEntityName(String lpKey) {
return LOAD_PROFILE_PREFIX + lpKey;
}

/**
* Provides the name of a unique entity class.
*
* @param cls Class extends UniqueEntity
* @return the table name
*/
public Optional<String> getEntityName(Class<? extends Entity> cls) {
return entityPersistenceNamingStrategy.getEntityName(cls);
}

/**
* Provides the name of a time series. Used to determine the table name in SQL database.
*
* @param timeSeries to be named TimeSeries
* @return the table name
*/
public <T extends TimeSeries<E, V>, E extends TimeSeriesEntry<V>, V extends Value>
Optional<String> getEntityName(T timeSeries) {
if (timeSeries instanceof IndividualTimeSeries individualTimeSeries) {
Optional<E> maybeFirstElement = individualTimeSeries.getEntries().stream().findFirst();
if (maybeFirstElement.isPresent()) {
Class<? extends Value> valueClass = maybeFirstElement.get().getValue().getClass();
return Optional.of(getTimeSeriesEntityName(ColumnScheme.parse(valueClass).orElseThrow()));
} else {
logger.error("Unable to determine content of time series {}", timeSeries);
return Optional.empty();
}
} else if (timeSeries instanceof LoadProfileInput loadProfileInput) {
return Optional.of(getLoadProfileEntityName(loadProfileInput.getType().getKey()));
} else {
logger.error("There is no naming strategy defined for {}", timeSeries);
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ protected String processOperationTime(OperationTime operationTime, String fieldN
operationTime
.getEndDate()
.ifPresent(endDate -> resultStringBuilder.append(processZonedDateTime(endDate)));

return resultStringBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ Try<LinkedHashMap<String, String>, ProcessorProviderException> handleEntity(T en
.transformF(ProcessorProviderException::new));
}

public <T extends Entity> Set<LinkedHashMap<String, String>> handleEntities(List<T> entities)
throws ProcessorProviderException {
Set<T> setOfEntities = new HashSet<>(entities);
Set<LinkedHashMap<String, String>> setOfMaps = new HashSet<>();
for (T entity : setOfEntities) {
LinkedHashMap<String, String> entryResult = handleEntity(entity).getOrThrow();

/* Prepare the actual result and add them to the set of all results */
setOfMaps.add(new LinkedHashMap<>(entryResult));
}
return setOfMaps;
}

/**
* Get the correct entity processor
*
Expand Down
Loading

0 comments on commit 6723cba

Please sign in to comment.