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; } }
@Override public String toString() { return name + " [" + type.getName() + ']'; }
/** * Returns true if given type is named "content", as it's a reserved type name for blobs. */ public static boolean isContentType(Type type) { if (type != null && type.getName().equals(CONTENT)) { return true; } return false; }
@Override public void registerType(Type type) { types.put(type.getName(), type); }
protected void registerType(Type type) { types.put(type.getName(), type); }
@Override protected Node getChildForWrite(Node node, String name, Type type) throws PropertyException { return session.getChildPropertyForWrite(node, name, type.getName()); }
@Override protected Node getChild(Node node, String name, Type type) throws PropertyException { return session.getChildProperty(node, name, type.getName()); }
@SuppressWarnings({ "unchecked" }) private <T> T[] castArrayPropertyValue(Type type, List<Object> values) throws IOException { if (type instanceof StringType) { return values.toArray((T[]) Array.newInstance(String.class, values.size())); } else if (type instanceof BooleanType) { return values.toArray((T[]) Array.newInstance(Boolean.class, values.size())); } else if (type instanceof LongType) { return values.toArray((T[]) Array.newInstance(Long.class, values.size())); } else if (type instanceof DoubleType) { return values.toArray((T[]) Array.newInstance(Double.class, values.size())); } else if (type instanceof IntegerType) { return values.toArray((T[]) Array.newInstance(Integer.class, values.size())); } else if (type instanceof BinaryType) { return values.toArray((T[]) Array.newInstance(Byte.class, values.size())); } else if (type instanceof DateType) { return values.toArray((T[]) Array.newInstance(Calendar.class, values.size())); } throw new MarshallingException("Primitive type not found: " + type.getName()); }
private void inferSuperType(DocumentType docType) { Type superType = docType.getSuperType(); if (superType != null) { documentSuperTypes.put(docType.getName(), superType.getName()); } }
protected boolean isDomain(DocumentModel documentModel) { Type type = documentModel.getDocumentType(); while (type != null) { if ("Domain".equals(type.getName())) { return true; } type = type.getSuperType(); } return false; }
protected boolean isTypeRelation(String typeName) { do { if (TYPE_RELATION.equals(typeName)) { return true; } Type t = schemaManager.getDocumentType(typeName); if (t != null) { t = t.getSuperType(); } typeName = t == null ? null : t.getName(); } while (typeName != null); return false; }
protected void setOptions(Map<String, String> options) { for (Entry<String, String> option : options.entrySet()) { String key = option.getKey(); String type = option.getValue(); if (key.equals(OPTION_DOCTYPE)) { SchemaManager sm = Framework.getService(SchemaManager.class); DocumentType documentType = sm.getDocumentType(type); if (documentType == null) { throw new IllegalArgumentException("Unknown type: " + type + " for graph: " + name); } Type[] th = documentType.getTypeHierarchy(); String baseType = th.length == 0 ? type : th[th.length - 1].getName(); if (!REL_TYPE.equals(baseType)) { throw new IllegalArgumentException("Not a Relation type: " + type + " for graph: " + name); } docType = type; } } }
/** * * @param document * @param type * @return true if document extends a document with givent type. */ public static boolean isSubTypeOf(DocumentModel document, String type) { DocumentType documentType = document.getDocumentType(); if (documentType != null) { Type superType = documentType.getSuperType(); if (superType != null) { return StringUtils.equals(type, superType.getName()); } } return false; }
@Override public String getName(Type input) { return context.getService().encodeName(input.getName()); }
protected static Object checkBoolValue(String nxqlName, Object value) { if (!"0".equals(value) && !"1".equals(value)) { return value; } switch (nxqlName) { case NXQL.ECM_ISPROXY: case NXQL.ECM_ISCHECKEDIN: case NXQL.ECM_ISTRASHED: case NXQL.ECM_ISVERSION: case NXQL.ECM_ISVERSION_OLD: case NXQL.ECM_ISLATESTMAJORVERSION: case NXQL.ECM_ISLATESTVERSION: break; default: SchemaManager schemaManager = Framework.getService(SchemaManager.class); Field field = schemaManager.getField(nxqlName); if (field == null || !BooleanType.ID.equals(field.getType().getName())) { return value; } } return "0".equals(value) ? "false" : "true"; }
/** * 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); } } }
protected String guessFieldType(String field) { String fieldType; if (ES_SCORE_FIELD.equals(field)) { // this special field should not have an unmappedType return null; } try { SchemaManager schemaManager = Framework.getService(SchemaManager.class); fieldType = schemaManager.getField(field).getType().getName(); } catch (NullPointerException e) { // probably an internal field without schema fieldType = "keyword"; } switch (fieldType) { case "integer": case "long": case "boolean": case "date": return fieldType; } return "keyword"; }
@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; }
@Override protected void writeEntityBody(DocumentType docType, JsonGenerator jg) throws IOException { jg.writeStringField("name", docType.getName()); if (docType.getSuperType() != null) { jg.writeStringField("parent", docType.getSuperType().getName()); } else { jg.writeNullField("parent"); } jg.writeArrayFieldStart("facets"); for (String facet : docType.getFacets()) { jg.writeString(facet); } jg.writeEndArray(); jg.writeArrayFieldStart("schemas"); Writer<Schema> schemaWriter = registry.getWriter(ctx, Schema.class, APPLICATION_JSON_TYPE); for (Schema schema : docType.getSchemas()) { OutputStream out = new OutputStreamWithJsonWriter(jg); schemaWriter.write(schema, Schema.class, Schema.class, APPLICATION_JSON_TYPE, out); } jg.writeEndArray(); }