Search in sources :

Example 11 with MetadataTools

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

the class CustomConditionFrame method fillEntitySelect.

protected void fillEntitySelect(Param param) {
    if (!entitySelect.isEnabled()) {
        entitySelect.setValue(null);
        return;
    }
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    MessageTools messageTools = AppBeans.get(MessageTools.class);
    Map<String, Object> items = new TreeMap<>();
    Object selectedItem = null;
    if (ParamType.ENTITY.equals(typeSelect.getValue())) {
        for (MetaClass metaClass : metadataTools.getAllJpaEntityMetaClasses()) {
            if (!metadataTools.isSystemLevel(metaClass)) {
                items.put(messageTools.getEntityCaption(metaClass) + " (" + metaClass.getName() + ")", metaClass);
            }
        }
        if (param != null && Param.Type.ENTITY.equals(param.getType())) {
            Class javaClass = param.getJavaClass();
            Metadata metadata = AppBeans.get(Metadata.class);
            selectedItem = metadata.getClass(javaClass);
        }
        entitySelect.setOptionsMap(items);
        entitySelect.setValue(selectedItem);
    } else if (ParamType.ENUM.equals(typeSelect.getValue())) {
        if (param != null && Param.Type.ENUM.equals(param.getType())) {
            selectedItem = param.getJavaClass();
        }
        boolean selectedItemFound = false;
        for (Class enumClass : metadataTools.getAllEnums()) {
            items.put(getEnumClassName(enumClass), enumClass);
            if (selectedItem == null || selectedItem.equals(enumClass))
                selectedItemFound = true;
        }
        // the meta model, hence not in MetadataHelper.getAllEnums(). So we just add it here.
        if (selectedItem != null && !selectedItemFound) {
            items.put(getEnumClassName((Class) selectedItem), selectedItem);
        }
        entitySelect.setOptionsMap(items);
        entitySelect.setValue(selectedItem);
    }
}
Also used : MetadataTools(io.jmix.core.MetadataTools) MessageTools(io.jmix.core.MessageTools) MetaClass(io.jmix.core.metamodel.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) MetaClass(io.jmix.core.metamodel.model.MetaClass)

Example 12 with MetadataTools

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

the class PropertyOperationEditor method createComponent.

@Override
protected Component createComponent() {
    OpManager opManager = AppBeans.get(OpManager.class);
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    componentsFactory = AppBeans.get(ComponentsFactory.class);
    popupButton = componentsFactory.createComponent(PopupButton.class);
    MetaClass metaClass = condition.getEntityMetaClass();
    MetaPropertyPath propertyPath = metaClass.getPropertyPath(condition.getName());
    if (propertyPath == null) {
        throw new IllegalStateException(String.format("Unable to find property '%s' in entity %s", condition.getName(), metaClass));
    }
    MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
    for (Op op : opManager.availableOps(propertyMetaClass, propertyPath.getMetaProperty())) {
        OperatorChangeAction operatorChangeAction = new OperatorChangeAction(op);
        popupButton.addAction(operatorChangeAction);
    }
    Messages messages = AppBeans.get(Messages.class);
    popupButton.setCaption(messages.getMessage(Op.class, "Op." + condition.getOperator().name()));
    popupButton.setStyleName("condition-operation-button");
    return popupButton;
}
Also used : ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Op(com.haulmont.cuba.core.global.filter.Op) MetadataTools(io.jmix.core.MetadataTools) Messages(io.jmix.core.Messages) PopupButton(io.jmix.ui.component.PopupButton) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) OpManager(com.haulmont.cuba.core.global.filter.OpManager)

Example 13 with MetadataTools

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

the class PropertyConditionDescriptor method createCondition.

@Override
public AbstractCondition createCondition() {
    OpManager opManager = AppBeans.get(OpManager.class);
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    PropertyCondition propertyCondition = new PropertyCondition(this, entityAlias, propertiesPath);
    MetaPropertyPath propertyPath = datasourceMetaClass.getPropertyPath(name);
    if (propertyPath == null) {
        throw new IllegalStateException(String.format("Unable to find property '%s' in entity %s", name, datasourceMetaClass));
    }
    MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
    EnumSet<Op> ops = opManager.availableOps(propertyMetaClass, propertyPath.getMetaProperty());
    propertyCondition.setOperator(ops.iterator().next());
    return propertyCondition;
}
Also used : Op(com.haulmont.cuba.core.global.filter.Op) MetadataTools(io.jmix.core.MetadataTools) PropertyCondition(com.haulmont.cuba.gui.components.filter.condition.PropertyCondition) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) OpManager(com.haulmont.cuba.core.global.filter.OpManager)

Example 14 with MetadataTools

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

the class AbstractEditor method setItemInternal.

@SuppressWarnings("unchecked")
protected void setItemInternal(Entity item) {
    Datasource ds = getDatasourceInternal();
    DataSupplier dataservice = ds.getDataSupplier();
    DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
    DynamicAttributesGuiTools dynamicAttributesGuiTools = (DynamicAttributesGuiTools) getApplicationContext().getBean(DynamicAttributesGuiTools.NAME);
    if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getFrame().getId())) {
        ds.setLoadDynamicAttributes(true);
    }
    Class<? extends Entity> entityClass = item.getClass();
    Object entityId = EntityValues.getId(item);
    EntityStates entityStates = getApplicationContext().getBean(EntityStates.class);
    if (parentDs != null) {
        if (!PersistenceHelper.isNew(item) && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(EntityValues.getId(item)) && !entityStates.isLoadedWithFetchPlan(item, ds.getView())) {
            item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
            if (parentDs instanceof CollectionPropertyDatasourceImpl) {
                ((CollectionPropertyDatasourceImpl) parentDs).replaceItem(item);
            } else {
                ((CollectionDatasource) parentDs).updateItem(item);
            }
        }
        item = EntityCopyUtils.copyCompositions(item);
        handlePreviouslyDeletedCompositionItems(item, parentDs);
    } else if (!PersistenceHelper.isNew(item)) {
        item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
    }
    if (item == null) {
        throw new EntityAccessException(metadata.getClassNN(entityClass), entityId);
    }
    if (PersistenceHelper.isNew(item) && !ds.getMetaClass().equals(metadata.getClass(item))) {
        Entity newItem = ds.getDataSupplier().newInstance(ds.getMetaClass());
        MetadataTools metadataTools = (MetadataTools) getApplicationContext().getBean(MetadataTools.class);
        metadataTools.copy(item, newItem);
        item = newItem;
    }
    if (ds.getLoadDynamicAttributes()) {
        if (PersistenceHelper.isNew(item)) {
            dynamicAttributesGuiTools.initDefaultAttributeValues(item, metadata.getClass(item));
        }
        if (item instanceof Categorized) {
            dynamicAttributesGuiTools.listenCategoryChanges(ds);
        }
    }
    ds.setItem(item);
    if (PersistenceHelper.isNew(item)) {
        // make sure that they will be saved on commit.
        for (Datasource datasource : ds.getDsContext().getAll()) {
            if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == ds) {
                if (datasource.getItem() != null && PersistenceHelper.isNew(datasource.getItem()))
                    ((DatasourceImplementation) datasource).modified(datasource.getItem());
            }
        }
    }
    ((DatasourceImplementation) ds).setModified(false);
    Security security = (Security) getApplicationContext().getBean(Security.NAME);
    if (!PersistenceHelper.isNew(item)) {
        if (security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
            readOnlyDueToLock = false;
            LockService lockService = (LockService) getApplicationContext().getBean(LockService.NAME);
            LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), EntityValues.getId(item).toString());
            if (lockInfo == null) {
                justLocked = true;
                addAfterDetachListener(afterCloseEvent -> {
                    releaseLock();
                });
            } else if (!(lockInfo instanceof LockNotSupported)) {
                UserSessionSource userSessionSource = (UserSessionSource) getApplicationContext().getBean(UserSessionSource.NAME);
                Frame frame = (Frame) getFrame();
                frame.getWindowManager().showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUsername(), Datatypes.getNN(Date.class).format(lockInfo.getSince(), userSessionSource.getLocale())), Frame.NotificationType.HUMANIZED);
                readOnlyDueToLock = true;
                showEnableEditingBtn = false;
                setReadOnly(true);
            }
        } else {
            showEnableEditingBtn = false;
            setReadOnly(true);
        }
    }
}
Also used : Categorized(io.jmix.dynattr.model.Categorized) Entity(io.jmix.core.Entity) MetadataTools(io.jmix.core.MetadataTools) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) LockService(com.haulmont.cuba.core.app.LockService) LockNotSupported(io.jmix.core.pessimisticlocking.LockNotSupported) EntityStates(io.jmix.core.EntityStates) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) Security(com.haulmont.cuba.core.global.Security) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) EntityAccessException(io.jmix.core.EntityAccessException) LockInfo(io.jmix.core.pessimisticlocking.LockInfo) CollectionPropertyDatasourceImpl(com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)

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