|
| 1 | +package s10k.tool.common.domain; |
| 2 | + |
| 3 | +import static java.time.ZoneOffset.UTC; |
| 4 | +import static net.solarnetwork.util.StringUtils.commaDelimitedStringFromCollection; |
| 5 | +import static s10k.tool.common.util.DateUtils.isMidnight; |
| 6 | + |
| 7 | +import java.time.LocalDateTime; |
| 8 | +import java.time.ZonedDateTime; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Collection; |
| 11 | + |
| 12 | +import org.springframework.util.LinkedMultiValueMap; |
| 13 | +import org.springframework.util.MultiValueMap; |
| 14 | + |
| 15 | +import net.solarnetwork.domain.datum.ObjectDatumKind; |
| 16 | + |
| 17 | +/** |
| 18 | + * Search filter for source information. |
| 19 | + */ |
| 20 | +public record SourceFilter(Collection<Long> objectIds, Collection<String> sourceIds, ZonedDateTime startDate, |
| 21 | + ZonedDateTime endDate, boolean useLocalDates, String metadataFilter, Collection<String> propertyNames, |
| 22 | + Collection<String> instantaneousPropertyNames, Collection<String> accumulatingPropertyNames, |
| 23 | + Collection<String> statusPropertyNames) { |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a filter from array parameters. |
| 27 | + * |
| 28 | + * @param objectIds the object (node/location) IDs, or |
| 29 | + * {@code null} |
| 30 | + * @param sourceIds the source IDs, or {@code null} |
| 31 | + * @param startDate the minimum date the node must have data |
| 32 | + * after |
| 33 | + * @param endDate the maximum date (exclusive) the node must |
| 34 | + * have data after |
| 35 | + * @param useLocalDates {@code true} to treat the min/max dates as |
| 36 | + * node local, otherwise as UTC |
| 37 | + * @param metadataFilter a metadata filter |
| 38 | + * @param propertyNames the property names, or {@code null} |
| 39 | + * @param instantaneousPropertyNames the instantaneous property names, or |
| 40 | + * {@code null} |
| 41 | + * @param accumulatingPropertyNames the accumulating property names, or |
| 42 | + * {@code null} |
| 43 | + * @param statusPropertyNames the status property names, or {@code null} |
| 44 | + * @return the new filter instance |
| 45 | + */ |
| 46 | + public static SourceFilter sourceFilter(Long[] objectIds, String[] sourceIds, ZonedDateTime startDate, |
| 47 | + ZonedDateTime endDate, boolean useLocalDates, String metadataFilter, String[] propertyNames, |
| 48 | + String[] instantaneousPropertyNames, String[] accumulatingPropertyNames, String[] statusPropertyNames) { |
| 49 | + // @formatter:off |
| 50 | + return new SourceFilter( |
| 51 | + objectIds != null && objectIds.length > 0 ? Arrays.asList(objectIds) : null, |
| 52 | + sourceIds != null && sourceIds.length > 0 ? Arrays.asList(sourceIds) : null, |
| 53 | + startDate, |
| 54 | + endDate, |
| 55 | + useLocalDates, |
| 56 | + metadataFilter, |
| 57 | + propertyNames != null && propertyNames.length > 0 ? Arrays.asList(propertyNames) : null, |
| 58 | + instantaneousPropertyNames != null && instantaneousPropertyNames.length > 0 ? Arrays.asList(instantaneousPropertyNames) : null, |
| 59 | + accumulatingPropertyNames != null && accumulatingPropertyNames.length > 0 ? Arrays.asList(accumulatingPropertyNames) : null, |
| 60 | + statusPropertyNames != null && statusPropertyNames.length > 0 ? Arrays.asList(statusPropertyNames) : null |
| 61 | + ); |
| 62 | + // @formatter:on |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get a node multi-value map from this filter. |
| 67 | + * |
| 68 | + * @return the multi-value map, suitable for using as request parameters for |
| 69 | + * node datum stream metadata |
| 70 | + */ |
| 71 | + public MultiValueMap<String, Object> toNodeRequestMap() { |
| 72 | + return toRequestMap(ObjectDatumKind.Node); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get a location multi-value map from this filter. |
| 77 | + * |
| 78 | + * @return the multi-value map, suitable for using as request parameters for |
| 79 | + * location datum stream metadata |
| 80 | + */ |
| 81 | + public MultiValueMap<String, Object> toLocationRequestMap() { |
| 82 | + return toRequestMap(ObjectDatumKind.Location); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get a multi-value map from this filter. |
| 87 | + * |
| 88 | + * @return the multi-value map, suitable for using as request parameters |
| 89 | + */ |
| 90 | + public MultiValueMap<String, Object> toRequestMap(ObjectDatumKind kind) { |
| 91 | + var postBody = new LinkedMultiValueMap<String, Object>(4); |
| 92 | + if (objectIds != null && !objectIds.isEmpty()) { |
| 93 | + postBody.set((kind == ObjectDatumKind.Location ? "locationIds" : "nodeIds"), |
| 94 | + commaDelimitedStringFromCollection(objectIds)); |
| 95 | + } |
| 96 | + if (sourceIds != null && !sourceIds.isEmpty()) { |
| 97 | + postBody.set("sourceIds", commaDelimitedStringFromCollection(sourceIds)); |
| 98 | + } |
| 99 | + if (startDate != null) { |
| 100 | + LocalDateTime dt; |
| 101 | + if (useLocalDates) { |
| 102 | + dt = startDate.toLocalDateTime(); |
| 103 | + } else { |
| 104 | + dt = startDate.withZoneSameInstant(UTC).toLocalDateTime(); |
| 105 | + } |
| 106 | + postBody.set(useLocalDates ? "localStartDate" : "startDate", isMidnight(dt) ? dt.toLocalDate() : dt); |
| 107 | + } |
| 108 | + if (endDate != null) { |
| 109 | + LocalDateTime dt; |
| 110 | + if (useLocalDates) { |
| 111 | + dt = endDate.toLocalDateTime(); |
| 112 | + } else { |
| 113 | + dt = endDate.withZoneSameInstant(UTC).toLocalDateTime(); |
| 114 | + } |
| 115 | + postBody.set(useLocalDates ? "localEndDate" : "endDate", isMidnight(dt) ? dt.toLocalDate() : dt); |
| 116 | + } |
| 117 | + if (metadataFilter != null && !metadataFilter.isBlank()) { |
| 118 | + postBody.set("metadataFilter", metadataFilter); |
| 119 | + } |
| 120 | + if (propertyNames != null && !propertyNames.isEmpty()) { |
| 121 | + postBody.set("propertyNames", commaDelimitedStringFromCollection(propertyNames)); |
| 122 | + } |
| 123 | + if (instantaneousPropertyNames != null && !instantaneousPropertyNames.isEmpty()) { |
| 124 | + postBody.set("instantaneousPropertyNames", commaDelimitedStringFromCollection(instantaneousPropertyNames)); |
| 125 | + } |
| 126 | + if (accumulatingPropertyNames != null && !accumulatingPropertyNames.isEmpty()) { |
| 127 | + postBody.set("accumulatingPropertyNames", commaDelimitedStringFromCollection(accumulatingPropertyNames)); |
| 128 | + } |
| 129 | + if (statusPropertyNames != null && !statusPropertyNames.isEmpty()) { |
| 130 | + postBody.set("statusPropertyNames", commaDelimitedStringFromCollection(statusPropertyNames)); |
| 131 | + } |
| 132 | + return postBody; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments