protected String getName(Property property) { QName name = property.getField().getName(); return prefixed ? name.getPrefixedName() : name.getLocalName(); }
pathTokens.add(schema.getName()); for (PathNode node : path) { String name = node.getField().getName().getLocalName(); pathTokens.add(name);
/** * Validates sub fields for given complex field. * * @since 7.1 */ @SuppressWarnings("unchecked") private List<ConstraintViolation> validateComplexTypeField(Schema schema, List<PathNode> path, Field field, Object value) { assert field.getType().isComplexType(); List<ConstraintViolation> violations = new ArrayList<>(); ComplexType complexType = (ComplexType) field.getType(); // this code does not support other type than Map as value if (!(value instanceof Map)) { return violations; } Map<String, Object> map = (Map<String, Object>) value; for (Field child : complexType.getFields()) { Object item = map.get(child.getName().getLocalName()); List<PathNode> subPath = new ArrayList<>(path); subPath.add(new PathNode(child)); violations.addAll(validateAnyTypeField(schema, subPath, child, item, true)); } return violations; }
int index = Integer.parseInt(xpathToken); path.add(new PathNode(field, index)); } else if (xpathToken.equals(itemField.getName().getLocalName())) {
public static DataModel cloneDataModel(Schema schema, DataModel data) { DataModel dm = new DataModelImpl(schema.getName()); for (Field field : schema.getFields()) { String key = field.getName().getLocalName(); Object value; try { value = data.getData(key); } catch (PropertyException e1) { continue; } if (value == null) { continue; } Object clone = cloneField(field, key, value); dm.setData(key, clone); } return dm; }
@Override public String getFieldName() { return field.getName().getLocalName(); }
protected void addField(Field field) { QName name = field.getName(); fields.put(name, field); fieldsByName.put(name.getLocalName(), field); fieldsByName.put(name.getPrefixedName(), field); }
private void setStillSchema(Schema schema, DocumentModel doc) { Map<String, Object> data = new HashMap<String, Object>(); Collection<Field> fields = schema.getFields(); Iterator<Field> iterator = fields.iterator(); while(iterator.hasNext()){ Field field = iterator.next(); String name = field.getName().getLocalName(); data.put(name, null); } doc.setProperties(schema.getName(), data); }
/** * Converts the value of the given core property to JSON. * <p /> * CAUTION: this method will write the field name to {@link JsonGenerator} with its prefix without writing the start * and the end of object. * * @since 9.1 */ public void writeProperty(JsonGenerator jg, Property prop) throws PropertyException, IOException { PropertyConsumer fieldNameWriter; if (prefix == null) { fieldNameWriter = (j, p) -> j.writeFieldName(p.getName()); } else { fieldNameWriter = (j, p) -> j.writeFieldName(prefix + ':' + p.getField().getName().getLocalName()); } writeProperty(jg, prop, fieldNameWriter); }
@Override public Map<String, Object> newInstance() { if (TypeConstants.isContentType(this)) { // NXP-912: should return null for a blob. Since there is no // pluggable adapter mechanism on types, and since document model // properties consider that every complex property named "content" // should be dealt with a BlobProperty, this is hardcoded here. return null; } Map<String, Object> map = new HashMap<String, Object>(); for (Field field : fields.values()) { Object value; Type type = field.getType(); if (type.isComplexType()) { value = type.newInstance(); } else if (type.isListType()) { value = new ArrayList<Object>(); } else { value = field.getDefaultValue(); } map.put(field.getName().getLocalName(), value); } return map; }
@Override public Schema createSchema(ComplexType input) { Schema schema = Schema.createRecord(getName(input), null, input.getNamespace().prefix, false); List<Field> fields = new ArrayList<>(input.getFields().size()); for (org.nuxeo.ecm.core.schema.types.Field f : context.sort(input.getFields())) { String fieldName = context.getService().encodeName(f.getName().getLocalName()); Schema fieldSchema = context.createSchema(f.getType()); if (f.isNillable()) { fieldSchema = nullable(fieldSchema); } fields.add(new Field(fieldName, fieldSchema, null, (Object) null)); } schema.setFields(fields); return schema; }
private void writeSchemaProperties(JsonGenerator jg, DocumentModel doc, String schemaName) throws IOException { Writer<Property> propertyWriter = registry.getWriter(ctx, Property.class, APPLICATION_JSON_TYPE); // provides the current document to the property marshaller try (Closeable resource = ctx.wrap().with(ENTITY_TYPE, doc).open()) { Schema schema = schemaManager.getSchema(schemaName); String prefix = schema.getNamespace().prefix; if (prefix == null || prefix.length() == 0) { prefix = schemaName; } prefix = prefix + ":"; for (Field field : schema.getFields()) { String prefixedName = prefix + field.getName().getLocalName(); jg.writeFieldName(prefixedName); Property property = doc.getProperty(prefixedName); OutputStream out = new OutputStreamWithJsonWriter(jg); propertyWriter.write(property, Property.class, Property.class, APPLICATION_JSON_TYPE, out); } } }
private void writeSchemaProperties(JsonGenerator jg, DocumentModel doc, String schemaName) throws IOException { Writer<Property> propertyWriter = registry.getWriter(ctx, Property.class, APPLICATION_JSON_TYPE); // provides the current document to the property marshaller try (Closeable resource = ctx.wrap().with(ENTITY_TYPE, doc).open()) { Schema schema = schemaManager.getSchema(schemaName); String prefix = schema.getNamespace().prefix; if (prefix == null || prefix.length() == 0) { prefix = schemaName; } prefix = prefix + ":"; for (Field field : schema.getFields()) { String prefixedName = prefix + field.getName().getLocalName(); jg.writeFieldName(prefixedName); Property property = doc.getProperty(prefixedName); OutputStream out = new OutputStreamWithJsonWriter(jg); propertyWriter.write(property, Property.class, Property.class, APPLICATION_JSON_TYPE, out); } } } }
protected void writeUserProfile(JsonGenerator jg, DocumentModel up) throws IOException { Writer<Property> propertyWriter = registry.getWriter(ctx, Property.class, APPLICATION_JSON_TYPE); Schema schema = schemaManager.getSchema(USER_PROFILE_SCHEMA); // provides the user profile document to the property marshaller try (Closeable resource = ctx.wrap().with(ENTITY_TYPE, up).open()) { for (Field field : schema.getFields()) { jg.writeFieldName(field.getName().getLocalName()); Property property = up.getProperty(field.getName().getPrefixedName()); OutputStream out = new OutputStreamWithJsonWriter(jg); propertyWriter.write(property, Property.class, Property.class, APPLICATION_JSON_TYPE, out); } } } }
/** * @since 8.3 */ public static void writeVariables(DocumentRoute item, JsonGenerator jg, MarshallerRegistry registry, RenderingContext ctx, SchemaManager schemaManager) throws IOException, JsonGenerationException { String facet = (String) item.getDocument().getPropertyValue(GraphRoute.PROP_VARIABLES_FACET); if (StringUtils.isNotBlank(facet)) { CompositeType type = schemaManager.getFacet(facet); if (type != null) { boolean hasFacet = item.getDocument().hasFacet(facet); Writer<Property> propertyWriter = registry.getWriter(ctx, Property.class, APPLICATION_JSON_TYPE); // provides the current route to the property marshaller try (Closeable resource = ctx.wrap() .with(DocumentModelJsonWriter.ENTITY_TYPE, item.getDocument()) .open()) { for (Field f : type.getFields()) { String name = f.getName().getLocalName(); Property property = hasFacet ? item.getDocument().getProperty(name) : null; OutputStream out = new OutputStreamWithJsonWriter(jg); jg.writeFieldName(name); propertyWriter.write(property, Property.class, Property.class, APPLICATION_JSON_TYPE, out); } } } } }
protected void readDocumentSchemas(Element element, DocumentModel doc, boolean inlineBlobs) throws IOException { SchemaManager schemaManager = Framework.getService(SchemaManager.class); String[] schemaNames = doc.getSchemas(); for (String schemaName : schemaNames) { Element schemaElement = element.addElement(ExportConstants.SCHEMA_TAG).addAttribute("name", schemaName); Schema schema = schemaManager.getSchema(schemaName); Namespace targetNs = schema.getNamespace(); // If namespace prefix is empty, use schema name if (StringUtils.isEmpty(targetNs.prefix)) { targetNs = new Namespace(targetNs.uri, schema.getName()); } schemaElement.addNamespace(targetNs.prefix, targetNs.uri); DataModel dataModel = doc.getDataModel(schemaName); for (Field field : schema.getFields()) { Object value = dataModel.getData(field.getName().getLocalName()); readProperty(schemaElement, targetNs, field, value, inlineBlobs); } } }
/** * @since 8.3 */ public static void writeTaskVariables(GraphNode node, JsonGenerator jg, MarshallerRegistry registry, RenderingContext ctx, SchemaManager schemaManager) throws IOException { if (node == null || node.getDocument() == null) { return; } String facet = (String) node.getDocument().getPropertyValue(GraphNode.PROP_VARIABLES_FACET); if (StringUtils.isNotBlank(facet)) { CompositeType type = schemaManager.getFacet(facet); if (type != null) { boolean hasFacet = node.getDocument().hasFacet(facet); Writer<Property> propertyWriter = registry.getWriter(ctx, Property.class, APPLICATION_JSON_TYPE); // provides the current route to the property marshaller try (Closeable resource = ctx.wrap() .with(DocumentModelJsonWriter.ENTITY_TYPE, node.getDocument()) .open()) { for (Field f : type.getFields()) { String name = f.getName().getLocalName(); Property property = hasFacet ? node.getDocument().getProperty(name) : null; OutputStream out = new OutputStreamWithJsonWriter(jg); jg.writeFieldName(name); propertyWriter.write(property, Property.class, Property.class, APPLICATION_JSON_TYPE, out); } } } } }
/** * Create a schema from a ComplexType * * @since 5.7 * @param complexType * @param name * @param ns */ public SchemaImpl(ComplexType complexType, String name, Namespace ns, boolean isVersionWritabe) { super(null, SchemaNames.SCHEMAS, name, ns); this.isVersionWritabe = isVersionWritabe; if (complexType != null) { for (Field field : complexType.getFields()) { QName fieldname = QName.valueOf(field.getName().getLocalName(), ns.prefix); Type type = field.getType(); String defaultValue = type.encode(field.getDefaultValue()); Set<Constraint> constraint = field.getConstraints(); FieldImpl newField = new FieldImpl(fieldname, this, type, defaultValue, 0, constraint); newField.setConstant(field.isConstant()); addField(newField); } } }
private static void avoidBlobUpdate(Property propToClean, DocumentModel docRef) { if (propToClean instanceof BlobProperty) { // if the blob used to exist if (propToClean.getValue() == null) { try { Serializable value = docRef.getPropertyValue(propToClean.getXPath()); propToClean.setValue(value); } catch (PropertyNotFoundException e) { // As the blob property doesn't exist in the document in the first place, ignore the operation } } } else if (propToClean instanceof ComplexProperty) { ComplexProperty complexPropToClean = (ComplexProperty) propToClean; for (Field field : complexPropToClean.getType().getFields()) { Property childPropToClean = complexPropToClean.get(field.getName().getLocalName()); avoidBlobUpdate(childPropToClean, docRef); } } else if (propToClean instanceof ListProperty) { ListProperty listPropToClean = (ListProperty) propToClean; for (int i = 0; i < listPropToClean.size(); i++) { Property elPropToClean = listPropToClean.get(i); avoidBlobUpdate(elPropToClean, docRef); } } }
public static void applyAllPropertyValues(DocumentModel src, DocumentModel dst) { SchemaManager service = Framework.getService(SchemaManager.class); DocumentType type = service.getDocumentType(src.getType()); String[] schemas = type.getSchemaNames(); for (String schemaName : schemas) { Schema schema = service.getSchema(schemaName); DataModelImpl dataModel = (DataModelImpl) dst.getDataModel(schemaName); DataModelImpl fromDataModel = (DataModelImpl) src.getDataModel(schemaName); for (Field field : schema.getFields()) { String fieldName = field.getName().getLocalName(); Serializable data = (Serializable) fromDataModel.getData(fieldName); try { if (!(dataModel.getDocumentPart().get(fieldName) instanceof BlobProperty)) { dataModel.setData(fieldName, data); } else { dataModel.setData(fieldName, decodeBlob(data)); } } catch (PropertyNotFoundException e) { continue; } } } }