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

org.joinedworkz.common.helper.CmnModelHelper Maven / Gradle / Ivy

There is a newer version: 1.3.46
Show newest version
package org.joinedworkz.common.helper;

import com.google.common.collect.Iterables;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions;
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;
import org.joinedworkz.common.adapter.KeyAdapter;
import org.joinedworkz.common.info.DataInfo;
import org.joinedworkz.common.strategies.ColumnNameStrategy;
import org.joinedworkz.common.strategies.DtoFieldNameStrategy;
import org.joinedworkz.common.strategies.TableNameStrategy;
import org.joinedworkz.core.model.CmnCardinality;
import org.joinedworkz.core.model.CmnComplexType;
import org.joinedworkz.core.model.CmnComponent;
import org.joinedworkz.core.model.CmnContent;
import org.joinedworkz.core.model.CmnElement;
import org.joinedworkz.core.model.CmnEnumeration;
import org.joinedworkz.core.model.CmnField;
import org.joinedworkz.core.model.CmnFieldContainer;
import org.joinedworkz.core.model.CmnFieldSet;
import org.joinedworkz.core.model.CmnModel;
import org.joinedworkz.core.model.CmnNamedObject;
import org.joinedworkz.core.model.CmnObject;
import org.joinedworkz.core.model.CmnOperation;
import org.joinedworkz.core.model.CmnOperationContainer;
import org.joinedworkz.core.model.CmnOperationParameter;
import org.joinedworkz.core.model.CmnPlatform;
import org.joinedworkz.core.model.CmnProperty;
import org.joinedworkz.core.model.CmnPropertyDefinition;
import org.joinedworkz.core.model.CmnRelation;
import org.joinedworkz.core.model.CmnResource;
import org.joinedworkz.core.model.CmnResourceOperation;
import org.joinedworkz.core.model.CmnResponse;
import org.joinedworkz.core.model.CmnService;
import org.joinedworkz.core.model.CmnType;
import org.joinedworkz.core.model.data.CmnArgument;
import org.joinedworkz.core.model.data.CmnData;
import org.joinedworkz.core.model.data.CmnPathCallSegment;
import org.joinedworkz.core.model.data.CmnReferenceData;
import org.joinedworkz.core.model.data.CmnResourceData;
import org.joinedworkz.core.model.ux.CmnEventHandling;

@Singleton
@SuppressWarnings("all")
public class CmnModelHelper {
  @Inject
  private TableNameStrategy tableNameStrategy;

  @Inject
  private DtoFieldNameStrategy dtoNameStrategy;

  @Inject
  private ColumnNameStrategy columnNameStrategy;

  private static final char BRACKET = '(';

  private static final char DOT = '.';

  @Extension
  protected NameHelper nameHelper = new NameHelper();

  @Extension
  protected StringHelper stringHelper = new StringHelper();

  protected static final String JAVA_NAME = "javaName";

  protected static final String JAVA_TYPE = "javaType";

  protected static final String TABLE_NAME = "tableName";

  protected static final String COLUMN_NAME = "columnName";

  protected static final String COLUMN_TYPE = "columnType";

  public String lastSegment(final String type) {
    if ((type != null)) {
      final int bracketIndex = type.indexOf(CmnModelHelper.BRACKET);
      int relevantDotIndex = (-1);
      if ((bracketIndex > 0)) {
        final String beforeBracket = type.substring(0, bracketIndex);
        relevantDotIndex = beforeBracket.lastIndexOf(CmnModelHelper.DOT);
      } else {
        relevantDotIndex = type.lastIndexOf(CmnModelHelper.DOT);
      }
      if ((relevantDotIndex > 0)) {
        return type.substring((relevantDotIndex + 1));
      }
    }
    return type;
  }

  public String classPrefixForNamespace(final CmnModel model) {
    if ((model != null)) {
      final String[] segments = model.getNamespace().split("\\.");
      final int segmentCount = ((List)Conversions.doWrapArray(segments)).size();
      if ((segmentCount > 2)) {
        String _firstUpper = this.nameHelper.toFirstUpper(segments[(segmentCount - 2)]);
        String _firstUpper_1 = this.nameHelper.toFirstUpper(segments[(segmentCount - 1)]);
        return (_firstUpper + _firstUpper_1);
      } else {
        if ((segmentCount == 2)) {
          return this.nameHelper.toFirstUpper(segments[(segmentCount - 1)]);
        } else {
          return model.getNamespace();
        }
      }
    } else {
      return "UNDEFINED";
    }
  }

  public String packageSegments(final String type) {
    if ((type != null)) {
      final int lastDotIndex = type.lastIndexOf(".");
      if ((lastDotIndex > 0)) {
        return type.substring(0, lastDotIndex);
      }
    }
    return type;
  }

  public CmnModel getModel(final CmnElement cmnObject) {
    if ((cmnObject instanceof CmnModel)) {
      return ((CmnModel)cmnObject);
    } else {
      CmnObject _container = cmnObject.getContainer();
      boolean _tripleNotEquals = (_container != null);
      if (_tripleNotEquals) {
        return this.getModel(cmnObject.getContainer());
      } else {
        Class _class = null;
        if (cmnObject!=null) {
          _class=cmnObject.getClass();
        }
        String _canonicalName = _class.getCanonicalName();
        String _plus = ("Model not found for element of type " + _canonicalName);
        throw new RuntimeException(_plus);
      }
    }
  }

  public Iterable concreteFieldContainers(final CmnModel model) {
    final Function1 _function = (CmnObject e) -> {
      return Boolean.valueOf(this.isDtoObject(e));
    };
    final Function1 _function_1 = (CmnFieldContainer it) -> {
      return Boolean.valueOf(this.isConcrete(it));
    };
    return IterableExtensions.filter(Iterables.filter(IterableExtensions.filter(model.getModelElements(), _function), CmnFieldContainer.class), _function_1);
  }

  public boolean isDtoObject(final CmnObject it) {
    return ((it instanceof CmnFieldContainer) && (!(it instanceof CmnFieldSet)));
  }

  public boolean isConcrete(final CmnFieldContainer it) {
    return ((it.getAbstract() == null) || (!(it.getAbstract()).booleanValue()));
  }

  public boolean isFinalized(final CmnObject it) {
    return this.isTrue(this.getModel(it), "finalized");
  }

  public boolean isAvailableInPlatform(final CmnObject it, final String qualifiedPlatformName) {
    final CmnModel modelOfObject = this.getModel(it);
    boolean _isTrue = this.isTrue(modelOfObject, "finalized");
    if (_isTrue) {
      return this.isPlatform(modelOfObject.getPlatform(), qualifiedPlatformName);
    }
    return true;
  }

  public boolean isPlatform(final CmnPlatform platform, final String qualifiedPlatformName) {
    String _qualifiedName = platform.getQualifiedName();
    boolean _equals = Objects.equals(_qualifiedName, qualifiedPlatformName);
    if (_equals) {
      return true;
    } else {
      CmnPlatform _basePlatform = platform.getBasePlatform();
      boolean _tripleNotEquals = (_basePlatform != null);
      if (_tripleNotEquals) {
        return this.isPlatform(platform.getBasePlatform(), qualifiedPlatformName);
      } else {
        return false;
      }
    }
  }

  public boolean multipleOccurences(final Integer maxOccurs) {
    return ((maxOccurs != null) && (1 != (maxOccurs).intValue()));
  }

  public CmnFieldContainer getBaseFieldContainer(final CmnElement it) {
    if ((it instanceof CmnType)) {
      final CmnType baseType = ((CmnType)it).getBaseType();
      if ((baseType instanceof CmnComplexType)) {
        return ((CmnFieldContainer)baseType);
      }
    }
    return null;
  }

  public Set collectBaseTypes(final Set types) {
    LinkedHashSet collectedTypes = CollectionLiterals.newLinkedHashSet();
    for (final CmnType type : types) {
      this.addBaseTypeRecursive(collectedTypes, type);
    }
    return collectedTypes;
  }

  public void addBaseTypeRecursive(final Set collectedTypes, final CmnType type) {
    final CmnType baseType = type.getBaseType();
    if ((baseType != null)) {
      collectedTypes.add(baseType);
      this.addBaseTypeRecursive(collectedTypes, baseType);
    }
  }

  public Set determineMappedSourceTypes(final CmnComplexType dtoType) {
    final LinkedHashSet baseTypes = CollectionLiterals.newLinkedHashSet();
    final Function1 _function = (CmnField it) -> {
      CmnField _sourceField = it.getSourceField();
      return Boolean.valueOf((_sourceField != null));
    };
    final Function1 _function_1 = (CmnField it) -> {
      return it.getSourceField().getContainer();
    };
    final Set sourceTypes = IterableExtensions.toSet(Iterables.filter(IterableExtensions.map(IterableExtensions.filter(dtoType.getFields(), _function), _function_1), CmnComplexType.class));
    for (final CmnComplexType sourceType : sourceTypes) {
      {
        final CmnType baseType = sourceType.getBaseType();
        if (((baseType != null) && sourceTypes.contains(baseType))) {
          baseTypes.add(baseType);
        }
      }
    }
    sourceTypes.removeAll(baseTypes);
    return sourceTypes;
  }

  public String getName(final CmnElement cmnObject) {
    if ((cmnObject instanceof CmnNamedObject)) {
      return ((CmnNamedObject)cmnObject).getName();
    }
    return null;
  }

  public String getName(final CmnOperationParameter it) {
    String _name = it.getName();
    boolean _tripleNotEquals = (_name != null);
    if (_tripleNotEquals) {
      return it.getName();
    } else {
      CmnField _referencedField = it.getReferencedField();
      boolean _tripleNotEquals_1 = (_referencedField != null);
      if (_tripleNotEquals_1) {
        return it.getReferencedField().getName();
      }
    }
    return null;
  }

  public String qualifiedName(final CmnNamedObject it) {
    String _namespace = this.getModel(it).getNamespace();
    String _plus = (_namespace + ".");
    String _name = it.getName();
    return (_plus + _name);
  }

  public Iterable getPropertyValues(final CmnObject object, final String propertyName) {
    final Object propertyValue = this.getPropertyValue(object, propertyName);
    if ((propertyValue == null)) {
      return CollectionLiterals.emptySet();
    } else {
      if ((propertyValue instanceof Collection)) {
        return ((Iterable)propertyValue);
      } else {
        return Collections.singleton(propertyValue);
      }
    }
  }

  public  T getPropertyValue(final CmnObject it, final String propertyName) {
    final CmnProperty property = it.getProperty(propertyName);
    if ((property != null)) {
      Object _value = property.getValue();
      return ((T) _value);
    }
    return null;
  }

  public Integer getInteger(final CmnObject object, final String propertyName) {
    final CmnProperty property = object.getProperty(propertyName);
    if ((property != null)) {
      final Object propertyValue = property.getValue();
      if ((propertyValue instanceof Integer)) {
        return ((Integer) propertyValue);
      }
    }
    return null;
  }

  public Boolean getBoolean(final CmnObject object, final String propertyName) {
    final CmnProperty property = object.getProperty(propertyName);
    if ((property != null)) {
      final Object propertyValue = property.getValue();
      if ((propertyValue instanceof Boolean)) {
        return ((Boolean) propertyValue);
      }
    }
    return null;
  }

  public boolean isTrue(final CmnObject object, final String propertyName) {
    final CmnProperty property = object.getProperty(propertyName);
    return this.isTrue(property);
  }

  public boolean isTrue(final CmnProperty property) {
    if ((property != null)) {
      final Object propertyValue = property.getValue();
      if ((propertyValue instanceof Boolean)) {
        return ((Boolean) propertyValue).booleanValue();
      }
    }
    return false;
  }

  public boolean isFalse(final CmnObject object, final String propertyName) {
    final CmnProperty property = object.getProperty(propertyName);
    return this.isFalse(property);
  }

  public boolean isFalse(final CmnProperty property) {
    if ((property != null)) {
      final Object propertyValue = property.getValue();
      if ((propertyValue instanceof Boolean)) {
        boolean _booleanValue = ((Boolean) propertyValue).booleanValue();
        return (!_booleanValue);
      }
    }
    return false;
  }

  public String titleFromDescription(final String description) {
    final String titleLine = this.stringHelper.textInLineAfterMarker(description, "@title");
    if ((titleLine != null)) {
      return titleLine.trim();
    }
    return null;
  }

  public String descriptionText(final String description) {
    if ((description != null)) {
      final int lastAnnotationIndex = description.lastIndexOf("@");
      if ((lastAnnotationIndex == (-1))) {
        return description;
      } else {
        final int textBeginIndex = this.stringHelper.indexOfLineBreakAfter(description, lastAnnotationIndex);
        if ((textBeginIndex >= 0)) {
          return description.substring(textBeginIndex).trim();
        }
      }
    }
    return null;
  }

  public String determineEnumType(final CmnType type) {
    if ((type instanceof CmnEnumeration)) {
      String _xifexpression = null;
      boolean _hasStereoType = this.hasStereoType(type, "integer");
      if (_hasStereoType) {
        _xifexpression = "Integer";
      } else {
        _xifexpression = null;
      }
      return _xifexpression;
    }
    return null;
  }

  public String javaType(final CmnElement object) {
    if ((object != null)) {
      final String javaType = this.javaTypeProperty(object);
      if ((javaType != null)) {
        return javaType;
      }
      if ((object instanceof CmnField)) {
        CmnType _type = ((CmnField)object).getType();
        String _name = null;
        if (_type!=null) {
          _name=_type.getName();
        }
        return _name;
      } else {
        if ((object instanceof CmnType)) {
          return ((CmnType)object).getName();
        }
      }
    }
    return null;
  }

  public String javaTypeProperty(final CmnElement it) {
    return it.getString(CmnModelHelper.JAVA_TYPE);
  }

  public CmnType keyType(final CmnType it) {
    if ((it instanceof CmnFieldContainer)) {
      CmnField _keyFieldOfType = this.keyFieldOfType(it);
      CmnType _type = null;
      if (_keyFieldOfType!=null) {
        _type=_keyFieldOfType.getType();
      }
      return _type;
    }
    return null;
  }

  public String keyJavaName(final CmnType it) {
    if ((it instanceof CmnFieldContainer)) {
      CmnField _keyFieldOfType = this.keyFieldOfType(it);
      String _javaName = null;
      if (_keyFieldOfType!=null) {
        _javaName=this.javaName(_keyFieldOfType);
      }
      return _javaName;
    }
    return null;
  }

  public CmnField keyFieldOfType(final CmnType it) {
    return this.keyFieldOfType(it, true);
  }

  public CmnField keyFieldOfType(final CmnType it, final boolean throwExceptionIfNoKeyField) {
    if ((it instanceof CmnFieldContainer)) {
      KeyAdapter keyAdapter = it.getAdapter(KeyAdapter.class);
      if ((keyAdapter == null)) {
        List _keyFields = ((CmnFieldContainer)it).getKeyFields();
        CmnField _head = null;
        if (_keyFields!=null) {
          _head=IterableExtensions.head(_keyFields);
        }
        CmnField keyField = _head;
        if ((keyField == null)) {
          CmnType _baseType = it.getBaseType();
          CmnField _keyFieldOfType = null;
          if (_baseType!=null) {
            _keyFieldOfType=this.keyFieldOfType(_baseType, throwExceptionIfNoKeyField);
          }
          keyField = _keyFieldOfType;
        }
        if ((throwExceptionIfNoKeyField && (keyField == null))) {
          String _name = it.getName();
          String _plus = ("Key field not found of type: " + _name);
          System.err.println(_plus);
          int _size = ((CmnFieldContainer)it).getKeyFields().size();
          String _plus_1 = ("Key fields size: " + Integer.valueOf(_size));
          System.err.println(_plus_1);
          String _name_1 = it.getName();
          String _plus_2 = ("No key fields found for type: " + _name_1);
          throw new IllegalStateException(_plus_2);
        }
        KeyAdapter _keyAdapter = new KeyAdapter(keyField);
        keyAdapter = _keyAdapter;
        it.putAdapter(keyAdapter);
      }
      return keyAdapter.getKeyField();
    }
    return null;
  }

  public boolean isKey(final CmnField it) {
    boolean _xifexpression = false;
    CmnObject _container = it.getContainer();
    if ((_container instanceof CmnFieldContainer)) {
      CmnObject _container_1 = it.getContainer();
      return this.containsKeyField(((CmnFieldContainer) _container_1), it);
    } else {
      _xifexpression = this.hasStereoType(it, "key");
    }
    return _xifexpression;
  }

  public boolean containsKeyField(final CmnFieldContainer container, final CmnField field) {
    List _keyFields = container.getKeyFields();
    boolean _contains = false;
    if (_keyFields!=null) {
      _contains=_keyFields.contains(field);
    }
    final boolean containsKey = _contains;
    if (containsKey) {
      return true;
    } else {
      CmnObject _container = container.getContainer();
      if ((_container instanceof CmnFieldContainer)) {
        CmnObject _container_1 = container.getContainer();
        final boolean containerContainsKey = this.containsKeyField(((CmnFieldContainer) _container_1), field);
        if (containsKey) {
          return true;
        }
      }
    }
    return this.hasStereoType(field, "key");
  }

  public boolean isVersion(final CmnField it) {
    return this.hasStereoType(it, "version");
  }

  public String javaType(final CmnOperationParameter object) {
    CmnType _type = object.getType();
    boolean _tripleNotEquals = (_type != null);
    if (_tripleNotEquals) {
      return this.javaType(object.getType());
    } else {
      CmnField _referencedField = object.getReferencedField();
      boolean _tripleNotEquals_1 = (_referencedField != null);
      if (_tripleNotEquals_1) {
        return this.javaDtoFieldType(object.getReferencedField());
      }
    }
    return null;
  }

  public CmnType dtoFieldType(final CmnField field) {
    boolean _isReferenceField = this.isReferenceField(field);
    if (_isReferenceField) {
      if (((field.getIsRelation() != null) && field.getIsRelation().booleanValue())) {
        final CmnObject relatedContainer = field.getSourceField().getContainer();
        if ((relatedContainer instanceof CmnType)) {
          return this.keyType(((CmnType)relatedContainer));
        }
      }
      final CmnType keyFieldType = this.keyType(this.referencedRootField(field).getType());
      if ((keyFieldType == null)) {
        String _name = field.getName();
        String _plus = ("Error: key type not found for field " + _name);
        String _plus_1 = (_plus + " of type ");
        String _name_1 = field.getType().getName();
        String _plus_2 = (_plus_1 + _name_1);
        System.err.println(_plus_2);
      }
      return keyFieldType;
    } else {
      return field.getType();
    }
  }

  public CmnType typeOfField(final CmnField field) {
    CmnType _type = field.getType();
    boolean _tripleNotEquals = (_type != null);
    if (_tripleNotEquals) {
      return field.getType();
    } else {
      CmnField _sourceField = field.getSourceField();
      boolean _tripleNotEquals_1 = (_sourceField != null);
      if (_tripleNotEquals_1) {
        return this.typeOfField(field.getSourceField());
      }
    }
    return null;
  }

  public CmnField referencedRootField(final CmnField field) {
    if (((field.getSourceField() != null) && (!this.isRelationField(field)))) {
      String _name = this.getName(field.getContainer());
      String _plus = ("Get root of: " + _name);
      String _plus_1 = (_plus + ".");
      String _name_1 = field.getName();
      String _plus_2 = (_plus_1 + _name_1);
      System.out.println(_plus_2);
      return this.referencedRootField(field.getSourceField());
    } else {
      return field;
    }
  }

  public Iterable collectAllReferences(final CmnFieldContainer it, final List references) {
    final Function1 _function = (CmnField it_1) -> {
      return Boolean.valueOf(this.isReferenceField(it_1));
    };
    Iterables.addAll(references, IterableExtensions.filter(it.getFields(), _function));
    final CmnFieldContainer base = this.getBaseFieldContainer(it);
    if ((base != null)) {
      this.collectAllReferences(base, references);
    }
    return references;
  }

  public Iterable collectReferencesToBeImplemented(final CmnFieldContainer it, final List references) {
    final Function1 _function = (CmnField it_1) -> {
      return Boolean.valueOf(this.isReferenceField(it_1));
    };
    Iterables.addAll(references, IterableExtensions.filter(it.getFields(), _function));
    final CmnFieldContainer base = this.getBaseFieldContainer(it);
    if (((base != null) && (base.getAbstract()).booleanValue())) {
      this.collectReferencesToBeImplemented(base, references);
    }
    return references;
  }

  public boolean isReferenceField(final CmnField field) {
    Boolean _isReference = field.getIsReference();
    boolean _tripleEquals = (Boolean.TRUE == _isReference);
    if (_tripleEquals) {
      return true;
    } else {
      if (((field.getSourceField() != null) && (!this.isRelationField(field)))) {
        final HashSet processedFields = CollectionLiterals.newHashSet();
        processedFields.add(field);
        return this.isReferenceFieldRecursive(field.getSourceField(), processedFields);
      }
    }
    return false;
  }

  public boolean isReferenceFieldRecursive(final CmnField field, final Set processedFields) {
    boolean _contains = processedFields.contains(field);
    if (_contains) {
      String _name = field.getName();
      String _plus = ("Field already processed: " + _name);
      System.out.println(_plus);
      return false;
    }
    processedFields.add(field);
    Boolean _isReference = field.getIsReference();
    boolean _tripleEquals = (Boolean.TRUE == _isReference);
    if (_tripleEquals) {
      return true;
    } else {
      CmnField _sourceField = field.getSourceField();
      boolean _tripleNotEquals = (_sourceField != null);
      if (_tripleNotEquals) {
        boolean _contains_1 = processedFields.contains(field.getSourceField());
        if (_contains_1) {
          String _name_1 = field.getSourceField().getName();
          String _plus_1 = ("Source field already processed: " + _name_1);
          System.out.println(_plus_1);
          return false;
        }
        return this.isReferenceFieldRecursive(field.getSourceField(), processedFields);
      }
    }
    return false;
  }

  public String javaDtoFieldType(final CmnField field) {
    if ((field != null)) {
      boolean _isReferenceField = this.isReferenceField(field);
      boolean _not = (!_isReferenceField);
      if (_not) {
        final String typeFromProperty = this.javaTypeProperty(field);
        if ((typeFromProperty != null)) {
          return typeFromProperty;
        }
      }
      final CmnType dtoType = this.dtoFieldType(field);
      if ((dtoType != null)) {
        return this.javaType(dtoType);
      } else {
        return "String";
      }
    } else {
      return null;
    }
  }

  public String javaDtoFieldName(final CmnField field) {
    return this.dtoNameStrategy.apply(field);
  }

  public String sourceFieldJavaNameRecursive(final CmnField field) {
    CmnField _sourceField = field.getSourceField();
    boolean _tripleNotEquals = (_sourceField != null);
    if (_tripleNotEquals) {
      return this.sourceFieldJavaNameRecursive(field.getSourceField());
    } else {
      return this.javaName(field);
    }
  }

  public String javaName(final CmnField field) {
    final String javaName = field.getString(CmnModelHelper.JAVA_NAME);
    if ((javaName != null)) {
      return javaName;
    } else {
      return field.getName();
    }
  }

  public String javaName(final CmnOperationParameter it) {
    String _name = it.getName();
    boolean _tripleNotEquals = (_name != null);
    if (_tripleNotEquals) {
      return it.getName();
    } else {
      CmnField _referencedField = it.getReferencedField();
      boolean _tripleNotEquals_1 = (_referencedField != null);
      if (_tripleNotEquals_1) {
        return this.javaDtoFieldName(it.getReferencedField());
      }
    }
    return null;
  }

  public String parameterName(final CmnOperationParameter it) {
    String _name = it.getName();
    boolean _tripleNotEquals = (_name != null);
    if (_tripleNotEquals) {
      return it.getName();
    } else {
      CmnField _referencedField = it.getReferencedField();
      boolean _tripleNotEquals_1 = (_referencedField != null);
      if (_tripleNotEquals_1) {
        return this.javaName(it.getReferencedField());
      }
    }
    return null;
  }

  public String tableName(final CmnElement object) {
    if ((object instanceof CmnFieldContainer)) {
      final String tableName = ((CmnFieldContainer)object).getString(CmnModelHelper.TABLE_NAME);
      if ((tableName != null)) {
        return tableName;
      }
      return this.tableNameStrategy.defaultTableName(((CmnFieldContainer)object));
    }
    return "UNDEFINED_TABLE_NAME";
  }

  public String columnName(final CmnField field) {
    if ((field != null)) {
      final String javaName = field.getString(CmnModelHelper.COLUMN_NAME);
      if ((javaName != null)) {
        return javaName;
      }
      return this.columnNameStrategy.defaultColumnName(field);
    }
    return null;
  }

  public String columnType(final CmnField field) {
    final String columnType = field.getString(CmnModelHelper.COLUMN_TYPE);
    final String javaType = this.javaDtoFieldType(field);
    if ((columnType != null)) {
      return columnType;
    } else {
      if ((javaType != null)) {
        boolean _endsWith = javaType.endsWith("UUID");
        if (_endsWith) {
          return "UUID";
        } else {
          boolean _endsWith_1 = javaType.endsWith("Integer");
          if (_endsWith_1) {
            final Integer precision = this.getInteger(field, "precision");
            if (((precision != null) && ((precision).intValue() < 5))) {
              return "SMALLINT";
            } else {
              return "INTEGER";
            }
          } else {
            boolean _endsWith_2 = javaType.endsWith("Long");
            if (_endsWith_2) {
              return "BIGINT";
            } else {
              boolean _endsWith_3 = javaType.endsWith("String");
              if (_endsWith_3) {
                return "VARCHAR";
              }
            }
          }
        }
      }
    }
    CmnType _type = field.getType();
    if ((_type instanceof CmnEnumeration)) {
      final String enumType = this.determineEnumType(field.getType());
      boolean _equals = Objects.equals(enumType, "Integer");
      if (_equals) {
        return "INTEGER";
      } else {
        return "VARCHAR";
      }
    }
    CmnType _type_1 = field.getType();
    String _name = null;
    if (_type_1!=null) {
      _name=_type_1.getName();
    }
    String _upperCase = null;
    if (_name!=null) {
      _upperCase=_name.toUpperCase();
    }
    final String typeName = _upperCase;
    boolean _hasStereoTypeString = this.hasStereoTypeString(field.getType());
    if (_hasStereoTypeString) {
      boolean _notEquals = (!Objects.equals("TEXT", typeName));
      if (_notEquals) {
        return "VARCHAR";
      }
    }
    CmnType _type_2 = field.getType();
    if ((_type_2 instanceof CmnComplexType)) {
      return "JSON";
    }
    return typeName;
  }

  public String firstStereoType(final CmnObject it) {
    Set _stereoTypes = it.getStereoTypes();
    boolean _tripleNotEquals = (_stereoTypes != null);
    if (_tripleNotEquals) {
      return IterableExtensions.head(it.getStereoTypes());
    }
    return null;
  }

  public boolean hasStereoType(final CmnObject it, final String stereoTypeName) {
    return ((it.getStereoTypes() != null) && it.getStereoTypes().contains(stereoTypeName));
  }

  public boolean hasStereoType(final CmnComplexType it, final String stereoTypeName) {
    return ((it.getStereoTypes() != null) && it.getStereoTypes().contains(stereoTypeName));
  }

  public boolean hasStereoType(final CmnFieldContainer it, final String stereoTypeName) {
    return ((it.getStereoTypes() != null) && it.getStereoTypes().contains(stereoTypeName));
  }

  public boolean hasStereoTypeContainedIn(final CmnObject it, final String... stereoTypeNames) {
    Set _stereoTypes = it.getStereoTypes();
    boolean _tripleNotEquals = (_stereoTypes != null);
    if (_tripleNotEquals) {
      for (final String stereoTypeName : stereoTypeNames) {
        boolean _contains = it.getStereoTypes().contains(stereoTypeName);
        if (_contains) {
          return true;
        }
      }
    }
    return false;
  }

  public boolean hasStereoTypeString(final CmnType it) {
    return this.hasStereoType(it, "string");
  }

  public Iterable allFields(final CmnFieldContainer fieldContainer, final boolean pullAllBaseFields) {
    if ((fieldContainer instanceof CmnComplexType)) {
      final Collection allFields = CollectionLiterals.newArrayList();
      this.collectFields(allFields, ((CmnType)fieldContainer), pullAllBaseFields);
      return allFields;
    } else {
      return fieldContainer.getFields();
    }
  }

  public void collectFields(final Collection collectedFields, final CmnType type, final boolean pullAllBaseFields) {
    if ((type instanceof CmnComplexType)) {
      if ((pullAllBaseFields || this.hasAbstractBaseType(((CmnComplexType)type)))) {
        this.collectFields(collectedFields, ((CmnComplexType)type).getBaseType(), pullAllBaseFields);
      }
      collectedFields.addAll(((CmnComplexType)type).getFields());
    }
  }

  public Iterable allDtoFields(final CmnFieldContainer fieldContainer, final boolean pullAllBaseFields) {
    if ((fieldContainer instanceof CmnComplexType)) {
      final Collection allFields = CollectionLiterals.newArrayList();
      this.collectDtoFields(allFields, ((CmnType)fieldContainer), pullAllBaseFields);
      return allFields;
    } else {
      return this.dtoFields(fieldContainer);
    }
  }

  public void collectDtoFields(final Collection collectedFields, final CmnType type, final boolean pullAllBaseFields) {
    if ((type instanceof CmnComplexType)) {
      if ((pullAllBaseFields || this.hasAbstractBaseType(((CmnComplexType)type)))) {
        this.collectFields(collectedFields, ((CmnComplexType)type).getBaseType(), pullAllBaseFields);
      }
      Iterables.addAll(collectedFields, this.dtoFields(((CmnFieldContainer)type)));
    }
  }

  public Iterable dtoFields(final CmnFieldContainer it) {
    final Function1 _function = (CmnField it_1) -> {
      boolean _isM2NRelation = this.isM2NRelation(it_1);
      return Boolean.valueOf((!_isM2NRelation));
    };
    return IterableExtensions.filter(it.getFields(), _function);
  }

  public boolean isM2NRelation(final CmnField it) {
    boolean _isRelationField = this.isRelationField(it);
    if (_isRelationField) {
      return ((this.isCollection(it) && (it.getSourceField() != null)) && this.isCollection(it.getSourceField()));
    } else {
      return false;
    }
  }

  public Collection allOperations(final CmnOperationContainer operationContainer, final boolean pullAllBaseOperations) {
    if ((operationContainer instanceof CmnService)) {
      return ((CmnService)operationContainer).getOperations();
    } else {
      final Collection allOperations = CollectionLiterals.newArrayList();
      this.collectOperations(allOperations, operationContainer, pullAllBaseOperations);
      return allOperations;
    }
  }

  public void collectOperations(final Collection collectedOperations, final CmnElement type, final boolean pullAllBaseOperations) {
    if ((type instanceof CmnComplexType)) {
      if ((pullAllBaseOperations || this.hasAbstractBaseType(((CmnComplexType)type)))) {
        this.collectOperations(collectedOperations, ((CmnComplexType)type).getBaseType(), pullAllBaseOperations);
      }
    }
    if ((type instanceof CmnFieldContainer)) {
      collectedOperations.addAll(((CmnFieldContainer)type).getOperations());
    }
  }

  public CmnType determineReturnType(final CmnOperation operation) {
    CmnType _returnType = operation.getReturnType();
    boolean _tripleNotEquals = (_returnType != null);
    if (_tripleNotEquals) {
      return operation.getReturnType();
    } else {
      CmnOperation _baseOperation = operation.getBaseOperation();
      boolean _tripleNotEquals_1 = (_baseOperation != null);
      if (_tripleNotEquals_1) {
        return this.determineReturnType(operation.getBaseOperation());
      }
    }
    return null;
  }

  public boolean hasAbstractBaseType(final CmnComplexType it) {
    final CmnFieldContainer baseFieldContainer = this.getBaseFieldContainer(it);
    return this.isAbstract(baseFieldContainer);
  }

  public boolean isAbstract(final CmnFieldContainer it) {
    if ((it != null)) {
      final Boolean isAbstract = it.getAbstract();
      return ((isAbstract != null) && (isAbstract).booleanValue());
    } else {
      return false;
    }
  }

  public boolean isAbstract(final CmnOperation it) {
    if ((it != null)) {
      final Boolean isAbstract = it.getAbstract();
      return ((isAbstract != null) && (isAbstract).booleanValue());
    } else {
      return false;
    }
  }

  public boolean isCollection(final CmnCardinality it) {
    if ((it != null)) {
      return ((it.getMaxOccurs() != null) && (((it.getMaxOccurs()).intValue() < 0) || ((it.getMaxOccurs()).intValue() > 1)));
    } else {
      return false;
    }
  }

  public boolean isRequired(final CmnCardinality it) {
    if ((it != null)) {
      return ((it.getMinOccurs() != null) && ((it.getMinOccurs()).intValue() > 0));
    } else {
      return false;
    }
  }

  public boolean isCollection(final CmnOperationParameter it) {
    if ((it != null)) {
      CmnField _referencedField = it.getReferencedField();
      boolean _tripleNotEquals = (_referencedField != null);
      if (_tripleNotEquals) {
        return (this.isCollection(it.getReferencedField()) || this.isCollection(((CmnCardinality) it)));
      } else {
        return this.isCollection(((CmnCardinality) it));
      }
    } else {
      return false;
    }
  }

  public boolean isCollection(final CmnField field) {
    final boolean collectionIsSpecifiedOnField = ((field.getMaxOccurs() != null) && (((field.getMaxOccurs()).intValue() < 0) || ((field.getMaxOccurs()).intValue() > 1)));
    if (collectionIsSpecifiedOnField) {
      return true;
    }
    CmnRelation _relation = field.getRelation();
    boolean _tripleNotEquals = (_relation != null);
    if (_tripleNotEquals) {
      final CmnObject fieldContainer = field.getContainer();
      CmnComplexType _leftType = field.getRelation().getLeftType();
      boolean _equals = Objects.equals(_leftType, fieldContainer);
      if (_equals) {
        return ((field.getRelation().getLeftMaxOccurs() != null) && (((field.getRelation().getLeftMaxOccurs()).intValue() < 0) || ((field.getRelation().getLeftMaxOccurs()).intValue() > 1)));
      } else {
        CmnComplexType _rightType = field.getRelation().getRightType();
        /* Objects.equals(_rightType, fieldContainer); */
      }
      return ((field.getRelation().getRightMaxOccurs() != null) && (((field.getRelation().getRightMaxOccurs()).intValue() < 0) || ((field.getRelation().getRightMaxOccurs()).intValue() > 1)));
    }
    return false;
  }

  public boolean isRelationField(final CmnField it) {
    if (((it.getIsRelation() != null) && (it.getIsRelation()).booleanValue())) {
      return ((this.isCollection(it) && (it.getSourceField() != null)) && this.isCollection(it.getSourceField()));
    } else {
      return false;
    }
  }

  public List getImmutableFields(final CmnFieldContainer it, final boolean recursive) {
    if ((it != null)) {
      final boolean immutable = this.hasStereoType(it, "immutable");
      final ArrayList immutableFields = CollectionLiterals.newArrayList();
      if (recursive) {
        immutableFields.addAll(this.getImmutableFields(this.getBaseFieldContainer(it), true));
      }
      final Function1 _function = (CmnField it_1) -> {
        return Boolean.valueOf((immutable || this.hasStereoType(it_1, "immutable")));
      };
      Iterables.addAll(immutableFields, IterableExtensions.filter(it.getFields(), _function));
      return immutableFields;
    } else {
      return CollectionLiterals.emptyList();
    }
  }

  public boolean hasImmutableFields(final CmnFieldContainer it, final boolean recursive) {
    if ((((it != null) && (it.getFields() != null)) && (!it.getFields().isEmpty()))) {
      boolean _hasStereoType = this.hasStereoType(it, "immutable");
      if (_hasStereoType) {
        return true;
      }
      final Function1 _function = (CmnField it_1) -> {
        return Boolean.valueOf(this.hasStereoType(it_1, "immutable"));
      };
      boolean _exists = IterableExtensions.exists(it.getFields(), _function);
      if (_exists) {
        return true;
      }
      if (recursive) {
        return this.hasImmutableFields(this.getBaseFieldContainer(it), true);
      }
    }
    return false;
  }

  public Iterable allImmutableFields(final CmnFieldContainer complexType) {
    final Function1 _function = (CmnField it) -> {
      boolean _isCollection = this.isCollection(it);
      return Boolean.valueOf((!_isCollection));
    };
    final Iterable baseImmutableFields = IterableExtensions.filter(this.getImmutableFields(this.getBaseFieldContainer(complexType), true), _function);
    final Function1 _function_1 = (CmnField it) -> {
      boolean _isCollection = this.isCollection(it);
      return Boolean.valueOf((!_isCollection));
    };
    final Iterable immutableFields = IterableExtensions.filter(this.getImmutableFields(complexType, false), _function_1);
    if (((!IterableExtensions.isEmpty(immutableFields)) || (!IterableExtensions.isEmpty(baseImmutableFields)))) {
      final ArrayList allImmutableFields = CollectionLiterals.newArrayList();
      Iterables.addAll(allImmutableFields, baseImmutableFields);
      Iterables.addAll(allImmutableFields, immutableFields);
      return allImmutableFields;
    }
    return CollectionLiterals.emptyList();
  }

  public CmnResource lastResourceOfPath(final List resourcePath) {
    Object _xifexpression = null;
    if ((resourcePath != null)) {
      for (int i = (((Object[])Conversions.unwrapArray(resourcePath, Object.class)).length - 1); (i >= 0); i--) {
        {
          final CmnPathCallSegment segment = resourcePath.get(i);
          CmnResource _resource = segment.getResource();
          boolean _tripleNotEquals = (_resource != null);
          if (_tripleNotEquals) {
            return segment.getResource();
          }
        }
      }
    }
    return ((CmnResource)_xifexpression);
  }

  public DataInfo createDataInfo(final CmnData data) {
    final List composedResourcePath = CollectionLiterals.newArrayList();
    final DataInfo dataInfo = this.composeDataInfo(composedResourcePath, data);
    dataInfo.setResourcePath(composedResourcePath);
    final LinkedHashSet requiredData = CollectionLiterals.newLinkedHashSet();
    List _resourcePath = dataInfo.getResourcePath();
    for (final CmnPathCallSegment pathSegment : _resourcePath) {
      CmnArgument _parameter = pathSegment.getParameter();
      boolean _tripleNotEquals = (_parameter != null);
      if (_tripleNotEquals) {
        final CmnArgument parameter = pathSegment.getParameter();
        CmnData _data = parameter.getData();
        boolean _tripleNotEquals_1 = (_data != null);
        if (_tripleNotEquals_1) {
          requiredData.add(parameter.getData());
        } else {
          CmnNamedObject _fieldSource = parameter.getFieldSource();
          if ((_fieldSource instanceof CmnData)) {
            CmnNamedObject _fieldSource_1 = parameter.getFieldSource();
            requiredData.add(((CmnData) _fieldSource_1));
          }
        }
      }
    }
    dataInfo.setRequiredData(requiredData);
    ListExtensions.reverse(dataInfo.getResourcePath());
    boolean _isEmpty = dataInfo.getResourcePath().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
      final CmnResource lastResource = this.lastResourceOfPath(dataInfo.getResourcePath());
      if (((dataInfo.getType() == null) && (lastResource.getRepresentation() instanceof CmnComplexType))) {
        CmnResourceOperation _resourceOperation = dataInfo.getResourceOperation();
        List _responses = null;
        if (_resourceOperation!=null) {
          _responses=_resourceOperation.getResponses();
        }
        boolean _tripleNotEquals_2 = (_responses != null);
        if (_tripleNotEquals_2) {
          final CmnResponse firstResponse = IterableExtensions.head(dataInfo.getResourceOperation().getResponses());
          CmnContent _content = null;
          if (firstResponse!=null) {
            _content=firstResponse.getContent();
          }
          boolean _tripleNotEquals_3 = (_content != null);
          if (_tripleNotEquals_3) {
            final CmnContent response = firstResponse.getContent();
            final CmnType responseType = response.getType();
            if ((responseType != null)) {
              dataInfo.setType(((CmnComplexType) responseType));
              dataInfo.setMultiple(((response.getMaxOccurs() != null) && ((response.getMaxOccurs()).intValue() == (-1))));
            }
          }
        }
        CmnComplexType _type = dataInfo.getType();
        boolean _tripleEquals = (_type == null);
        if (_tripleEquals) {
          CmnType _representation = lastResource.getRepresentation();
          dataInfo.setType(((CmnComplexType) _representation));
          dataInfo.setMultiple(((lastResource.getMaxOccurs() != null) && ((lastResource.getMaxOccurs()).intValue() == (-1))));
        }
      }
      List _resourcePath_1 = dataInfo.getResourcePath();
      CmnPathCallSegment _last = null;
      if (_resourcePath_1!=null) {
        _last=IterableExtensions.last(_resourcePath_1);
      }
      final CmnPathCallSegment lastSegment = _last;
      CmnArgument _parameter_1 = lastSegment.getParameter();
      final boolean lastIsParameter = (_parameter_1 != null);
      if ((lastIsParameter || dataInfo.isItemOfCollection())) {
        dataInfo.setMultiple(false);
      }
    }
    CmnComplexType _type_1 = dataInfo.getType();
    if ((_type_1 instanceof CmnComplexType)) {
      final List fieldList = CollectionLiterals.newArrayList();
      Iterables.addAll(fieldList, this.allFields(dataInfo.getType(), true));
      dataInfo.setFields(fieldList);
      dataInfo.setKeyField(this.keyFieldOfType(dataInfo.getType(), false));
    } else {
      dataInfo.setFields(Collections.emptyList());
    }
    CmnResourceOperation _resourceOperation_1 = dataInfo.getResourceOperation();
    boolean _tripleNotEquals_4 = (_resourceOperation_1 != null);
    if (_tripleNotEquals_4) {
      final boolean pagination = this.isTrue(dataInfo.getResourceOperation(), "pagination");
      final boolean paging = this.isTrue(dataInfo.getResourceOperation(), "paging");
      if ((pagination || paging)) {
        dataInfo.setSupportPagination(true);
      } else {
        dataInfo.setSupportPagination(false);
      }
    }
    return dataInfo;
  }

  public DataInfo composeDataInfo(final List composedResourcePath, final CmnData data) {
    if ((data instanceof CmnReferenceData)) {
      this.addResourcesFromResourcePath(composedResourcePath, ((CmnReferenceData)data).getResourcePath());
      DataInfo dataInfo = null;
      CmnComponent _referencedComponent = ((CmnReferenceData)data).getReferencedComponent();
      boolean _tripleNotEquals = (_referencedComponent != null);
      if (_tripleNotEquals) {
        DataInfo _dataInfo = new DataInfo();
        dataInfo = _dataInfo;
        dataInfo.setReferencedComponent(((CmnReferenceData)data).getReferencedComponent());
      } else {
        final CmnData referencedData = ((CmnReferenceData)data).getReferencedData();
        dataInfo = this.composeDataInfo(composedResourcePath, referencedData);
      }
      dataInfo.setData(data);
      dataInfo.setName(((CmnReferenceData)data).getName());
      dataInfo.setItemOfCollection(((CmnReferenceData)data).isItemOfCollection());
      dataInfo.setQueryParameterValues(((CmnReferenceData)data).getQueryParameterValues());
      CmnResourceOperation _resourceOperation = ((CmnReferenceData)data).getResourceOperation();
      boolean _tripleNotEquals_1 = (_resourceOperation != null);
      if (_tripleNotEquals_1) {
        dataInfo.setResourceOperation(((CmnReferenceData)data).getResourceOperation());
      }
      CmnEventHandling _eventHandling = ((CmnReferenceData)data).getEventHandling();
      boolean _tripleNotEquals_2 = (_eventHandling != null);
      if (_tripleNotEquals_2) {
        dataInfo.setInitEventHandling(((CmnReferenceData)data).getEventHandling());
      }
      CmnType _type = ((CmnReferenceData)data).getType();
      if ((_type instanceof CmnComplexType)) {
        CmnType _type_1 = ((CmnReferenceData)data).getType();
        dataInfo.setType(((CmnComplexType) _type_1));
        CmnCardinality _cardinality = ((CmnReferenceData)data).getCardinality();
        boolean _tripleNotEquals_3 = (_cardinality != null);
        if (_tripleNotEquals_3) {
          dataInfo.setMultiple(((((CmnReferenceData)data).getCardinality().getMaxOccurs() != null) && ((((CmnReferenceData)data).getCardinality().getMaxOccurs()).intValue() == (-1))));
        } else {
          dataInfo.setMultiple(false);
        }
      }
      return dataInfo;
    } else {
      if ((data instanceof CmnResourceData)) {
        final DataInfo dataInfo_1 = new DataInfo();
        dataInfo_1.setData(data);
        dataInfo_1.setName(((CmnResourceData)data).getName());
        CmnType _type_2 = ((CmnResourceData)data).getType();
        boolean _tripleNotEquals_4 = (_type_2 != null);
        if (_tripleNotEquals_4) {
          CmnType _type_3 = ((CmnResourceData)data).getType();
          dataInfo_1.setType(((CmnComplexType) _type_3));
          CmnCardinality _cardinality_1 = ((CmnResourceData)data).getCardinality();
          boolean _tripleNotEquals_5 = (_cardinality_1 != null);
          if (_tripleNotEquals_5) {
            dataInfo_1.setMultiple(((((CmnResourceData)data).getCardinality().getMaxOccurs() != null) && ((((CmnResourceData)data).getCardinality().getMaxOccurs()).intValue() == (-1))));
          } else {
            dataInfo_1.setMultiple(false);
          }
        }
        final CmnProperty locationProperty = ((CmnResourceData)data).getProperty("location");
        if ((locationProperty != null)) {
          Object _value = locationProperty.getValue();
          if ((_value instanceof CmnPropertyDefinition)) {
            Object _value_1 = locationProperty.getValue();
            dataInfo_1.setLocationProperty(((CmnPropertyDefinition) _value_1));
          } else {
            String _canonicalName = CmnPropertyDefinition.class.getCanonicalName();
            String _plus = ("location is not instance of " + _canonicalName);
            System.out.println(_plus);
          }
        }
        this.addResourcesFromResourcePath(composedResourcePath, ((CmnResourceData)data).getResourcePath());
        dataInfo_1.setItemOfCollection(((CmnResourceData)data).isItemOfCollection());
        dataInfo_1.setQueryParameterValues(((CmnResourceData)data).getQueryParameterValues());
        CmnResourceOperation _resourceOperation_1 = ((CmnResourceData)data).getResourceOperation();
        boolean _tripleNotEquals_6 = (_resourceOperation_1 != null);
        if (_tripleNotEquals_6) {
          dataInfo_1.setResourceOperation(((CmnResourceData)data).getResourceOperation());
        }
        CmnEventHandling _eventHandling_1 = ((CmnResourceData)data).getEventHandling();
        boolean _tripleNotEquals_7 = (_eventHandling_1 != null);
        if (_tripleNotEquals_7) {
          dataInfo_1.setInitEventHandling(((CmnResourceData)data).getEventHandling());
        }
        return dataInfo_1;
      }
    }
    return null;
  }

  public void addResourcesFromResourcePath(final List composedResourcePath, final List resourcePath) {
    if ((resourcePath != null)) {
      for (int i = (((Object[])Conversions.unwrapArray(resourcePath, Object.class)).length - 1); (i >= 0); i--) {
        {
          final CmnPathCallSegment resourcePathSegment = resourcePath.get(i);
          composedResourcePath.add(resourcePathSegment);
        }
      }
    }
  }
}