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

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

There is a newer version: 1.3.46
Show newest version
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
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy