use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.
the class InputDialogFacetProvider method loadEnumParameter.
@SuppressWarnings("unchecked")
protected InputParameter loadEnumParameter(Element paramEl, ComponentLoader.ComponentContext context) {
String paramId = paramEl.attributeValue("id");
String classFqn = paramEl.attributeValue("enumClass");
InputParameter parameter;
Class clazz = loadParamClass(paramEl, classFqn, context);
if (EnumClass.class.isAssignableFrom(clazz)) {
parameter = InputParameter.enumParameter(paramId, clazz).withCaption(loadParamCaption(paramEl, context)).withRequired(loadParamRequired(paramEl)).withRequiredMessage(loadRequiredMessage(paramEl, context));
Field<?> field = loadField(paramId, paramEl, context);
if (field != null) {
parameter.withField(() -> field);
}
} else {
throw new GuiDevelopmentException(String.format("Unable to create InputDialog parameter '%s'. Class '%s' is not enum class", paramId, classFqn), context);
}
return parameter;
}
use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.
the class ParamWrapper method getValue.
@Override
public Object getValue() {
Object value = param.getValue();
if (value instanceof String && !StringUtils.isEmpty((String) value) && !((String) value).startsWith(ParametersHelper.CASE_INSENSITIVE_MARKER)) {
// try to wrap value for case-insensitive "like" search
if (condition instanceof PropertyCondition || condition instanceof DynamicAttributesCondition) {
String escapedValue = value.toString();
if (condition.getEntityMetaClass() != null) {
String thisStore = condition.getEntityMetaClass().getStore().getName();
CubaProperties properties = AppBeans.get(CubaProperties.class);
if (properties.getDisableEscapingLikeForDataStores() == null || !properties.getDisableEscapingLikeForDataStores().contains(thisStore)) {
escapedValue = QueryUtils.escapeForLike(escapedValue);
}
} else {
escapedValue = QueryUtils.escapeForLike(escapedValue);
}
Op op = condition.getOperator();
if (Op.CONTAINS.equals(op) || op.equals(Op.DOES_NOT_CONTAIN)) {
value = wrapValueForLike(escapedValue);
} else if (Op.STARTS_WITH.equals(op)) {
value = wrapValueForLike(escapedValue, false, true);
} else if (Op.ENDS_WITH.equals(op)) {
value = wrapValueForLike(escapedValue, true, false);
}
} else if (condition instanceof CustomCondition) {
String where = ((CustomCondition) condition).getWhere();
Op op = condition.getOperator();
Matcher matcher = LIKE_PATTERN.matcher(where);
if (matcher.find()) {
String stringValue = value.toString();
boolean escape = StringUtils.isNotEmpty(matcher.group(3));
if (escape) {
String escapeChar = matcher.group(4);
if (StringUtils.isNotEmpty(escapeChar)) {
stringValue = QueryUtils.escapeForLike(stringValue, escapeChar);
}
}
if (Op.STARTS_WITH.equals(op)) {
value = wrapValueForLike(stringValue, false, true);
} else if (Op.ENDS_WITH.equals(op)) {
value = wrapValueForLike(stringValue, true, false);
} else {
value = wrapValueForLike(stringValue);
}
}
}
} else if (value instanceof Entity) {
value = EntityValues.getId(((Entity) value));
} else if (value instanceof Collection) {
List<Object> list = new ArrayList<>(((Collection) value).size());
for (Object obj : ((Collection) value)) {
list.add(obj instanceof Entity ? EntityValues.getId(((Entity) obj)) : obj);
}
value = list;
} else if (value instanceof EnumClass) {
value = ((EnumClass) value).getId();
}
return value;
}
use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.
the class ParameterTransformerImpl method createParameterValue.
@Override
public ParameterValue createParameterValue(Field field, ScreenFragment widgetFragment) {
ParameterValue parameterValue = null;
ParameterType parameterType = getParameterType(field);
try {
if (parameterType == ENTITY) {
Object entity = FieldUtils.readField(field, widgetFragment, true);
if (entity != null) {
WidgetParam ann = field.getAnnotation(WidgetParam.class);
String fetchPlan = StringUtils.isNoneBlank(ann.fetchPlanName()) ? ann.fetchPlanName() : null;
parameterValue = new EntityParameterValue(metadata.getClass(entity).getName(), EntityValues.getId(entity).toString(), fetchPlan);
}
} else if (parameterType == ENTITY_LIST) {
List<Entity> listEntity = (List) FieldUtils.readField(field, widgetFragment, true);
WidgetParam ann = field.getAnnotation(WidgetParam.class);
if (listEntity != null) {
String fetchPlan = StringUtils.isNoneBlank(ann.fetchPlanName()) ? ann.fetchPlanName() : null;
List<EntityParameterValue> resultList = listEntity.stream().map(entity -> new EntityParameterValue(metadata.getClass(entity).getName(), EntityValues.getId(entity).toString(), fetchPlan)).collect(Collectors.toList());
parameterValue = new EntityListParameterValue(resultList);
}
} else if (parameterType == ENUM) {
EnumClass<String> rawValue = (EnumClass) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
String enumClassName = rawValue.getClass().toString();
parameterValue = new EnumParameterValue(enumClassName);
}
} else if (parameterType == DATE) {
Date rawValue = (Date) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new DateParameterValue(rawValue);
}
} else if (parameterType == DATETIME) {
Date rawValue = (Date) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new DateParameterValue(rawValue);
}
} else if (parameterType == TIME) {
Date rawValue = (Date) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new DateParameterValue(rawValue);
}
} else if (parameterType == ParameterType.UUID) {
UUID rawValue = (UUID) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new UuidParameterValue(rawValue);
}
} else if (parameterType == INTEGER) {
Integer rawValue = (Integer) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new IntegerParameterValue(rawValue);
}
} else if (parameterType == STRING) {
String rawValue = (String) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new StringParameterValue(rawValue);
}
} else if (parameterType == DECIMAL) {
BigDecimal rawValue = (BigDecimal) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new DecimalParameterValue(rawValue);
}
} else if (parameterType == BOOLEAN) {
Boolean rawValue = (Boolean) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new BooleanParameterValue(rawValue);
}
} else if (parameterType == LONG) {
Long rawValue = (Long) FieldUtils.readField(field, widgetFragment, true);
if (rawValue != null) {
parameterValue = new LongParameterValue(rawValue);
}
}
} catch (Exception e) {
throw new RuntimeException("Error on parameter init", e);
}
return parameterValue;
}
use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.
the class AppPropertiesLocator method getTypeStringify.
protected TypeStringify getTypeStringify(Method method) {
try {
Stringify stringifyAnn = method.getAnnotation(Stringify.class);
if (stringifyAnn != null) {
if ("".equals(stringifyAnn.method())) {
return stringifyAnn.stringify().getDeclaredConstructor().newInstance();
} else {
String methodName = stringifyAnn.method();
return new MethodTypeStringify(method.getReturnType().getMethod(methodName));
}
}
Factory factoryAnn = method.getAnnotation(Factory.class);
if (factoryAnn != null) {
TypeStringify typeStringify = getTypeStringifyForFactory(factoryAnn.factory());
if (typeStringify != null)
return typeStringify;
}
if (Double.class.isAssignableFrom(method.getReturnType()) || Float.class.isAssignableFrom(method.getReturnType()) || double.class.isAssignableFrom(method.getReturnType()) || float.class.isAssignableFrom(method.getReturnType())) {
return new DecimalStringify();
}
if (Date.class.isAssignableFrom(method.getReturnType())) {
return new DateStringify();
}
if (Entity.class.isAssignableFrom(method.getReturnType())) {
return new EntityStringify();
}
if (EnumClass.class.isAssignableFrom(method.getReturnType())) {
EnumStore mode = method.getAnnotation(EnumStore.class);
if (mode != null && EnumStoreMode.ID == mode.value()) {
@SuppressWarnings("unchecked") Class<EnumClass> enumeration = (Class<EnumClass>) method.getReturnType();
TypeStringify idStringify = TypeStringify.getInferred(ConfigUtil.getEnumIdType(enumeration));
return new EnumClassStringify(idStringify);
}
}
return TypeStringify.getInferred(method.getReturnType());
} catch (Exception e) {
log.warn("Error getting TypeStringify: " + e);
return new PrimitiveTypeStringify();
}
}
use of io.jmix.core.metamodel.datatype.impl.EnumClass in project jmix by jmix-framework.
the class AppPropertiesLocator method setDataType.
private void setDataType(Method method, AppPropertyEntity entity) {
Class<?> returnType = method.getReturnType();
if (returnType.isPrimitive()) {
if (returnType == boolean.class)
entity.setDataTypeName(datatypes.getIdByJavaClass(Boolean.class));
if (returnType == int.class)
entity.setDataTypeName(datatypes.getIdByJavaClass(Integer.class));
if (returnType == long.class)
entity.setDataTypeName(datatypes.getIdByJavaClass(Long.class));
if (returnType == double.class || returnType == float.class)
entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
if (returnType == char.class)
entity.setDataTypeName(datatypes.getIdByJavaClass(Character.class));
} else if (returnType.isEnum()) {
entity.setDataTypeName("enum");
EnumStore enumStoreAnn = method.getAnnotation(EnumStore.class);
if (enumStoreAnn != null && enumStoreAnn.value() == EnumStoreMode.ID) {
// noinspection unchecked
Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
entity.setEnumValues(Arrays.stream(enumeration.getEnumConstants()).map(ec -> String.valueOf(ec.getId())).collect(Collectors.joining(",")));
} else {
entity.setEnumValues(Arrays.stream(returnType.getEnumConstants()).map(Object::toString).collect(Collectors.joining(",")));
}
} else if (returnType == Float.class) {
entity.setDataTypeName(datatypes.getIdByJavaClass(Double.class));
} else {
Datatype<?> datatype = datatypes.get(returnType);
if (datatype != null)
entity.setDataTypeName(datatypes.getId(datatype));
else
entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
}
String dataTypeName = entity.getDataTypeName();
if (!dataTypeName.equals("enum")) {
Locale locale = userSessionSource.getLocale();
Datatype datatype = Datatypes.get(dataTypeName);
String v = null;
try {
v = entity.getDefaultValue();
datatype.parse(v, locale);
v = entity.getCurrentValue();
datatype.parse(v, locale);
} catch (ParseException e) {
log.debug("Cannot parse '{}' with {} datatype, using StringDatatype for property {}", v, datatype, entity.getName());
entity.setDataTypeName(datatypes.getIdByJavaClass(String.class));
}
}
}
Aggregations