/** * Array indexes must not end with `*` * @param indexes */ private void validateArrayIndexes(List<Index> indexes) { boolean endsWithAny = indexes.stream().flatMap(i -> i.getFields().stream()).map(f -> f.getField()).anyMatch(p -> p.getLast().equals(Path.ANY)); if(endsWithAny) { throw Error.get(MongoCrudConstants.ERR_INVALID_INDEX_FIELD); } }
/** * Get a reference to the hidden path's actual field. * * This does not guarantee the sub-path exists. * * @param path * @return */ public static Path getFieldForHidden(Path hiddenPath) { return hiddenPath.prefix(-2).mutableCopy().push(hiddenPath.getLast()); }
/** * Get a reference to the path's hidden sub-field. * * This does not guarantee the sub-path exists. * * @param path * @return */ public static Path getHiddenForField(Path path) { if (path.getLast().equals(Path.ANY)) { return path.prefix(-2).mutableCopy().push(HIDDEN_SUB_PATH).push(path.suffix(2)); } return path.prefix(-1).mutableCopy().push(HIDDEN_SUB_PATH).push(path.getLast()); }
/** * Modifies an existing node value * * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public JsonNode modify(Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Modifies an existing node value * * @param root The root node * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public static JsonNode modify(JsonNode root, Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(root, parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Modifies an existing node value * * @param root The root node * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public static JsonNode modify(JsonNode root, Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(root, parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Uses the current position of the cursor to translate the current node and any children it may have. * This is ultimately the driver behind this class and may be called recursively by implementing classes to process child nodes.<br> * <b>NOTE:</b> Calling method is responsible for moving the cursor to where it needs to be. * @param cursor - {@link JsonNodeCursor} * @param target - T */ protected void translate(JsonNodeCursor cursor, Object target) { JsonNode node = cursor.getCurrentNode(); FieldTreeNode fieldNode = entityMetadata.resolve(cursor.getCurrentPath()); Error.push(fieldNode.getFullPath().getLast()); try{ if (fieldNode instanceof SimpleField) { translate((SimpleField) fieldNode, node, target); } else if (fieldNode instanceof ObjectField) { translate((ObjectField) fieldNode, cursor, target); } else if (fieldNode instanceof ArrayField) { translate((ArrayField) fieldNode, cursor, target); } else{ throw Error.get(MetadataConstants.ERR_UNSUPPORTED_FEATURE + fieldNode.getClass().getName(), fieldNode.getFullPath().toString()); } } finally{ Error.pop(); } }
/** * Uses the current position of the cursor to translate the current node and any children it may have. * This is ultimately the driver behind this class and may be called recursively by implementing classes to process child nodes.<br> * <b>NOTE:</b> Calling method is responsible for moving the cursor to where it needs to be. * @param cursor - {@link JsonNodeCursor} * @param target - T */ protected void translate(JsonNodeCursor cursor, Object target) { JsonNode node = cursor.getCurrentNode(); FieldTreeNode fieldNode = entityMetadata.resolve(cursor.getCurrentPath()); Error.push(fieldNode.getFullPath().getLast()); try{ if (fieldNode instanceof SimpleField) { translate((SimpleField) fieldNode, node, target); } else if (fieldNode instanceof ObjectField) { translate((ObjectField) fieldNode, cursor, target); } else if (fieldNode instanceof ArrayField) { translate((ArrayField) fieldNode, cursor, target); } else{ throw Error.get(MetadataConstants.ERR_UNSUPPORTED_FEATURE + fieldNode.getClass().getName(), fieldNode.getFullPath().toString()); } } finally{ Error.pop(); } }