← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16875: use Hibernate sessionFactory to set persisted properties on Schema/Property instances, makes it v...

 

------------------------------------------------------------
revno: 16875
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-09-30 14:02:57 +0700
message:
  use Hibernate sessionFactory to set persisted properties on Schema/Property instances, makes it very easy to see what properties are actually persisted in schema endpoint (/api/shemas), will also be used for fields to give persisted fields only.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2014-08-16 15:11:14 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java	2014-09-30 07:02:57 +0000
@@ -85,10 +85,10 @@
 
     /**
      * Is this property persisted somewhere. This property will be used to create criteria queries
-     * on demand (default: true)
+     * on demand (default: false)
      */
     @NodeSimple
-    private boolean persisted = true;
+    private boolean persisted;
 
     /**
      * Name of collection wrapper.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java	2014-08-26 10:41:03 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java	2014-09-30 07:02:57 +0000
@@ -117,6 +117,11 @@
     private boolean metadata;
 
     /**
+     * Are any properties on this class being persisted, if false, this file does not have any hbm file attached to it.
+     */
+    private boolean persisted;
+
+    /**
      * List of authorities required for doing operations on this class.
      */
     private List<Authority> authorities = Lists.newArrayList();
@@ -284,6 +289,18 @@
     }
 
     @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public boolean isPersisted()
+    {
+        return persisted;
+    }
+
+    public void setPersisted( boolean persisted )
+    {
+        this.persisted = persisted;
+    }
+
+    @JsonProperty
     @JacksonXmlElementWrapper( localName = "authorities", namespace = DxfNamespaces.DXF_2_0 )
     @JacksonXmlProperty( localName = "authority", namespace = DxfNamespaces.DXF_2_0 )
     public List<Authority> getAuthorities()

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2014-08-26 10:41:03 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultSchemaService.java	2014-09-30 07:02:57 +0000
@@ -32,6 +32,7 @@
 import com.google.common.base.CaseFormat;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.hibernate.SessionFactory;
 import org.hisp.dhis.system.util.ReflectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.OrderComparator;
@@ -58,12 +59,21 @@
     @Autowired
     private List<SchemaDescriptor> descriptors = Lists.newArrayList();
 
+    @Autowired
+    private SessionFactory sessionFactory;
+
     @PostConstruct
     public void init()
     {
         for ( SchemaDescriptor descriptor : descriptors )
         {
             Schema schema = descriptor.getSchema();
+
+            if ( sessionFactory.getClassMetadata( schema.getKlass() ) != null )
+            {
+                schema.setPersisted( true );
+            }
+
             schema.setDisplayName( beautify( schema.getName() ) );
 
             if ( schema.getProperties().isEmpty() )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2014-06-15 11:00:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2014-09-30 07:02:57 +0000
@@ -35,11 +35,14 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.hibernate.SessionFactory;
+import org.hibernate.metadata.ClassMetadata;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.annotation.Description;
 import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.system.util.ReflectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StringUtils;
 
 import java.lang.reflect.Method;
@@ -57,9 +60,15 @@
  */
 public class Jackson2PropertyIntrospectorService extends AbstractPropertyIntrospectorService
 {
+    @Autowired
+    private SessionFactory sessionFactory;
+
     protected Map<String, Property> scanClass( Class<?> clazz )
     {
         Map<String, Property> propertyMap = Maps.newHashMap();
+        ClassMetadata classMetadata = sessionFactory.getClassMetadata( clazz );
+        List<String> classPropertyNames = Lists.newArrayList( classMetadata.getPropertyNames() );
+        List<String> classFieldNames = ReflectionUtils.getAllFieldNames( clazz );
 
         // TODO this is quite nasty, should find a better way of exposing properties at class-level
         if ( clazz.isAnnotationPresent( JacksonXmlRootElement.class ) )
@@ -88,29 +97,17 @@
             Method method = property.getGetterMethod();
             JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
 
-            String name = jsonProperty.value();
+            String fieldName = getFieldName( method );
+            property.setName( !StringUtils.isEmpty( jsonProperty.value() ) ? jsonProperty.value() : fieldName );
+            property.setReadable( true );
 
-            if ( StringUtils.isEmpty( name ) )
+            if ( classFieldNames.contains( fieldName ) )
             {
-                String[] getters = new String[]{
-                    "is", "has", "get"
-                };
-
-                name = method.getName();
-
-                for ( String getter : getters )
-                {
-                    if ( name.startsWith( getter ) )
-                    {
-                        name = name.substring( getter.length() );
-                    }
-                }
-
-                name = StringUtils.uncapitalize( name );
+                property.setFieldName( fieldName );
+                property.setPersisted( classPropertyNames.contains( property.getFieldName() ) );
+                property.setWritable( property.isPersisted() );
             }
 
-            property.setName( name );
-
             if ( method.isAnnotationPresent( Description.class ) )
             {
                 Description description = method.getAnnotation( Description.class );
@@ -141,7 +138,7 @@
 
                 if ( StringUtils.isEmpty( jacksonXmlProperty.localName() ) )
                 {
-                    property.setName( name );
+                    property.setName( property.getName() );
                 }
                 else
                 {
@@ -167,7 +164,7 @@
                 }
             }
 
-            propertyMap.put( name, property );
+            propertyMap.put( property.getName(), property );
 
             Class<?> returnType = method.getReturnType();
             property.setKlass( returnType );
@@ -212,6 +209,27 @@
         return propertyMap;
     }
 
+    private String getFieldName( Method method )
+    {
+        String name;
+
+        String[] getters = new String[]{
+            "is", "has", "get"
+        };
+
+        name = method.getName();
+
+        for ( String getter : getters )
+        {
+            if ( name.startsWith( getter ) )
+            {
+                name = name.substring( getter.length() );
+            }
+        }
+
+        return StringUtils.uncapitalize( name );
+    }
+
     private static List<Property> collectProperties( Class<?> klass )
     {
         List<Method> allMethods = ReflectionUtils.getAllMethods( klass );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-09-30 07:02:57 +0000
@@ -357,6 +357,19 @@
         return fields;
     }
 
+    public static List<String> getAllFieldNames( Class<?> klass )
+    {
+        List<Field> fields = getAllFields( klass );
+        List<String> fieldNames = new ArrayList<>();
+
+        for ( Field field : fields )
+        {
+            fieldNames.add( field.getName() );
+        }
+
+        return fieldNames;
+    }
+
     private static Method _findMethod( Class<?> clazz, String name )
     {
         return _findMethod( clazz, name, new Class[0] );
@@ -426,7 +439,7 @@
         }
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public static <T> T getFieldObject( Field field, T target )
     {
         return (T) invokeGetterMethod( field.getName(), target );