Search in sources :

Example 31 with Entity

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

the class ExcelExporter method formatValueCell.

protected void formatValueCell(Cell cell, @Nullable Object cellValue, @Nullable MetaPropertyPath metaPropertyPath, int sizersIndex, int notificationRequired, int level, @Nullable Integer groupChildCount) {
    if (cellValue == null) {
        if (metaPropertyPath != null && metaPropertyPath.getRange().isDatatype()) {
            Class javaClass = metaPropertyPath.getRange().asDatatype().getJavaClass();
            if (Boolean.class.equals(javaClass)) {
                cellValue = false;
            }
        } else {
            return;
        }
    }
    String childCountValue = "";
    if (groupChildCount != null) {
        childCountValue = " (" + groupChildCount + ")";
    }
    if (cellValue instanceof Number) {
        Number n = (Number) cellValue;
        Datatype datatype = null;
        if (metaPropertyPath != null) {
            Range range = metaPropertyPath.getMetaProperty().getRange();
            if (range.isDatatype()) {
                datatype = range.asDatatype();
            }
        }
        datatype = datatype == null ? datatypeRegistry.get(n.getClass()) : datatype;
        String str;
        // and we should skip it
        if (sizersIndex == 0 && level > 0) {
            str = createSpaceString(level) + datatype.format(n);
            cell.setCellValue(str);
        } else {
            try {
                str = datatype.format(n);
                Number result = (Number) datatype.parse(str);
                if (result != null) {
                    if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) {
                        cell.setCellValue(result.longValue());
                        cell.setCellStyle(integerFormatCellStyle);
                    } else {
                        cell.setCellValue(result.doubleValue());
                        cell.setCellStyle(doubleFormatCellStyle);
                    }
                }
            } catch (ParseException e) {
                throw new RuntimeException("Unable to parse numeric value", e);
            }
            cell.setCellType(CellType.NUMERIC);
        }
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Date) {
        Class javaClass = null;
        if (metaPropertyPath != null) {
            MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
            if (metaProperty.getRange().isDatatype()) {
                javaClass = metaProperty.getRange().asDatatype().getJavaClass();
            }
        }
        Date date = (Date) cellValue;
        cell.setCellValue(date);
        if (Objects.equals(java.sql.Time.class, javaClass)) {
            cell.setCellStyle(timeFormatCellStyle);
        } else if (Objects.equals(java.sql.Date.class, javaClass)) {
            cell.setCellStyle(dateFormatCellStyle);
        } else {
            cell.setCellStyle(dateTimeFormatCellStyle);
        }
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            String str = datatypeRegistry.get(Date.class).format(date);
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Boolean) {
        String str = "";
        if (sizersIndex == 0) {
            str += createSpaceString(level);
        }
        str += ((Boolean) cellValue) ? getMessage("excelExporter.true") : getMessage("excelExporter.false");
        cell.setCellValue(createStringCellValue(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Enum) {
        String message = (sizersIndex == 0 ? createSpaceString(level) : "") + messages.getMessage((Enum) cellValue);
        cell.setCellValue(message + childCountValue);
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(message, stdFont);
        }
    } else if (cellValue instanceof Entity) {
        Object entityVal = cellValue;
        String instanceName = metadataTools.getInstanceName(entityVal);
        String str = sizersIndex == 0 ? createSpaceString(level) + instanceName : instanceName;
        str = str + childCountValue;
        cell.setCellValue(createStringCellValue(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Collection) {
        String str = "";
        cell.setCellValue(createStringCellValue(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof byte[]) {
        String str = messages.getMessage("excelExporter.bytes");
        cell.setCellValue(createStringCellValue(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else {
        String strValue = cellValue == null ? "" : cellValue.toString();
        String str = sizersIndex == 0 ? createSpaceString(level) + strValue : strValue;
        str = str + childCountValue;
        cell.setCellValue(createStringCellValue(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    }
}
Also used : Entity(io.jmix.core.Entity) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) HSSFRichTextString(org.apache.poi.hssf.usermodel.HSSFRichTextString) Range(io.jmix.core.metamodel.model.Range) Datatype(io.jmix.core.metamodel.datatype.Datatype) ParseException(java.text.ParseException) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 32 with Entity

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

the class Param method createEntityLookup.

protected Component createEntityLookup(FilterDataContext filterDataContext, ValueProperty valueProperty) {
    MetaClass metaClass = metadata.getSession().getClassNN(javaClass);
    LookupType type = null;
    if (property != null && property.getRange().isClass()) {
        type = (LookupType) metadata.getTools().getMetaAnnotationAttributes(property.getAnnotations(), Lookup.class).get("type");
    }
    PersistenceManagerClient persistenceManager = (PersistenceManagerClient) applicationContext.getBean(PersistenceManagerClient.NAME);
    boolean useLookupScreen = type != null ? type == LookupType.SCREEN : persistenceManager.useLookupScreen(metaClass.getName());
    if (useLookupScreen) {
        if (inExpr) {
            ListEditor listEditor = uiComponents.create(ListEditor.class);
            listEditor.setItemType(ListEditor.ItemType.ENTITY);
            listEditor.setEntityName(metaClass.getName());
            initListEditor(listEditor, valueProperty);
            return listEditor;
        } else {
            PickerField<Entity> picker = uiComponents.create(PickerField.NAME);
            picker.setMetaClass(metaClass);
            picker.setWidth(theme.get("cuba.gui.filter.Param.textComponent.width"));
            picker.addAction(actions.create(LookupAction.ID));
            picker.addAction(actions.create(EntityClearAction.ID));
            picker.addValueChangeListener(e -> _setValue(e.getValue(), valueProperty));
            picker.setValue((Entity) _getValue(valueProperty));
            return picker;
        }
    } else {
        if (inExpr) {
            CollectionLoader<Entity> loader = createEntityOptionsLoader(metaClass);
            CollectionContainer<Entity> container = dataComponents.createCollectionContainer(metaClass.getJavaClass());
            loader.setContainer(container);
            ListEditor listEditor = uiComponents.create(ListEditor.class);
            listEditor.setItemType(ListEditor.ItemType.ENTITY);
            listEditor.setEntityName(metaClass.getName());
            // noinspection unchecked
            listEditor.setOptions(new ContainerOptions<>(container));
            // noinspection unchecked
            initListEditor(listEditor, valueProperty);
            // noinspection unchecked
            Consumer<CollectionContainer.CollectionChangeEvent<?>> listener = e -> listEditor.setValue(null);
            if (filterDataContext != null) {
                filterDataContext.registerCollectionLoader(this, loader);
                filterDataContext.registerContainerCollectionChangeListener(this, container, listener);
            }
            return listEditor;
        } else {
            CollectionLoader<Entity> loader = createEntityOptionsLoader(metaClass);
            CollectionContainer<Entity> container = dataComponents.createCollectionContainer(metaClass.getJavaClass());
            loader.setContainer(container);
            LookupPickerField<Entity> lookup = uiComponents.create(LookupPickerField.NAME);
            lookup.setWidth(theme.get("cuba.gui.filter.Param.textComponent.width"));
            lookup.addAction(actions.create(EntityClearAction.ID));
            lookup.setOptions(new ContainerOptions<>(container));
            Consumer<CollectionContainer.CollectionChangeEvent<?>> listener = e -> lookup.setValue(null);
            lookup.addValueChangeListener(e -> _setValue(e.getValue(), valueProperty));
            lookup.setValue((Entity) _getValue(valueProperty));
            if (filterDataContext != null) {
                filterDataContext.registerCollectionLoader(this, loader);
                filterDataContext.registerContainerCollectionChangeListener(this, container, listener);
            }
            return lookup;
        }
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) LookupAction(com.haulmont.cuba.gui.actions.picker.LookupAction) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CubaProperties(com.haulmont.cuba.CubaProperties) ParamsMap(io.jmix.core.common.util.ParamsMap) EventHub(io.jmix.core.common.event.EventHub) Lookup(com.haulmont.cuba.core.entity.annotation.Lookup) AppBeans(com.haulmont.cuba.core.global.AppBeans) StringUtils(org.apache.commons.lang3.StringUtils) EntityValues(io.jmix.core.entity.EntityValues) java.time(java.time) ListEditorHelper(com.haulmont.cuba.gui.components.listeditor.ListEditorHelper) PickerField(com.haulmont.cuba.gui.components.PickerField) ParseException(java.text.ParseException) DateInIntervalComponent(com.haulmont.cuba.gui.components.filter.dateinterval.DateInIntervalComponent) MetadataTools(io.jmix.core.MetadataTools) LookupType(com.haulmont.cuba.core.entity.annotation.LookupType) Subscription(io.jmix.core.common.event.Subscription) TextStringBuilder(org.apache.commons.text.TextStringBuilder) Datatypes(com.haulmont.chile.core.datatypes.Datatypes) Messages(com.haulmont.cuba.core.global.Messages) Datatype(io.jmix.core.metamodel.datatype.Datatype) Collectors(java.util.stream.Collectors) ThemeConstantsManager(io.jmix.ui.theme.ThemeConstantsManager) ContainerOptions(io.jmix.ui.component.data.options.ContainerOptions) UiComponents(com.haulmont.cuba.gui.UiComponents) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) IgnoreUserTimeZone(io.jmix.core.entity.annotation.IgnoreUserTimeZone) io.jmix.ui.component(io.jmix.ui.component) NotImplementedException(org.apache.commons.lang3.NotImplementedException) CollectionLoader(io.jmix.ui.model.CollectionLoader) java.util(java.util) WindowManager(com.haulmont.cuba.gui.WindowManager) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) SimpleDateFormat(java.text.SimpleDateFormat) BooleanUtils(org.apache.commons.lang3.BooleanUtils) QueryTransformerFactory(io.jmix.data.QueryTransformerFactory) Scope(org.springframework.context.annotation.Scope) Metadata(com.haulmont.cuba.core.global.Metadata) UserSession(com.haulmont.cuba.security.global.UserSession) Strings(com.google.common.base.Strings) FilterDataContext(com.haulmont.cuba.gui.components.FilterDataContext) ImmutableList(com.google.common.collect.ImmutableList) DatatypeRegistry(io.jmix.core.metamodel.datatype.DatatypeRegistry) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) WindowManagerProvider(com.haulmont.cuba.gui.WindowManagerProvider) EntityClearAction(io.jmix.ui.action.entitypicker.EntityClearAction) QueryTransformer(io.jmix.data.QueryTransformer) Nullable(javax.annotation.Nullable) Actions(io.jmix.ui.Actions) PersistenceManagerClient(com.haulmont.cuba.client.sys.PersistenceManagerClient) Logger(org.slf4j.Logger) DynAttrUtils(io.jmix.dynattr.DynAttrUtils) CollectionContainer(io.jmix.ui.model.CollectionContainer) ThemeConstants(io.jmix.ui.theme.ThemeConstants) TemporalType(javax.persistence.TemporalType) FetchPlan(io.jmix.core.FetchPlan) NotificationType(com.haulmont.cuba.gui.components.Frame.NotificationType) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ListEditor(com.haulmont.cuba.gui.components.ListEditor) ApplicationContext(org.springframework.context.ApplicationContext) Consumer(java.util.function.Consumer) ValueConversionException(io.jmix.ui.component.data.ValueConversionException) LookupField(com.haulmont.cuba.gui.components.LookupField) Entity(io.jmix.core.Entity) Element(org.dom4j.Element) DataComponents(io.jmix.ui.model.DataComponents) ListEditor(com.haulmont.cuba.gui.components.ListEditor) Entity(io.jmix.core.Entity) LookupType(com.haulmont.cuba.core.entity.annotation.LookupType) PersistenceManagerClient(com.haulmont.cuba.client.sys.PersistenceManagerClient) MetaClass(io.jmix.core.metamodel.model.MetaClass) Lookup(com.haulmont.cuba.core.entity.annotation.Lookup)

Example 33 with Entity

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

the class EditAction method internalOpenEditor.

protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem, Datasource parentDs, Map<String, Object> params) {
    LegacyFrame frameOwner = (LegacyFrame) target.getFrame().getFrameOwner();
    AbstractEditor window = frameOwner.openEditor(getWindowId(), existingItem, getOpenType(), params, parentDs);
    if (editorCloseListener == null) {
        window.addCloseListener(actionId -> {
            // move focus to owner
            if (target instanceof Component.Focusable) {
                ((Component.Focusable) target).focus();
            }
            if (Window.COMMIT_ACTION_ID.equals(actionId)) {
                Entity editedItem = window.getItem();
                if (editedItem != null) {
                    if (parentDs == null) {
                        editedItem = (Entity) AppBeans.get(GuiActionSupport.class).reloadEntityIfNeeded(editedItem, datasource);
                        // noinspection unchecked
                        datasource.updateItem(editedItem);
                    }
                    afterCommit(editedItem);
                    if (afterCommitHandler != null) {
                        afterCommitHandler.handle(editedItem);
                    }
                }
            }
            afterWindowClosed(window);
            if (afterWindowClosedHandler != null) {
                afterWindowClosedHandler.handle(window, actionId);
            }
        });
    } else {
        window.addCloseListener(editorCloseListener);
    }
}
Also used : Entity(io.jmix.core.Entity) LegacyFrame(com.haulmont.cuba.gui.screen.compatibility.LegacyFrame)

Example 34 with Entity

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

the class ExcludeAction method doRemove.

@SuppressWarnings("unchecked")
@Override
protected void doRemove(Set<Entity> selected, boolean autocommit) {
    CollectionDatasource ds = target.getDatasource();
    if (ds instanceof NestedDatasource) {
        // Clear reference to master entity
        Datasource masterDs = ((NestedDatasource) ds).getMaster();
        MetaProperty metaProperty = ((NestedDatasource) ds).getProperty();
        if (masterDs != null && metaProperty != null) {
            MetaProperty inverseProp = metaProperty.getInverse();
            if (inverseProp != null) {
                ExtendedEntities extendedEntities = metadata.getExtendedEntities();
                Class inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
                Class dsClass = extendedEntities.getEffectiveClass(ds.getMetaClass());
                if (inversePropClass.isAssignableFrom(dsClass)) {
                    for (Entity item : selected) {
                        EntityValues.setValue(item, inverseProp.getName(), null);
                    }
                }
            }
        }
    }
    for (Entity item : selected) {
        ds.modifyItem(item);
        ds.excludeItem(item);
    }
    if (autocommit && (ds.getCommitMode() != Datasource.CommitMode.PARENT)) {
        try {
            ds.commit();
        } catch (RuntimeException e) {
            ds.refresh();
            throw e;
        }
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) PropertyDatasource(com.haulmont.cuba.gui.data.PropertyDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) ExtendedEntities(io.jmix.core.ExtendedEntities) Entity(io.jmix.core.Entity) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 35 with Entity

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

the class AddConditionWindow method select.

public void select() {
    Set<Entity> selectedItems = tree.getSelected();
    if (selectedItems.isEmpty()) {
        showNotification(messages.getMessage("filter.addCondition.selectCondition"), NotificationType.WARNING);
        return;
    } else {
        for (Entity item : selectedItems) {
            if (item instanceof HeaderConditionDescriptor) {
                showNotification(messages.getMessage("filter.addCondition.youSelectedGroup"), NotificationType.WARNING);
                return;
            } else if (isEmbeddedProperty((AbstractConditionDescriptor) item)) {
                showNotification(messages.getMessage("filter.addCondition.youSelectedEmbedded"), NotificationType.WARNING);
                return;
            }
        }
    }
    close(COMMIT_ACTION_ID);
}
Also used : Entity(io.jmix.core.Entity) AbstractConditionDescriptor(com.haulmont.cuba.gui.components.filter.descriptor.AbstractConditionDescriptor) HeaderConditionDescriptor(com.haulmont.cuba.gui.components.filter.descriptor.HeaderConditionDescriptor)

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