Search in sources :

Example 1 with DynamicAttributesState

use of io.jmix.dynattr.DynamicAttributesState in project jmix by jmix-framework.

the class DynAttrEntityStateInitializer method initEntity.

@Override
public void initEntity(Object entity) {
    DynamicAttributesState state = new DynamicAttributesState(getEntityEntry(entity));
    state.setDynamicAttributes(new DynamicAttributes());
    addExtraState(entity, state);
}
Also used : DynamicAttributesState(io.jmix.dynattr.DynamicAttributesState) DynamicAttributes(io.jmix.dynattr.DynamicAttributes)

Example 2 with DynamicAttributesState

use of io.jmix.dynattr.DynamicAttributesState in project jmix by jmix-framework.

the class DynAttrLifecycleListener method entityReload.

@Override
public void entityReload(DataStoreEntityReloadEvent event) {
    LoadContext<?> loadContext = event.getLoadContext();
    SaveContext saveContext = event.getSaveContext();
    Object entity = saveContext.getEntitiesToSave().stream().filter(e -> Objects.equals(EntityValues.getId(e), loadContext.getId())).findFirst().orElse(null);
    DynamicAttributesState state = getExtraState(entity, DynamicAttributesState.class);
    if (state != null) {
        DynamicAttributes dynamicAttributes = state.getDynamicAttributes();
        if (dynamicAttributes != null) {
            loadContext.setHint(DynAttrQueryHints.LOAD_DYN_ATTR, true);
        }
    }
}
Also used : SaveContext(io.jmix.core.SaveContext) DynamicAttributesState(io.jmix.dynattr.DynamicAttributesState) DynamicAttributes(io.jmix.dynattr.DynamicAttributes)

Example 3 with DynamicAttributesState

use of io.jmix.dynattr.DynamicAttributesState in project jmix by jmix-framework.

the class DynAttrAwareEntityTransformer method transformForCollectionContainer.

@Override
public <E> E transformForCollectionContainer(E editedEntity, CollectionContainer<E> container) {
    boolean needDynamicAttributes = false;
    boolean dynamicAttributesAreLoaded = true;
    DynamicAttributesState state = EntitySystemAccess.getExtraState(editedEntity, DynamicAttributesState.class);
    if (state != null) {
        dynamicAttributesAreLoaded = state.getDynamicAttributes() != null;
    }
    if (container instanceof HasLoader) {
        DataLoader loader = ((HasLoader) container).getLoader();
        if (loader instanceof CollectionLoader) {
            Map<String, Serializable> hints = loader.getHints();
            needDynamicAttributes = hints != null && Boolean.TRUE.equals(hints.get(DynAttrQueryHints.LOAD_DYN_ATTR));
        }
    }
    FetchPlan fetchPlan = container.getFetchPlan();
    if (fetchPlan != null && !entityStates.isLoadedWithFetchPlan(editedEntity, fetchPlan)) {
        return dataManager.load(Id.of(editedEntity)).fetchPlan(fetchPlan).hint(DynAttrQueryHints.LOAD_DYN_ATTR, needDynamicAttributes).one();
    } else if (needDynamicAttributes && !dynamicAttributesAreLoaded) {
        dynAttrManager.loadValues(Collections.singletonList(editedEntity), fetchPlan, accessConstraintsRegistry.getConstraints());
        return editedEntity;
    } else {
        return editedEntity;
    }
}
Also used : DataLoader(io.jmix.ui.model.DataLoader) Serializable(java.io.Serializable) CollectionLoader(io.jmix.ui.model.CollectionLoader) DynamicAttributesState(io.jmix.dynattr.DynamicAttributesState) HasLoader(io.jmix.ui.model.HasLoader)

Example 4 with DynamicAttributesState

use of io.jmix.dynattr.DynamicAttributesState in project jmix by jmix-framework.

the class AttributeRecalculationManager method evaluateNewValue.

protected Object evaluateNewValue(Object entity, String script) {
    Map<String, Object> values = new HashMap<>();
    DynamicAttributesState extraState = EntitySystemAccess.getExtraState(entity, DynamicAttributesState.class);
    if (extraState != null && extraState.getDynamicAttributes() != null) {
        DynamicAttributes dynamicAttributes = extraState.getDynamicAttributes();
        for (String key : dynamicAttributes.getKeys()) {
            values.put(key, dynamicAttributes.getValue(key));
        }
    }
    Map<String, Object> params = new HashMap<>();
    params.put("entity", entity);
    params.put("dynamicAttributes", values);
    return scriptEvaluator.evaluate(new StaticScriptSource(script), params);
}
Also used : StaticScriptSource(org.springframework.scripting.support.StaticScriptSource) DynamicAttributesState(io.jmix.dynattr.DynamicAttributesState) DynamicAttributes(io.jmix.dynattr.DynamicAttributes)

Aggregations

DynamicAttributesState (io.jmix.dynattr.DynamicAttributesState)4 DynamicAttributes (io.jmix.dynattr.DynamicAttributes)3 SaveContext (io.jmix.core.SaveContext)1 CollectionLoader (io.jmix.ui.model.CollectionLoader)1 DataLoader (io.jmix.ui.model.DataLoader)1 HasLoader (io.jmix.ui.model.HasLoader)1 Serializable (java.io.Serializable)1 StaticScriptSource (org.springframework.scripting.support.StaticScriptSource)1