Search in sources :

Example 61 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class OrmCacheSupport method evictMasterEntity.

/**
 * Evicts an entity from cache if it has the given entity as an element of collection.
 *
 * @param entity    which is being updated and can potentially be an element of a collection
 * @param changes   changes in the entity. Null when creating and removing the entity.
 */
public void evictMasterEntity(BaseGenericIdEntity entity, @Nullable EntityAttributeChanges changes) {
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    for (MetaProperty property : metaClass.getProperties()) {
        if (!property.getRange().isClass() || property.getRange().getCardinality().isMany())
            continue;
        MetaProperty inverseProp = property.getInverse();
        if (inverseProp == null || !inverseProp.getRange().getCardinality().isMany())
            continue;
        // the inverse property is a collection
        if (metadata.getTools().isCacheable(property.getRange().asClass())) {
            if (changes != null) {
                for (String attributeName : changes.getOwnAttributes()) {
                    if (property.getName().equals(attributeName)) {
                        evictEntity(changes.getOldValue(attributeName));
                        break;
                    }
                }
            } else {
                Object masterEntity = entity.getValue(property.getName());
                evictEntity(masterEntity);
            }
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 62 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class DesktopPickerField method updateComponent.

protected void updateComponent(Object value) {
    String text;
    if (value == null) {
        text = "";
    } else {
        if (value instanceof Instance) {
            if (captionMode.equals(CaptionMode.ITEM)) {
                text = ((Instance) value).getInstanceName();
            } else {
                Object propertyValue = ((Instance) value).getValue(captionProperty);
                MetaClass valueClass = metadata.getClassNN(value.getClass());
                MetaProperty property = valueClass.getProperty(captionProperty);
                text = metadataTools.format(propertyValue, property);
            }
        } else {
            text = value.toString();
        }
    }
    impl.setValue(text);
    prevTextValue = text;
    updateMissingValueState();
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Instance(com.haulmont.chile.core.model.Instance) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 63 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class DesktopTokenList method setLookup.

@Override
public void setLookup(boolean lookup) {
    if (this.lookup != lookup) {
        if (lookup) {
            lookupAction = new PickerField.LookupAction(lookupPickerField) {

                @Nonnull
                @Override
                protected Map<String, Object> prepareScreenParams() {
                    Map<String, Object> screenParams = super.prepareScreenParams();
                    if (isMultiSelect()) {
                        screenParams = new HashMap<>(screenParams);
                        WindowParams.MULTI_SELECT.set(screenParams, true);
                        // for backward compatibility
                        screenParams.put("multiSelect", "true");
                    }
                    return screenParams;
                }

                @SuppressWarnings("unchecked")
                @Override
                protected void handleLookupWindowSelection(Collection items) {
                    if (items.isEmpty()) {
                        return;
                    }
                    @SuppressWarnings("unchecked") Collection<Entity> selected = items;
                    CollectionDatasource optionsDatasource = lookupPickerField.getOptionsDatasource();
                    if (optionsDatasource != null && lookupPickerField.isRefreshOptionsOnLookupClose()) {
                        optionsDatasource.refresh();
                        if (datasource != null) {
                            for (Object obj : getDatasource().getItems()) {
                                Entity entity = (Entity) obj;
                                if (getOptionsDatasource().containsItem(entity.getId())) {
                                    datasource.updateItem(getOptionsDatasource().getItem(entity.getId()));
                                }
                            }
                        }
                    }
                    // add all selected items to tokens
                    if (itemChangeHandler != null) {
                        for (Entity newItem : selected) {
                            itemChangeHandler.addItem(newItem);
                        }
                    } else if (datasource != null) {
                        // get master entity and inverse attribute in case of nested datasource
                        Entity masterEntity = getMasterEntity(datasource);
                        MetaProperty inverseProp = getInverseProperty(datasource);
                        for (Entity newItem : selected) {
                            if (!datasource.containsItem(newItem.getId())) {
                                // Initialize reference to master entity
                                if (inverseProp != null && isInitializeMasterReference(inverseProp)) {
                                    newItem.setValue(inverseProp.getName(), masterEntity);
                                }
                                datasource.addItem(newItem);
                            }
                        }
                    }
                    afterSelect(items);
                    if (afterLookupSelectionHandler != null) {
                        afterLookupSelectionHandler.onSelect(items);
                    }
                }
            };
            lookupPickerField.addAction(lookupAction);
            if (getLookupScreen() != null) {
                lookupAction.setLookupScreen(getLookupScreen());
            }
            lookupAction.setLookupScreenOpenType(lookupOpenMode);
            lookupAction.setLookupScreenParams(lookupScreenParams);
            lookupAction.setLookupScreenDialogParams(lookupScreenDialogParams);
        } else {
            lookupPickerField.removeAction(lookupAction);
        }
        lookupAction.setAfterLookupCloseHandler((window, actionId) -> {
            if (afterLookupCloseHandler != null) {
                afterLookupCloseHandler.onClose(window, actionId);
            }
        });
        lookupAction.setAfterLookupSelectionHandler(items -> {
            if (afterLookupSelectionHandler != null) {
                afterLookupSelectionHandler.onSelect(items);
            }
        });
    }
    this.lookup = lookup;
    rootPanel.refreshComponent();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Nonnull(javax.annotation.Nonnull) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 64 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class DesktopTokenList method getInverseProperty.

@Nullable
protected MetaProperty getInverseProperty(CollectionDatasource datasource) {
    if (datasource instanceof NestedDatasource) {
        MetaProperty metaProperty = ((NestedDatasource) datasource).getProperty();
        com.google.common.base.Preconditions.checkState(metaProperty != null);
        return metaProperty.getInverse();
    }
    return null;
}
Also used : NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 65 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class DesktopAbstractField method initRequired.

protected void initRequired(MetaPropertyPath metaPropertyPath) {
    MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
    boolean newRequired = metaProperty.isMandatory();
    Object notNullUiComponent = metaProperty.getAnnotations().get(NotNull.class.getName() + "_notnull_ui_component");
    if (Boolean.TRUE.equals(notNullUiComponent)) {
        newRequired = true;
    }
    setRequired(newRequired);
    if (StringUtils.isEmpty(getRequiredMessage())) {
        MessageTools messageTools = AppBeans.get(MessageTools.NAME);
        setRequiredMessage(messageTools.getDefaultRequiredMessage(metaPropertyPath.getMetaClass(), metaPropertyPath.toString()));
    }
}
Also used : MessageTools(com.haulmont.cuba.core.global.MessageTools) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)157 MetaClass (com.haulmont.chile.core.model.MetaClass)102 Entity (com.haulmont.cuba.core.entity.Entity)44 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)26 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Range (com.haulmont.chile.core.model.Range)13 Nullable (javax.annotation.Nullable)11 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)9 java.util (java.util)9 Element (org.dom4j.Element)9 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)8 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Query (com.haulmont.cuba.core.Query)5 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)5 Collection (java.util.Collection)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 PropertyDatasource (com.haulmont.cuba.gui.data.PropertyDatasource)4 Logger (org.slf4j.Logger)4