Skip to content

Commit

Permalink
Reverting introduction of NamingConvention
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-peter committed Mar 3, 2022
1 parent 6c97ff1 commit ae52c4d
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 101 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `"coordinate"` to `"coordinate id"` (as composite word)
- `"diffuseirradiation"` to `"diffuse irradiance"` (as composite word)
- Harmonized the column name definition for coordinate id across implementations of `WeatherSource`
- Allows for different case conventions based on the used data source technology

## [2.1.0] - 2022-01-05

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import edu.ie3.datamodel.models.value.WeatherValue;
import edu.ie3.util.TimeUtil;
import edu.ie3.util.naming.Naming;
import edu.ie3.util.naming.NamingConvention;
import java.time.ZoneId;
import java.util.*;

Expand All @@ -20,8 +18,7 @@ public abstract class TimeBasedWeatherValueFactory
extends TimeBasedValueFactory<TimeBasedWeatherValueData, WeatherValue> {
protected static final String UUID = "uuid";
protected static final String TIME = "time";
/* Hold a case agnostic representation of the composite word "coordinate id", that allows for later case conversion */
protected static final Naming COORDINATE_ID_NAMING = Naming.from("coordinate", "id");
protected static final String COORDINATE_ID = "coordinateid";

protected final TimeUtil timeUtil;

Expand All @@ -39,22 +36,12 @@ protected TimeBasedWeatherValueFactory(TimeUtil timeUtil) {
}

/**
* Return the field name for the coordinate id in flat case.
* Return the field name for the coordinate id
*
* @return the field name for the coordinate id
*/
public String getCoordinateIdFieldString() {
return COORDINATE_ID_NAMING.flatCase();
}

/**
* Return the field name for the coordinate id in desired case.
*
* @param convention The desired naming convention / casing
* @return the field name for the coordinate id in appropriate case
*/
public String getCoordinateIdFieldString(NamingConvention convention) {
return COORDINATE_ID_NAMING.as(convention);
return COORDINATE_ID;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
import edu.ie3.datamodel.models.value.WeatherValue;
import edu.ie3.util.interval.ClosedInterval;
import edu.ie3.util.naming.NamingConvention;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
Expand All @@ -34,9 +33,6 @@
/** Couchbase Source for weather data */
public class CouchbaseWeatherSource implements WeatherSource {
private static final Logger logger = LoggerFactory.getLogger(CouchbaseWeatherSource.class);

/* Column names in couchbase come in "flat" case by default */
private static final NamingConvention DEFAULT_NAMING_CONVENTION = NamingConvention.FLAT;
private static final String DEFAULT_TIMESTAMP_PATTERN = "yyyy-MM-dd'T'HH:mm:ssxxx";
/** The start of the document key, comparable to a table name in relational databases */
private static final String DEFAULT_KEY_PREFIX = "weather";
Expand All @@ -46,47 +42,26 @@ public class CouchbaseWeatherSource implements WeatherSource {
private final String keyPrefix;
private final CouchbaseConnector connector;
private final IdCoordinateSource coordinateSource;
/* Final name of the column within the database */
private final String coordinateIdColumnName;

/**
* Instantiate a weather source utilising a connection to a couchbase instance obtained via the
* connector. This convenient constructor uses the {@link
* CouchbaseWeatherSource#DEFAULT_KEY_PREFIX} as key prefix and {@link
* CouchbaseWeatherSource#DEFAULT_NAMING_CONVENTION} as naming convention.
* CouchbaseWeatherSource#DEFAULT_KEY_PREFIX} as key prefix.
*
* @param connector Connector, that establishes the connection to the couchbase instance
* @param coordinateSource Source to obtain actual coordinates from
* @param coordinateIdColumnName Name of the column containing the information about the
* coordinate identifier
* @param weatherFactory Factory to transfer field to value mapping into actual java object
* instances
*/
public CouchbaseWeatherSource(
CouchbaseConnector connector,
IdCoordinateSource coordinateSource,
String coordinateIdColumnName,
TimeBasedWeatherValueFactory weatherFactory) {
this(
connector, coordinateSource, DEFAULT_KEY_PREFIX, DEFAULT_NAMING_CONVENTION, weatherFactory);
}

/**
* Instantiate a weather source utilising a connection to a couchbase instance obtained via the
* connector. Uses {@link CouchbaseWeatherSource#DEFAULT_NAMING_CONVENTION} as naming convention.
*
* @param connector Connector, that establishes the connection to the couchbase instance
* @param coordinateSource Source to obtain actual coordinates from
* @param keyPrefix Prefix of entries, that belong to weather
* @param weatherFactory Factory to transfer field to value mapping into actual java object
* instances
* @deprecated Use {@link CouchbaseWeatherSource#CouchbaseWeatherSource(CouchbaseConnector,
* IdCoordinateSource, String, NamingConvention, TimeBasedWeatherValueFactory)} instead
*/
@Deprecated(since = "3.0", forRemoval = true)
public CouchbaseWeatherSource(
CouchbaseConnector connector,
IdCoordinateSource coordinateSource,
String keyPrefix,
TimeBasedWeatherValueFactory weatherFactory) {
this(connector, coordinateSource, keyPrefix, DEFAULT_NAMING_CONVENTION, weatherFactory);
this(connector, coordinateSource, coordinateIdColumnName, DEFAULT_KEY_PREFIX, weatherFactory);
}

/**
Expand All @@ -95,22 +70,23 @@ public CouchbaseWeatherSource(
*
* @param connector Connector, that establishes the connection to the couchbase instance
* @param coordinateSource Source to obtain actual coordinates from
* @param coordinateIdColumnName Name of the column containing the information about the
* coordinate identifier
* @param keyPrefix Prefix of entries, that belong to weather
* @param namingConvention the (case) convention, how columns are named
* @param weatherFactory Factory to transfer field to value mapping into actual java object
* instances
*/
public CouchbaseWeatherSource(
CouchbaseConnector connector,
IdCoordinateSource coordinateSource,
String coordinateIdColumnName,
String keyPrefix,
NamingConvention namingConvention,
TimeBasedWeatherValueFactory weatherFactory) {
this.connector = connector;
this.coordinateSource = coordinateSource;
this.coordinateIdColumnName = coordinateIdColumnName;
this.keyPrefix = keyPrefix;
this.weatherFactory = weatherFactory;
this.coordinateIdColumnName = weatherFactory.getCoordinateIdFieldString(namingConvention);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import edu.ie3.datamodel.models.value.WeatherValue;
import edu.ie3.util.StringUtils;
import edu.ie3.util.interval.ClosedInterval;
import edu.ie3.util.naming.Naming;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -31,11 +30,7 @@ public class InfluxDbWeatherSource implements WeatherSource {
private static final String WHERE = " where ";
private static final String MEASUREMENT_NAME_WEATHER = "weather";
private static final int MILLI_TO_NANO_FACTOR = 1000000;

/* Final name of the coordinate id field for use in factories */
private final String coordinateIdFieldName;
/* Final name of the column within the database */
private final String coordinateIdColumnName = Naming.from("coordinate", "id").snakeCase();
private final String coordinateIdColumnName;
private final InfluxDbConnector connector;
private final IdCoordinateSource coordinateSource;
private final TimeBasedWeatherValueFactory weatherValueFactory;
Expand All @@ -55,7 +50,7 @@ public InfluxDbWeatherSource(
this.connector = connector;
this.coordinateSource = coordinateSource;
this.weatherValueFactory = weatherValueFactory;
this.coordinateIdFieldName = weatherValueFactory.getCoordinateIdFieldString();
this.coordinateIdColumnName = weatherValueFactory.getCoordinateIdFieldString();
}

@Override
Expand Down Expand Up @@ -170,7 +165,7 @@ private Stream<Optional<TimeBasedValue<WeatherValue>>> optTimeBasedValueStream(
flatCaseFields.putIfAbsent("uuid", UUID.randomUUID().toString());

/* Get the corresponding coordinate id from map AND REMOVE THE ENTRY !!! */
int coordinateId = Integer.parseInt(flatCaseFields.remove(coordinateIdFieldName));
int coordinateId = Integer.parseInt(flatCaseFields.remove(coordinateIdColumnName));
return coordinateSource
.getCoordinate(coordinateId)
.map(point -> new TimeBasedWeatherValueData(flatCaseFields, point))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
import edu.ie3.datamodel.models.value.WeatherValue;
import edu.ie3.util.interval.ClosedInterval;
import edu.ie3.util.naming.NamingConvention;
import java.sql.*;
import java.time.ZonedDateTime;
import java.util.*;
Expand All @@ -25,8 +24,6 @@
public class SqlWeatherSource extends SqlDataSource<TimeBasedValue<WeatherValue>>
implements WeatherSource {
private static final String WHERE = " WHERE ";
/* Column names in sql come in "snake" case by default */
private static final NamingConvention DEFAULT_NAMING_CONVENTION = NamingConvention.SNAKE;

private final IdCoordinateSource idCoordinateSource;
private final String factoryCoordinateFieldName;
Expand All @@ -41,69 +38,39 @@ public class SqlWeatherSource extends SqlDataSource<TimeBasedValue<WeatherValue>
private final String queryTimeAndCoordinate;
private final String queryTimeIntervalAndCoordinates;

/**
* Initializes a new SqlWeatherSource. Uses {@link SqlWeatherSource#DEFAULT_NAMING_CONVENTION} as
* naming convention.
*
* @param connector the connector needed for database connection
* @param idCoordinateSource a coordinate source to map ids to points
* @param schemaName the database schema to use
* @param weatherTableName the name of the table containing weather data
* @param weatherFactory instance of a time based weather value factory
* @deprecated Use {@link SqlWeatherSource#SqlWeatherSource(SqlConnector, IdCoordinateSource,
* String, String, NamingConvention, TimeBasedWeatherValueFactory)} instead
*/
@Deprecated(since = "3.0", forRemoval = true)
public SqlWeatherSource(
SqlConnector connector,
IdCoordinateSource idCoordinateSource,
String schemaName,
String weatherTableName,
TimeBasedWeatherValueFactory weatherFactory) {
this(
connector,
idCoordinateSource,
schemaName,
weatherTableName,
DEFAULT_NAMING_CONVENTION,
weatherFactory);
}
/**
* Initializes a new SqlWeatherSource
*
* @param connector the connector needed for database connection
* @param idCoordinateSource a coordinate source to map ids to points
* @param schemaName the database schema to use
* @param weatherTableName the name of the table containing weather data
* @param namingConvention the (case) convention, how columns are named
* @param weatherFactory instance of a time based weather value factory
*/
public SqlWeatherSource(
SqlConnector connector,
IdCoordinateSource idCoordinateSource,
String schemaName,
String weatherTableName,
NamingConvention namingConvention,
TimeBasedWeatherValueFactory weatherFactory) {
super(connector);
this.idCoordinateSource = idCoordinateSource;
this.weatherFactory = weatherFactory;
this.factoryCoordinateFieldName = weatherFactory.getCoordinateIdFieldString();

/* Determine the correct column names in database */
String coordinateIdColumnName = weatherFactory.getCoordinateIdFieldString(namingConvention);
String dbTimeColumnName =
getDbColumnName(weatherFactory.getTimeFieldString(), weatherTableName);
String dbCoordinateIdColumnName = getDbColumnName(factoryCoordinateFieldName, weatherTableName);

// setup queries
this.queryTimeInterval =
createQueryStringForTimeInterval(schemaName, weatherTableName, dbTimeColumnName);
this.queryTimeAndCoordinate =
createQueryStringForTimeAndCoordinate(
schemaName, weatherTableName, dbTimeColumnName, coordinateIdColumnName);
schemaName, weatherTableName, dbTimeColumnName, dbCoordinateIdColumnName);
this.queryTimeIntervalAndCoordinates =
createQueryStringForTimeIntervalAndCoordinates(
schemaName, weatherTableName, dbTimeColumnName, coordinateIdColumnName);
schemaName, weatherTableName, dbTimeColumnName, dbCoordinateIdColumnName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CouchbaseWeatherSourceCosmoIT extends Specification implements WeatherSour
@Shared
CouchbaseWeatherSource source

static String coordinateIdColumnName = TimeBasedWeatherValueFactory.COORDINATE_ID_NAMING.flatCase()
static String coordinateIdColumnName = TimeBasedWeatherValueFactory.COORDINATE_ID.flatCase()

def setupSpec() {
// Copy import file with json array of documents into docker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import edu.ie3.test.common.CosmoWeatherTestData
import edu.ie3.test.helper.WeatherSourceTestHelper
import edu.ie3.util.TimeUtil
import edu.ie3.util.interval.ClosedInterval
import edu.ie3.util.naming.NamingConvention
import org.locationtech.jts.geom.Point
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
Expand All @@ -33,7 +32,6 @@ class SqlWeatherSourceCosmoIT extends Specification implements WeatherSourceTest

static String schemaName = "public"
static String weatherTableName = "weather"
static NamingConvention namingConvention = NamingConvention.SNAKE

def setupSpec() {
// Copy sql import script into docker
Expand All @@ -44,7 +42,7 @@ class SqlWeatherSourceCosmoIT extends Specification implements WeatherSourceTest

def connector = new SqlConnector(postgreSQLContainer.jdbcUrl, postgreSQLContainer.username, postgreSQLContainer.password)
def weatherFactory = new CosmoTimeBasedWeatherValueFactory(TimeUtil.withDefaults)
source = new SqlWeatherSource(connector, CosmoWeatherTestData.coordinateSource, schemaName, weatherTableName, namingConvention, weatherFactory)
source = new SqlWeatherSource(connector, CosmoWeatherTestData.coordinateSource, schemaName, weatherTableName, weatherFactory)
}

def "A NativeSqlWeatherSource can read and correctly parse a single value for a specific date and coordinate"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import edu.ie3.test.common.IconWeatherTestData
import edu.ie3.test.helper.WeatherSourceTestHelper
import edu.ie3.util.TimeUtil
import edu.ie3.util.interval.ClosedInterval
import edu.ie3.util.naming.NamingConvention
import org.locationtech.jts.geom.Point
import org.testcontainers.containers.Container
import org.testcontainers.containers.PostgreSQLContainer
Expand All @@ -34,7 +33,6 @@ class SqlWeatherSourceIconIT extends Specification implements WeatherSourceTestH

static String schemaName = "public"
static String weatherTableName = "weather"
static NamingConvention namingConvention = NamingConvention.SNAKE

def setupSpec() {
// Copy sql import script into docker
Expand All @@ -46,7 +44,7 @@ class SqlWeatherSourceIconIT extends Specification implements WeatherSourceTestH

def connector = new SqlConnector(postgreSQLContainer.jdbcUrl, postgreSQLContainer.username, postgreSQLContainer.password)
def weatherFactory = new IconTimeBasedWeatherValueFactory(TimeUtil.withDefaults)
source = new SqlWeatherSource(connector, IconWeatherTestData.coordinateSource, schemaName, weatherTableName, namingConvention, weatherFactory)
source = new SqlWeatherSource(connector, IconWeatherTestData.coordinateSource, schemaName, weatherTableName, weatherFactory)
}

def "A NativeSqlWeatherSource can read and correctly parse a single value for a specific date and coordinate"() {
Expand Down

0 comments on commit ae52c4d

Please sign in to comment.