Search in sources :

Example 41 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class EntityInspectorBrowse method createButtonsPanel.

protected void createButtonsPanel(Table table) {
    ButtonsPanel buttonsPanel = componentsFactory.createComponent(ButtonsPanel.class);
    createButton = componentsFactory.createComponent(Button.class);
    createButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "create"));
    CreateAction createAction = new CreateAction();
    table.addAction(createAction);
    createButton.setAction(createAction);
    createButton.setIcon(icons.get(CubaIcon.CREATE_ACTION));
    createButton.setFrame(frame);
    editButton = componentsFactory.createComponent(Button.class);
    editButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "edit"));
    EditAction editAction = new EditAction();
    table.addAction(editAction);
    editButton.setAction(editAction);
    editButton.setIcon(icons.get(CubaIcon.EDIT_ACTION));
    editButton.setFrame(frame);
    removeButton = componentsFactory.createComponent(Button.class);
    removeButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "remove"));
    RemoveAction removeAction = new RemoveAction(entitiesTable);
    removeAction.setAfterRemoveHandler(removedItems -> entitiesDs.refresh());
    table.addAction(removeAction);
    removeButton.setAction(removeAction);
    removeButton.setIcon(icons.get(CubaIcon.REMOVE_ACTION));
    removeButton.setFrame(frame);
    excelButton = componentsFactory.createComponent(Button.class);
    excelButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "excel"));
    excelButton.setAction(new ExcelAction(entitiesTable));
    excelButton.setIcon(icons.get(CubaIcon.EXCEL_ACTION));
    excelButton.setFrame(frame);
    refreshButton = componentsFactory.createComponent(Button.class);
    refreshButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "refresh"));
    refreshButton.setAction(new RefreshAction(entitiesTable));
    refreshButton.setIcon(icons.get(CubaIcon.REFRESH_ACTION));
    refreshButton.setFrame(frame);
    exportPopupButton = componentsFactory.createComponent(PopupButton.class);
    exportPopupButton.setIcon(icons.get(CubaIcon.DOWNLOAD));
    exportPopupButton.addAction(new ExportAction("exportJSON", JSON));
    exportPopupButton.addAction(new ExportAction("exportZIP", ZIP));
    importUpload = componentsFactory.createComponent(FileUploadField.class);
    importUpload.setUploadButtonIcon(icons.get(CubaIcon.UPLOAD));
    importUpload.setUploadButtonCaption(getMessage("import"));
    importUpload.addFileUploadSucceedListener(event -> {
        File file = fileUploadingAPI.getFile(importUpload.getFileId());
        if (file == null) {
            String errorMsg = String.format("Entities import upload error. File with id %s not found", importUpload.getFileId());
            throw new RuntimeException(errorMsg);
        }
        byte[] fileBytes;
        try (InputStream is = new FileInputStream(file)) {
            fileBytes = IOUtils.toByteArray(is);
        } catch (IOException e) {
            throw new RuntimeException("Unable to upload file", e);
        }
        try {
            fileUploadingAPI.deleteFile(importUpload.getFileId());
        } catch (FileStorageException e) {
            log.error("Unable to delete temp file", e);
        }
        try {
            Collection<Entity> importedEntities;
            if ("json".equals(Files.getFileExtension(importUpload.getFileName()))) {
                importedEntities = entityImportExportService.importEntitiesFromJSON(new String(fileBytes), createEntityImportView(selectedMeta));
            } else {
                importedEntities = entityImportExportService.importEntitiesFromZIP(fileBytes, createEntityImportView(selectedMeta));
            }
            showNotification(importedEntities.size() + " entities imported", NotificationType.HUMANIZED);
        } catch (Exception e) {
            showNotification(getMessage("importFailed"), e.getMessage(), NotificationType.ERROR);
            log.error("Entities import error", e);
        }
        entitiesDs.refresh();
    });
    buttonsPanel.add(createButton);
    buttonsPanel.add(editButton);
    buttonsPanel.add(removeButton);
    buttonsPanel.add(excelButton);
    buttonsPanel.add(refreshButton);
    buttonsPanel.add(exportPopupButton);
    buttonsPanel.add(importUpload);
    table.setButtonsPanel(buttonsPanel);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) File(java.io.File)

Example 42 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class EntityInspectorEditor method createEmbeddedFields.

/**
 * Recursively instantiates the embedded properties.
 * E.g. embedded properties of the embedded property will also be instantiated.
 *
 * @param metaClass meta class of the entity
 * @param item      entity instance
 */
protected void createEmbeddedFields(MetaClass metaClass, Entity item) {
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        if (isEmbedded(metaProperty)) {
            MetaClass embeddedMetaClass = metaProperty.getRange().asClass();
            Entity embedded = item.getValue(metaProperty.getName());
            if (embedded == null) {
                embedded = metadata.create(embeddedMetaClass);
                item.setValue(metaProperty.getName(), embedded);
            }
            createEmbeddedFields(embeddedMetaClass, embedded);
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 43 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class EntityInspectorEditor method createAddHandler.

@SuppressWarnings("unchecked")
protected Lookup.Handler createAddHandler(final MetaProperty metaProperty, final CollectionDatasource propertyDs) {
    Lookup.Handler result = new Lookup.Handler() {

        @Override
        public void handleLookup(Collection items) {
            for (Object item : items) {
                Entity entity = (Entity) item;
                if (!propertyDs.getItems().contains(entity)) {
                    MetaProperty inverseProperty = metaProperty.getInverse();
                    if (inverseProperty != null) {
                        if (!inverseProperty.getRange().getCardinality().isMany()) {
                            // set currently editing item to the child's parent property
                            entity.setValue(inverseProperty.getName(), datasource.getItem());
                            propertyDs.addItem(entity);
                        } else {
                            Collection properties = entity.getValue(inverseProperty.getName());
                            if (properties != null) {
                                properties.add(datasource.getItem());
                                propertyDs.addItem(entity);
                            }
                        }
                    }
                }
                propertyDs.addItem(entity);
            }
        }
    };
    propertyDs.refresh();
    return result;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 44 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class EntityLogBrowser method init.

@Override
public void init(Map<String, Object> params) {
    WindowParams.DISABLE_AUTO_REFRESH.set(params, true);
    usersDs.refresh();
    loggedEntityDs.refresh();
    Companion companion = getCompanion();
    if (companion != null) {
        companion.enableTextSelection(entityLogTable);
        companion.enableTextSelection(entityLogAttrTable);
    }
    systemAttrsList = Arrays.asList("createTs", "createdBy", "updateTs", "updatedBy", "deleteTs", "deletedBy", "version", "id");
    Map<String, Object> changeTypeMap = new LinkedHashMap<>();
    changeTypeMap.put(messages.getMessage(getClass(), "createField"), "C");
    changeTypeMap.put(messages.getMessage(getClass(), "modifyField"), "M");
    changeTypeMap.put(messages.getMessage(getClass(), "deleteField"), "D");
    changeTypeMap.put(messages.getMessage(getClass(), "restoreField"), "R");
    entityMetaClassesMap = getEntityMetaClasses();
    entityNameField.setOptionsMap(entityMetaClassesMap);
    changeTypeField.setOptionsMap(changeTypeMap);
    filterEntityNameField.setOptionsMap(entityMetaClassesMap);
    Action clearAction = instancePicker.getAction("clear");
    instancePicker.removeAction(clearAction);
    instancePicker.removeAction("lookup");
    addAction(new SaveAction());
    addAction(new CancelAction());
    Label label1 = factory.createComponent(Label.class);
    label1.setValue(messages.getMessage(getClass(), "show"));
    label1.setAlignment(Alignment.MIDDLE_LEFT);
    Label label2 = factory.createComponent(Label.class);
    label2.setValue(messages.getMessage(getClass(), "rows"));
    label2.setAlignment(Alignment.MIDDLE_LEFT);
    ButtonsPanel panel = entityLogTable.getButtonsPanel();
    showRowField = factory.createComponent(TextField.class);
    showRowField.setWidth(themeConstants.get("cuba.gui.EntityLogBrowser.showRowField.width"));
    showRowField.setValue(String.valueOf(DEFAULT_SHOW_ROWS));
    panel.add(label1);
    panel.add(showRowField);
    panel.add(label2);
    disableControls();
    setDateFieldTime();
    instancePicker.setEnabled(false);
    PickerField.LookupAction lookupAction = new PickerField.LookupAction(instancePicker) {

        @Override
        public void actionPerform(Component component) {
            final MetaClass metaClass = pickerField.getMetaClass();
            if (pickerField.isEditable()) {
                String currentWindowAlias = lookupScreen;
                if (currentWindowAlias == null) {
                    if (metaClass == null) {
                        throw new IllegalStateException("Please specify metaclass or property for PickerField");
                    }
                    currentWindowAlias = windowConfig.getLookupScreenId(metaClass);
                }
                Window lookupWindow = (Window) pickerField.getFrame();
                Lookup.Handler lookupWindowHandler = items -> {
                    if (!items.isEmpty()) {
                        Object item = items.iterator().next();
                        pickerField.setValue(item);
                        afterSelect(items);
                    }
                };
                if (config.hasWindow(currentWindowAlias)) {
                    lookupWindow.openLookup(currentWindowAlias, lookupWindowHandler, lookupScreenOpenType, lookupScreenParams != null ? lookupScreenParams : Collections.emptyMap());
                } else {
                    lookupWindow.openLookup(EntityInspectorBrowse.SCREEN_NAME, lookupWindowHandler, WindowManager.OpenType.THIS_TAB, ParamsMap.of("entity", metaClass.getName()));
                }
                lookupWindow.addCloseListener(actionId -> pickerField.requestFocus());
            }
        }
    };
    instancePicker.addAction(lookupAction);
    instancePicker.addAction(clearAction);
    entityNameField.addValueChangeListener(e -> {
        if (entityNameField.isEditable())
            fillAttributes((String) e.getValue(), null, true);
    });
    loggedEntityDs.addItemChangeListener(e -> {
        if (e.getItem() != null) {
            fillAttributes(e.getItem().getName(), e.getItem(), false);
            checkAllCheckboxes();
        } else {
            setSelectAllCheckBox(false);
            clearAttributes();
        }
    });
    filterEntityNameField.addValueChangeListener(e -> {
        if (e.getValue() != null) {
            instancePicker.setEnabled(true);
            MetaClass metaClass = metadata.getSession().getClassNN(e.getValue().toString());
            instancePicker.setMetaClass(metaClass);
        } else {
            instancePicker.setEnabled(false);
        }
        instancePicker.setValue(null);
    });
    selectAllCheckBox.addValueChangeListener(e -> enableAllCheckBoxes((boolean) e.getValue()));
    entityLogTable.addGeneratedColumn("entityId", entity -> {
        if (entity.getObjectEntityId() != null) {
            return new Table.PlainTextCell(entity.getObjectEntityId().toString());
        }
        return null;
    }, Table.PlainTextCell.class);
}
Also used : java.util(java.util) WindowManager(com.haulmont.cuba.gui.WindowManager) ParamsMap(com.haulmont.bali.util.ParamsMap) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) AppBeans(com.haulmont.cuba.core.global.AppBeans) MetaClass(com.haulmont.chile.core.model.MetaClass) EntityInspectorBrowse(com.haulmont.cuba.gui.app.core.entityinspector.EntityInspectorBrowse) Metadata(com.haulmont.cuba.core.global.Metadata) BooleanUtils(org.apache.commons.lang.BooleanUtils) Inject(javax.inject.Inject) DateUtils(org.apache.commons.lang.time.DateUtils) HasUuid(com.haulmont.cuba.core.entity.HasUuid) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Named(javax.inject.Named) DynamicAttributes(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) EntityLogService(com.haulmont.cuba.core.app.EntityLogService) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) com.haulmont.cuba.security.entity(com.haulmont.cuba.security.entity) WindowParams(com.haulmont.cuba.gui.WindowParams) WindowConfig(com.haulmont.cuba.gui.config.WindowConfig) TimeSource(com.haulmont.cuba.core.global.TimeSource) ThemeConstants(com.haulmont.cuba.gui.theme.ThemeConstants) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Entity(com.haulmont.cuba.core.entity.Entity) ReferenceToEntitySupport(com.haulmont.cuba.core.global.ReferenceToEntitySupport) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 45 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class FileDownloadHelper method initGeneratedColumn.

/**
 * Initializes a table column for downloading files.
 *
 * @param table         table displaying some entity
 * @param fileProperty  property of the entity which is a reference to {@link FileDescriptor}
 */
public static void initGeneratedColumn(final Table table, final String fileProperty) {
    final ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.NAME);
    final ExportDisplay exportDisplay = AppBeans.get(ExportDisplay.NAME);
    table.addGeneratedColumn(fileProperty + ".name", new Table.ColumnGenerator() {

        @Override
        public Component generateCell(final Entity entity) {
            final FileDescriptor fd = entity.getValueEx(fileProperty);
            if (fd == null) {
                return componentsFactory.createComponent(Label.class);
            }
            if (PersistenceHelper.isNew(fd)) {
                Label label = componentsFactory.createComponent(Label.class);
                label.setValue(fd.getName());
                return label;
            } else {
                Button button = componentsFactory.createComponent(Button.class);
                button.setStyleName("link");
                button.setAction(new AbstractAction("download") {

                    @Override
                    public void actionPerform(Component component) {
                        exportDisplay.show(fd);
                    }

                    @Override
                    public String getCaption() {
                        return fd.getName();
                    }
                });
                return button;
            }
        }
    });
}
Also used : ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Entity(com.haulmont.cuba.core.entity.Entity) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Aggregations

Entity (com.haulmont.cuba.core.entity.Entity)203 MetaClass (com.haulmont.chile.core.model.MetaClass)51 MetaProperty (com.haulmont.chile.core.model.MetaProperty)44 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)40 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)18 Test (org.junit.Test)15 Inject (javax.inject.Inject)14 java.util (java.util)12 EntityManager (com.haulmont.cuba.core.EntityManager)11 ParseException (java.text.ParseException)11 Element (org.dom4j.Element)11 Logger (org.slf4j.Logger)11 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 LoggerFactory (org.slf4j.LoggerFactory)9 Query (com.haulmont.cuba.core.Query)8 Server (com.haulmont.cuba.core.entity.Server)8 Transaction (com.haulmont.cuba.core.Transaction)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 Collectors (java.util.stream.Collectors)7