function setEntityFromJson()

in ui-modules/blueprint-composer/app/components/util/model/entity.model.js [1149:1225]


function setEntityFromJson(incomingModel, setChildren = true) {
    // ideally we'd be able to detect the type of `incomingModel`;
    // imagine we had `DslExpression` as a type and a `DslEntitySpecExpression` as a subclass of `DslExpression`, then:
    // * this code should throw an error if it's not-null, not undefined, but not a `DslExpression`.  
    // * the UI should then render it differently whether it is a `DslEntitySpecExpression` or not.
    // but for now we have the isDslish hack, and the UI renders it based on a REPLACED_DSL_ENTITYSPEC marker
    if (incomingModel && incomingModel.constructor.name !== 'Object') {
        if (isDslish(incomingModel)) {
            // no error
        } else {
            throw new TypeError('Entity cannot be set from [' + incomingModel.constructor.name + '] ... please supply an [Object]');
        }
    }
    METADATA.get(this).clear();
    return Object.keys(incomingModel).reduce((self, key)=> {
        if (UNSUPPORTED_CATALOG_FIELDS.indexOf(key) !== -1) {
            throw new Error('Catalog format not supported ... unsupported field [' + key + ']');
        }
        if (Object.keys(UNSUPPORTED_FIELDS).indexOf(key) !== -1) {
            throw new Error(`Field [${key}] not supported ... ${UNSUPPORTED_FIELDS[key]}`);
        }
        switch (key) {
            case FIELD.CHILDREN:
            case FIELD.SERVICES:
                if (setChildren) {
                    self.setChildrenFromJson(incomingModel[key]);
                }
                break;
            case FIELD.CONFIG:
                self.setConfigFromJson(incomingModel[key]);
                break;
            case FIELD.PARAMETERS:
                self.setParametersFromJson(incomingModel[key]);
                break;
            case FIELD.ENRICHERS:
                self.setEnrichersFromJson(incomingModel[key]);
                break;
            case FIELD.POLICIES:
                self.setPoliciesFromJson(incomingModel[key]);
                break;
            case FIELD.LOCATION:
                this.location = incomingModel[key];
                break;
            case FIELD.LOCATIONS:
                self.setLocationsFromJson(incomingModel[key]);
                break;
            case FIELD.INITIALIZERS:
                self.setInitializersFromJson(incomingModel[key]);
                break;
            case FIELD.TYPE:
                let parsedType = incomingModel[key].split(':');
                self.addMetadata(key, parsedType[0]);
                self.miscData.delete('version');
                if (parsedType.length > 1) {
                    self.miscData.set('version', parsedType[1]);
                }
                break;
            // This field is use to pass back information about a virtual item. A virtual item is an item that is not present
            // within the Brooklyn Catalog but translate to a real blueprint. As the composer is bidirectional, it requires
            // at least the virtual type that will replace the real type within the internal model. The composer will then
            // use its standard routines to get back the rest of the information. This needs to be used in concert with a
            // customised PaletteApiProvider implementation to get back the information about the virtual item.
            case FIELD.COMPOSER_META:
                let composerMetadata = incomingModel[key];
                if (composerMetadata.hasOwnProperty('virtualType')) {
                    self.addMetadata(FIELD.TYPE, composerMetadata.virtualType);
                }
                if (composerMetadata.hasOwnProperty('tags')) {
                    self.addMetadata(FIELD.TAGS, composerMetadata.virtualType);
                }
                break;
            default:
                self.addMetadata(key, incomingModel[key]);
        }
        return self;
    }, this);
}