| ... |
... |
@@ -23,6 +23,7 @@ package org.nuiton.topia.persistence.support; |
|
23
|
23
|
*/
|
|
24
|
24
|
|
|
25
|
25
|
import fr.ird.observe.datasource.SqlHelper;
|
|
|
26
|
+import fr.ird.observe.dto.ToolkitId;
|
|
26
|
27
|
import fr.ird.observe.entities.Entity;
|
|
27
|
28
|
import fr.ird.observe.spi.context.DataDtoEntityContext;
|
|
28
|
29
|
import org.hibernate.query.NativeQuery;
|
| ... |
... |
@@ -31,14 +32,18 @@ import org.hibernate.query.QueryParameter; |
|
31
|
32
|
|
|
32
|
33
|
import javax.persistence.Parameter;
|
|
33
|
34
|
import java.sql.Timestamp;
|
|
|
35
|
+import java.util.Collection;
|
|
34
|
36
|
import java.util.Collections;
|
|
35
|
37
|
import java.util.Comparator;
|
|
36
|
38
|
import java.util.LinkedList;
|
|
37
|
39
|
import java.util.List;
|
|
38
|
40
|
import java.util.Map;
|
|
|
41
|
+import java.util.Objects;
|
|
39
|
42
|
import java.util.function.BiConsumer;
|
|
|
43
|
+import java.util.function.BiFunction;
|
|
40
|
44
|
import java.util.function.Function;
|
|
41
|
45
|
import java.util.stream.Collectors;
|
|
|
46
|
+import java.util.stream.Stream;
|
|
42
|
47
|
|
|
43
|
48
|
|
|
44
|
49
|
/**
|
| ... |
... |
@@ -119,4 +124,36 @@ public interface QuerySupport { |
|
119
|
124
|
default int execute(NativeQuery<?> sqlQuery) {
|
|
120
|
125
|
return sqlQuery.executeUpdate();
|
|
121
|
126
|
}
|
|
|
127
|
+
|
|
|
128
|
+ class MapBuilder<K extends ToolkitId, V> {
|
|
|
129
|
+
|
|
|
130
|
+ private final Map<K, List<V>> result;
|
|
|
131
|
+
|
|
|
132
|
+ private final Map<String, K> keysById;
|
|
|
133
|
+ private final BiFunction<K, Object[], V> valueCreator;
|
|
|
134
|
+
|
|
|
135
|
+ private K currentKey;
|
|
|
136
|
+ private List<V> currentList;
|
|
|
137
|
+
|
|
|
138
|
+ public MapBuilder(Map<K, List<V>> result, Collection<K> keys, BiFunction<K, Object[], V> valueCreator) {
|
|
|
139
|
+ this.result = result;
|
|
|
140
|
+ this.keysById = keys.stream().collect(Collectors.toMap(ToolkitId::getId, Function.identity()));
|
|
|
141
|
+ this.valueCreator = valueCreator;
|
|
|
142
|
+ keysById.values().forEach(r -> result.put(r, new LinkedList<>()));
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ public void addRow(Object[] row) {
|
|
|
146
|
+ String keyId = (String) row[0];
|
|
|
147
|
+ if (currentKey == null || !Objects.equals(currentKey.getId(), keyId)) {
|
|
|
148
|
+ currentKey = keysById.get(keyId);
|
|
|
149
|
+ currentList = result.get(currentKey);
|
|
|
150
|
+ }
|
|
|
151
|
+ currentList.add(valueCreator.apply(currentKey, row));
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ public Map<K, List<V>> build(Stream<Object[]> data) {
|
|
|
155
|
+ data.forEach(this::addRow);
|
|
|
156
|
+ return result;
|
|
|
157
|
+ }
|
|
|
158
|
+ }
|
|
122
|
159
|
} |