use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class ConditionDescriptorsTreeBuilder method build.
@Override
public Tree<AbstractConditionDescriptor> build() {
Messages messages = AppBeans.get(Messages.class);
String messagesPack = filter.getFrame().getMessagesPack();
CollectionDatasource datasource = filter.getDatasource();
Tree<AbstractConditionDescriptor> tree = new Tree<>();
List<AbstractConditionDescriptor> propertyDescriptors = new ArrayList<>();
List<AbstractConditionDescriptor> customDescriptors = new ArrayList<>();
boolean propertiesExplicitlyDefined = false;
if (filter.getXmlDescriptor() != null) {
for (Element element : Dom4j.elements(filter.getXmlDescriptor())) {
AbstractConditionDescriptor conditionDescriptor;
if ("properties".equals(element.getName())) {
addMultiplePropertyDescriptors(element, propertyDescriptors, filter);
propertiesExplicitlyDefined = true;
} else if ("property".equals(element.getName())) {
conditionDescriptor = new PropertyConditionDescriptor(element, messagesPack, filterComponentName, datasource);
propertyDescriptors.add(conditionDescriptor);
propertiesExplicitlyDefined = true;
} else if ("custom".equals(element.getName())) {
conditionDescriptor = new CustomConditionDescriptor(element, messagesPack, filterComponentName, datasource);
customDescriptors.add(conditionDescriptor);
propertiesExplicitlyDefined = true;
} else {
throw new UnsupportedOperationException("Element not supported: " + element.getName());
}
}
}
if (!propertiesExplicitlyDefined) {
addMultiplePropertyDescriptors(".*", "", propertyDescriptors, filter);
}
propertyDescriptors.sort(new ConditionDescriptorComparator());
customDescriptors.sort(new ConditionDescriptorComparator());
HeaderConditionDescriptor propertyHeaderDescriptor = new HeaderConditionDescriptor("propertyConditions", messages.getMainMessage("filter.addCondition.propertyConditions"), filterComponentName, datasource);
HeaderConditionDescriptor customHeaderDescriptor = new HeaderConditionDescriptor("customConditions", messages.getMainMessage("filter.addCondition.customConditions"), filterComponentName, datasource);
Node<AbstractConditionDescriptor> propertyHeaderNode = new Node<>(propertyHeaderDescriptor);
Node<AbstractConditionDescriptor> customHeaderNode = new Node<>(customHeaderDescriptor);
int currentDepth = 0;
for (AbstractConditionDescriptor propertyDescriptor : propertyDescriptors) {
MetaClass propertyDsMetaClass = propertyDescriptor.getDatasourceMetaClass();
MetaPropertyPath propertyPath = propertyDsMetaClass.getPropertyPath(propertyDescriptor.getName());
if (propertyPath == null) {
log.error("Property path for {} of metaClass {} not found", propertyDescriptor.getName(), propertyDsMetaClass.getName());
continue;
}
MetaProperty metaProperty = propertyPath.getMetaProperty();
MetaClass propertyEnclosingMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
if (isPropertyAllowed(propertyEnclosingMetaClass, metaProperty) && !excludedProperties.contains(metaProperty.getName())) {
Node<AbstractConditionDescriptor> node = new Node<>(propertyDescriptor);
propertyHeaderNode.addChild(node);
if (currentDepth < hierarchyDepth) {
recursivelyFillPropertyDescriptors(node, currentDepth);
}
}
}
for (AbstractConditionDescriptor customDescriptor : customDescriptors) {
Node<AbstractConditionDescriptor> node = new Node<>(customDescriptor);
customHeaderNode.addChild(node);
}
List<Node<AbstractConditionDescriptor>> rootNodes = new ArrayList<>();
rootNodes.add(propertyHeaderNode);
if (!customDescriptors.isEmpty())
rootNodes.add(customHeaderNode);
if (!hideCustomConditions && security.isSpecificPermitted(CUSTOM_CONDITIONS_PERMISSION)) {
rootNodes.add(new Node<>(new CustomConditionCreator(filterComponentName, datasource)));
}
if (!hideDynamicAttributes && !dynamicAttributes.getAttributesForMetaClass(datasource.getMetaClass()).isEmpty()) {
rootNodes.add(new Node<>(new DynamicAttributesConditionCreator(filterComponentName, datasource, "")));
}
if (FtsConfigHelper.getEnabled()) {
rootNodes.add(new Node<>(new FtsConditionDescriptor(filterComponentName, datasource)));
}
tree.setRootNodes(rootNodes);
return tree;
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class ConditionDescriptorsTreeBuilder method recursivelyFillPropertyDescriptors.
protected void recursivelyFillPropertyDescriptors(Node<AbstractConditionDescriptor> parentNode, int currentDepth) {
currentDepth++;
List<AbstractConditionDescriptor> descriptors = new ArrayList<>();
MetaClass filterMetaClass = filter.getDatasource().getMetaClass();
String propertyId = parentNode.getData().getName();
MetaPropertyPath mpp = filterMetaClass.getPropertyPath(propertyId);
if (mpp == null) {
throw new RuntimeException("Unable to find property " + propertyId);
}
MetaProperty metaProperty = mpp.getMetaProperty();
if (metaProperty.getRange().isClass() && (metadataTools.getCrossDataStoreReferenceIdProperty(storeName, metaProperty) == null)) {
MetaClass childMetaClass = metaProperty.getRange().asClass();
for (MetaProperty property : childMetaClass.getProperties()) {
if (isPropertyAllowed(childMetaClass, property)) {
String propertyPath = mpp.toString() + "." + property.getName();
if (excludedProperties.contains(propertyPath))
continue;
PropertyConditionDescriptor childPropertyConditionDescriptor = new PropertyConditionDescriptor(propertyPath, null, filter.getFrame().getMessagesPack(), filterComponentName, filter.getDatasource());
descriptors.add(childPropertyConditionDescriptor);
}
}
}
descriptors.sort(new ConditionDescriptorComparator());
for (AbstractConditionDescriptor descriptor : descriptors) {
Node<AbstractConditionDescriptor> newNode = new Node<>(descriptor);
parentNode.addChild(newNode);
if (currentDepth < hierarchyDepth) {
recursivelyFillPropertyDescriptors(newNode, currentDepth);
}
}
if (metaProperty.getRange().isClass()) {
MetaClass childMetaClass = metaProperty.getRange().asClass();
if (!dynamicAttributes.getAttributesForMetaClass(childMetaClass).isEmpty()) {
DynamicAttributesConditionCreator descriptor = new DynamicAttributesConditionCreator(filterComponentName, filter.getDatasource(), propertyId);
Node<AbstractConditionDescriptor> newNode = new Node<>(descriptor);
parentNode.addChild(newNode);
}
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class CustomConditionFrame method fillEntitySelect.
protected void fillEntitySelect(Param param) {
if (!entitySelect.isEnabled()) {
entitySelect.setValue(null);
return;
}
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
MessageTools messageTools = AppBeans.get(MessageTools.NAME);
Map<String, Object> items = new TreeMap<>();
Object selectedItem = null;
if (ParamType.ENTITY.equals(typeSelect.getValue())) {
for (MetaClass metaClass : metadataTools.getAllPersistentMetaClasses()) {
if (!metadataTools.isSystemLevel(metaClass)) {
items.put(messageTools.getEntityCaption(metaClass) + " (" + metaClass.getName() + ")", metaClass);
}
}
if (param != null && Param.Type.ENTITY.equals(param.getType())) {
Class javaClass = param.getJavaClass();
Metadata metadata = AppBeans.get(Metadata.NAME);
selectedItem = metadata.getClass(javaClass);
}
entitySelect.setOptionsMap(items);
entitySelect.setValue(selectedItem);
} else if (ParamType.ENUM.equals(typeSelect.getValue())) {
if (param != null && Param.Type.ENUM.equals(param.getType())) {
selectedItem = param.getJavaClass();
}
boolean selectedItemFound = false;
for (Class enumClass : metadataTools.getAllEnums()) {
items.put(getEnumClassName(enumClass), enumClass);
if (selectedItem == null || selectedItem.equals(enumClass))
selectedItemFound = true;
}
// the metamodel, hence not in MetadataHelper.getAllEnums(). So we just add it here.
if (selectedItem != null && !selectedItemFound) {
items.put(getEnumClassName((Class) selectedItem), selectedItem);
}
entitySelect.setOptionsMap(items);
entitySelect.setValue(selectedItem);
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DynamicAttributesConditionFrame method fillCategorySelect.
protected void fillCategorySelect() {
DynamicAttributes dynamicAttributes = AppBeans.get(DynamicAttributes.NAME);
MetaClass metaClass = condition.getDatasource().getMetaClass();
if (!Strings.isNullOrEmpty(condition.getPropertyPath())) {
MetaPropertyPath propertyPath = metaClass.getPropertyPath(condition.getPropertyPath());
if (propertyPath == null) {
throw new RuntimeException("Property path " + condition.getPropertyPath() + " doesn't exist");
}
metaClass = propertyPath.getRange().asClass();
}
Collection<Category> categories = dynamicAttributes.getCategoriesForMetaClass(metaClass);
UUID catId = condition.getCategoryId();
Category selectedCategory = null;
Map<String, Object> categoriesMap = new TreeMap<>();
if (categories.size() == 1 && (catId == null || Objects.equals(catId, categories.iterator().next().getId()))) {
Category category = categories.iterator().next();
categoryLookup.setVisible(false);
categoryLabel.setVisible(false);
attributeLookup.requestFocus();
categoriesMap.put(category.getName(), category);
categoryLookup.setOptionsMap(categoriesMap);
categoryLookup.setValue(category);
fillAttributeSelect(category);
} else {
categoryLookup.setVisible(true);
categoryLabel.setVisible(true);
for (Category category : categories) {
categoriesMap.put(category.getName(), category);
if (category.getId().equals(catId)) {
selectedCategory = category;
}
}
categoryLookup.setOptionsMap(categoriesMap);
categoryLookup.setValue(selectedCategory);
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class EntitiesControllerManager method createEntity.
public CreatedEntityInfo createEntity(String entityJson, String entityName, String modelVersion) {
String transformedEntityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION);
MetaClass metaClass = restControllerUtils.getMetaClass(transformedEntityName);
checkCanCreateEntity(metaClass);
entityJson = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, entityJson);
Entity entity;
try {
entity = entitySerializationAPI.entityFromJson(entityJson, metaClass);
} catch (Exception e) {
throw new RestAPIException("Cannot deserialize an entity from JSON", "", HttpStatus.BAD_REQUEST, e);
}
EntityImportView entityImportView = entityImportViewBuilderAPI.buildFromJson(entityJson, metaClass);
Collection<Entity> importedEntities;
try {
importedEntities = entityImportExportService.importEntities(Collections.singletonList(entity), entityImportView, true);
} catch (EntityImportException e) {
throw new RestAPIException("Entity creation failed", e.getMessage(), HttpStatus.BAD_REQUEST, e);
}
// if many entities were created (because of @Composition references) we must find the main entity
return getMainEntityInfo(importedEntities, metaClass, modelVersion);
}
Aggregations