
org.grails.scaffolding.model.DomainModelServiceImpl.groovy Maven / Gradle / Ivy
Show all versions of fields Show documentation
package org.grails.scaffolding.model
import org.grails.datastore.mapping.config.Property
import org.grails.scaffolding.model.property.Constrained
import org.grails.scaffolding.model.property.DomainProperty
import org.grails.scaffolding.model.property.DomainPropertyFactory
import grails.util.GrailsClassUtils
import groovy.transform.CompileStatic
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.datastore.mapping.model.PersistentProperty
import org.grails.datastore.mapping.model.types.Embedded
import org.springframework.beans.factory.annotation.Autowired
import java.lang.reflect.Method
/**
* @see {@link DomainModelService}
* @author James Kleeh
*/
@CompileStatic
class DomainModelServiceImpl implements DomainModelService {
@Autowired
DomainPropertyFactory domainPropertyFactory
private static Method derivedMethod
static {
try {
derivedMethod = Property.class.getMethod("isDerived", (Class>[]) null)
} catch (NoSuchMethodException | SecurityException e) {
// no-op
}
}
/**
* Retrieves persistent properties and excludes:
* - Any properties listed in the {@code static scaffold = [exclude: []]} property on the domain class
*
- Any properties that have the constraint {@code [display: false]}
*
- Any properties whose name exist in the blackList
*
*
* @see {@link DomainModelService#getInputProperties}
* @param domainClass The persistent entity
* @param blackList The list of domain class property names to exclude
*/
protected List getProperties(PersistentEntity domainClass, List blacklist) {
List properties = domainClass.persistentProperties.collect {
domainPropertyFactory.build(it)
}
Object scaffoldProp = GrailsClassUtils.getStaticPropertyValue(domainClass.javaClass, 'scaffold')
if (scaffoldProp instanceof Map) {
Map scaffold = (Map)scaffoldProp
if (scaffold.containsKey('exclude')) {
if (scaffold.exclude instanceof Collection) {
blacklist.addAll((Collection)scaffold.exclude)
} else if (scaffold.exclude instanceof String) {
blacklist.add((String)scaffold.exclude)
}
}
}
properties.removeAll {
if (it.name in blacklist) {
return true
}
Constrained constrained = it.constrained
if (constrained && !constrained.display) {
return true
}
if (derivedMethod != null) {
Property property = it.mapping.mappedForm
if (derivedMethod.invoke(property, (Object[]) null)) {
return true
}
}
false
}
properties.sort()
properties
}
/**
* Blacklist:
* - version
*
- dateCreated
*
- lastUpdated
*
*
* @see {@link DomainModelServiceImpl#getProperties}
* @param domainClass The persistent entity
*/
List getInputProperties(PersistentEntity domainClass) {
getProperties(domainClass, ['version', 'dateCreated', 'lastUpdated'])
}
/**
* Blacklist:
* - version
*
*
* @see {@link DomainModelServiceImpl#getProperties}
* @param domainClass The persistent entity
*/
List getOutputProperties(PersistentEntity domainClass) {
getProperties(domainClass, ['version'])
}
/**
* The same as {@link #getOutputProperties(org.grails.datastore.mapping.model.PersistentEntity)} except the identifier is prepended
*
* @see {@link DomainModelServiceImpl#getOutputProperties}
* @param domainClass The persistent entity
*/
List getListOutputProperties(PersistentEntity domainClass) {
List properties = getOutputProperties(domainClass)
properties.add(0, domainPropertyFactory.build(domainClass.identity))
properties
}
/**
* Will return all properties in a domain class that the provided closure returns
* true for. Searches embedded properties
*
* @see {@link DomainModelService#findInputProperties}
* @param domainClass The persistent entity
* @param closure The closure that will be executed for each editable property
*/
List findInputProperties(PersistentEntity domainClass, Closure closure) {
List properties = []
getInputProperties(domainClass).each { DomainProperty domainProperty ->
PersistentProperty property = domainProperty.persistentProperty
if (property instanceof Embedded) {
getInputProperties(((Embedded)property).associatedEntity).each { DomainProperty embedded ->
embedded.rootProperty = domainProperty
if (closure.call(embedded)) {
properties.add(embedded)
}
}
} else {
if (closure.call(domainProperty)) {
properties.add(domainProperty)
}
}
}
properties
}
/**
* Returns true if the provided closure returns true for any domain class
* property. Searches embedded properties
*
* @see {@link DomainModelService#hasInputProperty}
* @param domainClass The persistent entity
* @param closure The closure that will be executed for each editable property
*/
Boolean hasInputProperty(PersistentEntity domainClass, Closure closure) {
findInputProperties(domainClass, closure).size() > 0
}
}