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

com.reprezen.genflow.rapidml.nodejs.ResourceStubGenerator Maven / Gradle / Ivy

package com.reprezen.genflow.rapidml.nodejs;

import com.google.common.base.Objects;
import com.reprezen.genflow.rapidml.nodejs.NodejsGenerator;
import com.reprezen.rapidml.CollectionResource;
import com.reprezen.rapidml.DataModel;
import com.reprezen.rapidml.DataType;
import com.reprezen.rapidml.Feature;
import com.reprezen.rapidml.HTTPMethods;
import com.reprezen.rapidml.LinkRelation;
import com.reprezen.rapidml.Method;
import com.reprezen.rapidml.ObjectRealization;
import com.reprezen.rapidml.ObjectResource;
import com.reprezen.rapidml.PrimitiveProperty;
import com.reprezen.rapidml.PropertyRealization;
import com.reprezen.rapidml.PropertyReference;
import com.reprezen.rapidml.ReferenceElement;
import com.reprezen.rapidml.ReferenceEmbed;
import com.reprezen.rapidml.ReferenceLink;
import com.reprezen.rapidml.ReferenceTreatment;
import com.reprezen.rapidml.ResourceAPI;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.ServiceDataResource;
import com.reprezen.rapidml.SourceReference;
import com.reprezen.rapidml.Structure;
import com.reprezen.rapidml.TypedResponse;
import com.reprezen.rapidml.URI;
import com.reprezen.rapidml.URIParameter;
import com.reprezen.rapidml.ZenModel;
import com.reprezen.rapidml.realization.processor.CycleDetector;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
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;
import org.eclipse.xtext.xbase.lib.ListExtensions;

@SuppressWarnings("all")
public class ResourceStubGenerator implements NodejsGenerator.Generator {
  private final ServiceDataResource resource;
  
  private final String fqName;
  
  @Extension
  private CycleDetector _cycleDetector = new CycleDetector();
  
  public ResourceStubGenerator(final ServiceDataResource resource) {
    this.resource = resource;
    String _fqName = this.getFqName(resource);
    this.fqName = _fqName;
  }
  
  @Override
  public String generate() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("const Resource = require(\'../../../lib/Resource\');");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ");
    String _name = this.resource.getName();
    _builder.append(_name, "");
    _builder.append(" extends Resource {");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    _builder.append("constructor(app, dao) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("super(app);");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("this.dao = dao;");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("this.resourceFQN = \'");
    _builder.append(this.fqName, "\t\t");
    _builder.append("\';");
    _builder.newLineIfNotEmpty();
    {
      EList _methods = this.resource.getMethods();
      for(final Method method : _methods) {
        _builder.append("\t\t");
        _builder.append("app.");
        HTTPMethods _httpMethod = method.getHttpMethod();
        String _literal = _httpMethod.getLiteral();
        String _lowerCase = _literal.toLowerCase();
        _builder.append(_lowerCase, "\t\t");
        _builder.append("(`${app.locals.basePath}");
        URI _uRI = this.resource.getURI();
        String _expressify = this.expressify(_uRI);
        _builder.append(_expressify, "\t\t");
        _builder.append("`, this.");
        String _id = method.getId();
        _builder.append(_id, "\t\t");
        _builder.append("Handler.bind(this));");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    {
      EList _methods_1 = this.resource.getMethods();
      boolean _hasElements = false;
      for(final Method method_1 : _methods_1) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate("\n", "\t");
        }
        _builder.append("\t");
        CharSequence _handler = this.getHandler(method_1);
        _builder.append(_handler, "\t");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("module.exports = ");
    String _name_1 = this.resource.getName();
    _builder.append(_name_1, "");
    _builder.append(";");
    _builder.newLineIfNotEmpty();
    return _builder.toString();
  }
  
  private CharSequence getHandler(final Method method) {
    StringConcatenation _builder = new StringConcatenation();
    String _id = method.getId();
    _builder.append(_id, "");
    _builder.append("Handler(req, res, next) {");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("const enrichments = ");
    String _enrichments = this.getEnrichments(method);
    String _trim = _enrichments.trim();
    _builder.append(_trim, "\t");
    _builder.append("; ");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("super.callMethodImpl(this.resourceFQN, \'");
    String _id_1 = method.getId();
    _builder.append(_id_1, "\t");
    _builder.append("\', enrichments, req, res, next);");
    _builder.newLineIfNotEmpty();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
  
  public String getEnrichments(final Method method) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    {
      EList _responses = method.getResponses();
      boolean _hasElements = false;
      for(final TypedResponse response : _responses) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",", "\t");
        }
        _builder.append("\t");
        _builder.append("\'");
        int _statusCode = response.getStatusCode();
        String _format = String.format("%03d", Integer.valueOf(_statusCode));
        _builder.append(_format, "\t");
        _builder.append("\': ");
        CharSequence _elvis = null;
        CharSequence _enrichmentSpec = this.getEnrichmentSpec(response);
        if (_enrichmentSpec != null) {
          _elvis = _enrichmentSpec;
        } else {
          _elvis = "null";
        }
        _builder.append(_elvis, "\t");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("}");
    _builder.newLine();
    return _builder.toString();
  }
  
  private CharSequence getEnrichmentSpec(final TypedResponse response) {
    CharSequence _xblockexpression = null;
    {
      final Structure type = this.getStructType(response);
      CharSequence _xifexpression = null;
      if ((type != null)) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("{");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("type: \'");
        Structure _structType = this.getStructType(response);
        String _fqName = this.getFqName(_structType);
        _builder.append(_fqName, "\t");
        _builder.append("\',");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("multi: ");
        boolean _isMultivalued = this.isMultivalued(response);
        _builder.append(_isMultivalued, "\t");
        _builder.append(",");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("fields: [");
        _builder.newLine();
        {
          List _enrichmentProperties = this.getEnrichmentProperties(response);
          boolean _hasElements = false;
          for(final PropertyRealization prop : _enrichmentProperties) {
            if (!_hasElements) {
              _hasElements = true;
            } else {
              _builder.appendImmediate(",", "\t\t");
            }
            _builder.append("\t\t");
            _builder.append("\'");
            Feature _baseProperty = prop.getBaseProperty();
            String _name = _baseProperty.getName();
            _builder.append(_name, "\t\t");
            _builder.append("\'");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("\t");
        _builder.append("],");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("enrichments: ");
        EList _governingReferenceTreatments = this.getGoverningReferenceTreatments(response);
        String _enrichmentSpec = this.getEnrichmentSpec(_governingReferenceTreatments);
        _builder.append(_enrichmentSpec, "\t");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
        _xifexpression = _builder;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }
  
  private EList getGoverningReferenceTreatments(final TypedResponse response) {
    EList treatments = response.getReferenceTreatments();
    boolean _and = false;
    boolean _isEmpty = treatments.isEmpty();
    if (!_isEmpty) {
      _and = false;
    } else {
      ResourceDefinition _resourceType = response.getResourceType();
      boolean _tripleNotEquals = (_resourceType != null);
      _and = _tripleNotEquals;
    }
    if (_and) {
      ResourceDefinition _resourceType_1 = response.getResourceType();
      EList _referenceTreatments = ((ServiceDataResource) _resourceType_1).getReferenceTreatments();
      treatments = _referenceTreatments;
    }
    return treatments;
  }
  
  private List getEnrichmentProperties(final TypedResponse response) {
    ResourceDefinition _resourceType = response.getResourceType();
    final ServiceDataResource resource = ((ServiceDataResource) _resourceType);
    ResourceDefinition _resourceType_1 = response.getResourceType();
    boolean _tripleNotEquals = (_resourceType_1 != null);
    if (_tripleNotEquals) {
      EList _allReferenceTreatments = resource.getAllReferenceTreatments();
      final Function1 _function = (ReferenceTreatment it) -> {
        boolean _and = false;
        if (!(it instanceof ReferenceLink)) {
          _and = false;
        } else {
          ReferenceElement _referenceElement = it.getReferenceElement();
          String _name = _referenceElement.getName();
          boolean _equals = Objects.equal(_name, "ObjectResourceLink");
          _and = _equals;
        }
        return Boolean.valueOf(_and);
      };
      Iterable _filter = IterableExtensions.filter(_allReferenceTreatments, _function);
      final ReferenceTreatment orLink = IterableExtensions.head(_filter);
      if ((orLink != null)) {
        return Collections.unmodifiableList(CollectionLiterals.newArrayList());
      } else {
        ResourceDefinition _resourceType_2 = response.getResourceType();
        ObjectRealization _properties = ((ServiceDataResource) _resourceType_2).getProperties();
        return _properties.getAllIncludedProperties();
      }
    } else {
      ObjectRealization _properties_1 = response.getProperties();
      return _properties_1.getAllIncludedProperties();
    }
  }
  
  private String getEnrichmentSpec(final EList treatments) {
    String _xblockexpression = null;
    {
      boolean _isEmpty = treatments.isEmpty();
      if (_isEmpty) {
        return "{}";
      }
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("{");
      _builder.newLine();
      {
        boolean _hasElements = false;
        for(final ReferenceTreatment treatment : treatments) {
          if (!_hasElements) {
            _hasElements = true;
          } else {
            _builder.appendImmediate(",", "\t");
          }
          _builder.append("\t");
          CharSequence _enrichmentSpecItem = this.getEnrichmentSpecItem(treatment);
          _builder.append(_enrichmentSpecItem, "\t");
          _builder.newLineIfNotEmpty();
        }
      }
      _builder.append("}");
      _builder.newLine();
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  private CharSequence getEnrichmentSpecItem(final ReferenceTreatment treatment) {
    CharSequence _xifexpression = null;
    boolean _visit = this._cycleDetector.visit(treatment);
    if (_visit) {
      CharSequence _xtrycatchfinallyexpression = null;
      try {
        StringConcatenation _builder = new StringConcatenation();
        ReferenceElement _referenceElement = treatment.getReferenceElement();
        String _name = _referenceElement.getName();
        _builder.append(_name, "");
        _builder.append(": {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("type: \'");
        ReferenceElement _referenceElement_1 = treatment.getReferenceElement();
        Structure _dataType = _referenceElement_1.getDataType();
        String _name_1 = _dataType.getName();
        _builder.append(_name_1, "\t");
        _builder.append("\',");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("multi: ");
        boolean _isMultivalued = this.isMultivalued(treatment);
        _builder.append(_isMultivalued, "\t");
        _builder.append(",");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("fields: [");
        _builder.newLine();
        {
          List _elvis = null;
          ObjectRealization _linkDescriptor = treatment.getLinkDescriptor();
          EList _allIncludedProperties = null;
          if (_linkDescriptor!=null) {
            _allIncludedProperties=_linkDescriptor.getAllIncludedProperties();
          }
          List _map = null;
          if (_allIncludedProperties!=null) {
            final Function1 _function = (PropertyRealization it) -> {
              return it.getBaseProperty();
            };
            _map=ListExtensions.map(_allIncludedProperties, _function);
          }
          if (_map != null) {
            _elvis = _map;
          } else {
            BasicEList _basicEList = new BasicEList();
            _elvis = _basicEList;
          }
          boolean _hasElements = false;
          for(final Feature feature : _elvis) {
            if (!_hasElements) {
              _hasElements = true;
            } else {
              _builder.appendImmediate(",", "\t\t");
            }
            _builder.append("\t\t");
            _builder.append("\'");
            String _name_2 = feature.getName();
            _builder.append(_name_2, "\t\t");
            _builder.append("\'");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("\t");
        _builder.append("],");
        _builder.newLine();
        {
          if ((treatment instanceof ReferenceLink)) {
            _builder.append("\t");
            _builder.append("link: {");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("\t");
            _builder.append("href: \'");
            String _linkURI = this.getLinkURI(treatment);
            _builder.append(_linkURI, "\t\t");
            _builder.append("\',");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("\t");
            _builder.append("boundParams: ");
            String _boundParams = this.boundParams(((ReferenceLink)treatment));
            String _trim = _boundParams.trim();
            _builder.append(_trim, "\t\t");
            _builder.append(",");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("\t");
            _builder.append("rel: \'");
            String _elvis_1 = null;
            LinkRelation _linkRelation = ((ReferenceLink)treatment).getLinkRelation();
            String _name_3 = null;
            if (_linkRelation!=null) {
              _name_3=_linkRelation.getName();
            }
            if (_name_3 != null) {
              _elvis_1 = _name_3;
            } else {
              _elvis_1 = "related";
            }
            _builder.append(_elvis_1, "\t\t");
            _builder.append("\'");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("},");
            _builder.newLine();
          }
        }
        _builder.append("\t");
        _builder.append("enrichments: ");
        String _nestedTreatments = this.getNestedTreatments(treatment);
        _builder.append(_nestedTreatments, "\t");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
        _xtrycatchfinallyexpression = _builder;
      } finally {
        this._cycleDetector.leave(treatment);
      }
      _xifexpression = _xtrycatchfinallyexpression;
    }
    return _xifexpression;
  }
  
  private boolean isMultivalued(final TypedResponse response) {
    boolean _xifexpression = false;
    ResourceDefinition _resourceType = response.getResourceType();
    boolean _tripleNotEquals = (_resourceType != null);
    if (_tripleNotEquals) {
      ResourceDefinition _resourceType_1 = response.getResourceType();
      _xifexpression = (_resourceType_1 instanceof CollectionResource);
    } else {
      _xifexpression = false;
    }
    return _xifexpression;
  }
  
  private boolean isMultivalued(final ReferenceTreatment treatment) {
    boolean _xblockexpression = false;
    {
      ReferenceElement _referenceElement = treatment.getReferenceElement();
      int _maxOccurs = _referenceElement.getMaxOccurs();
      final boolean propIsMulti = (_maxOccurs < 0);
      boolean _switchResult = false;
      boolean _matched = false;
      if (!_matched) {
        if (treatment instanceof ReferenceLink) {
          _matched=true;
          boolean _and = false;
          if (!propIsMulti) {
            _and = false;
          } else {
            ResourceDefinition _targetResource = ((ReferenceLink)treatment).getTargetResource();
            _and = (_targetResource instanceof ObjectResource);
          }
          _switchResult = _and;
        }
      }
      if (!_matched) {
        if (treatment instanceof ReferenceEmbed) {
          _matched=true;
          _switchResult = propIsMulti;
        }
      }
      _xblockexpression = _switchResult;
    }
    return _xblockexpression;
  }
  
  private String boundParams(final ReferenceLink link) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("{");
    _builder.newLine();
    {
      ResourceDefinition _targetResource = link.getTargetResource();
      URI _uRI = _targetResource.getURI();
      EList _uriParameters = _uRI.getUriParameters();
      final Function1 _function = (URIParameter it) -> {
        SourceReference _sourceReference = it.getSourceReference();
        return Boolean.valueOf((_sourceReference instanceof PropertyReference));
      };
      Iterable _filter = IterableExtensions.filter(_uriParameters, _function);
      boolean _hasElements = false;
      for(final URIParameter param : _filter) {
        if (!_hasElements) {
          _hasElements = true;
        } else {
          _builder.appendImmediate(",", "\t");
        }
        _builder.append("\t");
        String _name = param.getName();
        _builder.append(_name, "\t");
        _builder.append(": \'");
        SourceReference _sourceReference = param.getSourceReference();
        PrimitiveProperty _conceptualFeature = ((PropertyReference) _sourceReference).getConceptualFeature();
        String _name_1 = _conceptualFeature.getName();
        _builder.append(_name_1, "\t");
        _builder.append("\'");
        _builder.newLineIfNotEmpty();
      }
    }
    _builder.append("}");
    _builder.newLine();
    return _builder.toString();
  }
  
  private String getNestedTreatments(final ReferenceTreatment treatment) {
    String _switchResult = null;
    boolean _matched = false;
    if (!_matched) {
      if (treatment instanceof ReferenceLink) {
        _matched=true;
        _switchResult = "{}";
      }
    }
    if (!_matched) {
      if (treatment instanceof ReferenceEmbed) {
        _matched=true;
        EList _nestedReferenceTreatments = ((ReferenceEmbed)treatment).getNestedReferenceTreatments();
        _switchResult = this.getEnrichmentSpec(_nestedReferenceTreatments);
      }
    }
    return _switchResult;
  }
  
  private String getLinkURI(final ReferenceTreatment treatment) {
    String _switchResult = null;
    boolean _matched = false;
    if (!_matched) {
      if (treatment instanceof ReferenceLink) {
        _matched=true;
        ResourceDefinition _targetResource = ((ReferenceLink)treatment).getTargetResource();
        URI _uRI = _targetResource.getURI();
        _switchResult = this.expressify(_uRI);
      }
    }
    if (!_matched) {
      if (treatment instanceof ReferenceEmbed) {
        _matched=true;
        _switchResult = null;
      }
    }
    return _switchResult;
  }
  
  private Structure getStructType(final TypedResponse response) {
    Structure _elvis = null;
    ResourceDefinition _resourceType = response.getResourceType();
    Structure _dataType = null;
    if (((ServiceDataResource) _resourceType)!=null) {
      _dataType=((ServiceDataResource) _resourceType).getDataType();
    }
    if (_dataType != null) {
      _elvis = _dataType;
    } else {
      Structure _dataType_1 = response.getDataType();
      _elvis = _dataType_1;
    }
    return _elvis;
  }
  
  private String getFqName(final ResourceDefinition resource) {
    String _xblockexpression = null;
    {
      EObject _eContainer = resource.eContainer();
      final ResourceAPI api = ((ResourceAPI) _eContainer);
      EObject _eContainer_1 = api.eContainer();
      final ZenModel model = ((ZenModel) _eContainer_1);
      StringConcatenation _builder = new StringConcatenation();
      String _name = model.getName();
      _builder.append(_name, "");
      _builder.append(".");
      String _name_1 = api.getName();
      _builder.append(_name_1, "");
      _builder.append(".");
      String _name_2 = resource.getName();
      _builder.append(_name_2, "");
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  private String getFqName(final DataType type) {
    String _xblockexpression = null;
    {
      EObject _eContainer = type.eContainer();
      final DataModel dataModel = ((DataModel) _eContainer);
      EObject _eContainer_1 = dataModel.eContainer();
      final ZenModel model = ((ZenModel) _eContainer_1);
      StringConcatenation _builder = new StringConcatenation();
      String _name = model.getName();
      _builder.append(_name, "");
      _builder.append(".");
      String _name_1 = dataModel.getName();
      _builder.append(_name_1, "");
      _builder.append(".");
      String _name_2 = type.getName();
      _builder.append(_name_2, "");
      _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
  }
  
  private String expressify(final URI uri) {
    String _string = uri.toString();
    return _string.replaceAll("/\\{([^/]+)\\}((?=/)|$)", "/:$1");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy