Search in sources :

Example 1 with Datatype

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

the class JSONConverter method processServiceMethodResult.

@Override
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
    MyJSONObject root = new MyJSONObject();
    if (result instanceof Entity) {
        Entity entity = (Entity) result;
        MyJSONObject entityObject = _process(entity);
        root.set("result", entityObject);
    } else if (result instanceof Collection) {
        if (!checkCollectionItemTypes((Collection) result, Entity.class))
            throw new IllegalArgumentException("Items that are not instances of Entity found in service method result");
        // noinspection unchecked
        ArrayList list = new ArrayList((Collection) result);
        MetaClass metaClass;
        if (!list.isEmpty())
            metaClass = ((Entity) list.get(0)).getMetaClass();
        else
            metaClass = AppBeans.get(Metadata.class).getClasses().iterator().next();
        MyJSONObject.Array processed = _process(list, metaClass, null);
        root.set("result", processed);
    } else {
        if (result != null && resultType != Void.TYPE) {
            Datatype datatype = getDatatype(resultType);
            root.set("result", datatype != null ? datatype.format(result) : result.toString());
        } else {
            root.set("result", null);
        }
    }
    return root.toString();
}
Also used : JSONArray(org.json.JSONArray) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 2 with Datatype

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

the class XMLConverter2 method processServiceMethodResult.

@Override
@Nonnull
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element resultEl = document.addElement("result");
    if (result instanceof Entity) {
        Entity entity = (Entity) result;
        Document convertedEntity = _process(entity, null);
        resultEl.add(convertedEntity.getRootElement());
    } else if (result instanceof Collection) {
        if (!checkCollectionItemTypes((Collection) result, Entity.class))
            throw new IllegalArgumentException("Items that are not instances of Entity class found in service method result");
        ArrayList list = new ArrayList((Collection) result);
        MetaClass metaClass = null;
        if (!list.isEmpty())
            metaClass = ((Entity) list.get(0)).getMetaClass();
        Document processed = _process(list, metaClass, null);
        resultEl.add(processed.getRootElement());
    } else {
        if (result != null && resultType != Void.TYPE) {
            Datatype datatype = getDatatype(resultType);
            resultEl.setText(datatype != null ? datatype.format(result) : result.toString());
        } else {
            encodeNull(resultEl);
        }
    }
    return documentToString(document);
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Element(org.dom4j.Element) Document(org.dom4j.Document) Datatype(com.haulmont.chile.core.datatypes.Datatype) Nonnull(javax.annotation.Nonnull)

Example 3 with Datatype

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

the class FilterConditionUtils method formatParamValue.

public static String formatParamValue(Param param, Object value) {
    // noinspection unchecked
    Datatype datatype = Datatypes.get(param.getJavaClass());
    MetaProperty property = param.getProperty();
    if (property != null) {
        TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
        if (tt == TemporalType.DATE) {
            datatype = Datatypes.getNN(java.sql.Date.class);
        }
    }
    if (datatype != null) {
        UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.class);
        return datatype.format(value, userSessionSource.getLocale());
    }
    return value.toString();
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Datatype(com.haulmont.chile.core.datatypes.Datatype) TemporalType(javax.persistence.TemporalType)

Example 4 with Datatype

use of com.haulmont.chile.core.datatypes.Datatype 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 5 with Datatype

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

the class DsContextLoader method loadProperties.

private void loadProperties(Element element, ValueDatasource datasource) {
    Element propsEl = element.element("properties");
    if (propsEl != null) {
        for (Element propEl : Dom4j.elements(propsEl)) {
            String name = propEl.attributeValue("name");
            String className = propEl.attributeValue("class");
            if (className != null) {
                datasource.addProperty(name, ReflectionHelper.getClass(className));
            } else {
                String typeName = propEl.attributeValue("datatype");
                Datatype datatype = typeName == null ? Datatypes.getNN(String.class) : Datatypes.get(typeName);
                datasource.addProperty(name, datatype);
            }
        }
        String idProperty = propsEl.attributeValue("idProperty");
        if (idProperty != null) {
            if (datasource.getMetaClass().getProperty(idProperty) == null)
                throw new DevelopmentException(String.format("Property '%s' is not defined", idProperty));
            datasource.setIdName(idProperty);
        }
    }
}
Also used : Element(org.dom4j.Element) Datatype(com.haulmont.chile.core.datatypes.Datatype) DevelopmentException(com.haulmont.cuba.core.global.DevelopmentException)

Aggregations

Datatype (com.haulmont.chile.core.datatypes.Datatype)27 ParseException (java.text.ParseException)9 Entity (com.haulmont.cuba.core.entity.Entity)6 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)6 Datatypes (com.haulmont.chile.core.datatypes.Datatypes)3 AdaptiveNumberDatatype (com.haulmont.chile.core.datatypes.impl.AdaptiveNumberDatatype)3 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Element (org.dom4j.Element)3 Logger (org.slf4j.Logger)3 JsonObject (com.google.gson.JsonObject)2 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 Metadata (com.haulmont.cuba.core.global.Metadata)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 RestAPIException (com.haulmont.restapi.exception.RestAPIException)2 Map (java.util.Map)2 Inject (javax.inject.Inject)2 Document (org.dom4j.Document)2