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

com.reprezen.genflow.rapidml.xsd.TraceHelper Maven / Gradle / Ivy

package com.reprezen.genflow.rapidml.xsd;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.reprezen.genflow.api.template.IGenTemplateContext;
import com.reprezen.genflow.api.trace.GenTemplateTraceBuilder;
import com.reprezen.genflow.api.zenmodel.ZenModelLocator;
import com.reprezen.genflow.api.zenmodel.util.CommonServices;
import com.reprezen.genflow.rapidml.xsd.Helpers;
import com.reprezen.genflow.rapidml.xsd.ParamsHelper;
import com.reprezen.genflow.rapidml.xsd.XMLSchemaGenTemplate;
import com.reprezen.rapidml.DataModel;
import com.reprezen.rapidml.EnumConstant;
import com.reprezen.rapidml.Enumeration;
import com.reprezen.rapidml.Feature;
import com.reprezen.rapidml.HTTPMethods;
import com.reprezen.rapidml.Method;
import com.reprezen.rapidml.PropertyRealization;
import com.reprezen.rapidml.ReferenceElement;
import com.reprezen.rapidml.ResourceAPI;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.ServiceDataResource;
import com.reprezen.rapidml.Structure;
import com.reprezen.rapidml.TypedMessage;
import com.reprezen.rapidml.TypedRequest;
import com.reprezen.rapidml.TypedResponse;
import com.reprezen.rapidml.UserDefinedType;
import com.reprezen.rapidml.ZenModel;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Exceptions;
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 TraceHelper {
  private final IGenTemplateContext context;
  
  private final Map traceItems = Maps.newHashMap();
  
  private final ZenModelLocator locator;
  
  @Extension
  private ParamsHelper paramsHelper;
  
  public TraceHelper(final Helpers helpers) {
    IGenTemplateContext _context = helpers.getContext();
    this.context = _context;
    ZenModelLocator _zenModelLocator = helpers.getZenModelLocator();
    this.locator = _zenModelLocator;
    ParamsHelper _paramsHelper = helpers.getParamsHelper();
    this.paramsHelper = _paramsHelper;
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder traceForApi(final ResourceAPI api) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("resourceAPI:");
      String _locate = this.locator.locate(api);
      _builder.append(_locate, "");
      return this.getTrace(api, _builder.toString());
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder traceForComplexType(final ResourceDefinition resource) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("complexType:");
      String _locate = this.locator.locate(resource);
      _builder.append(_locate, "");
      return this.getTrace(resource, _builder.toString());
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setTypeNameInTrace(final EObject obj, final String name) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _trace = this.getTrace(obj, _builder.toString(), true);
      return _trace.withProperty("typeName", name);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setRootElementInTrace(final EObject obj, final String name) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _trace = this.getTrace(obj, _builder.toString(), true);
      return _trace.withProperty("rootElementName", name);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setElementNameInTrace(final EObject obj, final String name) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _trace = this.getTrace(obj, _builder.toString(), true);
      return _trace.withProperty("elementName", name);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setListElementInTrace(final EObject obj, final String name) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _trace = this.getTrace(obj, _builder.toString(), true);
      return _trace.withProperty("listElementName", name);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setListItemElementInTrace(final EObject obj, final String name) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _trace = this.getTrace(obj, _builder.toString(), true);
      return _trace.withProperty("listItemElementName", name);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setAttributeNameInTrace(final EObject obj, final String name) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _trace = this.getTrace(obj, _builder.toString(), true);
      return _trace.withProperty("attributeName", name);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public GenTemplateTraceBuilder.GenTemplateTraceItemBuilder setIdInTrace(final EObject obj) {
    try {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("namesInSchema:");
      String _locate = this.locator.locate(obj);
      _builder.append(_locate, "");
      return this.getTrace(obj, _builder.toString(), true);
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  private GenTemplateTraceBuilder.GenTemplateTraceItemBuilder getTrace(final EObject obj, final String key) {
    return this.getTrace(obj, key, false);
  }
  
  private GenTemplateTraceBuilder.GenTemplateTraceItemBuilder getTrace(final EObject obj, final String key, final boolean withId) {
    try {
      boolean _containsKey = this.traceItems.containsKey(key);
      boolean _not = (!_containsKey);
      if (_not) {
        String[] _split = key.split(":");
        final String type = IterableExtensions.head(((Iterable)Conversions.doWrapArray(_split)));
        GenTemplateTraceBuilder.GenTemplateTraceItemBuilder _addTraceItem = this.context.addTraceItem(type);
        String _locate = this.locator.locate(obj);
        final GenTemplateTraceBuilder.GenTemplateTraceItemBuilder item = _addTraceItem.withLocator(_locate);
        this.traceItems.put(key, item);
      }
      final GenTemplateTraceBuilder.GenTemplateTraceItemBuilder item_1 = this.traceItems.get(key);
      if (withId) {
        String _id = this.getId(obj);
        item_1.withProperty("id", _id);
      }
      return item_1;
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  private final Map idMap = Maps.newHashMap();
  
  private String getId(final EObject obj) {
    String _switchResult = null;
    XMLSchemaGenTemplate.TypeNamingMethod _typeNamingMethod = this.paramsHelper.typeNamingMethod();
    if (_typeNamingMethod != null) {
      switch (_typeNamingMethod) {
        case SIMPLE_NAME:
          _switchResult = this.getSimpleId(obj);
          break;
        case FULLY_QUALIFIED_NAME:
          _switchResult = this.getFqName(obj);
          break;
        default:
          break;
      }
    }
    String id = _switchResult;
    boolean _isValidIdFor = this.isValidIdFor(id, obj);
    if (_isValidIdFor) {
      return id;
    } else {
      for (int i = 1;; i++) {
        {
          final String disamb = ((id + "_") + Integer.valueOf(i));
          boolean _isValidIdFor_1 = this.isValidIdFor(disamb, obj);
          if (_isValidIdFor_1) {
            return disamb;
          }
        }
      }
    }
  }
  
  private boolean isValidIdFor(final String id, final EObject obj) {
    boolean _containsKey = this.idMap.containsKey(id);
    boolean _not = (!_containsKey);
    if (_not) {
      this.idMap.put(id, obj);
      return true;
    } else {
      EObject _get = this.idMap.get(id);
      boolean _tripleEquals = (obj == _get);
      if (_tripleEquals) {
        return true;
      } else {
        return false;
      }
    }
  }
  
  public String getSimpleId(final EObject obj) {
    String _switchResult = null;
    boolean _matched = false;
    if (!_matched) {
      if (obj instanceof ResourceAPI) {
        _matched=true;
        _switchResult = ((ResourceAPI)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof ResourceDefinition) {
        _matched=true;
        _switchResult = ((ResourceDefinition)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof TypedMessage) {
        _matched=true;
        _switchResult = CommonServices.getMessageTypeName(((TypedMessage)obj));
      }
    }
    if (!_matched) {
      if (obj instanceof DataModel) {
        _matched=true;
        _switchResult = ((DataModel)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof Structure) {
        _matched=true;
        _switchResult = ((Structure)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof Enumeration) {
        _matched=true;
        _switchResult = ((Enumeration)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof UserDefinedType) {
        _matched=true;
        _switchResult = ((UserDefinedType)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof Feature) {
        _matched=true;
        _switchResult = ((Feature)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof ReferenceElement) {
        _matched=true;
        _switchResult = ((ReferenceElement)obj).getName();
      }
    }
    if (!_matched) {
      if (obj instanceof PropertyRealization) {
        _matched=true;
        Feature _baseProperty = ((PropertyRealization)obj).getBaseProperty();
        _switchResult = this.getSimpleId(_baseProperty);
      }
    }
    if (!_matched) {
      if (obj instanceof Method) {
        _matched=true;
        _switchResult = ((Method)obj).getName();
      }
    }
    return _switchResult;
  }
  
  public String getFqName(final EObject obj) {
    Iterable _fqNameComponents = this.getFqNameComponents(obj);
    final Function1 _function = (String it) -> {
      return Boolean.valueOf((it != null));
    };
    Iterable _filter = IterableExtensions.filter(_fqNameComponents, _function);
    return IterableExtensions.join(_filter, ".");
  }
  
  public Iterable getFqNameComponents(final EObject obj) {
    List _xblockexpression = null;
    {
      final List parts = Lists.newArrayList();
      EObject current = obj;
      while ((current != null)) {
        {
          boolean _matched = false;
          if (!_matched) {
            if (current instanceof ZenModel) {
              _matched=true;
              String _name = ((ZenModel)current).getName();
              parts.add(_name);
              String _namespace = ((ZenModel)current).getNamespace();
              parts.add(_namespace);
            }
          }
          if (!_matched) {
            if (current instanceof ResourceAPI) {
              _matched=true;
              String _name = ((ResourceAPI)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof ServiceDataResource) {
              _matched=true;
              String _name = ((ServiceDataResource)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof Method) {
              _matched=true;
              HTTPMethods _httpMethod = ((Method)current).getHttpMethod();
              String _name = _httpMethod.getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof TypedRequest) {
              _matched=true;
              parts.add("Request");
            }
          }
          if (!_matched) {
            if (current instanceof TypedResponse) {
              _matched=true;
              int _statusCode = ((TypedResponse)current).getStatusCode();
              String _string = Integer.valueOf(_statusCode).toString();
              parts.add(_string);
            }
          }
          if (!_matched) {
            if (current instanceof DataModel) {
              _matched=true;
              String _name = ((DataModel)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof Structure) {
              _matched=true;
              String _name = ((Structure)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof Feature) {
              _matched=true;
              String _name = ((Feature)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof ReferenceElement) {
              _matched=true;
              String _name = ((ReferenceElement)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof Enumeration) {
              _matched=true;
              String _name = ((Enumeration)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof EnumConstant) {
              _matched=true;
              String _name = ((EnumConstant)current).getName();
              parts.add(_name);
            }
          }
          if (!_matched) {
            if (current instanceof UserDefinedType) {
              _matched=true;
              String _name = ((UserDefinedType)current).getName();
              parts.add(_name);
            }
          }
          EObject _eContainer = current.eContainer();
          current = _eContainer;
        }
      }
      _xblockexpression = ListExtensions.reverse(parts);
    }
    return _xblockexpression;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy