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"));
}
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;
}
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;
}
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();
}
}
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();
}
Aggregations