Search in sources :

Example 36 with Entity

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

the class ParameterTransformerImpl method loadEntity.

protected Entity loadEntity(EntityParameterValue parameter) {
    Class entityClass = metadata.getClass(parameter.getMetaClassName()).getJavaClass();
    FluentLoader.ById loader = dataManager.load(entityClass).id(java.util.UUID.fromString(parameter.getEntityId()));
    if (parameter.getFetchPlanName() != null) {
        loader.fetchPlan(parameter.getFetchPlanName());
    }
    return (Entity) loader.optional().orElse(null);
}
Also used : Entity(io.jmix.core.Entity) FluentLoader(io.jmix.core.FluentLoader) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 37 with Entity

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

the class JmixEclipseLinkQuery method getQuery.

private JpaQuery<E> getQuery() {
    if (query == null) {
        FetchPlan fetchPlan = fetchPlans.isEmpty() ? null : fetchPlans.get(0);
        if (isNative) {
            log.trace("Creating SQL query: {}", queryString);
            if (resultClass == null)
                query = (JpaQuery) entityManager.createNativeQuery(queryString);
            else {
                if (!Entity.class.isAssignableFrom(resultClass)) {
                    throw new IllegalArgumentException("Non-entity result class for native query is not supported" + " by EclipseLink: " + resultClass);
                }
                Class effectiveClass = extendedEntities.getEffectiveClass(resultClass);
                query = (JpaQuery) entityManager.createNativeQuery(queryString, effectiveClass);
            }
        } else {
            log.trace("Creating JPQL query: {}", queryString);
            transformedQueryString = transformQueryString();
            log.trace("Transformed JPQL query: {}", transformedQueryString);
            processParams(transformedQueryString);
            Class effectiveClass = getEffectiveResultClass();
            query = buildJPAQuery(transformedQueryString, effectiveClass);
            if (fetchPlan != null) {
                MetaClass metaClass = metadata.getClass(fetchPlan.getEntityClass());
                if (!metadataTools.isCacheable(metaClass) || !singleResultExpected) {
                    query.setHint(QueryHints.REFRESH, HintValues.TRUE);
                    query.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeByMapping);
                }
            }
        }
        if (flushMode == null) {
            if (fetchPlan != null && !fetchPlan.loadPartialEntities()) {
                query.setFlushMode(FlushModeType.AUTO);
            } else {
                query.setFlushMode(FlushModeType.COMMIT);
            }
        } else {
            query.setFlushMode(flushMode);
        }
        boolean nullParam = false;
        for (Param param : params) {
            param.apply(query);
            if (param.value == null)
                nullParam = true;
        }
        addMacroParams(query);
        // disable SQL caching to support "is null" generation
        if (nullParam)
            query.setHint(QueryHints.PREPARE, HintValues.FALSE);
        // nested collections in some cases
        if (maxResults != null && !singleResultExpected)
            query.setMaxResults(maxResults);
        if (firstResult != null && !singleResultExpected)
            query.setFirstResult(firstResult);
        if (lockMode != null)
            query.setLockMode(lockMode);
        if (hints != null) {
            for (Map.Entry<String, Object> hint : hints.entrySet()) {
                hintsProcessor.applyQueryHint(query, hint.getKey(), hint.getValue());
            }
        }
        for (int i = 0; i < fetchPlans.size(); i++) {
            if (i == 0)
                fetchGroupMgr.setFetchPlan(query, queryString, fetchPlans.get(i), singleResultExpected);
            else
                fetchGroupMgr.addFetchPlan(query, queryString, fetchPlans.get(i), singleResultExpected);
        }
    }
    return query;
}
Also used : Entity(io.jmix.core.Entity) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaClass(io.jmix.core.metamodel.model.MetaClass) EnumClass(io.jmix.core.metamodel.datatype.impl.EnumClass)

Example 38 with Entity

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

the class CubaLinkCellClickListener method accept.

@Override
public void accept(Table.Column.ClickEvent clickEvent) {
    if (!clickEvent.isText()) {
        return;
    }
    Table.Column column = (Table.Column) clickEvent.getSource();
    Entity rowItem = (Entity) clickEvent.getItem();
    if (column.getXmlDescriptor() != null) {
        String invokeMethodName = column.getXmlDescriptor().attributeValue("linkInvoke");
        if (StringUtils.isNotEmpty(invokeMethodName)) {
            callControllerInvoke(rowItem, column.getStringId(), invokeMethodName);
            return;
        }
    }
    Entity entity;
    Object value = EntityValues.getValueEx(rowItem, column.getStringId());
    if (value instanceof Entity) {
        entity = (Entity) value;
    } else {
        entity = rowItem;
    }
    WindowManager wm;
    Window window = (Window) CubaComponentsHelper.getWindowImplementation(table);
    if (window == null) {
        throw new IllegalStateException("Please specify Frame for Table");
    } else {
        wm = window.getWindowManager();
    }
    Messages messages = applicationContext.getBean(Messages.class);
    if (entity instanceof SoftDelete && ((SoftDelete) entity).isDeleted()) {
        wm.showNotification(messages.getMainMessage("OpenAction.objectIsDeleted"), Frame.NotificationType.HUMANIZED);
        return;
    }
    if (window.getFrameOwner() instanceof LegacyFrame) {
        LegacyFrame frameOwner = (LegacyFrame) window.getFrameOwner();
        DataSupplier dataSupplier = frameOwner.getDsContext().getDataSupplier();
        entity = dataSupplier.reload(entity, View.MINIMAL);
    } else {
        DataManager dataManager = applicationContext.getBean(DataManager.class);
        entity = dataManager.reload(entity, View.MINIMAL);
    }
    WindowConfig windowConfig = applicationContext.getBean(WindowConfig.class);
    String windowAlias = null;
    if (column.getXmlDescriptor() != null) {
        windowAlias = column.getXmlDescriptor().attributeValue("linkScreen");
    }
    if (StringUtils.isEmpty(windowAlias)) {
        windowAlias = windowConfig.getEditorScreenId(applicationContext.getBean(Metadata.class).getClass(entity));
    }
    WindowManager.OpenType screenOpenType = WindowManager.OpenType.THIS_TAB;
    if (column.getXmlDescriptor() != null) {
        String openTypeAttribute = column.getXmlDescriptor().attributeValue("linkScreenOpenType");
        if (StringUtils.isNotEmpty(openTypeAttribute)) {
            screenOpenType = WindowManager.OpenType.valueOf(openTypeAttribute);
        }
    }
    AbstractEditor editor = (AbstractEditor) wm.openEditor(windowConfig.getWindowInfo(windowAlias), entity, screenOpenType);
    editor.addCloseListener(actionId -> {
        // move focus to component
        table.focus();
        if (Window.COMMIT_ACTION_ID.equals(actionId)) {
            Entity editorItem = editor.getItem();
            handleEditorCommit(editorItem, rowItem, column.getStringId());
        }
    });
}
Also used : Window(com.haulmont.cuba.gui.components.Window) Entity(io.jmix.core.Entity) Table(com.haulmont.cuba.gui.components.Table) Messages(com.haulmont.cuba.core.global.Messages) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) Metadata(io.jmix.core.Metadata) DataManager(com.haulmont.cuba.core.global.DataManager) AbstractEditor(com.haulmont.cuba.gui.components.AbstractEditor) WindowManager(com.haulmont.cuba.gui.WindowManager) WindowConfig(com.haulmont.cuba.gui.config.WindowConfig) DataSupplier(com.haulmont.cuba.gui.data.DataSupplier)

Example 39 with Entity

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

the class FilterEditor method init.

@Override
public void init(Map<String, Object> params) {
    super.init(params);
    if (Boolean.TRUE.equals(useShortConditionForm)) {
        setCaption(messages.getMessage("filter.editor.captionShortForm"));
    }
    getDialogOptions().setWidth(theme.get("cuba.gui.filterEditor.dialog.width")).setHeight(theme.get("cuba.gui.filterEditor.dialog.height")).setResizable(true);
    filterEntity = (FilterEntity) params.get("filterEntity");
    if (filterEntity == null) {
        throw new RuntimeException("Filter entity was not passed to filter editor");
    }
    filter = (Filter) params.get("filter");
    ConditionsTree paramConditions = (ConditionsTree) params.get("conditionsTree");
    conditions = paramConditions.createCopy();
    refreshConditionsDs();
    conditionsTree.expandTree();
    MetaProperty property = metadata.getClassNN(FilterEntity.class).getProperty("name");
    Map<String, Object> annotations = property.getAnnotations();
    Integer maxLength = (Integer) annotations.get("length");
    if (maxLength != null) {
        filterName.setMaxLength(maxLength);
    }
    if (!messages.getMessage("filter.adHocFilter").equals(filterEntity.getName())) {
        filterName.setValue(filterEntity.getName());
    }
    availableForAllCb.setValue(filterEntity.getUsername() == null);
    defaultCb.setValue(filterEntity.getIsDefault());
    applyDefaultCb.setValue(filterEntity.getApplyDefault());
    globalDefaultCb.setValue(filterEntity.getGlobalDefault());
    if (filterEntity.getUsername() != null) {
        globalDefaultCb.setEnabled(false);
    }
    if (!security.isSpecificPermitted(GLOBAL_FILTER_PERMISSION)) {
        availableForAllCb.setVisible(false);
        availableForAllLabel.setVisible(false);
        globalDefaultCb.setVisible(false);
        globalDefaultLabel.setVisible(false);
    }
    availableForAllCb.addValueChangeListener(e -> {
        Boolean isFilterAvailableForAll = e.getValue();
        globalDefaultCb.setEnabled(isFilterAvailableForAll);
        if (!isFilterAvailableForAll) {
            globalDefaultCb.setValue(null);
        }
    });
    boolean manualApplyRequired = filter.getManualApplyRequired() != null ? filter.getManualApplyRequired() : AppBeans.get(CubaProperties.class).isGenericFilterManualApplyRequired();
    if (!manualApplyRequired) {
        applyDefaultCb.setVisible(manualApplyRequired);
        applyDefaultLabel.setVisible(manualApplyRequired);
    }
    if (!(filter.getFrame().getFrameOwner() instanceof LegacyFrame)) {
        ScreenSettingsFacet settingsFacet = UiControllerUtils.getFacet(filter.getFrame(), ScreenSettingsFacet.class);
        if (settingsFacet == null) {
            defaultCb.setVisible(false);
            defaultLabel.setVisible(false);
        }
    }
    if (filterEntity.getFolder() != null) {
        availableForAllCb.setVisible(false);
        availableForAllLabel.setVisible(false);
        globalDefaultCb.setVisible(false);
        globalDefaultLabel.setVisible(false);
        defaultCb.setVisible(false);
        defaultLabel.setVisible(false);
    }
    conditionsDs.addItemChangeListener(e -> {
        if (!treeItemChangeListenerEnabled) {
            return;
        }
        // commit previously selected condition
        if (activeConditionFrame != null) {
            List<Validatable> validatables = new ArrayList<>();
            Collection<Component> frameComponents = ComponentsHelper.getComponents(activeConditionFrame);
            for (Component frameComponent : frameComponents) {
                if (frameComponent instanceof Validatable) {
                    validatables.add((Validatable) frameComponent);
                }
            }
            if (validate(validatables)) {
                activeConditionFrame.commit();
            } else {
                treeItemChangeListenerEnabled = false;
                conditionsTree.setSelected(e.getPrevItem());
                treeItemChangeListenerEnabled = true;
                return;
            }
        }
        if (e.getItem() == null) {
            activeConditionFrame = null;
        } else {
            if (e.getItem() instanceof PropertyCondition) {
                activeConditionFrame = propertyConditionFrame;
            } else if (e.getItem() instanceof DynamicAttributesCondition) {
                activeConditionFrame = dynamicAttributesConditionFrame;
            } else if (e.getItem() instanceof CustomCondition) {
                activeConditionFrame = customConditionFrame;
            } else if (e.getItem() instanceof GroupCondition) {
                activeConditionFrame = groupConditionFrame;
            } else if (e.getItem() instanceof FtsCondition) {
                activeConditionFrame = ftsConditionFrame;
            } else {
                log.warn("Conditions frame for condition with type " + e.getItem().getClass().getSimpleName() + " not found");
            }
        }
        propertyConditionFrame.setVisible(false);
        customConditionFrame.setVisible(false);
        dynamicAttributesConditionFrame.setVisible(false);
        groupConditionFrame.setVisible(false);
        ftsConditionFrame.setVisible(false);
        if (activeConditionFrame != null) {
            activeConditionFrame.setVisible(true);
            activeConditionFrame.setCondition(e.getItem());
            if (Boolean.TRUE.equals(useShortConditionForm)) {
                for (String componentName : componentsToHideInShortForm) {
                    Component component = activeConditionFrame.getComponent(componentName);
                    if (component != null) {
                        if (BooleanUtils.isTrue(showConditionHiddenOption) && componentsForHiddenOption.contains(componentName)) {
                            continue;
                        }
                        component.setVisible(false);
                    }
                }
            }
        }
    });
    addConditionHelper = new AddConditionHelper(filter, BooleanUtils.isTrue(hideDynamicAttributes), BooleanUtils.isTrue(hideCustomConditions), condition -> {
        AbstractCondition item = conditionsDs.getItem();
        if (item != null && item instanceof GroupCondition) {
            Node<AbstractCondition> newNode = new Node<>(condition);
            Node<AbstractCondition> selectedNode = conditions.getNode(item);
            selectedNode.addChild(newNode);
            refreshConditionsDs();
            conditionsTree.expand(item);
        } else {
            conditions.getRootNodes().add(new Node<>(condition));
            refreshConditionsDs();
        }
        conditionsTree.setSelected(condition);
    });
    FilterHelper filterHelper = AppBeans.get(FilterHelper.class);
    filterHelper.initConditionsDragAndDrop(conditionsTree, conditions);
    if (Boolean.TRUE.equals(useShortConditionForm)) {
        filterPropertiesGrid.setVisible(false);
    }
}
Also used : Node(io.jmix.core.common.datastruct.Node) AddConditionHelper(com.haulmont.cuba.gui.components.filter.AddConditionHelper) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CubaProperties(com.haulmont.cuba.CubaProperties) BooleanUtils(org.apache.commons.lang3.BooleanUtils) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) GroupType(com.haulmont.cuba.gui.components.filter.GroupType) AbstractWindow(com.haulmont.cuba.gui.components.AbstractWindow) com.haulmont.cuba.gui.components.filter.condition(com.haulmont.cuba.gui.components.filter.condition) Action(io.jmix.ui.action.Action) Filter(com.haulmont.cuba.gui.components.Filter) DialogAction(io.jmix.ui.action.DialogAction) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) Logger(org.slf4j.Logger) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) ThemeConstants(io.jmix.ui.theme.ThemeConstants) FetchPlan(io.jmix.core.FetchPlan) Collectors(java.util.stream.Collectors) GroupConditionDescriptor(com.haulmont.cuba.gui.components.filter.descriptor.GroupConditionDescriptor) WindowParam(io.jmix.ui.WindowParam) UiControllerUtils(io.jmix.ui.screen.UiControllerUtils) ScreenSettingsFacet(io.jmix.ui.settings.facet.ScreenSettingsFacet) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) FilterHelper(com.haulmont.cuba.gui.components.filter.FilterHelper) Entity(io.jmix.core.Entity) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) io.jmix.ui.component(io.jmix.ui.component) ScreenSettingsFacet(io.jmix.ui.settings.facet.ScreenSettingsFacet) Node(io.jmix.core.common.datastruct.Node) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame) AddConditionHelper(com.haulmont.cuba.gui.components.filter.AddConditionHelper) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) FilterEntity(com.haulmont.cuba.security.entity.FilterEntity) FilterHelper(com.haulmont.cuba.gui.components.filter.FilterHelper)

Example 40 with Entity

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

the class ListEditorPopupWindow method createComponentForEntity.

protected Field createComponentForEntity() {
    Preconditions.checkNotNullArgument(entityName, messages.getMainMessage("entityNameParamNotDefined"));
    MetaClass metaClass = metadata.getClassNN(entityName);
    Field<?> componentForEntity;
    if (BooleanUtils.isNotTrue(useLookupField)) {
        PickerField pickerField = uiComponents.create(PickerField.class);
        pickerField.setMetaClass(metaClass);
        PickerField.LookupAction lookupAction;
        if (!Strings.isNullOrEmpty(entityJoinClause) || !Strings.isNullOrEmpty(entityWhereClause)) {
            lookupAction = dynamicAttributesGuiTools.createLookupAction(pickerField, entityJoinClause, entityWhereClause);
            pickerField.addAction(lookupAction);
        } else {
            lookupAction = pickerField.addLookupAction();
            if (!Strings.isNullOrEmpty(lookupScreen)) {
                lookupAction.setLookupScreen(lookupScreen);
            }
        }
        componentForEntity = pickerField;
        lookupAction.setAfterLookupSelectionHandler(items -> {
            if (items != null && items.size() > 0) {
                for (Object item : items) {
                    if (item != null && !valueExists(item)) {
                        this.addValueToLayout(item, ListEditorHelper.getValueCaption(item, itemType, timeZone));
                    }
                }
            }
            componentForEntity.setValue(null);
        });
    } else {
        LookupField lookupField = uiComponents.create(LookupField.class);
        CollectionDatasource optionsDs;
        if (!Strings.isNullOrEmpty(entityJoinClause) || !Strings.isNullOrEmpty(entityWhereClause)) {
            optionsDs = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaClass, entityJoinClause, entityWhereClause);
        } else {
            optionsDs = DsBuilder.create().setMetaClass(metaClass).setViewName(View.MINIMAL).buildCollectionDatasource();
            optionsDs.refresh();
        }
        lookupField.setOptionsDatasource(optionsDs);
        lookupField.setOptionCaptionProvider(optionCaptionProvider);
        componentForEntity = lookupField;
        componentForEntity.addValueChangeListener(e -> {
            Entity selectedEntity = (Entity) e.getValue();
            if (selectedEntity != null && !valueExists(selectedEntity)) {
                this.addValueToLayout(selectedEntity, ListEditorHelper.getValueCaption(selectedEntity, itemType, timeZone, optionCaptionProvider));
            }
            componentForEntity.setValue(null);
        });
    }
    return componentForEntity;
}
Also used : Entity(io.jmix.core.Entity) MetaClass(io.jmix.core.metamodel.model.MetaClass) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource)

Aggregations

Entity (io.jmix.core.Entity)94 MetaClass (io.jmix.core.metamodel.model.MetaClass)20 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)18 CommitContext (com.haulmont.cuba.core.global.CommitContext)10 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)10 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 Test (org.junit.jupiter.api.Test)10 Server (com.haulmont.cuba.core.model.common.Server)8 Datasource (com.haulmont.cuba.gui.data.Datasource)8 Collectors (java.util.stream.Collectors)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 FetchPlan (io.jmix.core.FetchPlan)7 Logger (org.slf4j.Logger)7 Metadata (io.jmix.core.Metadata)6 MetadataTools (io.jmix.core.MetadataTools)6 EntityValues (io.jmix.core.entity.EntityValues)6 java.util (java.util)6 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 LoggerFactory (org.slf4j.LoggerFactory)6