@@ -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 )));
0 commit comments