Search in sources :

Example 1 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class PropertyDatasourceImpl method getView.

@Override
public FetchPlan getView() {
    if (view == null) {
        MetaClass metaMetaClass = masterDs.getMetaClass();
        if (metadata.getTools().isJpaEntity(metaMetaClass) || metadata.getTools().isJpaEmbeddable(metaMetaClass)) {
            FetchPlan masterView = masterDs.getView();
            if (masterView == null) {
                throw new DevelopmentException("No view for datasource " + masterDs.getId(), ParamsMap.of("masterDs", masterDs.getId(), "propertyDs", getId()));
            }
            FetchPlanProperty property = masterView.getProperty(metaProperty.getName());
            if (property == null) {
                return null;
            }
            if (property.getFetchPlan() == null) {
                throw new DevelopmentException(String.format("Invalid view definition: %s. Property '%s' must have a view", masterView, property), ParamsMap.of("masterDs", masterDs.getId(), "propertyDs", getId(), "masterView", masterView, "property", property));
            }
            view = metadata.getViewRepository().getView(getMetaClass(), property.getFetchPlan().getName());
            // anonymous (nameless) view
            if (view == null)
                view = property.getFetchPlan();
        }
    }
    return view;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) FetchPlanProperty(io.jmix.core.FetchPlanProperty) FetchPlan(io.jmix.core.FetchPlan) DevelopmentException(io.jmix.core.DevelopmentException)

Example 2 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class MultiEntityDataLoader method loadData.

@Override
public List<Map<String, Object>> loadData(ReportQuery dataSet, BandData parentBand, Map<String, Object> params) {
    Map<String, Object> additionalParams = dataSet.getAdditionalParams();
    String paramName = (String) additionalParams.get(DataSet.LIST_ENTITIES_PARAM_NAME);
    if (StringUtils.isBlank(paramName)) {
        paramName = DEFAULT_LIST_ENTITIES_PARAM_NAME;
    }
    boolean hasNestedCollection = paramName.contains(NESTED_COLLECTION_SEPARATOR);
    String entityParameterName = StringUtils.substringBefore(paramName, NESTED_COLLECTION_SEPARATOR);
    String nestedCollectionName = StringUtils.substringAfter(paramName, NESTED_COLLECTION_SEPARATOR);
    FetchPlan nestedCollectionFetchPLan = null;
    dataSet = ProxyWrapper.unwrap(dataSet);
    Object entities = null;
    if (params.containsKey(paramName)) {
        entities = params.get(paramName);
    } else if (hasNestedCollection && params.containsKey(entityParameterName)) {
        Entity entity = (Entity) params.get(entityParameterName);
        entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
        if (entity != null) {
            entities = EntityValues.getValueEx(entity, nestedCollectionName);
            if (dataSet instanceof DataSet) {
                FetchPlan entityFetchPlan = getFetchPlan(entity, (DataSet) dataSet);
                if (entityFetchPlan != null && entityFetchPlan.getProperty(nestedCollectionName) != null) {
                    // noinspection ConstantConditions
                    nestedCollectionFetchPLan = entityFetchPlan.getProperty(nestedCollectionName).getFetchPlan();
                }
            }
        }
    }
    if (!(entities instanceof Collection)) {
        if (hasNestedCollection) {
            throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter, " + "or the entity does not contain nested collection '%s'", entityParameterName, nestedCollectionName));
        } else {
            throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter or it has type other than collection", paramName));
        }
    }
    Collection<Entity> entitiesList = (Collection) entities;
    params.put(paramName, entitiesList);
    List<Map<String, Object>> resultList = new ArrayList<>();
    for (Entity entity : entitiesList) {
        if (!hasNestedCollection) {
            entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
        }
        if (dataSet instanceof DataSet) {
            if (hasNestedCollection) {
                if (nestedCollectionFetchPLan != null) {
                    resultList.add(new EntityMap(entity, nestedCollectionFetchPLan, beanFactory));
                } else {
                    resultList.add(new EntityMap(entity, beanFactory));
                }
            } else {
                resultList.add(new EntityMap(entity, getFetchPlan(entity, (DataSet) dataSet), beanFactory));
            }
        } else {
            resultList.add(new EntityMap(entity, beanFactory));
        }
    }
    return resultList;
}
Also used : Entity(io.jmix.core.Entity) DataSet(io.jmix.reports.entity.DataSet) ArrayList(java.util.ArrayList) FetchPlan(io.jmix.core.FetchPlan) EntityMap(io.jmix.reports.app.EntityMap) Collection(java.util.Collection) EntityMap(io.jmix.reports.app.EntityMap) Map(java.util.Map)

Example 3 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class ImapDataProvider method saveAttachments.

public void saveAttachments(ImapMessage msg, Collection<ImapMessageAttachment> attachments) {
    SaveContext saveContext = new SaveContext();
    FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(ImapMessageAttachment.class, "imap-msg-attachment-full");
    attachments.forEach(attachment -> {
        attachment.setImapMessage(msg);
        saveContext.saving(attachment, fetchPlan);
    });
    msg.setAttachmentsLoaded(true);
    saveContext.saving(msg, fetchPlanRepository.getFetchPlan(ImapMessage.class, "imap-msg-full"));
    dataManager.save(saveContext);
}
Also used : SaveContext(io.jmix.core.SaveContext) FetchPlan(io.jmix.core.FetchPlan)

Example 4 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class BulkEditorWindow method createView.

/**
 * Creates a view, loading only necessary properties.
 * Referenced entities will be loaded with a MINIMAL view.
 *
 * @param meta meta class
 * @return View instance
 */
protected View createView(MetaClass meta) {
    View view = new View(meta.getJavaClass(), false);
    for (MetaProperty metaProperty : meta.getProperties()) {
        if (!managedFields.containsKey(metaProperty.getName()) && !managedEmbeddedProperties.contains(metaProperty.getName())) {
            continue;
        }
        switch(metaProperty.getType()) {
            case DATATYPE:
            case ENUM:
                view.addProperty(metaProperty.getName());
                break;
            case EMBEDDED:
                {
                    // build view for embedded property
                    FetchPlan propView = createEmbeddedView(metaProperty.getRange().asClass(), metaProperty.getName());
                    view.addProperty(metaProperty.getName(), propView);
                    break;
                }
            case ASSOCIATION:
            case COMPOSITION:
                FetchPlan propView = viewRepository.getView(metaProperty.getRange().asClass(), View.MINIMAL);
                // in some cases JPA loads extended entities as instance of base class which leads to ClassCastException
                // loading property lazy prevents this from happening
                view.addProperty(metaProperty.getName(), propView);
                break;
            default:
                throw new IllegalStateException("unknown property type");
        }
    }
    return view;
}
Also used : MetaProperty(io.jmix.core.metamodel.model.MetaProperty) FetchPlan(io.jmix.core.FetchPlan)

Example 5 with FetchPlan

use of io.jmix.core.FetchPlan in project jmix by jmix-framework.

the class BulkEditorWindow method createEmbeddedView.

protected View createEmbeddedView(MetaClass meta, String fqnPrefix) {
    View view = new View(meta.getJavaClass(), false);
    for (MetaProperty metaProperty : meta.getProperties()) {
        String fqn = fqnPrefix + "." + metaProperty.getName();
        if (!managedFields.containsKey(fqn)) {
            continue;
        }
        switch(metaProperty.getType()) {
            case DATATYPE:
            case ENUM:
                view.addProperty(metaProperty.getName());
                break;
            case EMBEDDED:
                {
                    FetchPlan propView = createEmbeddedView(metaProperty.getRange().asClass(), fqn);
                    view.addProperty(metaProperty.getName(), propView);
                    break;
                }
            case ASSOCIATION:
            case COMPOSITION:
                FetchPlan propView = viewRepository.getView(metaProperty.getRange().asClass(), View.MINIMAL);
                // in some cases JPA loads extended entities as instance of base class which leads to ClassCastException
                // loading property lazy prevents this from happening
                view.addProperty(metaProperty.getName(), propView);
                break;
            default:
                throw new IllegalStateException("unknown property type");
        }
    }
    return view;
}
Also used : MetaProperty(io.jmix.core.metamodel.model.MetaProperty) FetchPlan(io.jmix.core.FetchPlan)

Aggregations

FetchPlan (io.jmix.core.FetchPlan)61 Test (org.junit.jupiter.api.Test)48 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)45 View (com.haulmont.cuba.core.global.View)35 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)10 Pet (com.haulmont.cuba.core.model.Pet)5 SoftDeleteOneToOneA (com.haulmont.cuba.core.model.SoftDeleteOneToOneA)5 Group (com.haulmont.cuba.core.model.common.Group)5 User (com.haulmont.cuba.core.model.common.User)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 DataManager (com.haulmont.cuba.core.global.DataManager)3 LoadContext (com.haulmont.cuba.core.global.LoadContext)3 FetchPlanProperty (io.jmix.core.FetchPlanProperty)3 Metadata (io.jmix.core.Metadata)3 MetadataTools (io.jmix.core.MetadataTools)3 MetaClass (io.jmix.core.metamodel.model.MetaClass)3 SoftDeleteOneToOneB (com.haulmont.cuba.core.model.SoftDeleteOneToOneB)2 Permission (com.haulmont.cuba.core.model.common.Permission)2 QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 Entity (io.jmix.core.Entity)2