← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21077: minor

 

------------------------------------------------------------
revno: 21077
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-11-17 14:00:29 +0700
message:
  minor
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.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/AttributeValue.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java	2015-07-03 11:01:19 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/AttributeValue.java	2015-11-17 07:00:29 +0000
@@ -82,6 +82,12 @@
         this.value = value;
     }
 
+    public AttributeValue( String value, Attribute attribute )
+    {
+        this( value );
+        this.attribute = attribute;
+    }
+
     public void setAutoFields()
     {
         if ( created == null )
@@ -121,7 +127,7 @@
     {
         return "AttributeValue{" +
             "id=" + id +
-            ", attribute=" + ( attribute != null ? attribute.getUid() : "" ) +
+            ", attribute=" + (attribute != null ? attribute.getUid() : "") +
             ", value='" + value + '\'' +
             '}';
     }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2015-11-05 03:16:36 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/GenericStore.java	2015-11-17 07:00:29 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.attribute.Attribute;
+
 import java.util.List;
 
 /**
@@ -123,4 +125,12 @@
     List<T> getAllNoAcl( int first, int max );
 
     int getCountNoAcl();
+
+    /**
+     * Get object where an attribute value of a certain attribute exists.
+     *
+     * @param attribute Attribute
+     * @return Object if object.attributeValues.attribute=attribute exists
+     */
+    T getByAttribute( Attribute attribute );
 }

=== 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-09-15 09:54:24 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2015-11-17 07:00:29 +0000
@@ -30,6 +30,9 @@
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.analytics.AggregationType;
+import org.hisp.dhis.attribute.Attribute;
+import org.hisp.dhis.attribute.AttributeService;
+import org.hisp.dhis.attribute.AttributeValue;
 import org.hisp.dhis.common.ValueType;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
@@ -56,6 +59,9 @@
     @Autowired
     private DataSetService dataSetService;
 
+    @Autowired
+    private AttributeService attributeService;
+
     // -------------------------------------------------------------------------
     // Tests
     // -------------------------------------------------------------------------
@@ -411,4 +417,26 @@
         assertTrue( dataElements.contains( dataElementD ) );
         assertTrue( dataElements.contains( dataElementF ) );
     }
+
+    @Test
+    public void testDataElementFromAttribute()
+    {
+        Attribute attribute = new Attribute( "test", ValueType.TEXT );
+        attribute.setDataElementAttribute( true );
+        attributeService.addAttribute( attribute );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+        dataElementStore.save( dataElementA );
+        dataElementStore.save( dataElementB );
+
+        AttributeValue attributeValue = new AttributeValue( "SOME VALUE", attribute );
+        attributeService.addAttributeValue( attributeValue );
+
+        dataElementA.getAttributeValues().add( attributeValue );
+        dataElementStore.update( dataElementA );
+
+        DataElement dataElement = dataElementStore.getByAttribute( attribute );
+        assertEquals( dataElement.getUid(), dataElementA.getUid() );
+    }
 }

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2015-11-09 21:08:21 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/hibernate/HibernateGenericStore.java	2015-11-17 07:00:29 +0000
@@ -42,6 +42,7 @@
 import org.hibernate.criterion.Property;
 import org.hibernate.criterion.Restrictions;
 import org.hibernate.criterion.Subqueries;
+import org.hisp.dhis.attribute.Attribute;
 import org.hisp.dhis.common.AuditLogUtil;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.GenericStore;
@@ -52,6 +53,8 @@
 import org.hisp.dhis.hibernate.exception.ReadAccessDeniedException;
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.interpretation.Interpretation;
+import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.schema.SchemaService;
 import org.hisp.dhis.security.acl.AccessStringHelper;
 import org.hisp.dhis.security.acl.AclService;
 import org.hisp.dhis.user.CurrentUserService;
@@ -89,6 +92,9 @@
     @Autowired
     protected CurrentUserService currentUserService;
 
+    @Autowired
+    protected SchemaService schemaService;
+
     /**
      * Allows injection (e.g. by a unit test)
      */
@@ -517,6 +523,24 @@
             .uniqueResult()).intValue();
     }
 
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public T getByAttribute( Attribute attribute )
+    {
+        Schema schema = schemaService.getDynamicSchema( getClazz() );
+
+        if ( schema == null )
+        {
+            return null;
+        }
+
+        Criteria criteria = getCriteria();
+        criteria.createAlias( "attributeValues", "av" );
+        criteria.add( Restrictions.eq( "av.attribute", attribute ) );
+
+        return (T) criteria.uniqueResult();
+    }
+
     //----------------------------------------------------------------------------------------------------------------
     // Helpers
     //----------------------------------------------------------------------------------------------------------------