Search in sources :

Example 1 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class OriginalEntityLoadInfo method create.

/**
 * Create a new info instance.
 * @param entity    entity instance
 * @return          info instance
 */
public static OriginalEntityLoadInfo create(Entity entity) {
    Objects.requireNonNull(entity, "entity is null");
    Metadata metadata = AppBeans.get(Metadata.class);
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    ExtendedEntities extendedEntities = AppBeans.get(ExtendedEntities.class);
    MetaClass metaClass = metadata.getSession().getClass(entity.getClass());
    MetaClass originalMetaClass = extendedEntities.getOriginalMetaClass(metaClass);
    if (originalMetaClass != null) {
        metaClass = originalMetaClass;
    }
    MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(metaClass);
    boolean stringKey = primaryKeyProperty != null && primaryKeyProperty.getJavaType().equals(String.class);
    return new OriginalEntityLoadInfo((UUID) EntityValues.getId(entity), metaClass, stringKey);
}
Also used : ExtendedEntities(io.jmix.core.ExtendedEntities) MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) Metadata(io.jmix.core.Metadata) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 2 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class ContainerValueSource method setApplicationContext.

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
    MetaClass metaClass = container.getEntityMetaClass();
    MetadataTools metadataTools = applicationContext.getBean(MetadataTools.class);
    this.metaPropertyPath = metadataTools.resolveMetaPropertyPath(metaClass, property);
    this.container.addItemChangeListener(this::containerItemChanged);
    this.container.addItemPropertyChangeListener(this::containerItemPropertyChanged);
    @SuppressWarnings("unchecked") InstanceContainer<?> parentCont = container;
    for (int i = 1; i < this.metaPropertyPath.length(); i++) {
        MetaPropertyPath intermediatePath = new MetaPropertyPath(this.metaPropertyPath.getMetaClass(), Arrays.copyOf(this.metaPropertyPath.getMetaProperties(), i));
        String pathToTarget = Joiner.on('.').join(Arrays.copyOfRange(this.metaPropertyPath.getPath(), i, this.metaPropertyPath.length()));
        DataComponents dataComponents = applicationContext.getBean(DataComponents.class);
        @SuppressWarnings("unchecked") InstanceContainer<Object> propertyCont = dataComponents.createInstanceContainer(intermediatePath.getRangeJavaClass());
        parentCont.addItemChangeListener(event -> {
            if (event.getItem() != null) {
                setState(BindingState.ACTIVE);
            } else {
                setState(BindingState.INACTIVE);
            }
            propertyCont.setItem(event.getItem() != null ? EntityValues.getValueEx(event.getItem(), intermediatePath.getMetaProperty().getName()) : null);
        });
        parentCont.addItemPropertyChangeListener(event -> {
            if (Objects.equals(event.getProperty(), intermediatePath.getMetaProperty().getName())) {
                Object entity = event.getValue();
                Object prevEntity = event.getPrevValue();
                propertyCont.setItem(entity);
                V prevValue = prevEntity != null ? EntityValues.getValueEx(prevEntity, pathToTarget) : null;
                V value = entity != null ? EntityValues.getValueEx(entity, pathToTarget) : null;
                events.publish(ValueChangeEvent.class, new ValueChangeEvent<>(this, prevValue, value));
            }
        });
        if (i == this.metaPropertyPath.length() - 1) {
            propertyCont.addItemPropertyChangeListener(event -> {
                if (Objects.equals(event.getProperty(), this.metaPropertyPath.getMetaProperty().getName())) {
                    events.publish(ValueChangeEvent.class, new ValueChangeEvent<>(this, (V) event.getPrevValue(), (V) event.getValue()));
                }
            });
        }
        parentCont = propertyCont;
    }
}
Also used : MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath)

Example 3 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class AbstractEditor method postCommit.

/**
 * Hook to be implemented in subclasses. Called by the framework after committing datasources.
 * The default implementation notifies about commit and calls {@link #postInit()} if the window is not closing.
 *
 * @param committed whether any data were actually changed and committed
 * @param close     whether the window is going to be closed
 * @return true to continue, false to abort
 */
protected boolean postCommit(boolean committed, boolean close) {
    if (committed && !close) {
        if (showSaveNotification) {
            Entity entity = getItem();
            MetadataTools metadataTools = getApplicationContext().getBean(MetadataTools.class);
            showNotification(messages.formatMessage("", "info.EntitySave", messageTools.getEntityCaption(metadata.getClass(entity)), metadataTools.getInstanceName(entity)), NotificationType.TRAY);
        }
        postInit();
        afterWindowApplyPostInit();
    }
    return true;
}
Also used : Entity(io.jmix.core.Entity) MetadataTools(io.jmix.core.MetadataTools)

Example 4 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class FilterConditionUtils method getPropertyLocCaption.

public static String getPropertyLocCaption(MetaClass metaClass, String propertyPath) {
    MessageTools messageTools = AppBeans.get(MessageTools.class);
    MetaPropertyPath mpp = metaClass.getPropertyPath(propertyPath);
    if (mpp == null) {
        return propertyPath;
    } else {
        MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
        MetaProperty[] metaProperties = mpp.getMetaProperties();
        StringBuilder sb = new StringBuilder();
        MetaPropertyPath parentMpp = null;
        MetaClass tempMetaClass;
        for (int i = 0; i < metaProperties.length; i++) {
            if (i == 0) {
                parentMpp = new MetaPropertyPath(metaClass, metaProperties[i]);
                tempMetaClass = metaClass;
            } else {
                parentMpp = new MetaPropertyPath(parentMpp, metaProperties[i]);
                tempMetaClass = metadataTools.getPropertyEnclosingMetaClass(parentMpp);
            }
            sb.append(messageTools.getPropertyCaption(tempMetaClass, metaProperties[i].getName()));
            if (i < metaProperties.length - 1) {
                sb.append(".");
            }
        }
        return sb.toString();
    }
}
Also used : MessageTools(io.jmix.core.MessageTools) MetadataTools(io.jmix.core.MetadataTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 5 with MetadataTools

use of io.jmix.core.MetadataTools in project jmix by jmix-framework.

the class PropertyCondition method updateText.

@Override
protected void updateText() {
    Metadata metadata = AppBeans.get(Metadata.class);
    MetadataTools metadataTools = metadata.getTools();
    String nameToUse = !Strings.isNullOrEmpty(propertiesPath) ? propertiesPath : name;
    boolean useCrossDataStoreRefId = false;
    boolean stringType = false;
    String thisStore = metaClass.getStore().getName();
    MetaPropertyPath propertyPath = metaClass.getPropertyPath(name);
    if (propertyPath != null) {
        MetaProperty metaProperty = propertyPath.getMetaProperty();
        String refIdProperty = metadataTools.getCrossDataStoreReferenceIdProperty(thisStore, propertyPath.getMetaProperty());
        if (refIdProperty != null) {
            useCrossDataStoreRefId = true;
            int lastdDot = nameToUse.lastIndexOf('.');
            if (lastdDot == -1) {
                nameToUse = refIdProperty;
            } else {
                nameToUse = nameToUse.substring(0, lastdDot + 1) + refIdProperty;
            }
        }
        stringType = String.class.equals(metaProperty.getJavaType());
    }
    if (operator == Op.DATE_INTERVAL) {
        text = dateIntervalConditionToJpql(nameToUse);
        return;
    }
    StringBuilder sb = new StringBuilder();
    StringBuilder sbJoin = new StringBuilder();
    if (operator == Op.NOT_IN) {
        sb.append("((");
    }
    String path = (metaClass instanceof KeyValueMetaClass && Strings.isNullOrEmpty(propertiesPath)) ? entityAlias : entityAlias + "." + nameToUse;
    String joinAlias = nameToUse.replace(".", "_") + RandomStringUtils.randomAlphabetic(5);
    if (Param.Type.ENTITY == param.getType() && !useCrossDataStoreRefId) {
        String primaryKeyName = metadataTools.getPrimaryKeyName(metadata.getClassNN(param.getJavaClass()));
        if (operator == Op.NOT_IN) {
            sbJoin.append("left join ").append(path).append(" ").append(joinAlias);
            sb.append(joinAlias).append(".").append(primaryKeyName);
        } else {
            sb.append(path).append(".").append(primaryKeyName);
        }
    } else {
        sb.append(path);
    }
    if (operator != Op.NOT_EMPTY) {
        PersistenceManagerClient persistenceManager = AppBeans.get(PersistenceManagerClient.class);
        if (operator == Op.EQUAL && stringType) /*&& persistenceManager.emulateEqualsByLike(thisStore)*/
        {
            sb.append(" ").append(Op.CONTAINS.forJpql());
        } else if (operator == Op.NOT_EQUAL && stringType) /*&& persistenceManager.emulateEqualsByLike(thisStore)*/
        {
            sb.append(" ").append(Op.DOES_NOT_CONTAIN.forJpql());
        } else {
            sb.append(" ").append(operator.forJpql());
        }
    }
    if (!operator.isUnary()) {
        sb.append(" :").append(param.getName());
        if (operator == Op.ENDS_WITH || operator == Op.STARTS_WITH || operator == Op.CONTAINS || operator == Op.DOES_NOT_CONTAIN) {
            CubaProperties properties = AppBeans.get(CubaProperties.class);
            if (properties.getDisableEscapingLikeForDataStores() == null || !properties.getDisableEscapingLikeForDataStores().contains(thisStore)) {
                sb.append(" ESCAPE '").append(QueryUtils.ESCAPE_CHARACTER).append("' ");
            }
        }
        if (operator == Op.NOT_IN) {
            if (Param.Type.ENTITY == param.getType() && !useCrossDataStoreRefId) {
                String primaryKeyName = metadataTools.getPrimaryKeyName(metadata.getClassNN(param.getJavaClass()));
                sb.append(") or (").append(joinAlias).append(".").append(primaryKeyName).append(" is null)) ");
            } else {
                sb.append(") or (").append(path).append(" is null)) ");
            }
        }
    }
    if (operator == Op.NOT_EMPTY) {
        sb.append(BooleanUtils.isTrue((Boolean) param.getValue()) ? " is not null" : " is null");
    }
    text = sb.toString();
    join = sbJoin.toString();
}
Also used : MetadataTools(io.jmix.core.MetadataTools) CubaProperties(com.haulmont.cuba.CubaProperties) KeyValueMetaClass(io.jmix.core.impl.keyvalue.KeyValueMetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) PersistenceManagerClient(com.haulmont.cuba.client.sys.PersistenceManagerClient) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Aggregations

MetadataTools (io.jmix.core.MetadataTools)14 MetaClass (io.jmix.core.metamodel.model.MetaClass)9 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)7 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)6 Metadata (com.haulmont.cuba.core.global.Metadata)3 Op (com.haulmont.cuba.core.global.filter.Op)2 OpManager (com.haulmont.cuba.core.global.filter.OpManager)2 Entity (io.jmix.core.Entity)2 MessageTools (io.jmix.core.MessageTools)2 Messages (io.jmix.core.Messages)2 Range (io.jmix.core.metamodel.model.Range)2 CubaProperties (com.haulmont.cuba.CubaProperties)1 PersistenceManagerClient (com.haulmont.cuba.client.sys.PersistenceManagerClient)1 LockService (com.haulmont.cuba.core.app.LockService)1 Security (com.haulmont.cuba.core.global.Security)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 PropertyCondition (com.haulmont.cuba.gui.components.filter.condition.PropertyCondition)1 NestedDatasource (com.haulmont.cuba.gui.data.NestedDatasource)1 RuntimePropsDatasource (com.haulmont.cuba.gui.data.RuntimePropsDatasource)1 CollectionPropertyDatasourceImpl (com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)1