org.joinedworkz.common.strategies.DtoFieldNameStrategy.xtend Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-base Show documentation
Show all versions of common-base Show documentation
DSL based modeling framework - facilities common base
package org.joinedworkz.common.strategies
import org.joinedworkz.common.helper.NameHelper
import org.joinedworkz.core.facility.AbstractStrategy
import org.joinedworkz.core.model.CmnField
import org.joinedworkz.core.model.CmnObject
import javax.inject.Singleton
import org.joinedworkz.common.helper.CmnModelHelper
import javax.inject.Inject
import org.joinedworkz.common.helper.Inflector
@Singleton
class DtoFieldNameStrategy extends AbstractStrategy {
@Inject
extension NameHelper
@Inject
extension CmnModelHelper
@Inject
Inflector inflector
override String apply(CmnObject obj) {
if (obj instanceof CmnField) {
return obj.dtoFieldName
}
}
protected def String dtoFieldName(CmnField field) {
val fieldName = field.javaName
if (field.isReferenceField) {
/* use reference name in case it is defined */
val explicitReferenceName = field.determineExplicitReferenceName
if (explicitReferenceName !== null) {
return explicitReferenceName
}
/* custom name of a field overrides the standard reference field naming */
if (field.sourceField !== null) {
if (field.isRelation !== null && field.isRelation.booleanValue) {
/* in case of a relation is the sourceField the opposite Field */
return dtoReferenceFieldName(fieldName, field)
} else {
val sourceFieldJavaName = field.sourceFieldJavaNameRecursive
if (fieldName != sourceFieldJavaName) {
return fieldName
}
}
}
return dtoReferenceFieldName(fieldName, field)
} else {
return fieldName
}
}
protected def determineExplicitReferenceName(CmnField field) {
val referenceName = field.getString("referenceName")
if (referenceName !== null) {
return referenceName
}
if (field.sourceField !== null) {
val sourceFieldContainer = field.sourceField.container
val defaultReferenceName = sourceFieldContainer.getString("defaultReferenceName")
if (defaultReferenceName !== null) {
return defaultReferenceName
}
} else {
val referenceNameOfReferencedType = field.type.getString("defaultReferenceName")
if (referenceNameOfReferencedType !== null) {
return referenceNameOfReferencedType
}
}
}
protected def dtoReferenceFieldName(String fieldName, CmnField field) {
val keyName = field.type.keyJavaName
val isCollection = field.collection
if (isCollection) {
val singularFieldName = inflector.singularize(fieldName)
val pluralKeyName = keyName !== null ? inflector.pluralize(keyName) : null
return composeNameParts(singularFieldName, pluralKeyName, isCollection)
} else {
return composeNameParts(fieldName, keyName, isCollection)
}
}
protected def composeNameParts(String fieldName, String keyName, boolean isCollection) {
return fieldName + keyName?.toFirstUpper
}
}