Search in sources :

Example 1 with PersistentProperty

use of org.grails.datastore.mapping.model.PersistentProperty in project grails-core by grails.

the class DefaultGrailsDomainClass method initializePersistentProperties.

private void initializePersistentProperties() {
    if (!propertiesInitialized) {
        verifyContextIsInitialized();
        propertyMap = new LinkedHashMap<>();
        PersistentProperty identity = persistentEntity.getIdentity();
        if (identity != null) {
            identifier = new DefaultGrailsDomainClassProperty(this, persistentEntity, identity);
        }
        // First go through the properties of the class and create domain properties
        // populating into a map
        populateDomainClassProperties();
        // set properties from map values
        persistentProperties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
        // if no identifier property throw exception
        if (identifier == null) {
            throw new GrailsDomainException("Identity property not found, but required in domain class [" + getFullName() + "]");
        } else {
            propertyMap.put(identifier.getName(), identifier);
        }
        // if no version property throw exception
        if (version != null) {
            propertyMap.put(version.getName(), version);
        }
        List<MetaProperty> properties = getMetaClass().getProperties();
        for (MetaProperty property : properties) {
            String name = property.getName();
            if (!propertyMap.containsKey(name) && !NameUtils.isConfigurational(name) && !Modifier.isStatic(property.getModifiers())) {
                propertyMap.put(name, new MetaGrailsDomainClassProperty(this, property));
            }
        }
        this.properties = propertyMap.values().toArray(new GrailsDomainClassProperty[propertyMap.size()]);
    }
    propertiesInitialized = true;
}
Also used : GrailsDomainClassProperty(grails.core.GrailsDomainClassProperty) PersistentProperty(org.grails.datastore.mapping.model.PersistentProperty) MetaProperty(groovy.lang.MetaProperty) GrailsDomainException(org.grails.core.exceptions.GrailsDomainException)

Example 2 with PersistentProperty

use of org.grails.datastore.mapping.model.PersistentProperty in project grails-core by grails.

the class DefaultGrailsDomainClass method getPersistentProperty.

public GrailsDomainClassProperty getPersistentProperty(String name) {
    initializePersistentProperties();
    if (propertyMap.containsKey(name)) {
        return propertyMap.get(name);
    }
    int indexOfDot = name.indexOf('.');
    if (indexOfDot > 0) {
        String basePropertyName = name.substring(0, indexOfDot);
        if (propertyMap.containsKey(basePropertyName)) {
            verifyContextIsInitialized();
            PersistentProperty prop = persistentEntity.getPropertyByName(basePropertyName);
            PersistentEntity referencedEntity = prop.getOwner();
            GrailsDomainClass referencedDomainClass = (GrailsDomainClass) grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, referencedEntity.getName());
            if (referencedDomainClass != null) {
                String restOfPropertyName = name.substring(indexOfDot + 1);
                return referencedDomainClass.getPropertyByName(restOfPropertyName);
            }
        }
    }
    return null;
}
Also used : PersistentEntity(org.grails.datastore.mapping.model.PersistentEntity) GrailsDomainClass(grails.core.GrailsDomainClass) PersistentProperty(org.grails.datastore.mapping.model.PersistentProperty)

Aggregations

PersistentProperty (org.grails.datastore.mapping.model.PersistentProperty)2 GrailsDomainClass (grails.core.GrailsDomainClass)1 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)1 MetaProperty (groovy.lang.MetaProperty)1 GrailsDomainException (org.grails.core.exceptions.GrailsDomainException)1 PersistentEntity (org.grails.datastore.mapping.model.PersistentEntity)1