Search in sources :

Example 61 with MetaClass

use of com.haulmont.chile.core.model.MetaClass 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 62 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DataManagerBean method writeCrossDataStoreReferences.

protected boolean writeCrossDataStoreReferences(Entity entity, Collection<Entity> allEntities) {
    if (Stores.getAdditional().isEmpty())
        return false;
    boolean repeatRequired = false;
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    for (MetaProperty property : metaClass.getProperties()) {
        if (property.getRange().isClass() && !property.getRange().getCardinality().isMany()) {
            MetaClass propertyMetaClass = property.getRange().asClass();
            if (!Objects.equals(metadataTools.getStoreName(propertyMetaClass), metadataTools.getStoreName(metaClass))) {
                List<String> relatedProperties = metadataTools.getRelatedProperties(property);
                if (relatedProperties.size() == 0) {
                    continue;
                }
                if (relatedProperties.size() > 1) {
                    log.warn("More than 1 related property is defined for attribute {}, skip handling different data store", property);
                    continue;
                }
                String relatedPropertyName = relatedProperties.get(0);
                if (PersistenceHelper.isLoaded(entity, relatedPropertyName)) {
                    Entity refEntity = entity.getValue(property.getName());
                    if (refEntity == null) {
                        entity.setValue(relatedPropertyName, null);
                    } else {
                        Object refEntityId = refEntity.getId();
                        if (refEntityId instanceof IdProxy) {
                            Object realId = ((IdProxy) refEntityId).get();
                            if (realId == null) {
                                if (allEntities.stream().anyMatch(e -> e.getId().equals(refEntityId))) {
                                    repeatRequired = true;
                                } else {
                                    log.warn("No entity with ID={} in the context, skip handling different data store", refEntityId);
                                }
                            } else {
                                entity.setValue(relatedPropertyName, realId);
                            }
                        } else if (refEntityId instanceof EmbeddableEntity) {
                            MetaProperty relatedProperty = metaClass.getPropertyNN(relatedPropertyName);
                            if (!relatedProperty.getRange().isClass()) {
                                log.warn("PK of entity referenced by {} is a EmbeddableEntity, but related property {} is not", property, relatedProperty);
                            } else {
                                entity.setValue(relatedPropertyName, metadataTools.copy((Entity) refEntityId));
                            }
                        } else {
                            entity.setValue(relatedPropertyName, refEntityId);
                        }
                    }
                }
            }
        }
    }
    return repeatRequired;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 63 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DataManagerBean method load.

@Nullable
@Override
public <E extends Entity> E load(LoadContext<E> context) {
    MetaClass metaClass = metadata.getClassNN(context.getMetaClass());
    String storeName = metadata.getTools().getStoreName(metaClass);
    if (storeName == null) {
        log.debug("Data store for {} is not defined, returning null", metaClass);
        return null;
    }
    DataStore storage = storeFactory.get(storeName);
    E entity = storage.load(context);
    if (entity != null)
        readCrossDataStoreReferences(Collections.singletonList(entity), context.getView(), metaClass);
    return entity;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) Nullable(javax.annotation.Nullable)

Example 64 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DataServiceQueryBuilder method restrictByPreviousResults.

public void restrictByPreviousResults(UUID sessionId, int queryKey) {
    QueryTransformer transformer = QueryTransformerFactory.createTransformer(queryString);
    MetaClass metaClass = metadata.getClassNN(entityName);
    MetaProperty primaryKey = metadata.getTools().getPrimaryKeyProperty(metaClass);
    if (primaryKey == null)
        throw new IllegalStateException(String.format("Entity %s has no primary key", entityName));
    Class type = primaryKey.getJavaType();
    String entityIdField;
    if (UUID.class.equals(type)) {
        entityIdField = "entityId";
    } else if (Long.class.equals(type)) {
        entityIdField = "longEntityId";
    } else if (Integer.class.equals(type)) {
        entityIdField = "intEntityId";
    } else if (String.class.equals(type)) {
        entityIdField = "stringEntityId";
    } else {
        throw new IllegalStateException(String.format("Unsupported primary key type: %s for %s", type.getSimpleName(), entityName));
    }
    transformer.addJoinAndWhere(", sys$QueryResult _qr", String.format("_qr.%s = {E}.%s and _qr.sessionId = :_qr_sessionId and _qr.queryKey = %s", entityIdField, primaryKey.getName(), queryKey));
    queryString = transformer.getResult();
    this.queryParams.put("_qr_sessionId", sessionId);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 65 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class DataServiceQueryBuilder method init.

public void init(String queryString, Map<String, Object> queryParams, String[] noConversionParams, Object id, String entityName) {
    this.entityName = entityName;
    if (!StringUtils.isBlank(queryString)) {
        this.queryString = queryString;
        this.queryParams = queryParams;
        this.noConversionParams = noConversionParams;
    } else {
        MetaClass metaClass = metadata.getClassNN(entityName);
        String pkName = metadata.getTools().getPrimaryKeyName(metaClass);
        if (pkName == null)
            throw new IllegalStateException(String.format("Entity %s has no primary key", entityName));
        this.queryString = "select e from " + entityName + " e where e." + pkName + " = :entityId";
        this.queryParams = new HashMap<>();
        this.queryParams.put("entityId", id);
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10