|
5 | 5 | */
|
6 | 6 | package edu.ie3.datamodel.io.naming;
|
7 | 7 |
|
| 8 | +import static edu.ie3.datamodel.io.naming.EntityPersistenceNamingStrategy.logger; |
| 9 | + |
8 | 10 | import edu.ie3.datamodel.io.naming.timeseries.ColumnScheme;
|
9 | 11 | import edu.ie3.datamodel.models.Entity;
|
| 12 | +import edu.ie3.datamodel.models.timeseries.TimeSeries; |
| 13 | +import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry; |
| 14 | +import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries; |
| 15 | +import edu.ie3.datamodel.models.timeseries.repetitive.LoadProfileInput; |
| 16 | +import edu.ie3.datamodel.models.value.Value; |
10 | 17 | import java.util.Optional;
|
11 | 18 |
|
12 | 19 | /** A naming strategy for database entities */
|
13 | 20 | public class DatabaseNamingStrategy {
|
14 | 21 |
|
15 | 22 | private static final String TIME_SERIES_PREFIX = "time_series_";
|
| 23 | + |
| 24 | + private static final String LOAD_PROFILE_PREFIX = "load_profile_"; |
| 25 | + |
16 | 26 | private final EntityPersistenceNamingStrategy entityPersistenceNamingStrategy;
|
17 | 27 |
|
18 | 28 | public DatabaseNamingStrategy(EntityPersistenceNamingStrategy entityPersistenceNamingStrategy) {
|
@@ -42,7 +52,48 @@ public String getTimeSeriesEntityName(ColumnScheme columnScheme) {
|
42 | 52 | return TIME_SERIES_PREFIX + columnScheme.getScheme();
|
43 | 53 | }
|
44 | 54 |
|
| 55 | + /** |
| 56 | + * Provides the name of a load profile given by the load profile key |
| 57 | + * |
| 58 | + * @param lpKey Load profile key |
| 59 | + * @return the table name |
| 60 | + */ |
| 61 | + private String getLoadProfileEntityName(String lpKey) { |
| 62 | + return LOAD_PROFILE_PREFIX + lpKey; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Provides the name of a unique entity class. |
| 67 | + * |
| 68 | + * @param cls Class extends UniqueEntity |
| 69 | + * @return the table name |
| 70 | + */ |
45 | 71 | public Optional<String> getEntityName(Class<? extends Entity> cls) {
|
46 | 72 | return entityPersistenceNamingStrategy.getEntityName(cls);
|
47 | 73 | }
|
| 74 | + |
| 75 | + /** |
| 76 | + * Provides the name of a time series. Used to determine the table name in SQL database. |
| 77 | + * |
| 78 | + * @param timeSeries to be named TimeSeries |
| 79 | + * @return the table name |
| 80 | + */ |
| 81 | + public <T extends TimeSeries<E, V>, E extends TimeSeriesEntry<V>, V extends Value> |
| 82 | + Optional<String> getEntityName(T timeSeries) { |
| 83 | + if (timeSeries instanceof IndividualTimeSeries individualTimeSeries) { |
| 84 | + Optional<E> maybeFirstElement = individualTimeSeries.getEntries().stream().findFirst(); |
| 85 | + if (maybeFirstElement.isPresent()) { |
| 86 | + Class<? extends Value> valueClass = maybeFirstElement.get().getValue().getClass(); |
| 87 | + return Optional.of(getTimeSeriesEntityName(ColumnScheme.parse(valueClass).orElseThrow())); |
| 88 | + } else { |
| 89 | + logger.error("Unable to determine content of time series {}", timeSeries); |
| 90 | + return Optional.empty(); |
| 91 | + } |
| 92 | + } else if (timeSeries instanceof LoadProfileInput loadProfileInput) { |
| 93 | + return Optional.of(getLoadProfileEntityName(loadProfileInput.getType().getKey())); |
| 94 | + } else { |
| 95 | + logger.error("There is no naming strategy defined for {}", timeSeries); |
| 96 | + return Optional.empty(); |
| 97 | + } |
| 98 | + } |
48 | 99 | }
|
0 commit comments