All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.reprezen.genflow.rapidml.csharp.generators.DataModelGenerator Maven / Gradle / Ivy

package com.reprezen.genflow.rapidml.csharp.generators;

import com.google.common.base.Objects;
import com.reprezen.genflow.api.template.IGenTemplateContext;
import com.reprezen.genflow.rapidml.csharp.Config;
import com.reprezen.genflow.rapidml.csharp.helpers.DocHelper;
import com.reprezen.genflow.rapidml.csharp.helpers.FileHelper;
import com.reprezen.genflow.rapidml.csharp.helpers.FileRole;
import com.reprezen.genflow.rapidml.csharp.helpers.NameHelper;
import com.reprezen.genflow.rapidml.csharp.helpers.TypeHelper;
import com.reprezen.genflow.rapidml.csharp.helpers.UtilsHelper;
import com.reprezen.rapidml.DataModel;
import com.reprezen.rapidml.DataType;
import com.reprezen.rapidml.EnumConstant;
import com.reprezen.rapidml.Enumeration;
import com.reprezen.rapidml.Feature;
import com.reprezen.rapidml.PrimitiveProperty;
import com.reprezen.rapidml.PrimitiveType;
import com.reprezen.rapidml.ReferenceProperty;
import com.reprezen.rapidml.SingleValueType;
import com.reprezen.rapidml.Structure;
import com.reprezen.rapidml.ZenModel;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
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 DataModelGenerator {
  private final ZenModel model;
  
  private final IGenTemplateContext context;
  
  private final Config config;
  
  @Extension
  private NameHelper nameHelper;
  
  @Extension
  private TypeHelper typeHelper;
  
  public DataModelGenerator(final ZenModel model, final IGenTemplateContext context, final Config config) {
    this.model = model;
    this.context = context;
    this.config = config;
    NameHelper _forModel = NameHelper.forModel(model);
    this.nameHelper = _forModel;
    TypeHelper _forModel_1 = TypeHelper.forModel(model);
    this.typeHelper = _forModel_1;
  }
  
  public void generate() {
    EList _dataModels = this.model.getDataModels();
    for (final DataModel dataModel : _dataModels) {
      {
        EList _ownedDataTypes = dataModel.getOwnedDataTypes();
        final Function1 _function = (DataType it) -> {
          return Boolean.valueOf((it instanceof Enumeration));
        };
        Iterable _filter = IterableExtensions.filter(_ownedDataTypes, _function);
        final Function1 _function_1 = (DataType it) -> {
          return ((Enumeration) it);
        };
        Iterable _map = IterableExtensions.map(_filter, _function_1);
        for (final Enumeration type : _map) {
          {
            @Extension
            final FileHelper fileHelper = FileHelper.of(type, FileRole.ENUMS, this.context, this.config);
            CharSequence _generate = this.generate(type);
            String _name = type.getName();
            String _csharpFileName = fileHelper.getCsharpFileName(_name);
            fileHelper.writeFile(_generate, _csharpFileName);
            String _enumerationName = this.nameHelper.getEnumerationName(type);
            this.typeHelper.noteEnumerationType(_enumerationName);
          }
        }
        EList _ownedDataTypes_1 = dataModel.getOwnedDataTypes();
        final Function1 _function_2 = (DataType it) -> {
          return Boolean.valueOf((it instanceof Structure));
        };
        Iterable _filter_1 = IterableExtensions.filter(_ownedDataTypes_1, _function_2);
        final Function1 _function_3 = (DataType it) -> {
          return ((Structure) it);
        };
        Iterable _map_1 = IterableExtensions.map(_filter_1, _function_3);
        for (final Structure type_1 : _map_1) {
          {
            @Extension
            final FileHelper fileHelper = FileHelper.of(type_1, FileRole.INTERFACE, this.context, this.config);
            String _generate = this.generate(type_1);
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("I");
            String _name = type_1.getName();
            _builder.append(_name, "");
            String _csharpFileName = fileHelper.getCsharpFileName(_builder);
            fileHelper.writeFile(_generate, _csharpFileName);
          }
        }
      }
    }
  }
  
  private String generate(final Structure structure) {
    String _xblockexpression = null;
    {
      final String name = this.nameHelper.getTypeInterfaceName(structure);
      StringConcatenation _builder = new StringConcatenation();
      CharSequence _serializationAttributes = this.getSerializationAttributes(structure);
      _builder.append(_serializationAttributes, "");
      _builder.newLineIfNotEmpty();
      CharSequence _generatedAttr = UtilsHelper.generatedAttr();
      _builder.append(_generatedAttr, "");
      _builder.newLineIfNotEmpty();
      _builder.append("public interface ");
      _builder.append(name, "");
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      {
        EList _ownedFeatures = structure.getOwnedFeatures();
        for(final Feature field : _ownedFeatures) {
          _builder.append("    ");
          CharSequence _simpleDoc = DocHelper.simpleDoc(field);
          _builder.append(_simpleDoc, "    ");
          CharSequence _serializationAttributes_1 = this.getSerializationAttributes(field);
          _builder.append(_serializationAttributes_1, "    ");
          CharSequence _generate = this.generate(field);
          _builder.append(_generate, "    ");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          _builder.newLine();
        }
      }
      _builder.append("}");
      _builder.newLine();
      _builder.newLine();
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  private CharSequence generate(final Feature field) {
    CharSequence _xblockexpression = null;
    {
      String _switchResult = null;
      boolean _matched = false;
      if (!_matched) {
        if (field instanceof PrimitiveProperty) {
          _matched=true;
          SingleValueType _type = ((PrimitiveProperty)field).getType();
          String _name = _type.getName();
          _switchResult = this.typeHelper.getCsharpType(_name);
        }
      }
      if (!_matched) {
        if (field instanceof ReferenceProperty) {
          _matched=true;
          Structure _dataType = ((ReferenceProperty)field).getDataType();
          String _name = _dataType.getName();
          _switchResult = ("I" + _name);
        }
      }
      final String typeName = _switchResult;
      _xblockexpression = this.generate(field, typeName);
    }
    return _xblockexpression;
  }
  
  private CharSequence generate(final Feature field, final String typeName) {
    CharSequence _xblockexpression = null;
    {
      String _xifexpression = null;
      int _maxOccurs = field.getMaxOccurs();
      boolean _equals = (_maxOccurs == (-1));
      if (_equals) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("IEnumerable<");
        String _maybeNullable = this.typeHelper.maybeNullable(typeName);
        _builder.append(_maybeNullable, "");
        _builder.append(">");
        _xifexpression = _builder.toString();
      } else {
        StringConcatenation _builder_1 = new StringConcatenation();
        String _maybeNullable_1 = this.typeHelper.maybeNullable(typeName);
        _builder_1.append(_maybeNullable_1, "");
        _xifexpression = _builder_1.toString();
      }
      final String type = _xifexpression;
      StringConcatenation _builder_2 = new StringConcatenation();
      _builder_2.append(type, "");
      _builder_2.append(" ");
      String _name = field.getName();
      String _initialUpper = this.nameHelper.initialUpper(_name);
      _builder_2.append(_initialUpper, "");
      _builder_2.append(" { get; set; }");
      _xblockexpression = _builder_2;
    }
    return _xblockexpression;
  }
  
  private CharSequence generate(final Enumeration enumeration) {
    CharSequence _xblockexpression = null;
    {
      final String name = this.nameHelper.getEnumerationName(enumeration);
      final boolean defaultInts = this.canUseDefaultInts(enumeration);
      StringConcatenation _builder = new StringConcatenation();
      CharSequence _simpleDoc = DocHelper.simpleDoc(enumeration);
      _builder.append(_simpleDoc, "");
      CharSequence _generatedAttr = UtilsHelper.generatedAttr();
      _builder.append(_generatedAttr, "");
      _builder.newLineIfNotEmpty();
      _builder.append("public enum ");
      _builder.append(name, "");
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      {
        EList _enumConstants = enumeration.getEnumConstants();
        boolean _hasElements = false;
        for(final EnumConstant constant : _enumConstants) {
          if (!_hasElements) {
            _hasElements = true;
          } else {
            _builder.appendImmediate(",", "    ");
          }
          _builder.append("    ");
          CharSequence _generate = this.generate(constant, defaultInts);
          _builder.append(_generate, "    ");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("}");
      _builder.newLine();
      _builder.newLine();
      {
        PrimitiveType _baseType = enumeration.getBaseType();
        String _name = _baseType.getName();
        boolean _equals = Objects.equal(_name, "string");
        if (_equals) {
          CharSequence _genValueExtension = this.genValueExtension(enumeration);
          _builder.append(_genValueExtension, "");
        }
      }
      _builder.newLineIfNotEmpty();
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
  
  private CharSequence generate(final EnumConstant enumConstant, final boolean defaultInts) {
    StringConcatenation _builder = new StringConcatenation();
    CharSequence _simpleDoc = DocHelper.simpleDoc(enumConstant);
    _builder.append(_simpleDoc, "");
    CharSequence _serializationAttributes = this.getSerializationAttributes(enumConstant);
    _builder.append(_serializationAttributes, "");
    _builder.newLineIfNotEmpty();
    String _name = enumConstant.getName();
    _builder.append(_name, "");
    {
      if ((!defaultInts)) {
        _builder.append(" = ");
        int _integerValue = enumConstant.getIntegerValue();
        _builder.append(_integerValue, "");
      }
    }
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  private boolean canUseDefaultInts(final Enumeration enumeration) {
    PrimitiveType _baseType = enumeration.getBaseType();
    String _name = _baseType.getName();
    boolean _equals = Objects.equal(_name, "string");
    if (_equals) {
      return true;
    }
    EList _enumConstants = enumeration.getEnumConstants();
    for (final EnumConstant enumConstant : _enumConstants) {
      int _implicitIntegerValue = enumConstant.getImplicitIntegerValue();
      int _integerValue = enumConstant.getIntegerValue();
      boolean _notEquals = (_implicitIntegerValue != _integerValue);
      if (_notEquals) {
        return false;
      }
    }
    return true;
  }
  
  private CharSequence genValueExtension(final Enumeration enumeration) {
    CharSequence _xblockexpression = null;
    {
      final String name = this.nameHelper.getEnumerationName(enumeration);
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("public static class ");
      _builder.append(name, "");
      _builder.append("Extension {");
      _builder.newLineIfNotEmpty();
      _builder.append("    ");
      _builder.append("public static string ToValue(this ");
      _builder.append(name, "    ");
      _builder.append(" enumConstant) {");
      _builder.newLineIfNotEmpty();
      _builder.append("        ");
      _builder.append("switch (enumConstant) {");
      _builder.newLine();
      {
        EList _enumConstants = enumeration.getEnumConstants();
        for(final EnumConstant enumConstant : _enumConstants) {
          _builder.append("            ");
          _builder.append("case ");
          _builder.append(name, "            ");
          _builder.append(".");
          String _name = enumConstant.getName();
          _builder.append(_name, "            ");
          _builder.append(": ");
          _builder.newLineIfNotEmpty();
          _builder.append("            ");
          _builder.append("    ");
          _builder.append("return \"");
          String _literalValue = enumConstant.getLiteralValue();
          _builder.append(_literalValue, "                ");
          _builder.append("\";");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("            ");
      _builder.append("default:");
      _builder.newLine();
      _builder.append("                ");
      _builder.append("throw new ArgumentException(\"Illegal value for enumeration \'");
      String _enumerationName = this.nameHelper.getEnumerationName(enumeration);
      _builder.append(_enumerationName, "                ");
      _builder.append("\': \" + ((int) enumConstant));");
      _builder.newLineIfNotEmpty();
      _builder.append("        ");
      _builder.append("}");
      _builder.newLine();
      _builder.append("    ");
      _builder.append("}");
      _builder.newLine();
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = _builder;
    }
    return _xblockexpression;
  }
  
  private CharSequence getSerializationAttributes(final Structure structure) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _isGenerateJsonSerialization = this.config.isGenerateJsonSerialization();
      if (_isGenerateJsonSerialization) {
        _builder.append("[JsonObject(MemberSerialization.OptIn)]");
      }
    }
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  private CharSequence getSerializationAttributes(final Feature field) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _isGenerateJsonSerialization = this.config.isGenerateJsonSerialization();
      if (_isGenerateJsonSerialization) {
        _builder.append("[JsonProperty(\"");
        String _name = field.getName();
        _builder.append(_name, "");
        _builder.append("\")]");
        _builder.newLineIfNotEmpty();
        {
          boolean _isEnumField = this.isEnumField(field);
          if (_isEnumField) {
            _builder.append("[JsonConverter(typeof(StringEnumConverter))]");
          }
        }
        _builder.newLineIfNotEmpty();
      }
    }
    return _builder;
  }
  
  private boolean isEnumField(final Feature field) {
    boolean _and = false;
    if (!(field instanceof PrimitiveProperty)) {
      _and = false;
    } else {
      SingleValueType _type = ((PrimitiveProperty) field).getType();
      _and = (_type instanceof Enumeration);
    }
    return _and;
  }
  
  private CharSequence getSerializationAttributes(final EnumConstant enumConstant) {
    StringConcatenation _builder = new StringConcatenation();
    {
      boolean _isGenerateJsonSerialization = this.config.isGenerateJsonSerialization();
      if (_isGenerateJsonSerialization) {
        _builder.append("[EnumMember]");
      }
    }
    return _builder;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy