Search in sources :

Example 1 with IdProxy

use of com.haulmont.cuba.core.entity.IdProxy 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 2 with IdProxy

use of com.haulmont.cuba.core.entity.IdProxy in project cuba by cuba-platform.

the class CrossDataStoreReferenceLoader method loadBatch.

private void loadBatch(CrossDataStoreProperty crossDataStoreProperty, List<Entity> entities) {
    List<Object> idList = entities.stream().map(e -> e.getValue(crossDataStoreProperty.relatedPropertyName)).filter(Objects::nonNull).distinct().collect(Collectors.toList());
    if (idList.isEmpty())
        return;
    MetaClass cdsrMetaClass = crossDataStoreProperty.property.getRange().asClass();
    LoadContext<Entity> loadContext = new LoadContext<>(cdsrMetaClass);
    MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(cdsrMetaClass);
    if (primaryKeyProperty == null || !primaryKeyProperty.getRange().isClass()) {
        String queryString = String.format("select e from %s e where e.%s in :idList", cdsrMetaClass, crossDataStoreProperty.primaryKeyName);
        loadContext.setQuery(LoadContext.createQuery(queryString).setParameter("idList", idList));
    } else {
        // composite key entity
        StringBuilder sb = new StringBuilder("select e from ");
        sb.append(cdsrMetaClass).append(" e where ");
        MetaClass idMetaClass = primaryKeyProperty.getRange().asClass();
        for (Iterator<MetaProperty> it = idMetaClass.getProperties().iterator(); it.hasNext(); ) {
            MetaProperty property = it.next();
            sb.append("e.").append(crossDataStoreProperty.primaryKeyName).append(".").append(property.getName());
            sb.append(" in :list_").append(property.getName());
            if (it.hasNext())
                sb.append(" and ");
        }
        LoadContext.Query query = LoadContext.createQuery(sb.toString());
        for (MetaProperty property : idMetaClass.getProperties()) {
            List<Object> propList = idList.stream().map(o -> ((Entity) o).getValue(property.getName())).collect(Collectors.toList());
            query.setParameter("list_" + property.getName(), propList);
        }
        loadContext.setQuery(query);
    }
    loadContext.setView(crossDataStoreProperty.viewProperty.getView());
    List<Entity> loadedEntities = dataManager.loadList(loadContext);
    for (Entity entity : entities) {
        Object relatedPropertyValue = entity.getValue(crossDataStoreProperty.relatedPropertyName);
        loadedEntities.stream().filter(e -> {
            Object id = e.getId() instanceof IdProxy ? ((IdProxy) e.getId()).getNN() : e.getId();
            return id.equals(relatedPropertyValue);
        }).findAny().ifPresent(e -> entity.setValue(crossDataStoreProperty.property.getName(), e));
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Preconditions(com.haulmont.bali.util.Preconditions) MetaClass(com.haulmont.chile.core.model.MetaClass) Scope(org.springframework.context.annotation.Scope) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) IdProxy(com.haulmont.cuba.core.entity.IdProxy) Entity(com.haulmont.cuba.core.entity.Entity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) IdProxy(com.haulmont.cuba.core.entity.IdProxy) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 3 with IdProxy

use of com.haulmont.cuba.core.entity.IdProxy in project cuba by cuba-platform.

the class QueryImpl method setParameter.

@Override
public TypedQuery<T> setParameter(int position, Object value, boolean implicitConversions) {
    checkState();
    DbmsFeatures dbmsFeatures = DbmsSpecificFactory.getDbmsFeatures();
    if (isNative && (value instanceof UUID) && (dbmsFeatures.getUuidTypeClassName() != null)) {
        Class c = ReflectionHelper.getClass(dbmsFeatures.getUuidTypeClassName());
        try {
            value = ReflectionHelper.newInstance(c, value);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("Error setting parameter value", e);
        }
    } else if (value instanceof IdProxy) {
        value = ((IdProxy) value).get();
    } else if (implicitConversions) {
        value = handleImplicitConversions(value);
    }
    params.add(new Param(position, value));
    return this;
}
Also used : DbmsFeatures(com.haulmont.cuba.core.sys.persistence.DbmsFeatures) MetaClass(com.haulmont.chile.core.model.MetaClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) IdProxy(com.haulmont.cuba.core.entity.IdProxy)

Aggregations

IdProxy (com.haulmont.cuba.core.entity.IdProxy)3 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)2 MetaClass (com.haulmont.chile.core.model.MetaClass)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Entity (com.haulmont.cuba.core.entity.Entity)2 Sets (com.google.common.collect.Sets)1 Preconditions (com.haulmont.bali.util.Preconditions)1 Datatype (com.haulmont.chile.core.datatypes.Datatype)1 IgnoreUserTimeZone (com.haulmont.cuba.core.entity.annotation.IgnoreUserTimeZone)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 DbmsFeatures (com.haulmont.cuba.core.sys.persistence.DbmsFeatures)1 ParseException (java.text.ParseException)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Scope (org.springframework.context.annotation.Scope)1 Component (org.springframework.stereotype.Component)1