mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-05 04:38:10 +08:00
Refs #1688 Rewrite MetadataTools.isPersistent & isTransient
This commit is contained in:
parent
418852ea89
commit
72c2db2266
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 10.12.2008 12:42:44
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core;
|
||||
|
||||
import com.haulmont.chile.core.model.Session;
|
||||
import com.haulmont.chile.core.model.MetaModel;
|
||||
import com.haulmont.chile.core.model.utils.PrintUtils;
|
||||
import com.haulmont.cuba.core.global.MetadataProvider;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class MetadataProviderTest extends CubaTestCase
|
||||
{
|
||||
public void test() {
|
||||
Session session = MetadataProvider.getSession();
|
||||
assertNotNull(session);
|
||||
|
||||
Collection<MetaModel> models = session.getModels();
|
||||
for (MetaModel model : models) {
|
||||
System.out.println("Model: " + model.getName());
|
||||
System.out.println(PrintUtils.printClassHierarchy(model));
|
||||
}
|
||||
}
|
||||
}
|
65
modules/core/test/com/haulmont/cuba/core/MetadataTest.java
Normal file
65
modules/core/test/com/haulmont/cuba/core/MetadataTest.java
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 10.12.2008 12:42:44
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core;
|
||||
|
||||
import com.haulmont.chile.core.model.MetaClass;
|
||||
import com.haulmont.chile.core.model.MetaModel;
|
||||
import com.haulmont.chile.core.model.Session;
|
||||
import com.haulmont.chile.core.model.utils.PrintUtils;
|
||||
import com.haulmont.cuba.core.entity.Folder;
|
||||
import com.haulmont.cuba.core.global.AppBeans;
|
||||
import com.haulmont.cuba.core.global.Metadata;
|
||||
import com.haulmont.cuba.core.global.MetadataTools;
|
||||
import com.haulmont.cuba.security.entity.EntityLogItem;
|
||||
import com.haulmont.cuba.security.entity.User;
|
||||
import com.haulmont.cuba.security.entity.UserSessionEntity;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class MetadataTest extends CubaTestCase
|
||||
{
|
||||
public void test() {
|
||||
Session session = AppBeans.get(Metadata.class).getSession();
|
||||
assertNotNull(session);
|
||||
|
||||
Collection<MetaModel> models = session.getModels();
|
||||
for (MetaModel model : models) {
|
||||
System.out.println("Model: " + model.getName());
|
||||
System.out.println(PrintUtils.printClassHierarchy(model));
|
||||
}
|
||||
}
|
||||
|
||||
public void testPersistentAndTransientProperties() throws Exception {
|
||||
Metadata metadata = AppBeans.get(Metadata.class);
|
||||
MetadataTools tools = metadata.getTools();
|
||||
|
||||
// User
|
||||
MetaClass metaClass = metadata.getSession().getClassNN(User.class);
|
||||
assertTrue(tools.isPersistent(metaClass.getPropertyNN("login")));
|
||||
assertTrue(tools.isPersistent(metaClass.getPropertyNN("group")));
|
||||
assertTrue(tools.isPersistent(metaClass.getPropertyNN("userRoles")));
|
||||
|
||||
// EntityLogItem
|
||||
metaClass = metadata.getSession().getClassNN(EntityLogItem.class);
|
||||
assertTrue(tools.isPersistent(metaClass.getPropertyNN("user")));
|
||||
assertFalse(tools.isPersistent(metaClass.getPropertyNN("attributes")));
|
||||
assertTrue(tools.isTransient(metaClass.getPropertyNN("attributes")));
|
||||
|
||||
// Folder
|
||||
metaClass = metadata.getSession().getClassNN(Folder.class);
|
||||
assertTrue(tools.isPersistent(metaClass.getPropertyNN("name")));
|
||||
assertTrue(tools.isTransient(new Folder(), "itemStyle"));
|
||||
|
||||
// UserSessionEntity
|
||||
metaClass = metadata.getSession().getClassNN(UserSessionEntity.class);
|
||||
assertTrue(tools.isTransient(metaClass.getPropertyNN("login")));
|
||||
}
|
||||
}
|
@ -224,6 +224,9 @@ public class JPAAnnotationsLoader extends ChileAnnotationsLoader implements Clas
|
||||
|
||||
@Override
|
||||
protected void onPropertyLoaded(MetaProperty metaProperty, Field field) {
|
||||
if (isPersistent(field))
|
||||
metaProperty.getAnnotations().put("persistent", true);
|
||||
|
||||
Column column = field.getAnnotation(Column.class);
|
||||
if (column != null && column.length() != 0) {
|
||||
metaProperty.getAnnotations().put("length", column.length());
|
||||
@ -234,4 +237,13 @@ public class JPAAnnotationsLoader extends ChileAnnotationsLoader implements Clas
|
||||
metaProperty.getAnnotations().put("temporal", temporal.value());
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isPersistent(Field field) {
|
||||
return field.isAnnotationPresent(Column.class)
|
||||
|| field.isAnnotationPresent(OneToOne.class)
|
||||
|| field.isAnnotationPresent(OneToMany.class)
|
||||
|| field.isAnnotationPresent(ManyToOne.class)
|
||||
|| field.isAnnotationPresent(ManyToMany.class)
|
||||
|| field.isAnnotationPresent(Embedded.class);
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,7 @@ public class MetadataTools {
|
||||
* owning object. This is true if the property is ManyToMany, or if it is OneToMany with certain CasacdeType defined.
|
||||
*/
|
||||
public boolean isCascade(MetaProperty metaProperty) {
|
||||
Objects.requireNonNull(metaProperty, "metaProperty is null");
|
||||
OneToMany oneToMany = metaProperty.getAnnotatedElement().getAnnotation(OneToMany.class);
|
||||
if (oneToMany != null) {
|
||||
final Collection<CascadeType> cascadeTypes = Arrays.asList(oneToMany.cascade());
|
||||
@ -114,6 +115,7 @@ public class MetadataTools {
|
||||
* {@link BaseEntity}, {@link Updatable}, {@link SoftDelete}, {@link Versioned}
|
||||
*/
|
||||
public boolean isSystem(MetaProperty metaProperty) {
|
||||
Objects.requireNonNull(metaProperty, "metaProperty is null");
|
||||
String name = metaProperty.getName();
|
||||
for (String property : BaseEntity.PROPERTIES) {
|
||||
if (name.equals(property))
|
||||
@ -139,6 +141,7 @@ public class MetadataTools {
|
||||
* @see #isPersistent(com.haulmont.chile.core.model.MetaProperty)
|
||||
*/
|
||||
public boolean isPersistent(MetaPropertyPath metaPropertyPath) {
|
||||
Objects.requireNonNull(metaPropertyPath, "metaPropertyPath is null");
|
||||
for (MetaProperty metaProperty : metaPropertyPath.getMetaProperties()) {
|
||||
if (!isPersistent(metaProperty))
|
||||
return false;
|
||||
@ -150,25 +153,31 @@ public class MetadataTools {
|
||||
* Determine whether the given property is persistent, that is stored in the database.
|
||||
*/
|
||||
public boolean isPersistent(MetaProperty metaProperty) {
|
||||
return !metaProperty.getAnnotatedElement().isAnnotationPresent(
|
||||
com.haulmont.chile.core.annotations.MetaProperty.class);
|
||||
Objects.requireNonNull(metaProperty, "metaProperty is null");
|
||||
return Boolean.TRUE.equals(metaProperty.getAnnotations().get("persistent"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given property is transient, that is not stored in the database.
|
||||
* <p/>Unlike {@link #isTransient(com.haulmont.chile.core.model.MetaProperty)} for objects and properties not
|
||||
* registered in metadata this method returns <code>true</code>.
|
||||
* @param object entity instance
|
||||
* @param property property name
|
||||
*/
|
||||
public boolean isTransient(Object object, String property) {
|
||||
return isAnnotationPresent(object, property, Transient.class);
|
||||
Objects.requireNonNull(object, "object is null");
|
||||
MetaClass metaClass = metadata.getSession().getClass(object.getClass());
|
||||
if (metaClass == null)
|
||||
return true;
|
||||
MetaProperty metaProperty = metaClass.getProperty(property);
|
||||
return metaProperty == null || !isPersistent(metaProperty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given property is transient, that is not stored in the database.
|
||||
*/
|
||||
public boolean isTransient(MetaProperty metaProperty) {
|
||||
boolean isMetaProperty = metaProperty.getAnnotatedElement().isAnnotationPresent(com.haulmont.chile.core.annotations.MetaProperty.class);
|
||||
return isMetaProperty || metaProperty.getAnnotatedElement().isAnnotationPresent(Transient.class);
|
||||
return !isPersistent(metaProperty);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,7 +185,8 @@ public class MetadataTools {
|
||||
* @see Embedded
|
||||
*/
|
||||
public boolean isEmbedded(MetaProperty metaProperty) {
|
||||
return metaProperty.getAnnotatedElement().getAnnotation(Embedded.class) != null;
|
||||
Objects.requireNonNull(metaProperty, "metaProperty is null");
|
||||
return metaProperty.getAnnotatedElement().isAnnotationPresent(Embedded.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user