Search in sources :

Example 11 with BaseGenericIdEntity

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

the class EditAction method internalOpenEditor.

protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem, Datasource parentDs, Map<String, Object> params) {
    Window.Editor window = target.getFrame().openEditor(getWindowId(), existingItem, getOpenType(), params, parentDs);
    if (editorCloseListener == null) {
        window.addCloseListener(actionId -> {
            // move focus to owner
            target.requestFocus();
            if (Window.COMMIT_ACTION_ID.equals(actionId)) {
                Entity editedItem = window.getItem();
                if (editedItem != null) {
                    if (parentDs == null) {
                        if (editedItem instanceof BaseGenericIdEntity) {
                            BaseGenericIdEntity genericEditedEntity = (BaseGenericIdEntity) editedItem;
                            if (datasource.getLoadDynamicAttributes() && genericEditedEntity.getDynamicAttributes() == null) {
                                DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                                dynamicAttributesGuiTools.reloadDynamicAttributes(genericEditedEntity);
                            }
                        }
                        // 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 : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Example 12 with BaseGenericIdEntity

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

the class PersistenceSecurityImpl method calculateFilteredData.

@SuppressWarnings("unchecked")
protected boolean calculateFilteredData(Entity entity, Set<EntityId> handled, boolean checkPermitted) {
    MetaClass metaClass = entity.getMetaClass();
    if (!isPermittedInMemory(entity) && checkPermitted) {
        return true;
    }
    EntityId entityId = new EntityId(referenceToEntitySupport.getReferenceId(entity), metaClass.getName());
    if (handled.contains(entityId)) {
        return false;
    }
    handled.add(entityId);
    if (entity instanceof BaseGenericIdEntity) {
        BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
        for (MetaProperty property : metaClass.getProperties()) {
            if (metadataTools.isPersistent(property) && PersistenceHelper.isLoaded(entity, property.getName())) {
                Object value = entity.getValue(property.getName());
                if (value instanceof Collection) {
                    Set filtered = new LinkedHashSet();
                    for (Entity item : (Collection<Entity>) value) {
                        if (calculateFilteredData(item, handled, true)) {
                            filtered.add(referenceToEntitySupport.getReferenceId(item));
                        }
                    }
                    if (!filtered.isEmpty()) {
                        securityTokenManager.addFiltered(baseGenericIdEntity, property.getName(), filtered);
                    }
                } else if (value instanceof Entity) {
                    Entity valueEntity = (Entity) value;
                    if (calculateFilteredData(valueEntity, handled, true)) {
                        securityTokenManager.addFiltered(baseGenericIdEntity, property.getName(), referenceToEntitySupport.getReferenceId(valueEntity));
                    }
                }
            }
        }
        securityTokenManager.writeSecurityToken(baseGenericIdEntity);
    }
    return false;
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 13 with BaseGenericIdEntity

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

the class ComponentsHelper method handleFilteredAttributes.

/**
 * Set field's "required" flag to false if the value has been filtered by Row Level Security
 * This is necessary to allow user to submit form with filtered attribute even if attribute is required
 */
public static void handleFilteredAttributes(Field component, Datasource datasource, MetaPropertyPath mpp) {
    if (component.isRequired() && datasource.getState() == Datasource.State.VALID && datasource.getItem() != null && mpp.getMetaProperty().getRange().isClass()) {
        Entity targetItem = datasource.getItem();
        MetaProperty[] propertiesChain = mpp.getMetaProperties();
        if (propertiesChain.length > 1) {
            String basePropertyItem = Arrays.stream(propertiesChain).limit(propertiesChain.length - 1).map(MetadataObject::getName).collect(Collectors.joining("."));
            targetItem = datasource.getItem().getValueEx(basePropertyItem);
        }
        if (targetItem instanceof BaseGenericIdEntity) {
            String metaPropertyName = mpp.getMetaProperty().getName();
            Object value = targetItem.getValue(metaPropertyName);
            BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) targetItem;
            String[] filteredAttributes = getFilteredAttributes(baseGenericIdEntity);
            if (value == null && filteredAttributes != null && ArrayUtils.contains(filteredAttributes, metaPropertyName)) {
                component.setRequired(false);
            }
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) MetadataObject(com.haulmont.chile.core.model.MetadataObject) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 14 with BaseGenericIdEntity

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

the class DynamicAttributesGuiTools method reloadDynamicAttributes.

/**
 * Reload dynamic attributes on the entity
 */
@SuppressWarnings("unchecked")
public void reloadDynamicAttributes(BaseGenericIdEntity entity) {
    MetaClass metaClass = entity.getMetaClass();
    View view = new View(metaClass.getJavaClass(), false).addProperty(metadata.getTools().getPrimaryKeyName(metaClass));
    LoadContext loadContext = new LoadContext(metaClass).setView(view).setLoadDynamicAttributes(true).setId(entity.getId());
    BaseGenericIdEntity reloadedEntity = (BaseGenericIdEntity) dataManager.load(loadContext);
    if (reloadedEntity != null) {
        entity.setDynamicAttributes(reloadedEntity.getDynamicAttributes());
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity)

Example 15 with BaseGenericIdEntity

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

the class DynamicAttributesGuiTools method initDefaultAttributeValues.

public void initDefaultAttributeValues(BaseGenericIdEntity item, MetaClass metaClass) {
    Preconditions.checkNotNullArgument(metaClass, "metaClass is null");
    Collection<CategoryAttribute> attributes = dynamicAttributes.getAttributesForMetaClass(metaClass);
    if (item.getDynamicAttributes() == null) {
        item.setDynamicAttributes(new HashMap<>());
    }
    Date currentTimestamp = AppBeans.get(TimeSource.NAME, TimeSource.class).currentTimestamp();
    boolean entityIsCategorized = item instanceof Categorized && ((Categorized) item).getCategory() != null;
    for (CategoryAttribute categoryAttribute : attributes) {
        String code = DynamicAttributesUtils.encodeAttributeCode(categoryAttribute.getCode());
        if (entityIsCategorized && !categoryAttribute.getCategory().equals(((Categorized) item).getCategory())) {
            // cleanup attributes from not dedicated category
            item.setValue(code, null);
            continue;
        }
        if (item.getValue(code) != null) {
            // skip not null attributes
            continue;
        }
        if (categoryAttribute.getDefaultValue() != null) {
            if (BooleanUtils.isTrue(categoryAttribute.getIsEntity())) {
                MetaClass entityMetaClass = metadata.getClassNN(categoryAttribute.getJavaClassForEntity());
                LoadContext<Entity> lc = new LoadContext<>(entityMetaClass).setView(View.MINIMAL);
                String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(entityMetaClass);
                lc.setQueryString(format("select e from %s e where e.%s = :entityId", entityMetaClass.getName(), pkName)).setParameter("entityId", categoryAttribute.getDefaultValue());
                Entity defaultEntity = dataManager.load(lc);
                item.setValue(code, defaultEntity);
            } else {
                item.setValue(code, categoryAttribute.getDefaultValue());
            }
        } else if (Boolean.TRUE.equals(categoryAttribute.getDefaultDateIsCurrent())) {
            item.setValue(code, currentTimestamp);
        }
    }
}
Also used : Categorized(com.haulmont.cuba.core.entity.Categorized) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass)

Aggregations

BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Entity (com.haulmont.cuba.core.entity.Entity)13 MetaProperty (com.haulmont.chile.core.model.MetaProperty)12 MetaClass (com.haulmont.chile.core.model.MetaClass)11 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)3 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 Categorized (com.haulmont.cuba.core.entity.Categorized)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 SecurityState (com.haulmont.cuba.core.entity.SecurityState)2 Element (org.dom4j.Element)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)1 DataService (com.haulmont.cuba.core.app.DataService)1 DynamicAttributes (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)1 BaseDbGeneratedIdEntity (com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity)1 CategoryAttributeValue (com.haulmont.cuba.core.entity.CategoryAttributeValue)1