protected boolean compatibleTypes(Type targetType, Type sourceType) { if (!sourceType.getName().equals(targetType.getName())) { return false; } if (sourceType.isComplexType()) { for (Field field : ((ComplexType) sourceType).getFields()) { Field targetField = ((ComplexType) targetType).getField(field.getName()); if (targetField == null || !field.getType().equals(targetField.getType())) { return false; } } } if (sourceType.isListType()) { if (!((ListType) sourceType).getFieldType().equals(((ListType) targetType).getFieldType())) { return false; } if (((ListType) sourceType).getFieldType().isComplexType()) { return compatibleTypes(((ListType) targetType).getFieldType(), ((ListType) sourceType).getFieldType()); } } return true; } }
/** * This method should be the only one to create {@link ConstraintViolation}. * * @since 7.1 */ private List<ConstraintViolation> validateSimpleTypeField(Schema schema, List<PathNode> path, Field field, Object value) { Type type = field.getType(); assert type.isSimpleType() || type.isListType(); // list type to manage ArrayProperty List<ConstraintViolation> violations = new ArrayList<>(); Set<Constraint> constraints; if (type.isListType()) { // ArrayProperty constraints = ((ListType) type).getFieldType().getConstraints(); } else { constraints = field.getConstraints(); } for (Constraint constraint : constraints) { if (!constraint.validate(value)) { ConstraintViolation violation = new ConstraintViolation(schema, path, constraint, value); violations.add(violation); } } return violations; }
continue; // not binary text } else if (type.isListType()) { Type fieldType = ((ListType) type).getFieldType(); if (fieldType.isComplexType()) { findBlobPaths((ComplexType) fieldType, fieldPath + "/*", schema, paths);
/** * Returns the MongoDB field for this reference. */ public FieldInfo walkReference(Reference ref) { FieldInfo fieldInfo = walkReference(ref.name); if (DATE_CAST.equals(ref.cast)) { Type type = fieldInfo.type; if (!(type instanceof DateType || (type instanceof ListType && ((ListType) type).getFieldType() instanceof DateType))) { throw new QueryParseException("Cannot cast to " + ref.cast + ": " + ref.name); } // fieldInfo.isDateCast = true; } return fieldInfo; }
public static List<Object> decodeList(ListType lt, ArrayNode jsonArray) { List<Object> result = new ArrayList<Object>(); Type currentObjectType = lt.getFieldType(); for (int i = 0; i < jsonArray.size(); i++) { JsonNode node = jsonArray.get(i); if (node.isArray()) { result.add(decodeList((ListType) currentObjectType, (ArrayNode) node)); } else if (node.isObject()) { result.add(decode((ComplexType) currentObjectType, (ObjectNode) node)); } else if (node.isTextual()) { result.add(node.textValue()); } else if (node.isNumber()) { result.add(node.numberValue()); } else if (node.isBoolean()) { result.add(node.booleanValue()); } } return result; }
protected Field resolveSubField(ListType listType, String subName, boolean fallbackOnSubElement) { Type itemType = listType.getFieldType(); if (itemType.isComplexType() && subName != null) { ComplexType complexType = (ComplexType) itemType; Field subField = complexType.getField(subName); return subField; } if (fallbackOnSubElement) { return listType.getField(); } return null; }
protected static Type getBaseType(Type type) { if (type instanceof SimpleTypeImpl) { return getBaseType(type.getSuperType()); } if (type instanceof ListType) { return getBaseType(((ListType) type).getFieldType()); } return type; }
private void fillScalarProperty(Property property, JsonNode jn) throws IOException { if ((property instanceof ArrayProperty) && jn.isArray()) { List<Object> values = new ArrayList<>(); Iterator<JsonNode> it = jn.elements(); JsonNode item; Type fieldType = ((ListType) property.getType()).getFieldType(); while (it.hasNext()) { item = it.next(); values.add(getScalarPropertyValue(property, item, fieldType)); } property.setValue(castArrayPropertyValue(((SimpleType) fieldType).getPrimitiveType(), values)); } else { property.setValue(getScalarPropertyValue(property, jn, property.getType())); } }
@Override protected List<Node> updateList(Node node, String name, Property property) throws PropertyException { Collection<Property> properties = property.getChildren(); List<Node> childNodes = getChildAsList(node, name); int oldSize = childNodes.size(); int newSize = properties.size(); // remove extra list elements if (oldSize > newSize) { for (int i = oldSize - 1; i >= newSize; i--) { session.removeProperty(childNodes.remove(i)); } } // add new list elements if (oldSize < newSize) { String typeName = ((ListType) property.getType()).getFieldType().getName(); for (int i = oldSize; i < newSize; i++) { Node childNode = session.addChildProperty(node, name, Long.valueOf(i), typeName); childNodes.add(childNode); } } return childNodes; }
/** * Check if the given field type store a list of values and if the given value is compatible with the given type. We * assume the Type store a list of scalar values, not complex types. */ protected void checkFieldType(Type type, Object value) throws OperationException { if (!type.isListType()) { throw new OperationException("Only multivalued types can be set using this operation"); } ListType listType = (ListType) type; Type itemType = listType.getFieldType(); if (itemType.isComplexType()) { throw new UnsupportedOperationException("Manage only lists of scalar items"); } try { if (itemType.convert(value) == null) { String exceptionReason = String.format("Given type \"%s\" value is not a %s type", value, itemType.getName()); throw new UnsupportedOperationException(exceptionReason); } } catch (TypeException | ClassCastException e) { String exceptionReason = String.format("Given type \"%s\" value is not a %s type", value, itemType.getName()); throw new OperationException(exceptionReason, e); } } }
@OperationMethod(collector = DocumentModelCollector.class) public DocumentModel run(DocumentModel doc) throws OperationException, IOException { Property complexProperty = doc.getProperty(xpath); ListType listType = (ListType) complexProperty.getField().getType(); if (!listType.getFieldType().isComplexType()) { throw new OperationException("Property type " + listType.getFieldType().getClass().getName() + " is not supported by this operation"); } List<Object> newVals = ComplexTypeJSONDecoder.decodeList(listType, complexJsonProperties); for (Object newVal : newVals) { complexProperty.addValue(newVal); } doc = session.saveDocument(doc); if (save) { session.save(); } return doc; }
/** * Checks if a field is a primitive type or array. */ private static boolean isScalarField(Field field) { Type fieldType = field.getType(); if (fieldType.isComplexType()) { // complex type return false; } if (!fieldType.isListType()) { // primitive type return true; } // array or complex list? return ((ListType) fieldType).getFieldType().isSimpleType(); }
protected void writeListProperty(JsonGenerator jg, Property prop, PropertyConsumer fieldNameWriter) throws PropertyException, IOException { // test if array/list is empty - don't write empty case if (!writeEmpty && (prop == null || (prop instanceof ArrayProperty && prop.getValue() == null) || (prop instanceof ListProperty && prop.getChildren().isEmpty()))) { return; } fieldNameWriter.accept(jg, prop); jg.writeStartArray(); if (prop instanceof ArrayProperty) { Object[] ar = (Object[]) prop.getValue(); if (ar == null) { jg.writeEndArray(); return; } Type type = ((ListType) prop.getType()).getFieldType(); for (Object o : ar) { jg.writeString(type.encode(o)); } } else { for (Property p : prop.getChildren()) { // it's a list of complex object, don't write field names writeProperty(jg, p, PropertyConsumer.nothing()); } } jg.writeEndArray(); }
protected void writeListProperty(JsonGenerator jg, Property prop) throws IOException { jg.writeStartArray(); if (prop instanceof ArrayProperty) { Object[] ar = (Object[]) prop.getValue(); if (ar == null) { jg.writeEndArray(); return; } Type itemType = ((ListType) prop.getType()).getFieldType(); ObjectResolver resolver = itemType.getObjectResolver(); String path = prop.getXPath(); for (Object o : ar) { if (!fetchProperty(jg, resolver, o, path)) { writeScalarPropertyValue(jg, ((SimpleType) itemType).getPrimitiveType(), o); } } } else { ListProperty listp = (ListProperty) prop; for (Property p : listp.getChildren()) { writeProperty(jg, p); } } jg.writeEndArray(); }
Type fieldType = listType.getFieldType(); if (fieldType.isSimpleType()) {
} else { Type fieldType = ((ListType) type).getFieldType(); if (fieldType.isSimpleType()) {
/** * Given a document property, updates its value with the given blob. The property can be a blob list or a blob. If a * blob list the blob is appended to the list, if a blob then it will be set as the property value. Both blob list * formats are supported: the file list (blob holder list) and simple blob list. */ public static void addBlob(Property p, Blob blob) throws PropertyException { if (p.isList()) { // detect if a list of simple blobs or a list of files (blob // holder) Type ft = ((ListProperty) p).getType().getFieldType(); if (ft.isComplexType() && ((ComplexType) ft).getFieldsCount() == 1) { p.addValue(createBlobHolderMap(blob)); } else { p.addValue(blob); } } else { p.setValue(blob); } }
ListType listType = (ListType) complexProperty.getField().getType(); if (!listType.getFieldType().isComplexType() && !listType.isListType()) { throw new OperationException("Property type is not supported by this operation"); ListType listType = (ListType) complexProperty.getField().getType(); if (!listType.getFieldType().isComplexType() && !listType.isListType()) { throw new OperationException("Property type is not supported by this operation");
ListType ltype = (ListType) type; if (ltype.isScalarList() && !decodeStringListAsJSON) { p.setValue(readStringList(value, (SimpleType) ltype.getFieldType())); return; } else {
Schema array = Schema.createArray(context.createSchema(((ListType) input).getFieldType())); String logicalType = ((ListType) input).isArray() ? "array" : "list"; new LogicalType(logicalType).addToSchema(array);