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.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;
    this.nameHelper = NameHelper.forModel(model);
    this.typeHelper = TypeHelper.forModel(model);
  }
  
  public void generate() {
    EList _dataModels = this.model.getDataModels();
    for (final DataModel dataModel : _dataModels) {
      final Function1 _function = (DataType it) -> {
        return Boolean.valueOf((it instanceof Structure));
      };
      final Function1 _function_1 = (DataType it) -> {
        return ((Structure) it);
      };
      Iterable _map = IterableExtensions.map(IterableExtensions.filter(dataModel.getOwnedDataTypes(), _function), _function_1);
      for (final Structure type : _map) {
        {
          @Extension
          final FileHelper fileHelper = FileHelper.of(type, FileRole.POCOS, this.context, this.config);
          fileHelper.writeFile(this.generatePoco(type), fileHelper.getCsharpFileName(type.getName()));
        }
      }
    }
  }
  
  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 (field instanceof PrimitiveProperty) {
        _matched=true;
        _switchResult = this.typeHelper.getCsharpType(((PrimitiveProperty)field).getType().getName());
      }
      if (!_matched) {
        if (field instanceof ReferenceProperty) {
          _matched=true;
          String _name = ((ReferenceProperty)field).getDataType().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 _initialUpper = this.nameHelper.initialUpper(field.getName());
      _builder_2.append(_initialUpper);
      _builder_2.append(" { get; set; }");
      _xblockexpression = _builder_2;
    }
    return _xblockexpression;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy