Search in sources :

Example 1 with LoggedAttribute

use of io.jmix.audit.entity.LoggedAttribute in project jmix by jmix-framework.

the class EntityLogImpl method loadEntities.

protected void loadEntities() {
    log.debug("Loading entities");
    entitiesManual = new HashMap<>();
    entitiesAuto = new HashMap<>();
    transaction.executeWithoutResult(status -> {
        TypedQuery<LoggedEntity> q = entityManager.createQuery("select e from audit_LoggedEntity e where e.auto = true or e.manual = true", LoggedEntity.class);
        List<LoggedEntity> list = q.getResultList();
        for (LoggedEntity loggedEntity : list) {
            if (loggedEntity.getName() == null) {
                throw new IllegalStateException("Unable to initialize EntityLog: empty LoggedEntity.name");
            }
            Set<String> attributes = new HashSet<>();
            for (LoggedAttribute loggedAttribute : loggedEntity.getAttributes()) {
                if (loggedAttribute.getName() == null) {
                    throw new IllegalStateException("Unable to initialize EntityLog: empty LoggedAttribute.name");
                }
                attributes.add(loggedAttribute.getName());
            }
            if (BooleanUtils.isTrue(loggedEntity.getAuto()))
                entitiesAuto.put(loggedEntity.getName(), attributes);
            if (BooleanUtils.isTrue(loggedEntity.getManual()))
                entitiesManual.put(loggedEntity.getName(), attributes);
        }
    });
    log.debug("Loaded: entitiesAuto={}, entitiesManual={}", entitiesAuto.size(), entitiesManual.size());
}
Also used : LoggedEntity(io.jmix.audit.entity.LoggedEntity) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute)

Example 2 with LoggedAttribute

use of io.jmix.audit.entity.LoggedAttribute in project jmix by jmix-framework.

the class EntityLogBrowser method onRemoveBtnClick.

@Subscribe("removeBtn")
protected void onRemoveBtnClick(Button.ClickEvent event) {
    Set<LoggedEntity> selectedItems = loggedEntityTable.getSelected();
    if (!selectedItems.isEmpty()) {
        dialogs.createOptionDialog().withCaption(messages.getMessage("dialogs.Confirmation")).withMessage(messages.getMessage("dialogs.Confirmation.Remove")).withActions(new DialogAction(DialogAction.Type.YES).withHandler(e -> {
            for (LoggedEntity item : selectedItems) {
                if (item.getAttributes() != null) {
                    Set<LoggedAttribute> attributes = new HashSet<>(item.getAttributes());
                    for (LoggedAttribute attribute : attributes) {
                        dataContext.remove(attribute);
                    }
                    dataContext.commit();
                }
                dataContext.remove(item);
                dataContext.commit();
            }
            loggedEntityDc.getMutableItems().removeAll(selectedItems);
            entityLog.invalidateCache();
        }), new DialogAction(DialogAction.Type.NO)).show();
    }
}
Also used : LoggedEntity(io.jmix.audit.entity.LoggedEntity) DialogAction(io.jmix.ui.action.DialogAction) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute)

Example 3 with LoggedAttribute

use of io.jmix.audit.entity.LoggedAttribute in project jmix by jmix-framework.

the class EntityLogBrowser method fillAttributes.

protected void fillAttributes(String metaClassName, LoggedEntity item, boolean editable) {
    clearAttributes();
    setSelectAllCheckBox(false);
    if (metaClassName != null) {
        MetaClass metaClass = extendedEntities.getEffectiveMetaClass(metadata.getClass(metaClassName));
        List<MetaProperty> metaProperties = new ArrayList<>(metaClass.getProperties());
        selectAllCheckBox.setEditable(editable);
        Set<LoggedAttribute> enabledAttr = null;
        if (item != null)
            enabledAttr = item.getAttributes();
        for (MetaProperty property : metaProperties) {
            if (allowLogProperty(property)) {
                if (metadataTools.isEmbedded(property)) {
                    MetaClass embeddedMetaClass = property.getRange().asClass();
                    for (MetaProperty embeddedProperty : embeddedMetaClass.getProperties()) {
                        if (allowLogProperty(embeddedProperty)) {
                            addAttribute(enabledAttr, String.format("%s.%s", property.getName(), embeddedProperty.getName()), metaClass, editable);
                        }
                    }
                } else {
                    addAttribute(enabledAttr, property.getName(), metaClass, editable);
                }
            }
        }
        Collection<MetaProperty> additionalProperties = metadataTools.getAdditionalProperties(metaClass);
        if (additionalProperties != null) {
            for (MetaProperty property : additionalProperties) {
                if (allowLogProperty(property)) {
                    addAttribute(enabledAttr, property.getName(), metaClass, editable);
                }
            }
        }
    }
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 4 with LoggedAttribute

use of io.jmix.audit.entity.LoggedAttribute in project jmix by jmix-framework.

the class EntityLogBrowser method onSaveBtnClick.

@Subscribe("saveBtn")
protected void onSaveBtnClick(Button.ClickEvent event) {
    LoggedEntity selectedEntity = loggedEntityTable.getSelected().iterator().next();
    selectedEntity = dataContext.merge(selectedEntity);
    Set<LoggedAttribute> enabledAttributes = selectedEntity.getAttributes();
    for (Component c : attributesBoxScroll.getComponents()) {
        CheckBox currentCheckBox = (CheckBox) c;
        if (SELECT_ALL_CHECK_BOX.equals(currentCheckBox.getId()))
            continue;
        Boolean currentCheckBoxValue = currentCheckBox.getValue();
        MetaClass metaClass = metadata.getClass(selectedEntity.getName());
        if (currentCheckBoxValue && !isEntityHaveAttribute(currentCheckBox.getId(), metaClass, enabledAttributes)) {
            // add attribute if checked and not exist in table
            LoggedAttribute newLoggedAttribute = dataContext.create(LoggedAttribute.class);
            newLoggedAttribute.setName(currentCheckBox.getId());
            newLoggedAttribute.setEntity(selectedEntity);
        }
        if (!currentCheckBoxValue && isEntityHaveAttribute(currentCheckBox.getId(), metaClass, enabledAttributes)) {
            // remove attribute if unchecked and exist in table
            LoggedAttribute removeAtr = getLoggedAttribute(currentCheckBox.getId(), enabledAttributes);
            if (removeAtr != null)
                dataContext.remove(removeAtr);
        }
    }
    dataContext.commit();
    loggedEntityDl.load();
    disableControls();
    loggedEntityTable.setEnabled(true);
    loggedEntityTable.focus();
    entityLog.invalidateCache();
}
Also used : LoggedEntity(io.jmix.audit.entity.LoggedEntity) MetaClass(io.jmix.core.metamodel.model.MetaClass) LoggedAttribute(io.jmix.audit.entity.LoggedAttribute) LookupComponent(io.jmix.ui.screen.LookupComponent)

Aggregations

LoggedAttribute (io.jmix.audit.entity.LoggedAttribute)4 LoggedEntity (io.jmix.audit.entity.LoggedEntity)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 DialogAction (io.jmix.ui.action.DialogAction)1 LookupComponent (io.jmix.ui.screen.LookupComponent)1