Search in sources :

Example 1 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class AbstractViewRepositoryTest method intersectPropertiesExtendedViewsOneLevel.

@Test
public void intersectPropertiesExtendedViewsOneLevel() {
    View view = metadata.getViewRepository().getView(testDetailEntity, "intersectViewOne");
    assertTrue(view.containsProperty("embeddable"));
    assertTrue(view.containsProperty("parts"));
}
Also used : View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 2 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class RelatedEntitiesServiceBean method getRelatedIds.

@SuppressWarnings("unchecked")
@Override
public List<Object> getRelatedIds(List<Object> parentIds, String parentMetaClass, String relationProperty) {
    checkNotNullArgument(parentIds, "parents argument is null");
    checkNotNullArgument(parentMetaClass, "parentMetaClass argument is null");
    checkNotNullArgument(relationProperty, "relationProperty argument is null");
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(metadata.getClassNN(parentMetaClass));
    Class parentClass = metaClass.getJavaClass();
    MetaProperty metaProperty = metaClass.getPropertyNN(relationProperty);
    // return empty list only after all argument checks
    if (parentIds.isEmpty()) {
        return Collections.emptyList();
    }
    MetaClass propertyMetaClass = extendedEntities.getEffectiveMetaClass(metaProperty.getRange().asClass());
    Class propertyClass = propertyMetaClass.getJavaClass();
    List<Object> relatedIds = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String parentPrimaryKey = metadata.getTools().getPrimaryKeyName(metaClass);
        String queryString = "select x from " + parentMetaClass + " x where x." + parentPrimaryKey + " in :ids";
        Query query = em.createQuery(queryString);
        String relatedPrimaryKey = metadata.getTools().getPrimaryKeyName(propertyMetaClass);
        View view = new View(parentClass);
        view.addProperty(relationProperty, new View(propertyClass).addProperty(relatedPrimaryKey));
        query.setView(view);
        query.setParameter("ids", parentIds);
        List<Entity> resultList = query.getResultList();
        for (Entity e : resultList) {
            Object value = e.getValue(relationProperty);
            if (value instanceof Entity) {
                relatedIds.add(((Entity) value).getId());
            } else if (value instanceof Collection) {
                for (Object collectionItem : (Collection) value) {
                    relatedIds.add(((Entity) collectionItem).getId());
                }
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return relatedIds;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) View(com.haulmont.cuba.core.global.View) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 3 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class QueryCacheManager method getResultListFromCache.

/**
 * Get query results from query cache by specified {@code queryKey}
 */
@SuppressWarnings("unchecked")
public <T> List<T> getResultListFromCache(QueryKey queryKey, List<View> views) {
    log.debug("Looking for query in cache: {}", queryKey.printDescription());
    List<T> resultList = null;
    QueryResult queryResult = queryCache.get(queryKey);
    if (queryResult != null) {
        MetaClass metaClass = metadata.getClassNN(queryResult.getType());
        String storeName = metadata.getTools().getStoreName(metaClass);
        EntityManager em = persistence.getEntityManager(storeName);
        resultList = new ArrayList<>(queryResult.getResult().size());
        if (!metadata.getTools().isCacheable(metaClass)) {
            log.warn("Using cacheable query without entity cache for {}", queryResult.getType());
        }
        for (Object id : queryResult.getResult()) {
            resultList.add((T) em.find(metaClass.getJavaClass(), id, views.toArray(new View[views.size()])));
        }
    } else {
        log.debug("Query results are not found in cache: {}", queryKey.printDescription());
    }
    return resultList;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) MetadataObject(com.haulmont.chile.core.model.MetadataObject) View(com.haulmont.cuba.core.global.View)

Example 4 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class EclipseLinkQueriesTest method testJoinOnWithToManyView.

// join on, view contains ToMany attribute
@Test
public void testJoinOnWithToManyView() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        View view = new View(Group.class).addProperty("constraints");
        TypedQuery<Group> query = em.createQuery("select g from sec$Group g join sys$QueryResult qr on qr.entityId = g.id where qr.queryKey = 1", Group.class);
        query.setView(view);
        List<Group> result = query.getResultList();
        tx.commit();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 5 with View

use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.

the class GetReferenceIdTest method testWithFetchGroup.

@Test
public void testWithFetchGroup() throws Exception {
    User user = null;
    // not in a view
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(new View(User.class, false).addProperty("login").addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))).setLoadPartialEntities(true));
        q.setParameter(1, this.user.getId());
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            PersistenceTools.RefId refId = cont.persistence().getTools().getReferenceId(user, "group");
            assertFalse(refId.isLoaded());
            try {
                refId.getValue();
                fail();
            } catch (IllegalStateException e) {
            // ok
            }
        }
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
    // in a view
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(new View(User.class, false).addProperty("login").addProperty("group", new View(Group.class).addProperty("name")).addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))).setLoadPartialEntities(true));
        q.setParameter(1, this.user.getId());
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            PersistenceTools.RefId refId = cont.persistence().getTools().getReferenceId(user, "group");
            assertTrue(refId.isLoaded());
            assertEquals(TestSupport.COMPANY_GROUP_ID, refId.getValue());
        }
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Aggregations

View (com.haulmont.cuba.core.global.View)55 Test (org.junit.Test)24 EntityManager (com.haulmont.cuba.core.EntityManager)22 Transaction (com.haulmont.cuba.core.Transaction)21 User (com.haulmont.cuba.security.entity.User)19 Group (com.haulmont.cuba.security.entity.Group)10 MetaClass (com.haulmont.chile.core.model.MetaClass)5 ViewProperty (com.haulmont.cuba.core.global.ViewProperty)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Entity (com.haulmont.cuba.core.entity.Entity)2 ViewRepository (com.haulmont.cuba.core.global.ViewRepository)2 Role (com.haulmont.cuba.security.entity.Role)2 SoftDeleteOneToOneB (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)1 QueryResult (com.haulmont.cuba.core.entity.QueryResult)1 Server (com.haulmont.cuba.core.entity.Server)1