|
21 | 21 | import java.util.Iterator;
|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.Map;
|
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Collectors; |
24 | 26 | import javax.jcr.Node;
|
| 27 | +import javax.jcr.PathNotFoundException; |
25 | 28 | import javax.jcr.Property;
|
26 | 29 | import javax.jcr.PropertyIterator;
|
27 | 30 | import javax.jcr.PropertyType;
|
@@ -194,8 +197,55 @@ private RestQueryResult.RestRow createRestRow( Session session,
|
194 | 197 | Row resultRow ) throws RepositoryException {
|
195 | 198 | RestQueryResult.RestRow restRow = restQueryResult.new RestRow();
|
196 | 199 | Map<Value, String> binaryPropertyPaths = null;
|
197 |
| - |
| 200 | + Map<String, Node> nodesBySelectorName = null; |
| 201 | + Node defaultNode = null; |
| 202 | + String defaultSelectorName = null; |
| 203 | + try { |
| 204 | + defaultNode = resultRow.getNode(); |
| 205 | + defaultSelectorName = result.getSelectorNames()[0]; |
| 206 | + } catch (RepositoryException e) { |
| 207 | + // there are multiple selectors.... |
| 208 | + nodesBySelectorName = Arrays.stream(result.getSelectorNames()) |
| 209 | + .collect(Collectors.toMap(Function.identity(), selectorName -> { |
| 210 | + try { |
| 211 | + return resultRow.getNode(selectorName); |
| 212 | + } catch (RepositoryException re) { |
| 213 | + throw new RuntimeException(re); |
| 214 | + } |
| 215 | + })); |
| 216 | + } |
| 217 | + |
198 | 218 | for (String columnName : columnNames) {
|
| 219 | + Node activeNode = defaultNode; |
| 220 | + String activeSelectorName = defaultSelectorName; |
| 221 | + if (defaultNode == null) { |
| 222 | + // there are multiple selectors so we must see which one refers to the current column... |
| 223 | + assert nodesBySelectorName != null; |
| 224 | + for (Map.Entry<String, Node> nodeBySelector : nodesBySelectorName.entrySet()) { |
| 225 | + String selectorName = nodeBySelector.getKey(); |
| 226 | + if (columnName.startsWith(selectorName)) { |
| 227 | + activeNode = nodeBySelector.getValue(); |
| 228 | + activeSelectorName = selectorName; |
| 229 | + break; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if (activeNode != null) { |
| 235 | + // the column name by default has the [selectorName].[propertyName] format... |
| 236 | + String propertyName = columnName.replaceFirst(activeSelectorName + "\\.", ""); |
| 237 | + Property property = null; |
| 238 | + try { |
| 239 | + // try to locate the actual property based on the column name |
| 240 | + property = activeNode.getProperty(propertyName); |
| 241 | + List<String> values = restPropertyValues(property, baseUrl, session); |
| 242 | + restRow.addValue(columnName, values.size() == 1 ? values.get(0) : values); |
| 243 | + continue; |
| 244 | + } catch (PathNotFoundException e) { |
| 245 | + // we didn't locate the actual node property, so just read the value from the result row |
| 246 | + } |
| 247 | + } |
| 248 | + |
199 | 249 | Value value = resultRow.getValue(columnName);
|
200 | 250 | if (value == null) {
|
201 | 251 | continue;
|
|
0 commit comments