Search in sources :

Example 1 with EmbeddedParameters

use of com.haulmont.cuba.core.entity.annotation.EmbeddedParameters in project cuba by cuba-platform.

the class EclipseLinkSessionEventListener method preLogin.

@Override
public void preLogin(SessionEvent event) {
    Session session = event.getSession();
    setPrintInnerJoinOnClause(session);
    Map<Class, ClassDescriptor> descriptorMap = session.getDescriptors();
    for (Map.Entry<Class, ClassDescriptor> entry : descriptorMap.entrySet()) {
        MetaClass metaClass = metadata.getSession().getClassNN(entry.getKey());
        ClassDescriptor desc = entry.getValue();
        setCacheable(metaClass, desc, session);
        if (Entity.class.isAssignableFrom(desc.getJavaClass())) {
            // set DescriptorEventManager that doesn't invoke listeners for base classes
            desc.setEventManager(new DescriptorEventManager() {

                @Override
                public void notifyListeners(DescriptorEvent event) {
                    if (hasAnyListeners()) {
                        for (int index = 0; index < getEventListeners().size(); index++) {
                            DescriptorEventListener listener = (DescriptorEventListener) getEventListeners().get(index);
                            notifyListener(listener, event);
                        }
                    }
                }
            });
            desc.getEventManager().addListener(descriptorEventListener);
        }
        if (SoftDelete.class.isAssignableFrom(desc.getJavaClass())) {
            desc.getQueryManager().setAdditionalCriteria("this.deleteTs is null");
            desc.setDeletePredicate(entity -> entity instanceof SoftDelete && PersistenceHelper.isLoaded(entity, "deleteTs") && ((SoftDelete) entity).isDeleted());
        }
        List<DatabaseMapping> mappings = desc.getMappings();
        for (DatabaseMapping mapping : mappings) {
            // support UUID
            String attributeName = mapping.getAttributeName();
            MetaProperty metaProperty = metaClass.getPropertyNN(attributeName);
            if (metaProperty.getRange().isDatatype()) {
                if (metaProperty.getJavaType().equals(UUID.class)) {
                    ((DirectToFieldMapping) mapping).setConverter(UuidConverter.getInstance());
                    setDatabaseFieldParameters(session, mapping.getField());
                }
            } else if (metaProperty.getRange().isClass() && !metaProperty.getRange().getCardinality().isMany()) {
                MetaClass refMetaClass = metaProperty.getRange().asClass();
                MetaProperty refPkProperty = metadata.getTools().getPrimaryKeyProperty(refMetaClass);
                if (refPkProperty != null && refPkProperty.getJavaType().equals(UUID.class)) {
                    for (DatabaseField field : ((OneToOneMapping) mapping).getForeignKeyFields()) {
                        setDatabaseFieldParameters(session, field);
                    }
                }
            }
            // embedded attributes
            if (mapping instanceof AggregateObjectMapping) {
                EmbeddedParameters embeddedParameters = metaProperty.getAnnotatedElement().getAnnotation(EmbeddedParameters.class);
                if (embeddedParameters != null && !embeddedParameters.nullAllowed())
                    ((AggregateObjectMapping) mapping).setIsNullAllowed(false);
            }
            if (mapping.isOneToManyMapping()) {
                OneToManyMapping oneToManyMapping = (OneToManyMapping) mapping;
                if (SoftDelete.class.isAssignableFrom(oneToManyMapping.getReferenceClass())) {
                    oneToManyMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
                }
            }
            if (mapping.isOneToOneMapping()) {
                OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
                if (SoftDelete.class.isAssignableFrom(oneToOneMapping.getReferenceClass())) {
                    if (mapping.isManyToOneMapping()) {
                        oneToOneMapping.setSoftDeletionForBatch(false);
                        oneToOneMapping.setSoftDeletionForValueHolder(false);
                    } else {
                        OneToOne oneToOne = metaProperty.getAnnotatedElement().getAnnotation(OneToOne.class);
                        if (oneToOne != null) {
                            if (Strings.isNullOrEmpty(oneToOne.mappedBy())) {
                                oneToOneMapping.setSoftDeletionForBatch(false);
                                oneToOneMapping.setSoftDeletionForValueHolder(false);
                            } else {
                                oneToOneMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : EmbeddedParameters(com.haulmont.cuba.core.entity.annotation.EmbeddedParameters) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) DescriptorEventListener(org.eclipse.persistence.descriptors.DescriptorEventListener) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) DescriptorEventManager(org.eclipse.persistence.descriptors.DescriptorEventManager) OneToOne(javax.persistence.OneToOne) MetaClass(com.haulmont.chile.core.model.MetaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Map(java.util.Map) Session(org.eclipse.persistence.sessions.Session)

Example 2 with EmbeddedParameters

use of com.haulmont.cuba.core.entity.annotation.EmbeddedParameters in project cuba by cuba-platform.

the class MetadataImpl method createEmbedded.

protected void createEmbedded(Entity entity) {
    MetaClass metaClass = getClassNN(entity.getClass());
    for (MetaProperty property : metaClass.getProperties()) {
        if (property.getRange().isClass() && tools.isEmbedded(property)) {
            EmbeddedParameters embeddedParameters = property.getAnnotatedElement().getAnnotation(EmbeddedParameters.class);
            if (embeddedParameters != null && !embeddedParameters.nullAllowed()) {
                MetaClass embeddableMetaClass = property.getRange().asClass();
                Entity embeddableEntity = create(embeddableMetaClass);
                entity.setValue(property.getName(), embeddableEntity);
            }
        }
    }
}
Also used : EmbeddedParameters(com.haulmont.cuba.core.entity.annotation.EmbeddedParameters) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 EmbeddedParameters (com.haulmont.cuba.core.entity.annotation.EmbeddedParameters)2 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)1 Map (java.util.Map)1 OneToOne (javax.persistence.OneToOne)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)1 DescriptorEventListener (org.eclipse.persistence.descriptors.DescriptorEventListener)1 DescriptorEventManager (org.eclipse.persistence.descriptors.DescriptorEventManager)1 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 Session (org.eclipse.persistence.sessions.Session)1