Search in sources :

Example 1 with FetchPlanProperty

use of io.jmix.core.FetchPlanProperty 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 FetchPlanProperty

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

the class ViewBuilderTest method testMerging.

@Test
public void testMerging() {
    FetchPlan view1 = ViewBuilder.of(Pet.class).add("owner", FetchPlan.LOCAL).build();
    FetchPlan view2 = ViewBuilder.of(Pet.class).addView(view1).add("name").build();
    FetchPlanProperty ownerProp = view2.getProperty("owner");
    assertTrue(ownerProp != null && ownerProp.getFetchPlan() != null);
    assertTrue(ownerProp.getFetchPlan().containsProperty("name"));
}
Also used : FetchPlanProperty(io.jmix.core.FetchPlanProperty) FetchPlan(io.jmix.core.FetchPlan) Pet(com.haulmont.cuba.core.model.Pet) Test(org.junit.jupiter.api.Test) CoreTest(com.haulmont.cuba.core.testsupport.CoreTest)

Example 3 with FetchPlanProperty

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

the class CollectionDsHelper method createProperties.

public static List<MetaPropertyPath> createProperties(FetchPlan fetchPlan, MetaClass metaClass) {
    List<MetaPropertyPath> properties = new ArrayList<>();
    MetadataTools metadataTools = AppBeans.get(MetadataTools.class);
    if (fetchPlan != null && metadataTools.isJpaEntity(metaClass)) {
        for (FetchPlanProperty property : fetchPlan.getProperties()) {
            final String name = property.getName();
            final MetaProperty metaProperty = metaClass.findProperty(name);
            if (metaProperty == null) {
                String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
                throw new DevelopmentException(message);
            }
            if (!metadataTools.isJpa(metaProperty)) {
                String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
                LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
                continue;
            }
            final Range range = metaProperty.getRange();
            if (range == null) {
                continue;
            }
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
        // add all non-persistent properties
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            if (!metadataTools.isJpa(metaProperty)) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    } else {
        if (fetchPlan != null) {
            LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", fetchPlan.getName(), metaClass.getName());
        }
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            final Range range = metaProperty.getRange();
            if (range == null)
                continue;
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    }
    return properties;
}
Also used : MetadataTools(io.jmix.core.MetadataTools) FetchPlanProperty(io.jmix.core.FetchPlanProperty) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Range(io.jmix.core.metamodel.model.Range) DevelopmentException(io.jmix.core.DevelopmentException)

Aggregations

FetchPlanProperty (io.jmix.core.FetchPlanProperty)3 DevelopmentException (io.jmix.core.DevelopmentException)2 FetchPlan (io.jmix.core.FetchPlan)2 Pet (com.haulmont.cuba.core.model.Pet)1 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)1 MetadataTools (io.jmix.core.MetadataTools)1 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 Range (io.jmix.core.metamodel.model.Range)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1