Search in sources :

Example 21 with CategoryAttribute

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

the class Param method createRuntimeEnumLookup.

protected Component createRuntimeEnumLookup(final ValueProperty valueProperty) {
    if (javaClass == Boolean.class) {
        return createBooleanField(valueProperty);
    }
    DataService dataService = AppBeans.get(DataService.NAME);
    LoadContext<CategoryAttribute> context = new LoadContext<>(CategoryAttribute.class);
    LoadContext.Query q = context.setQueryString("select a from sys$CategoryAttribute a where a.id = :id");
    context.setView("_local");
    q.setParameter("id", categoryAttrId);
    CategoryAttribute categoryAttribute = dataService.load(context);
    if (categoryAttribute == null) {
        throw new EntityAccessException(CategoryAttribute.class, categoryAttrId);
    }
    runtimeEnum = new LinkedList<>();
    String enumerationString = categoryAttribute.getEnumeration();
    String[] array = StringUtils.split(enumerationString, ',');
    for (String s : array) {
        String trimmedValue = StringUtils.trimToNull(s);
        if (trimmedValue != null) {
            runtimeEnum.add(trimmedValue);
        }
    }
    if (inExpr) {
        ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
        listEditor.setItemType(ListEditor.ItemType.STRING);
        listEditor.setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
        initListEditor(listEditor, valueProperty);
        return listEditor;
    } else {
        LookupField lookup = componentsFactory.createComponent(LookupField.class);
        lookup.setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
        lookup.addValueChangeListener(e -> {
            _setValue(e.getValue(), valueProperty);
        });
        lookup.setValue(_getValue(valueProperty));
        return lookup;
    }
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) DataService(com.haulmont.cuba.core.app.DataService)

Example 22 with CategoryAttribute

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

the class DynamicAttributeCustomFieldGenerator method generateField.

@Override
public Component generateField(Datasource datasource, String propertyId) {
    ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.class);
    ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
    MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(datasource.getMetaClass(), propertyId);
    if (metaPropertyPath == null) {
        log.error("MetaPropertyPath for dynamic attribute {} not found", propertyId);
        return null;
    }
    CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
    if (categoryAttribute == null) {
        log.error("Dynamic attribute {} not found", propertyId);
        return null;
    }
    listEditor.setEntityJoinClause(categoryAttribute.getJoinClause());
    listEditor.setEntityWhereClause(categoryAttribute.getWhereClause());
    ListEditor.ItemType itemType = listEditorItemTypeFromDynamicAttrType(categoryAttribute.getDataType());
    listEditor.setItemType(itemType);
    Metadata metadata = AppBeans.get(Metadata.class);
    Scripting scripting = AppBeans.get(Scripting.class);
    if (!Strings.isNullOrEmpty(categoryAttribute.getEntityClass())) {
        Class<?> clazz = scripting.loadClass(categoryAttribute.getEntityClass());
        if (clazz == null) {
            log.error("Unable to find class of entity {} for dynamic attribute {}", categoryAttribute.getEntityClass(), categoryAttribute.getCode());
            return null;
        }
        MetaClass metaClass = metadata.getClassNN(clazz);
        listEditor.setEntityName(metaClass.getName());
        listEditor.setUseLookupField(BooleanUtils.isTrue(categoryAttribute.getLookup()));
    }
    // noinspection unchecked
    datasource.addStateChangeListener(e -> {
        if (e.getState() == Datasource.State.VALID) {
            Object value = datasource.getItem().getValue(propertyId);
            if (value != null && value instanceof Collection) {
                listEditor.setValue(value);
            }
        }
    });
    listEditor.addValueChangeListener(e -> {
        datasource.getItem().setValue(propertyId, e.getValue());
    });
    listEditor.setWidthFull();
    return listEditor;
}
Also used : ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) ListEditor(com.haulmont.cuba.gui.components.ListEditor) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) Metadata(com.haulmont.cuba.core.global.Metadata) Collection(java.util.Collection) Scripting(com.haulmont.cuba.core.global.Scripting)

Example 23 with CategoryAttribute

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

the class DynamicAttributesGuiTools method getAttributesToShowOnTheScreen.

/**
 * Get attributes which should be added automatically to the screen and component.
 * Based on visibility settings from category attribute editor.
 */
public Set<CategoryAttribute> getAttributesToShowOnTheScreen(MetaClass metaClass, String screen, @Nullable String component) {
    Collection<CategoryAttribute> attributesForMetaClass = dynamicAttributes.getAttributesForMetaClass(metaClass);
    Set<CategoryAttribute> categoryAttributes = new LinkedHashSet<>();
    for (CategoryAttribute attribute : attributesForMetaClass) {
        if (attributeShouldBeShownOnTheScreen(screen, component, attribute)) {
            categoryAttributes.add(attribute);
        }
    }
    return categoryAttributes;
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute)

Example 24 with CategoryAttribute

use of com.haulmont.cuba.core.entity.CategoryAttribute 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)

Example 25 with CategoryAttribute

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

the class AbstractComponentGenerationStrategy method createEntityField.

protected Component createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
    String linkAttribute = null;
    Element xmlDescriptor = context.getXmlDescriptor();
    if (xmlDescriptor != null) {
        linkAttribute = xmlDescriptor.attributeValue("link");
    }
    if (!Boolean.parseBoolean(linkAttribute)) {
        CollectionDatasource optionsDatasource = context.getOptionsDatasource();
        if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
            DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
            CategoryAttribute attribute = metaProperty.getAttribute();
            if (Boolean.TRUE.equals(attribute.getLookup())) {
                DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                optionsDatasource = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaProperty.getRange().asClass(), attribute.getJoinClause(), attribute.getWhereClause());
            }
        }
        PickerField pickerField;
        if (optionsDatasource == null) {
            pickerField = componentsFactory.createComponent(PickerField.class);
            setDatasource(pickerField, context);
            if (mpp.getMetaProperty().getType() == MetaProperty.Type.ASSOCIATION) {
                pickerField.addLookupAction();
                if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
                    DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                    DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
                    dynamicAttributesGuiTools.initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
                }
                boolean actionsByMetaAnnotations = ComponentsHelper.createActionsByMetaAnnotations(pickerField);
                if (!actionsByMetaAnnotations) {
                    pickerField.addClearAction();
                }
            } else {
                pickerField.addOpenAction();
                pickerField.addClearAction();
            }
        } else {
            LookupPickerField lookupPickerField = componentsFactory.createComponent(LookupPickerField.class);
            setDatasource(lookupPickerField, context);
            lookupPickerField.setOptionsDatasource(optionsDatasource);
            pickerField = lookupPickerField;
            ComponentsHelper.createActionsByMetaAnnotations(pickerField);
        }
        if (xmlDescriptor != null) {
            String captionProperty = xmlDescriptor.attributeValue("captionProperty");
            if (StringUtils.isNotEmpty(captionProperty)) {
                pickerField.setCaptionMode(CaptionMode.PROPERTY);
                pickerField.setCaptionProperty(captionProperty);
            }
        }
        return pickerField;
    } else {
        EntityLinkField linkField = componentsFactory.createComponent(EntityLinkField.class);
        setDatasource(linkField, context);
        setLinkFieldAttributes(linkField, context);
        return linkField;
    }
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Aggregations

CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)34 MetaClass (com.haulmont.chile.core.model.MetaClass)15 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)12 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 Nullable (javax.annotation.Nullable)6 Element (org.dom4j.Element)5 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)4 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)4 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)4 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)4 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)3 Joiner (com.google.common.base.Joiner)2 Strings (com.google.common.base.Strings)2 Dom4j (com.haulmont.bali.util.Dom4j)2 Enumeration (com.haulmont.chile.core.datatypes.Enumeration)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 Category (com.haulmont.cuba.core.entity.Category)2 Entity (com.haulmont.cuba.core.entity.Entity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2