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

org.joinedworkz.common.strategies.DtoFieldNameStrategy Maven / Gradle / Ivy

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

import java.util.Objects;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.xtext.xbase.lib.Extension;
import org.joinedworkz.common.helper.CmnModelHelper;
import org.joinedworkz.common.helper.Inflector;
import org.joinedworkz.common.helper.NameHelper;
import org.joinedworkz.core.facility.AbstractStrategy;
import org.joinedworkz.core.model.CmnField;
import org.joinedworkz.core.model.CmnObject;

@Singleton
@SuppressWarnings("all")
public class DtoFieldNameStrategy extends AbstractStrategy {
  @Inject
  @Extension
  private NameHelper _nameHelper;

  @Inject
  @Extension
  private CmnModelHelper _cmnModelHelper;

  @Inject
  private Inflector inflector;

  @Override
  public String apply(final CmnObject obj) {
    if ((obj instanceof CmnField)) {
      return this.dtoFieldName(((CmnField)obj));
    }
    return null;
  }

  protected String dtoFieldName(final CmnField field) {
    final String fieldName = this._cmnModelHelper.javaName(field);
    boolean _isReferenceField = this._cmnModelHelper.isReferenceField(field);
    if (_isReferenceField) {
      final String explicitReferenceName = this.determineExplicitReferenceName(field);
      if ((explicitReferenceName != null)) {
        return explicitReferenceName;
      }
      CmnField _sourceField = field.getSourceField();
      boolean _tripleNotEquals = (_sourceField != null);
      if (_tripleNotEquals) {
        if (((field.getIsRelation() != null) && field.getIsRelation().booleanValue())) {
          return this.dtoReferenceFieldName(fieldName, field);
        } else {
          final String sourceFieldJavaName = this._cmnModelHelper.sourceFieldJavaNameRecursive(field);
          boolean _notEquals = (!Objects.equals(fieldName, sourceFieldJavaName));
          if (_notEquals) {
            return fieldName;
          }
        }
      }
      return this.dtoReferenceFieldName(fieldName, field);
    } else {
      return fieldName;
    }
  }

  protected String determineExplicitReferenceName(final CmnField field) {
    final String referenceName = field.getString("referenceName");
    if ((referenceName != null)) {
      return referenceName;
    }
    CmnField _sourceField = field.getSourceField();
    boolean _tripleNotEquals = (_sourceField != null);
    if (_tripleNotEquals) {
      final CmnObject sourceFieldContainer = field.getSourceField().getContainer();
      final String defaultReferenceName = sourceFieldContainer.getString("defaultReferenceName");
      if ((defaultReferenceName != null)) {
        return defaultReferenceName;
      }
    } else {
      final String referenceNameOfReferencedType = field.getType().getString("defaultReferenceName");
      if ((referenceNameOfReferencedType != null)) {
        return referenceNameOfReferencedType;
      }
    }
    return null;
  }

  protected String dtoReferenceFieldName(final String fieldName, final CmnField field) {
    final String keyName = this._cmnModelHelper.keyJavaName(field.getType());
    final boolean isCollection = this._cmnModelHelper.isCollection(field);
    if (isCollection) {
      final String singularFieldName = this.inflector.singularize(fieldName);
      String _xifexpression = null;
      if ((keyName != null)) {
        _xifexpression = this.inflector.pluralize(keyName);
      } else {
        _xifexpression = null;
      }
      final String pluralKeyName = _xifexpression;
      return this.composeNameParts(singularFieldName, pluralKeyName, isCollection);
    } else {
      return this.composeNameParts(fieldName, keyName, isCollection);
    }
  }

  protected String composeNameParts(final String fieldName, final String keyName, final boolean isCollection) {
    String _firstUpper = null;
    if (keyName!=null) {
      _firstUpper=this._nameHelper.toFirstUpper(keyName);
    }
    return (fieldName + _firstUpper);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy