|
| 1 | +package de.ii.xtraplatform.cli; |
| 2 | + |
| 3 | +import de.ii.ldproxy.cfg.LdproxyCfg; |
| 4 | +import de.ii.ogcapi.foundation.domain.OgcApiDataV2; |
| 5 | +import de.ii.ogcapi.styles.domain.MbStyleStylesheet; |
| 6 | +import de.ii.xtraplatform.values.domain.*; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.*; |
| 9 | +import java.util.function.Consumer; |
| 10 | +import shadow.com.fasterxml.jackson.core.type.TypeReference; |
| 11 | +import shadow.com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import shadow.com.google.common.base.Strings; |
| 13 | + |
| 14 | +public class AutoValueHandler { |
| 15 | + |
| 16 | + private static final Set<String> ALLOWED_TYPES = Set.of("maplibre-styles"); |
| 17 | + |
| 18 | + public static Result preCheck(Map<String, String> parameters, LdproxyCfg ldproxyCfg) { |
| 19 | + if (Objects.isNull(ldproxyCfg)) { |
| 20 | + return Result.failure("Not connected to store"); |
| 21 | + } |
| 22 | + |
| 23 | + ldproxyCfg.initStore(); |
| 24 | + |
| 25 | + String type = parameters.get("type"); |
| 26 | + String name = parameters.get("name"); |
| 27 | + String apiId = parameters.get("apiId"); |
| 28 | + |
| 29 | + if (Strings.isNullOrEmpty(type)) { |
| 30 | + return Result.failure("No value type given"); |
| 31 | + } |
| 32 | + |
| 33 | + if (!ALLOWED_TYPES.contains(type)) { |
| 34 | + return Result.failure(String.format("Value type '%s' is not supported", type)); |
| 35 | + } |
| 36 | + |
| 37 | + if (Strings.isNullOrEmpty(name)) { |
| 38 | + return Result.failure("No name given"); |
| 39 | + } |
| 40 | + |
| 41 | + if (Strings.isNullOrEmpty(apiId)) { |
| 42 | + return Result.failure("No API id given"); |
| 43 | + } |
| 44 | + |
| 45 | + if (!ldproxyCfg.getEntityDataStore().forType(OgcApiDataV2.class).hasAny(apiId)) { |
| 46 | + return Result.failure(String.format("API with id '%s' not found", apiId)); |
| 47 | + } |
| 48 | + |
| 49 | + if (ldproxyCfg.hasValue(type, name, apiId)) { |
| 50 | + return Result.failure( |
| 51 | + String.format( |
| 52 | + "Value of type '%s' with name '%s' already exists for API '%s'", type, name, apiId)); |
| 53 | + } |
| 54 | + |
| 55 | + return Result.empty(); |
| 56 | + } |
| 57 | + |
| 58 | + public static Result analyze(Map<String, String> parameters, LdproxyCfg ldproxyCfg) { |
| 59 | + Result result = new Result(); |
| 60 | + |
| 61 | + try { |
| 62 | + String apiId = parameters.get("apiId"); |
| 63 | + |
| 64 | + AutoValueFactory<MbStyleStylesheet, String, Map<String, String>> valueFactory = |
| 65 | + (AutoValueFactory<MbStyleStylesheet, String, Map<String, String>>) |
| 66 | + getValueFactory(ldproxyCfg, "maplibre-styles"); |
| 67 | + |
| 68 | + Map<String, String> collectionColors = valueFactory.analyze(apiId); |
| 69 | + |
| 70 | + if (!collectionColors.isEmpty()) { |
| 71 | + result.success("All good"); |
| 72 | + result.details("Collection Colors", collectionColors); |
| 73 | + } else { |
| 74 | + result.success("No stylesheet information found"); |
| 75 | + } |
| 76 | + } catch (Throwable e) { |
| 77 | + e.printStackTrace(); |
| 78 | + if (Objects.nonNull(e.getMessage())) { |
| 79 | + result.error(e.getMessage()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + public static Result generate( |
| 87 | + Map<String, String> parameters, |
| 88 | + LdproxyCfg ldproxyCfg, |
| 89 | + Consumer<Result> tracker, |
| 90 | + ObjectMapper jsonMapper) { |
| 91 | + AutoValueFactory<MbStyleStylesheet, String, Map<String, String>> valueFactory = |
| 92 | + (AutoValueFactory<MbStyleStylesheet, String, Map<String, String>>) |
| 93 | + getValueFactory(ldproxyCfg, "maplibre-styles"); |
| 94 | + Result result = new Result(); |
| 95 | + |
| 96 | + try { |
| 97 | + String type = parameters.get("type"); |
| 98 | + String apiId = parameters.get("apiId"); |
| 99 | + String name = parameters.get("name"); |
| 100 | + String collectionColorsString = parameters.get("collectionColors"); |
| 101 | + boolean isTest = Objects.equals(collectionColorsString, "TEST"); |
| 102 | + |
| 103 | + if (Strings.isNullOrEmpty(collectionColorsString) && !isTest) { |
| 104 | + return Result.failure("No collections given"); |
| 105 | + } |
| 106 | + |
| 107 | + Map<String, String> collectionColors = |
| 108 | + isTest |
| 109 | + ? valueFactory.analyze(apiId) |
| 110 | + : jsonMapper.readValue( |
| 111 | + collectionColorsString, new TypeReference<Map<String, String>>() {}); |
| 112 | + |
| 113 | + MbStyleStylesheet stylesheet = valueFactory.generate(apiId, collectionColors); |
| 114 | + |
| 115 | + ldproxyCfg.writeValue(stylesheet, name, apiId); |
| 116 | + |
| 117 | + Path newPath = ldproxyCfg.getValuesPath().resolve(Path.of(type, apiId, name + ".json")); |
| 118 | + |
| 119 | + result.success( |
| 120 | + String.format("Value of type '%s' was created successfully: %s", type, newPath)); |
| 121 | + } catch (Throwable e) { |
| 122 | + e.printStackTrace(); |
| 123 | + if (Objects.nonNull(e.getMessage())) { |
| 124 | + result.error(e.getMessage()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return result; |
| 129 | + } |
| 130 | + |
| 131 | + private static AutoValueFactory<?, ?, ?> getValueFactory( |
| 132 | + LdproxyCfg ldproxyCfg, String valueType) { |
| 133 | + ValueFactory factory = ldproxyCfg.getValueFactories().get(valueType); |
| 134 | + |
| 135 | + AutoValueFactory<?, ?, ?> valueFactory = |
| 136 | + factory |
| 137 | + .auto() |
| 138 | + .orElseThrow( |
| 139 | + () -> |
| 140 | + new IllegalArgumentException( |
| 141 | + "No value factory found for value type " + valueType)); |
| 142 | + return valueFactory; |
| 143 | + } |
| 144 | +} |
0 commit comments