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;
}
}
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;
}
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;
}
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);
}
}
}
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;
}
}
Aggregations