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

com.reprezen.genflow.common.jsonschema.builder.ReferenceEmbedNode Maven / Gradle / Ivy

package com.reprezen.genflow.common.jsonschema.builder;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.reprezen.genflow.common.jsonschema.builder.JsonSchemaNodeFactory;
import com.reprezen.genflow.common.jsonschema.builder.NamedSchemaNode;
import com.reprezen.genflow.common.jsonschema.builder.ReferenceTreatmentNode;
import com.reprezen.genflow.common.jsonschema.builder.TypedMessageNode;
import com.reprezen.rapidml.Feature;
import com.reprezen.rapidml.RealizationContainer;
import com.reprezen.rapidml.ReferenceEmbed;
import com.reprezen.rapidml.ReferenceTreatment;
import com.reprezen.rapidml.ResourceDefinition;
import com.reprezen.rapidml.TypedMessage;
import com.reprezen.rapidml.util.TagUtils;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
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 ReferenceEmbedNode extends ReferenceTreatmentNode {
  private static class ReferenceEmbedItemNode extends ReferenceEmbedNode {
    private final boolean isArray;
    
    public ReferenceEmbedItemNode(final JsonSchemaNodeFactory director, final ReferenceEmbed element, final boolean isArray) {
      super(director, element);
      this.isArray = isArray;
    }
    
    @Override
    public void writeBody(final ObjectNode node) {
      node.put("type", "object");
      this.setObjectAsType(node);
      this.addVendorExtensions(node, this._extensionsHelper.getRapidExtensions(this.element));
      final ObjectNode propertiesNode = node.putObject("properties");
      final List requiredProperties = Lists.newArrayList();
      Iterables.addAll(requiredProperties, this.getRequiredPropertyNames(this.element.getReferenceRealization()));
      this.writeRequiredProperties(node, requiredProperties);
      List _includedPrimitiveProperties = this._zenModelHelper.getIncludedPrimitiveProperties(this.element);
      for (final Feature prop : _includedPrimitiveProperties) {
        this.factory.createFeatureNode(prop).write(propertiesNode);
      }
      EList _nestedReferenceTreatments = this.element.getNestedReferenceTreatments();
      for (final ReferenceTreatment referenceTreatment : _nestedReferenceTreatments) {
        {
          final ReferenceTreatmentNode nestedReferenceTreatmentNode = this.factory.createReferenceTreatmentNode(referenceTreatment);
          nestedReferenceTreatmentNode.inlineOrAddTopLevelNode(propertiesNode);
        }
      }
    }
    
    @Override
    public String getName() {
      String _xifexpression = null;
      if (this.isArray) {
        String _name = super.getName();
        _xifexpression = (_name + "_item");
      } else {
        _xifexpression = super.getName();
      }
      return _xifexpression;
    }
  }
  
  private final NamedSchemaNode defaultRealizationNode;
  
  public ReferenceEmbedNode(final JsonSchemaNodeFactory director, final ReferenceEmbed element) {
    super(director, element);
    boolean _useDefaultRealization = this.useDefaultRealization();
    if (_useDefaultRealization) {
      this.defaultRealizationNode = this.factory.createDefaultRealizationNode(element.getReferenceRealization());
    } else {
      this.defaultRealizationNode = null;
    }
  }
  
  @Override
  public ObjectNode inlineOrAddTopLevelNode(final ObjectNode parentNode) {
    if ((this.factory.getOptions().isInlineReferenceEmbeds() || (this.defaultRealizationNode != null))) {
      return this.write(parentNode);
    } else {
      final ObjectNode referenceTreatmentPropertyNode = parentNode.putObject(this.element.getReferenceElement().getName());
      this.factory.getDefinitionsNode().addReferenceToDefinition(referenceTreatmentPropertyNode, this);
      return referenceTreatmentPropertyNode;
    }
  }
  
  @Override
  public void writeType(final ObjectNode node) {
    if ((this.defaultRealizationNode != null)) {
      this.factory.getDefinitionsNode().addReferenceToDefinition(node, this.defaultRealizationNode);
    } else {
      final boolean isArray = this.isArrayProperty();
      final ReferenceEmbedNode.ReferenceEmbedItemNode nestedReferenceEmbedNode = new ReferenceEmbedNode.ReferenceEmbedItemNode(this.factory, this.element, isArray);
      if ((isArray && (!this.factory.getOptions().isInlineArrayItems()))) {
        this.factory.getDefinitionsNode().addReferenceToDefinition(node, nestedReferenceEmbedNode);
      } else {
        nestedReferenceEmbedNode.writeBody(node);
      }
    }
  }
  
  protected boolean useDefaultRealization() {
    return TagUtils.getTagWithName(this.element, TagUtils.WITH_DEFAULT_OBJECT_REALIZATION).isPresent();
  }
  
  @Override
  public String getName() {
    if ((this.defaultRealizationNode != null)) {
      return this.element.getReferenceElement().getName();
    }
    final ArrayList nameSegments = CollectionLiterals.newArrayList();
    nameSegments.add(this.getContainerName());
    final Function1 _function = (ReferenceEmbed it) -> {
      return it.getReferenceElement().getName();
    };
    nameSegments.addAll(ListExtensions.map(this.element.getEmbedHierarchy(), _function));
    nameSegments.add(this.element.getReferenceElement().getName());
    return IterableExtensions.join(nameSegments, "_");
  }
  
  private String getContainerName() {
    final EList embedHierarchy = this.element.getEmbedHierarchy();
    ReferenceEmbed _xifexpression = null;
    boolean _isEmpty = embedHierarchy.isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      final Function1 _function = (ReferenceEmbed it) -> {
        return Boolean.valueOf(true);
      };
      _xifexpression = IterableExtensions.findFirst(embedHierarchy, _function);
    } else {
      _xifexpression = this.element;
    }
    final ReferenceEmbed topLevelEmbed = _xifexpression;
    EObject _eContainer = topLevelEmbed.eContainer();
    final RealizationContainer parent = ((RealizationContainer) _eContainer);
    String _switchResult = null;
    boolean _matched = false;
    if (parent instanceof ResourceDefinition) {
      _matched=true;
      _switchResult = ((ResourceDefinition)parent).getName();
    }
    if (!_matched) {
      if (parent instanceof TypedMessage) {
        _matched=true;
        _switchResult = TypedMessageNode.getMessageDefinitionName(((TypedMessage)parent));
      }
    }
    final String containerName = _switchResult;
    return containerName;
  }
}