Skip to content

Commit 36723be

Browse files
committed
Implementing requested changes.
1 parent 3094391 commit 36723be

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

src/main/java/edu/ie3/datamodel/io/source/EntitySource.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public abstract class EntitySource {
3333
protected static final String OPERATOR = "operator";
3434
protected static final String NODE = "node";
3535
protected static final String TYPE = "type";
36+
protected static final String FIELDS_TO_VALUES_MAP = "fieldsToValuesMap";
3637

3738
DataSource dataSource;
3839

@@ -50,10 +51,24 @@ protected String buildSkippingMessage(
5051
+ missingElementsString;
5152
}
5253

53-
protected String getOrThrow(Map<String, String> map, String field) throws SourceException {
54-
return Optional.ofNullable(map.get(field))
55-
.orElseThrow(
56-
() -> new SourceException("Field " + field + " was not found in map " + map + "."));
54+
/**
55+
* Method for retrieving an element from a map. If the map doesn't contain the key an error
56+
* message is build and returned instead.
57+
*
58+
* <p>Should not be used for other purposes than creating error messages.
59+
*
60+
* @param map with value
61+
* @param key for the value
62+
* @param mapName name of the map used for the error message
63+
* @return either the value or an error message
64+
*/
65+
protected String safeMapGet(Map<String, String> map, String key, String mapName) {
66+
return Optional.ofNullable(map.get(key))
67+
.orElse(
68+
"Key '"
69+
+ key
70+
+ "' not found"
71+
+ (mapName.isEmpty() ? "!" : " in map '" + mapName + "'!"));
5772
}
5873

5974
/**
@@ -79,7 +94,7 @@ protected <T extends UniqueEntity> Optional<T> findFirstEntityByUuid(
7994
* the provided fields to values mapping. The provided fields to values mapping needs to have one
8095
* and only one field with key {@link #TYPE} and a corresponding UUID value. If the type can be
8196
* found in the provided collection based on the UUID it is returned wrapped in a {@link Success}.
82-
* Otherwise a {@link Failure} is returned and a warning is logged.
97+
* Otherwise, a {@link Failure} is returned and a warning is logged.
8398
*
8499
* @param types a collection of types that should be used for searching
85100
* @param fieldsToAttributes the field name to value mapping incl. the key {@link #TYPE}
@@ -97,15 +112,13 @@ protected <T extends AssetTypeInput> Try<T, SourceException> getAssetType(
97112
// if the type is not present we return an empty element and
98113
// log a warning
99114
if (assetType.isEmpty()) {
100-
return Try.of(
101-
() ->
102-
buildSkippingMessage(
103-
skippedClassString,
104-
getOrThrow(fieldsToAttributes, "uuid"),
105-
getOrThrow(fieldsToAttributes, "id"),
106-
TYPE + ": " + getOrThrow(fieldsToAttributes, TYPE)),
107-
SourceException.class)
108-
.flatMap(message -> Failure.of(new SourceException("Failure due to: " + message)));
115+
String skippingMessage =
116+
buildSkippingMessage(
117+
skippedClassString,
118+
safeMapGet(fieldsToAttributes, "uuid", FIELDS_TO_VALUES_MAP),
119+
safeMapGet(fieldsToAttributes, "id", FIELDS_TO_VALUES_MAP),
120+
TYPE + ": " + safeMapGet(fieldsToAttributes, TYPE, FIELDS_TO_VALUES_MAP));
121+
return new Failure<>(new SourceException("Failure due to: " + skippingMessage));
109122
}
110123
return new Success<>(assetType.get());
111124
}
@@ -270,8 +283,7 @@ protected <T extends AssetInput> AssetInputEntityData assetInputEntityDataStream
270283
operators,
271284
operatorUuid,
272285
entityClass.getSimpleName(),
273-
fieldsToAttributes.getOrDefault(
274-
"uuid", "No 'uuid' found in map " + fieldsToAttributes + "!"));
286+
safeMapGet(fieldsToAttributes, "uuid", FIELDS_TO_VALUES_MAP));
275287

276288
// remove fields that are passed as objects to constructor
277289
fieldsToAttributes.keySet().removeAll(new HashSet<>(Collections.singletonList(OPERATOR)));

src/main/java/edu/ie3/datamodel/io/source/SystemParticipantSource.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -889,17 +889,13 @@ protected Try<HpInputEntityData, SourceException> buildHpEntityData(
889889
// if the requested entity is not present we return an empty element and
890890
// log a warning
891891
if (hpInputEntityDataOpt.isEmpty()) {
892-
return Try.of(
893-
() ->
894-
buildSkippingMessage(
895-
typedEntityData.getTargetClass().getSimpleName(),
896-
getOrThrow(fieldsToAttributes, "uuid"),
897-
getOrThrow(fieldsToAttributes, "id"),
898-
"thermalBus: " + getOrThrow(fieldsToAttributes, THERMAL_BUS)),
899-
SourceException.class)
900-
.flatMap(
901-
skippingMessage ->
902-
Failure.of(new SourceException("Failure due to: " + skippingMessage)));
892+
String skippingMessage =
893+
buildSkippingMessage(
894+
typedEntityData.getTargetClass().getSimpleName(),
895+
safeMapGet(fieldsToAttributes, "uuid", FIELDS_TO_VALUES_MAP),
896+
safeMapGet(fieldsToAttributes, "id", FIELDS_TO_VALUES_MAP),
897+
"thermalBus: " + safeMapGet(fieldsToAttributes, THERMAL_BUS, FIELDS_TO_VALUES_MAP));
898+
return new Failure<>(new SourceException("Failure due to: " + skippingMessage));
903899
}
904900

905901
return new Success<>(hpInputEntityDataOpt.get());
@@ -948,29 +944,20 @@ protected Try<ChpInputEntityData, SourceException> buildChpEntityData(
948944
StringBuilder sB = new StringBuilder();
949945
if (thermalStorage.isEmpty()) {
950946
sB.append("thermalStorage: ")
951-
.append(
952-
fieldsToAttributes.getOrDefault(
953-
THERMAL_STORAGE,
954-
"No 'thermalStorage' found in map " + fieldsToAttributes + "!"));
947+
.append(safeMapGet(fieldsToAttributes, THERMAL_STORAGE, FIELDS_TO_VALUES_MAP));
955948
}
956949
if (thermalBus.isEmpty()) {
957950
sB.append("\nthermalBus: ")
958-
.append(
959-
fieldsToAttributes.getOrDefault(
960-
THERMAL_BUS, "No 'thermalBus' found in map " + fieldsToAttributes + "!"));
951+
.append(safeMapGet(fieldsToAttributes, THERMAL_BUS, FIELDS_TO_VALUES_MAP));
961952
}
962953

963-
return Try.of(
964-
() ->
965-
buildSkippingMessage(
966-
typedEntityData.getTargetClass().getSimpleName(),
967-
getOrThrow(fieldsToAttributes, "uuid"),
968-
getOrThrow(fieldsToAttributes, "id"),
969-
sB.toString()),
970-
SourceException.class)
971-
.flatMap(
972-
skippingMessage ->
973-
Failure.of(new SourceException("Failure due to: " + skippingMessage)));
954+
String skippingMessage =
955+
buildSkippingMessage(
956+
typedEntityData.getTargetClass().getSimpleName(),
957+
safeMapGet(fieldsToAttributes, "uuid", FIELDS_TO_VALUES_MAP),
958+
safeMapGet(fieldsToAttributes, "id", FIELDS_TO_VALUES_MAP),
959+
sB.toString());
960+
return new Failure<>(new SourceException("Failure due to: " + skippingMessage));
974961
}
975962

976963
// remove fields that are passed as objects to constructor

0 commit comments

Comments
 (0)