use of com.haulmont.cuba.core.entity.Category 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.cuba.core.entity.Category in project cuba by cuba-platform.
the class DynamicAttributesConditionFrame method commit.
@Override
public boolean commit() {
if (!super.commit())
return false;
String error = checkCondition();
if (error != null) {
showNotification(messages.getMainMessage(error), Frame.NotificationType.TRAY);
return false;
}
CategoryAttribute attribute = attributeLookup.getValue();
String alias = condition.getEntityAlias();
String cavAlias = "cav" + RandomStringUtils.randomNumeric(5);
String paramName;
String operation = operationLookup.<Op>getValue().forJpql();
Op op = operationLookup.getValue();
Class javaClass = DynamicAttributesUtils.getAttributeClass(attribute);
String propertyPath = Strings.isNullOrEmpty(condition.getPropertyPath()) ? "" : "." + condition.getPropertyPath();
ConditionParamBuilder paramBuilder = AppBeans.get(ConditionParamBuilder.class);
paramName = paramBuilder.createParamName(condition);
String cavEntityId = referenceToEntitySupport.getReferenceIdPropertyName(condition.getDatasource().getMetaClass());
String where;
if (op == Op.NOT_EMPTY) {
where = "(exists (select " + cavAlias + " from sys$CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'))";
} else {
String valueFieldName = "stringValue";
if (Entity.class.isAssignableFrom(javaClass))
valueFieldName = "entityValue." + referenceToEntitySupport.getReferenceIdPropertyName(metadata.getClassNN(javaClass));
else if (String.class.isAssignableFrom(javaClass))
valueFieldName = "stringValue";
else if (Integer.class.isAssignableFrom(javaClass))
valueFieldName = "intValue";
else if (Double.class.isAssignableFrom(javaClass))
valueFieldName = "doubleValue";
else if (Boolean.class.isAssignableFrom(javaClass))
valueFieldName = "booleanValue";
else if (Date.class.isAssignableFrom(javaClass))
valueFieldName = "dateValue";
if (!attribute.getIsCollection()) {
condition.setJoin(", sys$CategoryAttributeValue " + cavAlias + " ");
String paramStr = " ? ";
where = cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " " + operation + (op.isUnary() ? " " : paramStr) + "and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'";
where = where.replace("?", ":" + paramName);
} else {
where = "(exists (select " + cavAlias + " from sys$CategoryAttributeValue " + cavAlias + " where " + cavAlias + ".entity." + cavEntityId + "=" + "{E}" + propertyPath + ".id and " + cavAlias + "." + valueFieldName + " = :" + paramName + " and " + cavAlias + ".categoryAttribute.id='" + attributeLookup.<CategoryAttribute>getValue().getId() + "'))";
}
}
condition.setWhere(where);
condition.setUnary(op.isUnary());
condition.setEntityParamView(null);
condition.setEntityParamWhere(null);
condition.setInExpr(Op.IN.equals(op) || Op.NOT_IN.equals(op));
condition.setOperator(operationLookup.<Op>getValue());
Class paramJavaClass = op.isUnary() ? Boolean.class : javaClass;
condition.setJavaClass(javaClass);
Param param = Param.Builder.getInstance().setName(paramName).setJavaClass(paramJavaClass).setDataSource(condition.getDatasource()).setProperty(DynamicAttributesUtils.getMetaPropertyPath(null, attribute).getMetaProperty()).setInExpr(condition.getInExpr()).setRequired(condition.getRequired()).setCategoryAttrId(attribute.getId()).build();
Object defaultValue = condition.getParam().getDefaultValue();
param.setDefaultValue(defaultValue);
condition.setParam(param);
condition.setCategoryId(categoryLookup.<Category>getValue().getId());
condition.setCategoryAttributeId(attributeLookup.<CategoryAttribute>getValue().getId());
condition.setIsCollection(BooleanUtils.isTrue(attributeLookup.<CategoryAttribute>getValue().getIsCollection()));
condition.setLocCaption(attribute.getLocaleName());
condition.setCaption(caption.getValue());
return true;
}
use of com.haulmont.cuba.core.entity.Category in project cuba by cuba-platform.
the class DynamicAttributesCache method getAttributesForMetaClass.
public Collection<CategoryAttribute> getAttributesForMetaClass(MetaClass metaClass) {
MetaClass targetMetaClass = resolveTargetMetaClass(metaClass);
Collection<Category> categories = categoriesCache.get(targetMetaClass.getName());
List<CategoryAttribute> categoryAttributes = new ArrayList<>();
for (Category category : categories) {
categoryAttributes.addAll(Collections2.filter(category.getCategoryAttrs(), new Predicate<CategoryAttribute>() {
@Override
public boolean apply(@Nullable CategoryAttribute input) {
return input != null && StringUtils.isNotBlank(input.getCode());
}
}));
}
return categoryAttributes;
}
use of com.haulmont.cuba.core.entity.Category in project cuba by cuba-platform.
the class CategoryEditor method initIsDefaultCheckbox.
protected void initIsDefaultCheckbox() {
isDefault.setValue(BooleanUtils.isTrue(category.getIsDefault()));
isDefault.addValueChangeListener(e -> {
if (Boolean.TRUE.equals(e.getValue())) {
LoadContext<Category> lc = new LoadContext<>(Category.class).setView("category.defaultEdit");
lc.setQueryString("select c from sys$Category c where c.entityType = :entityType and not c.id = :id").setParameter("entityType", category.getEntityType()).setParameter("id", category.getId());
List<Category> result = dataManager.loadList(lc);
for (Category cat : result) {
cat.setIsDefault(false);
}
CommitContext commitCtx = new CommitContext(result);
dataManager.commit(commitCtx);
category.setIsDefault(true);
} else if (Boolean.FALSE.equals(e.getValue())) {
category.setIsDefault(false);
}
});
}
Aggregations