Search in sources :

Example 1 with Categorized

use of io.jmix.dynattr.model.Categorized in project jmix by jmix-framework.

the class DynamicAttributesGuiTools method initDefaultAttributeValues.

public void initDefaultAttributeValues(Entity item, MetaClass metaClass) {
    Preconditions.checkNotNullArgument(metaClass, "metaClass is null");
    Collection<AttributeDefinition> attributes = dynAttrMetadata.getAttributes(metaClass);
    DynamicAttributesState state = getExtraState(item, DynamicAttributesState.class);
    if (state != null) {
        DynamicAttributes dynamicAttributes = state.getDynamicAttributes();
        if (dynamicAttributes == null) {
            state.setDynamicAttributes(new DynamicAttributes());
        }
    }
    ZonedDateTime currentTimestamp = AppBeans.get(TimeSource.class).now();
    boolean entityIsCategorized = item instanceof Categorized && ((Categorized) item).getCategory() != null;
    for (AttributeDefinition attribute : attributes) {
        setDefaultAttributeValue(item, attribute, entityIsCategorized, currentTimestamp);
    }
}
Also used : Categorized(io.jmix.dynattr.model.Categorized) ZonedDateTime(java.time.ZonedDateTime)

Example 2 with Categorized

use of io.jmix.dynattr.model.Categorized in project jmix by jmix-framework.

the class RuntimePropsDatasourceImpl method initMetaClass.

protected void initMetaClass(Entity entity) {
    if (entity == null) {
        category = null;
        valid();
        initializedBefore = true;
        fireItemChanged(null);
        return;
    }
    Preconditions.checkArgument(dynamicAttributesGuiTools.hasDynamicAttributes(entity), "Dynamic attributes should be loaded explicitly");
    if (entity instanceof Categorized) {
        category = ((Categorized) entity).getCategory();
    }
    if (!initializedBefore && category == null) {
        category = getDefaultCategory();
        MetaClass entityMetaClass = metadata.getClassNN(entity.getClass());
        if (entityMetaClass.findProperty("category") != null) {
            EntityValues.setValue(entity, "category", category);
        }
    }
    item = new DynamicAttributesEntity(entity);
    if (PersistenceHelper.isNew(entity) || categoryChanged) {
        dynamicAttributesGuiTools.initDefaultAttributeValues(entity, resolveCategorizedEntityClass());
    }
    view = new View(DynamicAttributesEntity.class, false);
    for (MetaProperty property : metaClass.getProperties()) {
        view.addProperty(property.getName());
    }
    valid();
    initializedBefore = true;
    fireItemChanged(null);
}
Also used : Categorized(io.jmix.dynattr.model.Categorized) MetaClass(io.jmix.core.metamodel.model.MetaClass) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) View(com.haulmont.cuba.core.global.View)

Example 3 with Categorized

use of io.jmix.dynattr.model.Categorized in project jmix by jmix-framework.

the class DynamicAttributesGuiTools method setDefaultAttributeValue.

protected void setDefaultAttributeValue(Entity item, AttributeDefinition attribute, boolean entityIsCategorized, ZonedDateTime currentTimestamp) {
    String propertyName = DynAttrUtils.getPropertyFromAttributeCode(attribute.getCode());
    if (entityIsCategorized) {
        Category entityCategory = ((Categorized) item).getCategory();
        Category attributeCategory = ((CategoryAttribute) attribute.getSource()).getCategory();
        if (!Objects.equals(entityCategory, attributeCategory)) {
            // cleanup attributes from not dedicated category
            EntityValues.setValue(item, propertyName, null);
            return;
        }
    }
    if (EntityValues.getValue(item, propertyName) != null) {
        // skip not null attributes
        return;
    }
    if (attribute.getDefaultValue() != null) {
        if (attribute.getDataType() == AttributeType.ENTITY) {
            MetaClass entityMetaClass = metadata.getClassNN(attribute.getJavaType());
            LoadContext<Object> lc = new LoadContext<>(entityMetaClass).setFetchPlan(fetchPlanRepository.getFetchPlan(entityMetaClass, FetchPlan.INSTANCE_NAME));
            String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(entityMetaClass);
            lc.setQueryString(format("select e from %s e where e.%s = :entityId", entityMetaClass.getName(), pkName)).setParameter("entityId", attribute.getDefaultValue());
            Entity defaultEntity = (Entity) dataManager.load(lc);
            EntityValues.setValue(item, propertyName, defaultEntity);
        } else if (attribute.isCollection()) {
            List<Object> list = new ArrayList<>();
            list.add(attribute.getDefaultValue());
            EntityValues.setValue(item, propertyName, list);
        } else {
            EntityValues.setValue(item, propertyName, attribute.getDefaultValue());
        }
    } else if (Boolean.TRUE.equals(attribute.isDefaultDateCurrent())) {
        if (attribute.getDataType() == AttributeType.DATE_WITHOUT_TIME) {
            EntityValues.setValue(item, propertyName, currentTimestamp.toLocalDate());
        } else {
            EntityValues.setValue(item, propertyName, Date.from(currentTimestamp.toInstant()));
        }
    }
}
Also used : Categorized(io.jmix.dynattr.model.Categorized) Category(io.jmix.dynattr.model.Category) CategoryAttribute(io.jmix.dynattr.model.CategoryAttribute) MetaClass(io.jmix.core.metamodel.model.MetaClass)

Example 4 with Categorized

use of io.jmix.dynattr.model.Categorized 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

Categorized (io.jmix.dynattr.model.Categorized)4 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 LockService (com.haulmont.cuba.core.app.LockService)1 Security (com.haulmont.cuba.core.global.Security)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 View (com.haulmont.cuba.core.global.View)1 CollectionPropertyDatasourceImpl (com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)1 DatasourceImplementation (com.haulmont.cuba.gui.data.impl.DatasourceImplementation)1 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)1 Entity (io.jmix.core.Entity)1 EntityAccessException (io.jmix.core.EntityAccessException)1 EntityStates (io.jmix.core.EntityStates)1 MetadataTools (io.jmix.core.MetadataTools)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 LockInfo (io.jmix.core.pessimisticlocking.LockInfo)1 LockNotSupported (io.jmix.core.pessimisticlocking.LockNotSupported)1 Category (io.jmix.dynattr.model.Category)1 CategoryAttribute (io.jmix.dynattr.model.CategoryAttribute)1 ZonedDateTime (java.time.ZonedDateTime)1