Skip to content

Commit 2c1cc77

Browse files
committed
Reducing code smells
1 parent 2165981 commit 2c1cc77

File tree

2 files changed

+2
-57
lines changed

2 files changed

+2
-57
lines changed

src/main/java/edu/ie3/datamodel/io/source/couchbase/CouchbaseWeatherSource.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class CouchbaseWeatherSource implements WeatherSource {
4343
private final TimeBasedWeatherValueFactory weatherFactory;
4444

4545
private final String keyPrefix;
46-
private final NamingConvention namingConvention;
4746
private final CouchbaseConnector connector;
4847
private final IdCoordinateSource coordinateSource;
4948
private final String coordinateIdColumnName;
@@ -109,7 +108,6 @@ public CouchbaseWeatherSource(
109108
this.coordinateSource = coordinateSource;
110109
this.keyPrefix = keyPrefix;
111110
this.weatherFactory = weatherFactory;
112-
this.namingConvention = namingConvention;
113111
this.coordinateIdColumnName = weatherFactory.getCoordinateIdFieldString(namingConvention);
114112
}
115113

src/main/java/edu/ie3/datamodel/io/source/sql/SqlWeatherSource.java

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package edu.ie3.datamodel.io.source.sql;
77

8-
import edu.ie3.datamodel.exceptions.InvalidWeatherColumnNameException;
98
import edu.ie3.datamodel.io.connectors.SqlConnector;
109
import edu.ie3.datamodel.io.factory.timeseries.TimeBasedWeatherValueData;
1110
import edu.ie3.datamodel.io.factory.timeseries.TimeBasedWeatherValueFactory;
@@ -14,7 +13,6 @@
1413
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
1514
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
1615
import edu.ie3.datamodel.models.value.WeatherValue;
17-
import edu.ie3.util.StringUtils;
1816
import edu.ie3.util.interval.ClosedInterval;
1917
import edu.ie3.util.naming.NamingConvention;
2018
import java.sql.*;
@@ -34,12 +32,8 @@ public class SqlWeatherSource implements WeatherSource {
3432
private static final String WHERE = " WHERE ";
3533
private static final NamingConvention DEFAULT_NAMING_CONVENTION = NamingConvention.SNAKE;
3634

37-
private final NamingConvention namingConvention;
3835
private final SqlConnector connector;
3936
private final IdCoordinateSource idCoordinateSource;
40-
/* The column name represents the name of the column in the chosen naming convention. However, the field name is in
41-
* flat case always, as this is, what the factory expects. */
42-
private final String coordinateIdColumnName;
4337
private final String coordinateIdFieldName;
4438
private final TimeBasedWeatherValueFactory weatherFactory;
4539

@@ -99,19 +93,9 @@ public SqlWeatherSource(
9993
this.connector = connector;
10094
this.idCoordinateSource = idCoordinateSource;
10195
this.weatherFactory = weatherFactory;
102-
this.namingConvention = namingConvention;
103-
this.coordinateIdColumnName = weatherFactory.getCoordinateIdFieldString(namingConvention);
10496
this.coordinateIdFieldName = weatherFactory.getCoordinateIdFieldString();
105-
106-
String dbTimeColumnName =
107-
getDbColumnName(weatherFactory.getTimeFieldString(), connector, weatherTableName)
108-
.orElseThrow(
109-
() ->
110-
new InvalidWeatherColumnNameException(
111-
"Cannot find column for '"
112-
+ weatherFactory.getTimeFieldString()
113-
+ "' in provided weather data configuration."
114-
+ "Please ensure that the database connection is working and the column names are correct!"));
97+
String coordinateIdColumnName = weatherFactory.getCoordinateIdFieldString(namingConvention);
98+
String dbTimeColumnName = weatherFactory.getTimeFieldString();
11599

116100
// setup queries
117101
this.queryTimeInterval =
@@ -186,43 +170,6 @@ public Optional<TimeBasedValue<WeatherValue>> getWeather(ZonedDateTime date, Poi
186170
return Optional.of(timeBasedValues.get(0));
187171
}
188172

189-
/**
190-
* Determine the corresponding database column name based on the provided factory field parameter
191-
* name. Needed to support camel as well as snake case database column names.
192-
*
193-
* @param factoryColumnName the name of the field parameter set in the entity factory
194-
* @param connector the sql connector of this source
195-
* @param weatherTableName the table name where the weather is stored
196-
* @return the column name that corresponds to the provided field parameter or an empty optional
197-
* if no matching column can be found
198-
*/
199-
private Optional<String> getDbColumnName(
200-
String factoryColumnName, SqlConnector connector, String weatherTableName) {
201-
202-
// get the column names from the database
203-
Optional<String> dbColumnName = Optional.empty();
204-
try {
205-
ResultSet rs =
206-
connector.getConnection().getMetaData().getColumns(null, null, weatherTableName, null);
207-
208-
while (rs.next()) {
209-
String databaseColumnName = rs.getString("COLUMN_NAME");
210-
if (StringUtils.snakeCaseToCamelCase(databaseColumnName)
211-
.equalsIgnoreCase(factoryColumnName)) {
212-
dbColumnName = Optional.of(databaseColumnName);
213-
break;
214-
}
215-
}
216-
} catch (SQLException ex) {
217-
logger.error(
218-
"Cannot connect to database to retrieve db column name for factory column name '{}' in weather table '{}'",
219-
factoryColumnName,
220-
weatherTableName,
221-
ex);
222-
}
223-
return dbColumnName;
224-
}
225-
226173
/**
227174
* Creates a base query string without closing semicolon of the following pattern: <br>
228175
* {@code SELECT * FROM <schema>.<table>}

0 commit comments

Comments
 (0)