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.LinkRelation;
import com.reprezen.rapidml.Method;
import com.reprezen.rapidml.ObjectRealization;
import com.reprezen.rapidml.ObjectResource;
import com.reprezen.rapidml.PropertyRealization;
import com.reprezen.rapidml.PropertyReference;
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;
    this.fqName = this.getFqName(resource);
  }
  
  @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.");
        String _lowerCase = method.getHttpMethod().getLiteral().toLowerCase();
        _builder.append(_lowerCase, "\t\t");
        _builder.append("(`${app.locals.basePath}");
        String _expressify = this.expressify(this.resource.getURI());
        _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 _trim = this.getEnrichments(method).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("\'");
        String _format = String.format("%03d", Integer.valueOf(response.getStatusCode()));
        _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: \'");
        String _fqName = this.getFqName(this.getStructType(response));
        _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("\'");
            String _name = prop.getBaseProperty().getName();
            _builder.append(_name, "\t\t");
            _builder.append("\'");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("\t");
        _builder.append("],");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("enrichments: ");
        String _enrichmentSpec = this.getEnrichmentSpec(this.getGoverningReferenceTreatments(response));
        _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();
    if ((treatments.isEmpty() && (response.getResourceType() != null))) {
      ResourceDefinition _resourceType = response.getResourceType();
      treatments = ((ServiceDataResource) _resourceType).getReferenceTreatments();
    }
    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) {
      final Function1 _function = (ReferenceTreatment it) -> {
        return Boolean.valueOf(((it instanceof ReferenceLink) && Objects.equal(it.getReferenceElement().getName(), "ObjectResourceLink")));
      };
      final ReferenceTreatment orLink = IterableExtensions.head(IterableExtensions.filter(resource.getAllReferenceTreatments(), _function));
      if ((orLink != null)) {
        return Collections.unmodifiableList(CollectionLiterals.newArrayList());
      } else {
        ResourceDefinition _resourceType_2 = response.getResourceType();
        return ((ServiceDataResource) _resourceType_2).getProperties().getAllIncludedProperties();
      }
    } else {
      return response.getProperties().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();
        String _name = treatment.getReferenceElement().getName();
        _builder.append(_name);
        _builder.append(": {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("type: \'");
        String _name_1 = treatment.getReferenceElement().getDataType().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 _trim = this.boundParams(((ReferenceLink)treatment)).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;
    {
      int _maxOccurs = treatment.getReferenceElement().getMaxOccurs();
      final boolean propIsMulti = (_maxOccurs < 0);
      boolean _switchResult = false;
      boolean _matched = false;
      if (treatment instanceof ReferenceLink) {
        _matched=true;
        _switchResult = (propIsMulti && (((ReferenceLink)treatment).getTargetResource() instanceof ObjectResource));
      }
      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();
    {
      final Function1 _function = (URIParameter it) -> {
        SourceReference _sourceReference = it.getSourceReference();
        return Boolean.valueOf((_sourceReference instanceof PropertyReference));
      };
      Iterable _filter = IterableExtensions.filter(link.getTargetResource().getURI().getUriParameters(), _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();
        String _name_1 = ((PropertyReference) _sourceReference).getConceptualFeature().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 (treatment instanceof ReferenceLink) {
      _matched=true;
      _switchResult = "{}";
    }
    if (!_matched) {
      if (treatment instanceof ReferenceEmbed) {
        _matched=true;
        _switchResult = this.getEnrichmentSpec(((ReferenceEmbed)treatment).getNestedReferenceTreatments());
      }
    }
    return _switchResult;
  }
  
  private String getLinkURI(final ReferenceTreatment treatment) {
    String _switchResult = null;
    boolean _matched = false;
    if (treatment instanceof ReferenceLink) {
      _matched=true;
      _switchResult = this.expressify(((ReferenceLink)treatment).getTargetResource().getURI());
    }
    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) {
    return uri.toString().replaceAll("/\\{([^/]+)\\}((?=/)|$)", "/:$1");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy