use of com.haulmont.cuba.core.global.filter.Op 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.global.filter.Op in project cuba by cuba-platform.
the class DynamicAttributesConditionFrame method fillOperationSelect.
protected void fillOperationSelect(CategoryAttribute categoryAttribute) {
Class clazz = DynamicAttributesUtils.getAttributeClass(categoryAttribute);
OpManager opManager = AppBeans.get(OpManager.class);
EnumSet<Op> availableOps = BooleanUtils.isTrue(categoryAttribute.getIsCollection()) ? opManager.availableOpsForCollectionDynamicAttribute() : opManager.availableOps(clazz);
List<Op> ops = new LinkedList<>(availableOps);
operationLookup.setOptionsList(ops);
Op operator = condition.getOperator();
if (operator != null) {
operationLookup.setValue(operator);
}
}
use of com.haulmont.cuba.core.global.filter.Op in project cuba by cuba-platform.
the class PropertyConditionDescriptor method createCondition.
@Override
public AbstractCondition createCondition() {
OpManager opManager = AppBeans.get(OpManager.class);
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
PropertyCondition propertyCondition = new PropertyCondition(this, entityAlias);
MetaPropertyPath propertyPath = datasourceMetaClass.getPropertyPath(name);
if (propertyPath == null) {
throw new IllegalStateException(String.format("Unable to find property '%s' in entity %s", name, datasourceMetaClass));
}
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
EnumSet<Op> ops = opManager.availableOps(propertyMetaClass, propertyPath.getMetaProperty());
propertyCondition.setOperator(ops.iterator().next());
return propertyCondition;
}
use of com.haulmont.cuba.core.global.filter.Op in project cuba by cuba-platform.
the class PropertyOperationEditor method createComponent.
@Override
protected Component createComponent() {
OpManager opManager = AppBeans.get(OpManager.class);
MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
componentsFactory = AppBeans.get(ComponentsFactory.class);
popupButton = componentsFactory.createComponent(PopupButton.class);
MetaClass metaClass = condition.getDatasource().getMetaClass();
MetaPropertyPath propertyPath = metaClass.getPropertyPath(condition.getName());
if (propertyPath == null) {
throw new IllegalStateException(String.format("Unable to find property '%s' in entity %s", condition.getName(), metaClass));
}
MetaClass propertyMetaClass = metadataTools.getPropertyEnclosingMetaClass(propertyPath);
for (Op op : opManager.availableOps(propertyMetaClass, propertyPath.getMetaProperty())) {
OperatorChangeAction operatorChangeAction = new OperatorChangeAction(op);
popupButton.addAction(operatorChangeAction);
}
popupButton.setCaption(condition.getOperator().getLocCaption());
popupButton.setStyleName("condition-operation-button");
return popupButton;
}
use of com.haulmont.cuba.core.global.filter.Op in project cuba by cuba-platform.
the class RestFilterParser method parsePropertyCondition.
protected RestFilterPropertyCondition parsePropertyCondition(JsonObject conditionJsonObject, MetaClass metaClass) throws RestFilterParseException {
RestFilterPropertyCondition condition = new RestFilterPropertyCondition();
JsonElement propertyJsonElem = conditionJsonObject.get("property");
if (propertyJsonElem == null) {
throw new RestFilterParseException("Field 'property' is not defined for filter condition");
}
String propertyName = propertyJsonElem.getAsString();
JsonElement operatorJsonElem = conditionJsonObject.get("operator");
if (operatorJsonElem == null) {
throw new RestFilterParseException("Field 'operator' is not defined for filter condition");
}
String operator = operatorJsonElem.getAsString();
Op op = findOperator(operator);
boolean isValueRequired = op != Op.NOT_EMPTY;
JsonElement valueJsonElem = conditionJsonObject.get("value");
if (valueJsonElem == null && isValueRequired) {
throw new RestFilterParseException("Field 'value' is not defined for filter condition");
}
MetaPropertyPath propertyPath = metaClass.getPropertyPath(propertyName);
if (propertyPath == null) {
throw new RestFilterParseException("Property for " + metaClass.getName() + " not found: " + propertyName);
}
MetaProperty metaProperty = propertyPath.getMetaProperty();
EnumSet<Op> opsAvailableForJavaType = opManager.availableOps(metaProperty.getJavaType());
if (!opsAvailableForJavaType.contains(op)) {
throw new RestFilterParseException("Operator " + operator + " is not available for java type " + metaProperty.getJavaType().getCanonicalName());
}
if (metaProperty.getRange().isClass()) {
if (Entity.class.isAssignableFrom(metaProperty.getJavaType())) {
MetaClass _metaClass = metadata.getClass(metaProperty.getJavaType());
MetaProperty primaryKeyProperty = metadata.getTools().getPrimaryKeyProperty(_metaClass);
String pkName = primaryKeyProperty.getName();
propertyName += "." + pkName;
propertyPath = metaClass.getPropertyPath(propertyName);
if (propertyPath == null) {
throw new RestFilterParseException("Property " + propertyName + " for " + metaClass.getName() + " not found");
}
metaProperty = propertyPath.getMetaProperty();
}
}
if (isValueRequired) {
Object value = null;
if (op == Op.IN || op == Op.NOT_IN) {
if (!valueJsonElem.isJsonArray()) {
throw new RestFilterParseException("JSON array was expected as a value for condition with operator " + operator);
}
List<Object> parsedArrayValues = new ArrayList<>();
for (JsonElement arrayItemElem : valueJsonElem.getAsJsonArray()) {
parsedArrayValues.add(parseValue(metaProperty, arrayItemElem.getAsString()));
}
value = parsedArrayValues;
} else {
value = parseValue(metaProperty, valueJsonElem.getAsString());
}
condition.setValue(transformValue(value, op));
condition.setQueryParamName(generateQueryParamName());
}
condition.setPropertyName(propertyName);
condition.setOperator(op);
return condition;
}
Aggregations