Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.regnosys.rosetta.generator.jsonschema.JsonSchemaMetaFieldGenerator Maven / Gradle / Ivy
package com.regnosys.rosetta.generator.jsonschema;
import com.google.common.base.Objects;
import com.google.inject.Inject;
import com.regnosys.rosetta.generator.object.ExpandedAttribute;
import com.regnosys.rosetta.generator.object.ExpandedType;
import com.regnosys.rosetta.generator.util.RosettaAttributeExtensions;
import com.regnosys.rosetta.rosetta.RosettaMetaType;
import com.regnosys.rosetta.rosetta.simple.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
@SuppressWarnings("all")
public class JsonSchemaMetaFieldGenerator {
@Inject
@Extension
private JsonSchemaGeneratorHelper _jsonSchemaGeneratorHelper;
public Map generateMetaFields(final List data, final Iterable metaTypes) {
final HashMap result = CollectionLiterals.newHashMap();
final Function1> _function = (Data it) -> {
return RosettaAttributeExtensions.getExpandedAttributes(it);
};
final Function1 _function_1 = (ExpandedAttribute it) -> {
return Boolean.valueOf((it.hasMetas() && IterableExtensions.exists(it.getMetas(), ((Function1) (ExpandedAttribute it_1) -> {
return Boolean.valueOf((Objects.equal(it_1.getName(), "reference") || Objects.equal(it_1.getName(), "address")));
}))));
};
final Function1 _function_2 = (ExpandedAttribute it) -> {
return it.getType();
};
final Set refs = IterableExtensions.toSet(IterableExtensions.map(IterableExtensions.filter(IterableExtensions.flatMap(data, _function), _function_1), _function_2));
for (final ExpandedType ref : refs) {
result.put(this._jsonSchemaGeneratorHelper.getFilename(this._jsonSchemaGeneratorHelper.getMetaNamespace(ref), this._jsonSchemaGeneratorHelper.toReferenceWithMetaTypeName(ref)), this.generateReferenceWithMeta(ref));
}
final Function1> _function_3 = (Data it) -> {
return RosettaAttributeExtensions.getExpandedAttributes(it);
};
final Function1 _function_4 = (ExpandedAttribute it) -> {
return Boolean.valueOf((it.hasMetas() && (!IterableExtensions.exists(it.getMetas(), ((Function1) (ExpandedAttribute it_1) -> {
return Boolean.valueOf((Objects.equal(it_1.getName(), "reference") || Objects.equal(it_1.getName(), "address")));
})))));
};
final Function1 _function_5 = (ExpandedAttribute it) -> {
return it.getType();
};
final Set metas = IterableExtensions.toSet(IterableExtensions.map(IterableExtensions.filter(IterableExtensions.flatMap(data, _function_3), _function_4), _function_5));
for (final ExpandedType meta : metas) {
result.put(this._jsonSchemaGeneratorHelper.getFilename(this._jsonSchemaGeneratorHelper.getMetaNamespace(meta), this._jsonSchemaGeneratorHelper.toFieldWithMetaTypeName(meta)), this.generateFieldWithMeta(meta));
}
result.put(this._jsonSchemaGeneratorHelper.getFilename("com.rosetta.model.metafields", "MetaFields"), this.genMetaFields());
result.put(this._jsonSchemaGeneratorHelper.getFilename("com.rosetta.model.lib.meta", "Key"), this.genKey());
result.put(this._jsonSchemaGeneratorHelper.getFilename("com.rosetta.model.lib.meta", "Reference"), this.genReference());
return result;
}
private String generateFieldWithMeta(final ExpandedType type) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$schema\": \"http://json-schema.org/draft-04/schema#\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$anchor\": \"");
String _name = type.getModel().getName();
_builder.append(_name, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"type\": \"object\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"title\": \"");
CharSequence _fieldWithMetaTypeName = this._jsonSchemaGeneratorHelper.toFieldWithMetaTypeName(type);
_builder.append(_fieldWithMetaTypeName, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"properties\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"value\": {");
_builder.newLine();
_builder.append(" ");
String _attributeType = this.getAttributeType(type);
_builder.append(_attributeType, " ");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"meta\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$ref\": \"com-rosetta-model-metafields-MetaFields.schema.json\"");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
public String getAttributeType(final ExpandedType type) {
boolean _isBuiltInType = type.isBuiltInType();
if (_isBuiltInType) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"type\": \"");
String _jsonSchemaType = JsonSchemaTranslator.toJsonSchemaType(type);
_builder.append(_jsonSchemaType);
_builder.append("\"");
return _builder.toString();
} else {
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("\"$ref\": \"");
String _filename = this._jsonSchemaGeneratorHelper.getFilename(type.getModel().getName(), JsonSchemaTranslator.toJsonSchemaType(type));
_builder_1.append(_filename);
_builder_1.append("\"");
return _builder_1.toString();
}
}
private String generateReferenceWithMeta(final ExpandedType type) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$schema\": \"http://json-schema.org/draft-04/schema#\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$anchor\": \"");
String _name = type.getModel().getName();
_builder.append(_name, " ");
_builder.append(".metafields\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"type\": \"object\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"title\": \"ReferenceWithMeta");
String _metaTypeName = JsonSchemaGeneratorHelper.toMetaTypeName(type);
_builder.append(_metaTypeName, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"properties\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"globalReference\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"externalReference\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"address\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$ref\": \"com-rosetta-model-lib-meta-Reference.schema.json\"");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
private String genMetaFields() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$schema\": \"http://json-schema.org/draft-04/schema#\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$anchor\": \"com.rosetta.model.metafields\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"object\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"title\": \"MetaFields\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"properties\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"scheme\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"globalKey\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"externalKey\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"key\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"array\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"items\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$ref\": \"com-rosetta-model-lib-meta-Key.schema.json\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"minItems\": 0");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
private String genKey() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$schema\": \"http://json-schema.org/draft-04/schema#\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$anchor\": \"com.rosetta.model.lib.meta\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"object\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"title\": \"Key\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"properties\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"scope\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"value\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
private String genReference() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$schema\": \"http://json-schema.org/draft-04/schema#\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"$anchor\": \"com.rosetta.model.lib.meta\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"object\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"title\": \"Reference\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"properties\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"scope\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("},");
_builder.newLine();
_builder.append(" ");
_builder.append("\"value\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"type\": \"string\"");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
}