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

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

There is a newer version: 1.4.2
Show newest version
package com.reprezen.genflow.rapidml.csharp.generators;

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.Feature;
import com.reprezen.rapidml.PrimitiveProperty;
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 StructurePocoGenerator {
  private final ZenModel model;
  
  private final IGenTemplateContext context;
  
  private final Config config;
  
  @Extension
  private NameHelper nameHelper;
  
  @Extension
  private TypeHelper typeHelper;
  
  public StructurePocoGenerator(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 Structure));
      };
      Iterable _filter = IterableExtensions.filter(_ownedDataTypes, _function);
      final Function1 _function_1 = (DataType it) -> {
        return ((Structure) it);
      };
      Iterable _map = IterableExtensions.map(_filter, _function_1);
      for (final Structure type : _map) {
        {
          @Extension
          final FileHelper fileHelper = FileHelper.of(type, FileRole.POCOS, this.context, this.config);
          String _generatePoco = this.generatePoco(type);
          String _name = type.getName();
          String _csharpFileName = fileHelper.getCsharpFileName(_name);
          fileHelper.writeFile(_generatePoco, _csharpFileName);
        }
      }
    }
  }
  
  private String generatePoco(final Structure structure) {
    String _xblockexpression = null;
    {
      final String name = this.nameHelper.getTypePocoName(structure);
      StringConcatenation _builder = new StringConcatenation();
      CharSequence _simpleDoc = DocHelper.simpleDoc(structure);
      _builder.append(_simpleDoc, "");
      _builder.append("[DeserializeFrom(typeof(");
      String _typeInterfaceName = this.nameHelper.getTypeInterfaceName(structure);
      _builder.append(_typeInterfaceName, "");
      _builder.append("))]");
      _builder.newLineIfNotEmpty();
      CharSequence _generatedAttr = UtilsHelper.generatedAttr();
      _builder.append(_generatedAttr, "");
      _builder.newLineIfNotEmpty();
      _builder.append("public partial class ");
      _builder.append(name, "");
      _builder.append(" : I");
      _builder.append(name, "");
      _builder.append(" {");
      _builder.newLineIfNotEmpty();
      {
        EList _ownedFeatures = structure.getOwnedFeatures();
        for(final Feature field : _ownedFeatures) {
          _builder.append("    ");
          CharSequence _simpleDoc_1 = DocHelper.simpleDoc(field);
          _builder.append(_simpleDoc_1, "    ");
          CharSequence _generateFieldProperty = this.generateFieldProperty(field);
          _builder.append(_generateFieldProperty, "    ");
          _builder.newLineIfNotEmpty();
          _builder.append("    ");
          _builder.newLine();
        }
      }
      _builder.append("}");
      _builder.newLine();
      _builder.newLine();
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  private CharSequence generateFieldProperty(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.generateFieldProperty(field, typeName);
    }
    return _xblockexpression;
  }
  
  private CharSequence generateFieldProperty(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("public ");
      _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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy