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) {
/* custom name of a field overrides the standard reference field naming */
if (field.sourceField !== null) {
val sourceFieldJavaName = field.sourceFieldJavaNameRecursive
if (fieldName != sourceFieldJavaName) {
return fieldName
}
}
return dtoReferenceFieldName(fieldName, field)
} else {
return fieldName
}
}
protected def dtoReferenceFieldName(String fieldName, CmnField field) {
val keyName = field.type.keyJavaName
if (field.collection) {
val singularFieldName = inflector.singularize(fieldName)
val pluralKeyName = keyName !== null ? inflector.pluralize(keyName) : null
return composeNameParts(singularFieldName, pluralKeyName)
} else {
return composeNameParts(fieldName, keyName)
}
}
protected def composeNameParts(String fieldName, String keyName) {
return fieldName + keyName?.toFirstUpper
}
}