Search in sources :

Example 1 with EnumClass

use of com.haulmont.chile.core.datatypes.impl.EnumClass in project cuba by cuba-platform.

the class EnumsControllerManager method getAllEnumInfos.

public List<EnumInfo> getAllEnumInfos() {
    List<EnumInfo> results = new ArrayList<>();
    metadataTools.getAllEnums().stream().filter(enumClass -> EnumClass.class.isAssignableFrom(enumClass) && enumClass.isEnum()).forEach(enumClass -> {
        List<EnumValueInfo> enumValues = new ArrayList<>();
        Object[] enumConstants = enumClass.getEnumConstants();
        for (Object enumConstant : enumConstants) {
            Enum enumValue = (Enum) enumConstant;
            EnumValueInfo enumValueInfo = new EnumValueInfo(enumValue.name(), ((EnumClass) enumValue).getId(), messages.getMessage(enumValue));
            enumValues.add(enumValueInfo);
        }
        results.add(new EnumInfo(enumClass.getName(), enumValues));
    });
    return results;
}
Also used : Inject(javax.inject.Inject) HttpStatus(org.springframework.http.HttpStatus) Component(org.springframework.stereotype.Component) List(java.util.List) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) RestAPIException(com.haulmont.restapi.exception.RestAPIException) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) Messages(com.haulmont.cuba.core.global.Messages) EnumInfo(com.haulmont.restapi.data.EnumInfo) EnumValueInfo(com.haulmont.restapi.data.EnumValueInfo) ArrayList(java.util.ArrayList) EnumValueInfo(com.haulmont.restapi.data.EnumValueInfo) EnumInfo(com.haulmont.restapi.data.EnumInfo) ArrayList(java.util.ArrayList) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 2 with EnumClass

use of com.haulmont.chile.core.datatypes.impl.EnumClass in project cuba by cuba-platform.

the class ExcelExporter method formatValueCell.

protected void formatValueCell(HSSFCell cell, @Nullable Object cellValue, @Nullable MetaPropertyPath metaPropertyPath, int sizersIndex, int notificationRequired, int level, @Nullable Integer groupChildCount) {
    if (cellValue == null) {
        return;
    }
    String childCountValue = "";
    if (groupChildCount != null) {
        childCountValue = " (" + groupChildCount + ")";
    }
    if (cellValue instanceof IdProxy) {
        cellValue = ((IdProxy) cellValue).get();
    }
    if (cellValue instanceof Number) {
        Number n = (Number) cellValue;
        final Datatype datatype = Datatypes.getNN(n.getClass());
        String str;
        if (sizersIndex == 0) {
            str = createSpaceString(level) + datatype.format(n);
            cell.setCellValue(str);
        } else {
            try {
                str = datatype.format(n);
                Number result = (Number) datatype.parse(str);
                if (result != null) {
                    if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) {
                        cell.setCellValue(result.longValue());
                        cell.setCellStyle(integerFormatCellStyle);
                    } else {
                        cell.setCellValue(result.doubleValue());
                        cell.setCellStyle(doubleFormatCellStyle);
                    }
                }
            } catch (ParseException e) {
                throw new RuntimeException("Unable to parse numeric value", e);
            }
            cell.setCellType(CellType.NUMERIC);
        }
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Date) {
        Class javaClass = null;
        boolean supportTimezones = false;
        TimeZone timeZone = userSessionSource.getUserSession().getTimeZone();
        if (metaPropertyPath != null) {
            MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
            if (metaProperty.getRange().isDatatype()) {
                javaClass = metaProperty.getRange().asDatatype().getJavaClass();
            }
            Boolean ignoreUserTimeZone = metadataTools.getMetaAnnotationValue(metaProperty, IgnoreUserTimeZone.class);
            supportTimezones = timeZone != null && Objects.equals(Date.class, javaClass) && !Boolean.TRUE.equals(ignoreUserTimeZone);
        }
        Date date = (Date) cellValue;
        if (supportTimezones) {
            TimeZone currentTimeZone = LocaleUtil.getUserTimeZone();
            try {
                LocaleUtil.setUserTimeZone(timeZone);
                cell.setCellValue(date);
            } finally {
                if (Objects.equals(currentTimeZone, TimeZone.getDefault())) {
                    LocaleUtil.resetUserTimeZone();
                } else {
                    LocaleUtil.setUserTimeZone(currentTimeZone);
                }
            }
        } else {
            cell.setCellValue(date);
        }
        if (Objects.equals(java.sql.Time.class, javaClass)) {
            cell.setCellStyle(timeFormatCellStyle);
        } else if (Objects.equals(java.sql.Date.class, javaClass)) {
            cell.setCellStyle(dateFormatCellStyle);
        } else {
            cell.setCellStyle(dateTimeFormatCellStyle);
        }
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            String str = Datatypes.getNN(Date.class).format(date);
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Boolean) {
        String str = "";
        if (sizersIndex == 0) {
            str += createSpaceString(level);
        }
        str += ((Boolean) cellValue) ? trueStr : falseStr;
        cell.setCellValue(new HSSFRichTextString(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof EnumClass) {
        String nameKey = cellValue.getClass().getSimpleName() + "." + cellValue.toString();
        final String message = sizersIndex == 0 ? createSpaceString(level) + messages.getMessage(cellValue.getClass(), nameKey) : messages.getMessage(cellValue.getClass(), nameKey);
        cell.setCellValue(message + childCountValue);
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(message, stdFont);
        }
    } else if (cellValue instanceof Entity) {
        Entity entityVal = (Entity) cellValue;
        String instanceName = entityVal.getInstanceName();
        String str = sizersIndex == 0 ? createSpaceString(level) + instanceName : instanceName;
        str = str + childCountValue;
        cell.setCellValue(new HSSFRichTextString(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else if (cellValue instanceof Collection) {
        String str = "";
        cell.setCellValue(new HSSFRichTextString(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    } else {
        String strValue = cellValue == null ? "" : cellValue.toString();
        String str = sizersIndex == 0 ? createSpaceString(level) + strValue : strValue;
        str = str + childCountValue;
        cell.setCellValue(new HSSFRichTextString(str));
        if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
            sizers[sizersIndex].notifyCellValue(str, stdFont);
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Datatype(com.haulmont.chile.core.datatypes.Datatype) IgnoreUserTimeZone(com.haulmont.cuba.core.entity.annotation.IgnoreUserTimeZone) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) IdProxy(com.haulmont.cuba.core.entity.IdProxy) ParseException(java.text.ParseException) IgnoreUserTimeZone(com.haulmont.cuba.core.entity.annotation.IgnoreUserTimeZone) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 3 with EnumClass

use of com.haulmont.chile.core.datatypes.impl.EnumClass in project cuba by cuba-platform.

the class PersistenceTools method getOldEnumValue.

/**
 * Returns an old value of an enum attribute changed in the current transaction. The entity must be in the Managed state.
 * <p>
 * Unlike {@link #getOldValue(Entity, String)}, returns enum value and not its id.
 *
 * @param entity    entity instance
 * @param attribute attribute name
 * @return  an old value stored in the database. For a new entity returns null.
 * @throws IllegalArgumentException if the entity is not persistent or not in the Managed state
 */
@Nullable
public EnumClass getOldEnumValue(Entity entity, String attribute) {
    Object value = getOldValue(entity, attribute);
    if (value == null)
        return null;
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    MetaProperty metaProperty = metaClass.getPropertyNN(attribute);
    if (metaProperty.getRange().isEnum()) {
        for (Object o : metaProperty.getRange().asEnumeration().getValues()) {
            EnumClass enumValue = (EnumClass) o;
            if (value.equals(enumValue.getId()))
                return enumValue;
        }
    }
    return null;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 4 with EnumClass

use of com.haulmont.chile.core.datatypes.impl.EnumClass in project cuba by cuba-platform.

the class InstanceUtils method getInstanceName.

/**
 * @return Instance name as defined by {@link com.haulmont.chile.core.annotations.NamePattern}
 * or <code>toString()</code>.
 * @param instance  instance
 */
public static String getInstanceName(Instance instance) {
    checkNotNullArgument(instance, "instance is null");
    NamePatternRec rec = parseNamePattern(instance.getMetaClass());
    if (rec == null) {
        return instance.toString();
    } else {
        if (rec.methodName != null) {
            try {
                Method method = instance.getClass().getMethod(rec.methodName);
                Object result = method.invoke(instance);
                return (String) result;
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException("Error getting instance name", e);
            }
        }
        // lazy initialized messages, used only for enum values
        Messages messages = null;
        Object[] values = new Object[rec.fields.length];
        for (int i = 0; i < rec.fields.length; i++) {
            Object value = instance.getValue(rec.fields[i]);
            if (value == null) {
                values[i] = "";
            } else if (value instanceof Instance) {
                values[i] = getInstanceName((Instance) value);
            } else if (value instanceof EnumClass) {
                if (messages == null) {
                    messages = AppBeans.get(Messages.NAME);
                }
                values[i] = messages.getMessage((Enum) value);
            } else {
                values[i] = value;
            }
        }
        return String.format(rec.format, values);
    }
}
Also used : Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Example 5 with EnumClass

use of com.haulmont.chile.core.datatypes.impl.EnumClass in project cuba by cuba-platform.

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().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 (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();
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) ParseException(java.text.ParseException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass)

Aggregations

EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)10 Method (java.lang.reflect.Method)4 MetaProperty (com.haulmont.chile.core.model.MetaProperty)3 Entity (com.haulmont.cuba.core.entity.Entity)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ParseException (java.text.ParseException)3 Inject (javax.inject.Inject)3 LoggerFactory (org.slf4j.LoggerFactory)3 Component (org.springframework.stereotype.Component)3 Datatype (com.haulmont.chile.core.datatypes.Datatype)2 MetaClass (com.haulmont.chile.core.model.MetaClass)2 EnumStore (com.haulmont.cuba.core.config.EnumStore)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 Messages (com.haulmont.cuba.core.global.Messages)2 IOException (java.io.IOException)2 java.util (java.util)2 Nullable (javax.annotation.Nullable)2 StringUtils (org.apache.commons.lang.StringUtils)2 Logger (org.slf4j.Logger)2 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)2