← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21104: minor AttributeService method fixes, make sure that object that is connected to the attribute val...

 

------------------------------------------------------------
revno: 21104
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-11-18 12:35:33 +0700
message:
  minor AttributeService method fixes, make sure that object that is connected to the attribute value is of supported type for add/update AV
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/Attribute.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeStoreTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.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/attribute/Attribute.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/Attribute.java	2015-11-18 04:39:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/Attribute.java	2015-11-18 05:35:33 +0000
@@ -40,9 +40,27 @@
 import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.common.view.ExportView;
+import org.hisp.dhis.dataelement.CategoryOptionGroup;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
+import org.hisp.dhis.dataelement.DataElementGroup;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.indicator.IndicatorGroup;
 import org.hisp.dhis.option.OptionSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.trackedentity.TrackedEntity;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserGroup;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -390,6 +408,31 @@
         this.attributeValues = attributeValues;
     }
 
+    public List<Class<? extends IdentifiableObject>> getSupportedClasses()
+    {
+        List<Class<? extends IdentifiableObject>> klasses = new ArrayList<>();
+
+        if ( dataElementAttribute ) klasses.add( DataElement.class );
+        if ( dataElementGroupAttribute ) klasses.add( DataElementGroup.class );
+        if ( categoryOptionAttribute ) klasses.add( DataElementCategoryOption.class );
+        if ( categoryOptionGroupAttribute ) klasses.add( CategoryOptionGroup.class );
+        if ( indicatorAttribute ) klasses.add( Indicator.class );
+        if ( indicatorGroupAttribute ) klasses.add( IndicatorGroup.class );
+        if ( dataSetAttribute ) klasses.add( DataSet.class );
+        if ( organisationUnitAttribute ) klasses.add( OrganisationUnit.class );
+        if ( organisationUnitGroupAttribute ) klasses.add( OrganisationUnitGroup.class );
+        if ( organisationUnitGroupSetAttribute ) klasses.add( OrganisationUnitGroupSet.class );
+        if ( userAttribute ) klasses.add( User.class );
+        if ( userGroupAttribute ) klasses.add( UserGroup.class );
+        if ( programAttribute ) klasses.add( Program.class );
+        if ( programStageAttribute ) klasses.add( ProgramStage.class );
+        if ( trackedEntityAttribute ) klasses.add( TrackedEntity.class );
+        if ( trackedEntityAttributeAttribute ) klasses.add( TrackedEntityAttribute.class );
+
+        return klasses;
+    }
+
+
     @Override
     public void mergeWith( IdentifiableObject other, MergeStrategy strategy )
     {

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java	2015-08-19 05:26:31 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeService.java	2015-11-18 05:35:33 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObject;
+
 import java.util.List;
 
 /**
@@ -240,14 +242,14 @@
      *
      * @param attributeValue the attribute value.
      */
-    void addAttributeValue( AttributeValue attributeValue );
+    <T extends IdentifiableObject> void addAttributeValue( T object, AttributeValue attributeValue );
 
     /**
      * Updates an attribute value.
      *
      * @param attributeValue the attribute value.
      */
-    void updateAttributeValue( AttributeValue attributeValue );
+    <T extends IdentifiableObject> void updateAttributeValue( T object, AttributeValue attributeValue );
 
     /**
      * Deletes an attribute value.

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java	2015-11-17 07:00:29 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java	2015-11-18 05:35:33 +0000
@@ -72,8 +72,7 @@
 
     public AttributeValue()
     {
-        this.created = new Date();
-        this.lastUpdated = new Date();
+        setAutoFields();
     }
 
     public AttributeValue( String value )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java	2015-11-17 04:18:36 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/attribute/DefaultAttributeService.java	2015-11-18 05:35:33 +0000
@@ -28,7 +28,11 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.i18n.I18nService;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
@@ -68,6 +72,9 @@
         i18nService = service;
     }
 
+    @Autowired
+    private SchemaService schemaService;
+
     // -------------------------------------------------------------------------
     // Attribute implementation
     // -------------------------------------------------------------------------
@@ -243,16 +250,29 @@
     // AttributeValue implementation
     // -------------------------------------------------------------------------
 
+
     @Override
-    public void addAttributeValue( AttributeValue attributeValue )
+    public <T extends IdentifiableObject> void addAttributeValue( T object, AttributeValue attributeValue )
     {
+        if ( object == null || attributeValue == null || attributeValue.getAttribute() == null ||
+            !attributeValue.getAttribute().getSupportedClasses().contains( object.getClass() ) )
+        {
+            return;
+        }
+
         attributeValue.setAutoFields();
         attributeValueStore.save( attributeValue );
     }
 
     @Override
-    public void updateAttributeValue( AttributeValue attributeValue )
+    public <T extends IdentifiableObject> void updateAttributeValue( T object, AttributeValue attributeValue )
     {
+        if ( object == null || attributeValue == null || attributeValue.getAttribute() == null ||
+            !attributeValue.getAttribute().getSupportedClasses().contains( object.getClass() ) )
+        {
+            return;
+        }
+
         attributeValue.setAutoFields();
         attributeValueStore.update( attributeValue );
     }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeStoreTest.java	2015-11-17 11:55:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeStoreTest.java	2015-11-18 05:35:33 +0000
@@ -30,10 +30,14 @@
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.common.ValueType;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.indicator.IndicatorGroup;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -83,4 +87,22 @@
     {
         assertEquals( 1, attributeStore.getOrganisationUnitAttributes().size() );
     }
+
+    @Test
+    public void testGetSupportedClasses()
+    {
+        Attribute attribute = new Attribute( "AttributeName", ValueType.TEXT );
+        attribute.setDataElementAttribute( true );
+
+        assertEquals( 1, attribute.getSupportedClasses().size() );
+        assertTrue( attribute.getSupportedClasses().contains( DataElement.class ) );
+
+        attribute.setDataElementAttribute( false );
+        attribute.setIndicatorAttribute( true );
+        attribute.setIndicatorGroupAttribute( true );
+
+        assertEquals( 2, attribute.getSupportedClasses().size() );
+        assertTrue( attribute.getSupportedClasses().contains( Indicator.class ) );
+        assertTrue( attribute.getSupportedClasses().contains( IndicatorGroup.class ) );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java	2015-11-17 11:55:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/attribute/AttributeValueServiceTest.java	2015-11-18 05:35:33 +0000
@@ -30,6 +30,7 @@
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.common.ValueType;
+import org.hisp.dhis.dataelement.DataElement;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -54,7 +55,9 @@
         avB = new AttributeValue( "value 2" );
 
         Attribute attribute1 = new Attribute( "attribute 1", ValueType.TEXT );
+        attribute1.setDataElementAttribute( true );
         Attribute attribute2 = new Attribute( "attribute 2", ValueType.TEXT );
+        attribute2.setDataElementAttribute( true );
 
         attributeService.addAttribute( attribute1 );
         attributeService.addAttribute( attribute2 );
@@ -62,8 +65,11 @@
         avA.setAttribute( attribute1 );
         avB.setAttribute( attribute2 );
 
-        attributeService.addAttributeValue( avA );
-        attributeService.addAttributeValue( avB );
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+
+        attributeService.addAttributeValue( dataElementA, avA );
+        attributeService.addAttributeValue( dataElementB, avB );
     }
 
     @Test
@@ -82,8 +88,11 @@
         avA.setValue( "updated value 1" );
         avB.setValue( "updated value 2" );
 
-        attributeService.updateAttributeValue( avA );
-        attributeService.updateAttributeValue( avB );
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+
+        attributeService.updateAttributeValue( dataElementA, avA );
+        attributeService.updateAttributeValue( dataElementB, avB );
 
         avA = attributeService.getAttributeValue( avA.getId() );
         avB = attributeService.getAttributeValue( avB.getId() );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-11-18 03:02:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-11-18 05:35:33 +0000
@@ -431,7 +431,7 @@
         dataElementStore.save( dataElementB );
 
         AttributeValue attributeValue = new AttributeValue( "SOME VALUE", attribute );
-        attributeService.addAttributeValue( attributeValue );
+        attributeService.addAttributeValue( dataElementA, attributeValue );
 
         dataElementA.getAttributeValues().add( attributeValue );
         dataElementStore.update( dataElementA );
@@ -453,7 +453,7 @@
         dataElementStore.save( dataElementB );
 
         AttributeValue attributeValue = new AttributeValue( "SOME VALUE", attribute );
-        attributeService.addAttributeValue( attributeValue );
+        attributeService.addAttributeValue( dataElementA, attributeValue );
 
         dataElementA.getAttributeValues().add( attributeValue );
         dataElementStore.update( dataElementA );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2015-11-17 03:35:21 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2015-11-18 05:35:33 +0000
@@ -28,18 +28,9 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.system.util.PredicateUtils.idObjectCollectionsWithScanned;
-import static org.hisp.dhis.system.util.PredicateUtils.idObjects;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
@@ -93,9 +84,17 @@
 import org.hisp.dhis.validation.ValidationRule;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hisp.dhis.system.util.PredicateUtils.idObjectCollectionsWithScanned;
+import static org.hisp.dhis.system.util.PredicateUtils.idObjects;
 
 /**
  * Importer that can handle IdentifiableObject and NameableObject.
@@ -1195,7 +1194,7 @@
 
                 for ( AttributeValue attributeValue : attributeValues )
                 {
-                    attributeService.addAttributeValue( attributeValue );
+                    attributeService.addAttributeValue( object, attributeValue );
                 }
 
                 ReflectionUtils.invokeSetterMethod( "attributeValues", object, attributeValues );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.java	2015-03-30 09:48:48 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/AttributeUtils.java	2015-11-18 05:35:33 +0000
@@ -32,6 +32,7 @@
 import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.attribute.AttributeService;
 import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.springframework.util.StringUtils;
 
 import java.util.HashMap;
@@ -47,14 +48,14 @@
     /**
      * Given a list of JSON formatted values (with keys: 'id' and 'value'), this
      * method will add/update {@link AttributeValue} into the given {@code Set}.
-     * 
+     *
      * @param jsonAttributeValues List of JSON formatted values, needs two keys:
-     *        id => ID of attribute this value belongs to value => Actual value
-     * @param attributeValues Set that will be updated
+     *                            id => ID of attribute this value belongs to value => Actual value
+     * @param attributeValues     Set that will be updated
      * @param attributeService
      */
-    public static void updateAttributeValuesFromJson( Set<AttributeValue> attributeValues,
-        List<String> jsonAttributeValues, AttributeService attributeService )
+    public static <T extends IdentifiableObject> void updateAttributeValuesFromJson( T object,
+        Set<AttributeValue> attributeValues, List<String> jsonAttributeValues, AttributeService attributeService )
     {
         attributeValues.clear();
 
@@ -86,15 +87,15 @@
                     else
                     {
                         attributeValueItem.setValue( attributeValue.getValue() );
-                        attributeService.updateAttributeValue( attributeValueItem );
+                        attributeService.updateAttributeValue( object, attributeValueItem );
                         attributeValue = null;
                     }
                 }
             }
 
-            if ( attributeValue != null && attributeValue.getValue() != null && !attributeValue.getValue().isEmpty())
+            if ( attributeValue != null && attributeValue.getValue() != null && !attributeValue.getValue().isEmpty() )
             {
-                attributeService.addAttributeValue( attributeValue );
+                attributeService.addAttributeValue( object, attributeValue );
                 attributeValues.add( attributeValue );
             }
         }