Search in sources :

Example 1 with IPropertyParent

use of io.apicurio.datamodels.core.models.common.IPropertyParent in project apicurio-data-models by Apicurio.

the class DeleteAllPropertiesCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[DeleteAllPropertiesCommand] Executing.");
    this._oldProperties = new ArrayList<>();
    IPropertyParent schema = (IPropertyParent) this._schemaPath.resolve(document);
    if (this.isNullOrUndefined(schema)) {
        return;
    }
    schema.getPropertyNames().forEach(pname -> {
        Object data = JsonCompat.objectNode();
        JsonCompat.setPropertyString(data, Constants.PROP_NAME, pname);
        JsonCompat.setProperty(data, Constants.PROP_SCHEMA, Library.writeNode(schema.removeProperty(pname)));
        JsonCompat.setPropertyBoolean(data, Constants.PROP_REQUIRED, schema.isPropertyRequired(pname));
        schema.unsetPropertyRequired(pname);
        this._oldProperties.add(data);
    });
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent)

Example 2 with IPropertyParent

use of io.apicurio.datamodels.core.models.common.IPropertyParent in project apicurio-data-models by Apicurio.

the class ChangePropertyTypeCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[ChangePropertyTypeCommand] Executing: " + this._newType);
    IPropertySchema prop = (IPropertySchema) this._propPath.resolve(document);
    if (this.isNullOrUndefined(prop)) {
        return;
    }
    IPropertyParent parent = (IPropertyParent) ((Node) prop).parent();
    List<String> required = parent.getRequiredProperties();
    // Save the old info (for later undo operation)
    this._oldProperty = Library.writeNode((Node) prop);
    this._oldRequired = ModelUtils.isDefined(required) && required.size() > 0 && required.indexOf(prop.getPropertyName()) != -1;
    // Update the schema's type
    SimplifiedTypeUtil.setSimplifiedType((Schema) prop, this._newType);
    if (!this.isNullOrUndefined(this._newType.required)) {
        // Going from optional to required
        if (Boolean.TRUE.equals(this._newType.required) && !this._oldRequired) {
            if (this.isNullOrUndefined(required)) {
                required = new ArrayList<>();
                parent.setRequiredProperties(required);
                this._nullRequired = true;
            }
            required.add(prop.getPropertyName());
        }
        // Going from required to optional - remove property name from required list.
        if (Boolean.FALSE.equals(this._newType.required) && this._oldRequired) {
            required.remove(required.indexOf(prop.getPropertyName()));
        }
    }
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) Node(io.apicurio.datamodels.core.models.Node) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Example 3 with IPropertyParent

use of io.apicurio.datamodels.core.models.common.IPropertyParent in project apicurio-data-models by Apicurio.

the class DeleteAllPropertiesCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[DeleteAllPropertiesCommand] Reverting.");
    if (this.isNullOrUndefined(this._oldProperties) || this._oldProperties.size() == 0) {
        return;
    }
    IPropertyParent schema = (IPropertyParent) this._schemaPath.resolve(document);
    if (this.isNullOrUndefined(schema)) {
        return;
    }
    this._oldProperties.forEach(oldProp -> {
        String name = JsonCompat.getPropertyString(oldProp, Constants.PROP_NAME);
        Object schemaObj = JsonCompat.getProperty(oldProp, Constants.PROP_SCHEMA);
        Boolean required = JsonCompat.getPropertyBoolean(oldProp, Constants.PROP_REQUIRED);
        Schema prop = schema.createPropertySchema(name);
        Library.readNode(schemaObj, prop);
        schema.addProperty(name, prop);
        if (ModelUtils.isDefined(required) && required == Boolean.TRUE) {
            schema.setPropertyRequired(name);
        }
    });
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) Schema(io.apicurio.datamodels.core.models.common.Schema)

Example 4 with IPropertyParent

use of io.apicurio.datamodels.core.models.common.IPropertyParent in project apicurio-data-models by Apicurio.

the class ChangePropertyTypeCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[ChangePropertyTypeCommand] Reverting.");
    IPropertySchema prop = (IPropertySchema) this._propPath.resolve(document);
    if (this.isNullOrUndefined(prop)) {
        return;
    }
    IPropertyParent parent = (IPropertyParent) ((Node) prop).parent();
    List<String> required = parent.getRequiredProperties();
    boolean wasRequired = ModelUtils.isDefined(required) && required.size() > 0 && required.indexOf(prop.getPropertyName()) != -1;
    Schema oldProp = parent.createPropertySchema(this._propName);
    Library.readNode(this._oldProperty, oldProp);
    // Restore the schema attributes
    restoreSchemaInternalProperties((Schema) prop, oldProp);
    // Restore the "required" flag
    if (!this.isNullOrUndefined(this._newType.required)) {
        if (this._nullRequired) {
            parent.setRequiredProperties(null);
        } else {
            // Restoring optional from required
            if (wasRequired && !this._oldRequired) {
                required.remove(required.indexOf(prop.getPropertyName()));
            }
            // Restoring required from optional
            if (!wasRequired && this._oldRequired) {
                required.add(prop.getPropertyName());
            }
        }
    }
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) Schema(io.apicurio.datamodels.core.models.common.Schema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Example 5 with IPropertyParent

use of io.apicurio.datamodels.core.models.common.IPropertyParent in project apicurio-data-models by Apicurio.

the class DeletePropertyCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[DeletePropertyCommand] Executing.");
    this._oldProperty = null;
    IPropertySchema property = (IPropertySchema) this._propertyPath.resolve(document);
    if (this.isNullOrUndefined(property)) {
        return;
    }
    IPropertyParent schema = (IPropertyParent) ((Node) property).parent();
    this._oldProperty = Library.writeNode(schema.removeProperty(this._propertyName));
    this._oldRequired = schema.isPropertyRequired(this._propertyName);
    if (this._oldRequired) {
        schema.unsetPropertyRequired(this._propertyName);
    }
}
Also used : IPropertyParent(io.apicurio.datamodels.core.models.common.IPropertyParent) IPropertySchema(io.apicurio.datamodels.core.models.common.IPropertySchema)

Aggregations

IPropertyParent (io.apicurio.datamodels.core.models.common.IPropertyParent)6 IPropertySchema (io.apicurio.datamodels.core.models.common.IPropertySchema)4 Schema (io.apicurio.datamodels.core.models.common.Schema)3 Node (io.apicurio.datamodels.core.models.Node)1