use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class ViewHelper method intersectViews.
public static View intersectViews(View first, View second) {
if (first == null)
throw new IllegalArgumentException("View is null");
if (second == null)
throw new IllegalArgumentException("View is null");
View resultView = new View(first.getEntityClass());
Collection<ViewProperty> firstProps = first.getProperties();
for (ViewProperty firstProperty : firstProps) {
if (second.containsProperty(firstProperty.getName())) {
View resultPropView = null;
ViewProperty secondProperty = second.getProperty(firstProperty.getName());
if ((firstProperty.getView() != null) && (secondProperty.getView() != null)) {
resultPropView = intersectViews(firstProperty.getView(), secondProperty.getView());
}
resultView.addProperty(firstProperty.getName(), resultPropView);
}
}
return resultView;
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class EntityFactory method build.
@Override
public Object build(String string) {
if (StringUtils.isBlank(string)) {
return null;
}
EntityLoadInfo info = EntityLoadInfo.parse(string);
if (info == null) {
throw new IllegalArgumentException("Invalid entity info: " + string);
}
Entity entity;
String property = AppContext.getProperty("cuba.useCurrentTxForConfigEntityLoad");
Transaction tx = Boolean.valueOf(property) ? persistence.getTransaction() : persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
View view = null;
if (info.getViewName() != null) {
view = metadata.getViewRepository().getView(info.getMetaClass(), info.getViewName());
}
Class javaClass = info.getMetaClass().getJavaClass();
if (view != null) {
entity = em.find(javaClass, info.getId(), view);
} else {
entity = em.find(javaClass, info.getId());
}
tx.commit();
} finally {
tx.end();
}
return entity;
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class EntityManagerTest method testReloadSameTx.
@Test
public void testReloadSameTx() throws Exception {
User originalUser, reloadedUser;
View view = new View(User.class, false).addProperty("name");
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
originalUser = em.find(User.class, userId, view);
assertNotNull(originalUser);
reloadedUser = em.reload(originalUser, "user.edit");
tx.commit();
} finally {
tx.end();
}
assertTrue(originalUser == reloadedUser);
reloadedUser = reserialize(reloadedUser);
assertNotNull(reloadedUser);
assertNotNull(reloadedUser.getLogin());
assertTrue(PersistenceHelper.isLoaded(reloadedUser, "userRoles"));
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class EntityManagerTest method testFindCombinedView.
@Test
public void testFindCombinedView() throws Exception {
View view1 = new View(User.class, false).addProperty("name").addProperty("login");
View view2 = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
User user1;
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.persistence().getEntityManager();
user1 = em.find(User.class, userId, view1, view2);
tx.commit();
}
user1 = reserialize(user1);
assertNotNull(user1);
assertNotNull(user1.getCreatedBy());
assertNotNull(user1.getGroup());
}
use of com.haulmont.cuba.core.global.View in project cuba by cuba-platform.
the class SerializationTest method testManagedSerialization.
@Test
public void testManagedSerialization() throws Exception {
View view = getView();
User user;
User kryoUser;
User standardUser;
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
user = em.find(User.class, userId, view);
assertNotNull(user);
KryoSerialization kryoSerialization = new KryoSerialization();
kryoUser = (User) kryoSerialization.deserialize(kryoSerialization.serialize(user));
tx.commit();
} finally {
tx.end();
}
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
user = em.find(User.class, userId, view);
assertNotNull(user);
StandardSerialization standardSerialization = new StandardSerialization();
standardUser = (User) standardSerialization.deserialize(standardSerialization.serialize(user));
tx.commit();
} finally {
tx.end();
}
assertEquals(standardUser, kryoUser);
assertEquals(standardUser.getLogin(), kryoUser.getLogin());
assertEquals(standardUser.getName(), kryoUser.getName());
assertEquals(standardUser.getPosition(), kryoUser.getPosition());
assertEquals(standardUser.getGroup(), kryoUser.getGroup());
assertEquals(standardUser.getGroup().getName(), kryoUser.getGroup().getName());
assertEquals(standardUser.getUserRoles().get(0), kryoUser.getUserRoles().get(0));
assertEquals(standardUser.getUserRoles().get(0).getRole(), kryoUser.getUserRoles().get(0).getRole());
assertEquals(standardUser.getUserRoles().get(0).getRole().getName(), kryoUser.getUserRoles().get(0).getRole().getName());
}
Aggregations