← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19841: DataElementService, removed some boilerplate code using java 8 streams and filters

 

------------------------------------------------------------
revno: 19841
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-08-25 10:45:42 +0200
message:
  DataElementService, removed some boilerplate code using java 8 streams and filters
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/listener/ProgramStageDataEntrySMSListener.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceCategoryOptionGroupTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementServiceTest.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-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java	2015-08-25 07:52:52 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.java	2015-08-25 08:45:42 +0000
@@ -42,12 +42,11 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.hisp.dhis.common.GenericDimensionalObjectStore;
 import org.hisp.dhis.common.GenericNameableObjectStore;
 import org.hisp.dhis.common.ListMap;
-import org.hisp.dhis.commons.filter.Filter;
-import org.hisp.dhis.commons.filter.FilterUtils;
 import org.hisp.dhis.dataelement.comparator.DataElementCategoryComboSizeComparator;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.i18n.I18nService;
@@ -142,16 +141,7 @@
     @Override
     public List<DataElement> getDataElements( final Collection<Integer> identifiers )
     {
-        List<DataElement> dataElements = getAllDataElements();
-
-        return identifiers == null ? dataElements : FilterUtils.filter( dataElements, new Filter<DataElement>()
-        {
-            @Override
-            public boolean retain( DataElement dataElement )
-            {
-                return identifiers.contains( dataElement.getId() );
-            }
-        } );
+        return getAllDataElements().stream().filter( p -> identifiers.contains( p.getId() ) ).collect( Collectors.toList() );
     }
 
     @Override
@@ -176,20 +166,11 @@
     }
 
     @Override
-    public Set<DataElement> getDataElementsByZeroIsSignificantAndGroup( boolean zeroIsSignificant,
-        DataElementGroup dataElementGroup )
+    public Set<DataElement> getDataElementsByZeroIsSignificantAndGroup( boolean zeroIsSignificant, DataElementGroup dataElementGroup )
     {
-        Set<DataElement> dataElements = new HashSet<>();
-
-        for ( DataElement element : dataElementGroup.getMembers() )
-        {
-            if ( element.isZeroIsSignificant() )
-            {
-                dataElements.add( element );
-            }
-        }
-
-        return dataElements;
+        Set<DataElement> dataElements = new HashSet<>( dataElementGroup.getMembers() );
+        
+        return dataElements.stream().filter( p -> p.isZeroIsSignificant() ).collect( Collectors.toSet() );
     }
 
     @Override
@@ -201,7 +182,7 @@
     @Override
     public DataElement getDataElementByName( String name )
     {
-        List<DataElement> dataElements = new ArrayList<>( dataElementStore.getAllEqName( name ) );
+        List<DataElement> dataElements = dataElementStore.getAllEqName( name );
 
         if ( dataElements.isEmpty() )
         {
@@ -220,14 +201,9 @@
     @Override
     public DataElement getDataElementByShortName( String shortName )
     {
-        List<DataElement> dataElements = new ArrayList<>( dataElementStore.getAllEqShortName( shortName ) );
-
-        if ( dataElements.isEmpty() )
-        {
-            return null;
-        }
-
-        return i18n( i18nService, dataElements.get( 0 ) );
+        List<DataElement> dataElements = dataElementStore.getAllEqShortName( shortName );
+
+        return !dataElements.isEmpty() ? i18n( i18nService, dataElements.get( 0 ) ) : null;
     }
 
     @Override
@@ -245,16 +221,7 @@
     @Override
     public List<DataElement> getDataElementsByPeriodType( final PeriodType periodType )
     {
-        List<DataElement> dataElements = getAllDataElements();
-
-        return FilterUtils.filter( dataElements, new Filter<DataElement>()
-        {
-            @Override
-            public boolean retain( DataElement dataElement )
-            {
-                return dataElement.getPeriodType() != null && dataElement.getPeriodType().equals( periodType );
-            }
-        } );
+        return getAllDataElements().stream().filter( p -> p.getPeriodType() != null && p.getPeriodType().equals( periodType ) ).collect( Collectors.toList() );
     }
 
     @Override
@@ -459,16 +426,7 @@
     @Override
     public List<DataElementGroup> getDataElementGroups( final Collection<Integer> identifiers )
     {
-        List<DataElementGroup> groups = getAllDataElementGroups();
-
-        return identifiers == null ? groups : FilterUtils.filter( groups, new Filter<DataElementGroup>()
-        {
-            @Override
-            public boolean retain( DataElementGroup object )
-            {
-                return identifiers.contains( object.getId() );
-            }
-        } );
+        return getAllDataElementGroups().stream().filter( p -> identifiers.contains( p.getId() ) ).collect( Collectors.toList() );
     }
 
     @Override
@@ -492,21 +450,15 @@
     @Override
     public DataElementGroup getDataElementGroupByName( String name )
     {
-        List<DataElementGroup> dataElementGroups = new ArrayList<>(
-            dataElementGroupStore.getAllEqName( name ) );
-
-        if ( dataElementGroups.isEmpty() )
-        {
-            return null;
-        }
-
-        return i18n( i18nService, dataElementGroups.get( 0 ) );
+        List<DataElementGroup> dataElementGroups = dataElementGroupStore.getAllEqName( name );
+
+        return !dataElementGroups.isEmpty() ? i18n( i18nService, dataElementGroups.get( 0 ) ) : null;
     }
 
     @Override
     public DataElementGroup getDataElementGroupByShortName( String shortName )
     {
-        List<DataElementGroup> dataElementGroups = new ArrayList<>( dataElementGroupStore.getAllEqShortName( shortName ) );
+        List<DataElementGroup> dataElementGroups = dataElementGroupStore.getAllEqShortName( shortName );
 
         if ( dataElementGroups.isEmpty() )
         {
@@ -602,28 +554,15 @@
     @Override
     public DataElementGroupSet getDataElementGroupSetByName( String name )
     {
-        List<DataElementGroupSet> dataElementGroupSets = new ArrayList<>(
-            dataElementGroupSetStore.getAllEqName( name ) );
-
-        if ( dataElementGroupSets.isEmpty() )
-        {
-            return null;
-        }
-
-        return i18n( i18nService, dataElementGroupSets.get( 0 ) );
+        List<DataElementGroupSet> dataElementGroupSets = dataElementGroupSetStore.getAllEqName( name );
+
+        return !dataElementGroupSets.isEmpty() ? i18n( i18nService, dataElementGroupSets.get( 0 ) ) : null;
     }
 
     @Override
     public List<DataElementGroupSet> getCompulsoryDataElementGroupSetsWithMembers()
     {
-        return FilterUtils.filter( getAllDataElementGroupSets(), new Filter<DataElementGroupSet>()
-        {
-            @Override
-            public boolean retain( DataElementGroupSet object )
-            {
-                return object.isCompulsory() && object.hasDataElementGroups();
-            }
-        } );
+        return getAllDataElementGroupSets().stream().filter( p -> p.isCompulsory() && p.hasDataElementGroups() ).collect( Collectors.toList() );
     }
 
     @Override
@@ -635,16 +574,7 @@
     @Override
     public List<DataElementGroupSet> getDataElementGroupSets( final Collection<Integer> identifiers )
     {
-        List<DataElementGroupSet> groupSets = getAllDataElementGroupSets();
-
-        return identifiers == null ? groupSets : FilterUtils.filter( groupSets, new Filter<DataElementGroupSet>()
-        {
-            @Override
-            public boolean retain( DataElementGroupSet object )
-            {
-                return identifiers.contains( object.getId() );
-            }
-        } );
+        return getAllDataElementGroupSets().stream().filter( p -> identifiers.contains( p.getId() ) ).collect( Collectors.toList() );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java	2015-07-16 06:27:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/ExcelNodeSerializer.java	2015-08-25 08:45:42 +0000
@@ -28,7 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
+import java.io.OutputStream;
+import java.util.Date;
+import java.util.List;
+
 import org.apache.poi.ss.usermodel.Hyperlink;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
@@ -50,11 +53,7 @@
 import org.springframework.context.annotation.ScopedProxyMode;
 import org.springframework.stereotype.Component;
 
-import java.io.OutputStream;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
-import java.util.TimeZone;
+import com.google.common.collect.Lists;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java	2015-07-16 06:27:31 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/serializers/PdfNodeSerializer.java	2015-08-25 08:45:42 +0000
@@ -28,9 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
-import com.lowagie.text.Document;
-import com.lowagie.text.pdf.PdfPTable;
+import java.io.OutputStream;
+import java.util.Date;
+import java.util.List;
+
 import org.hisp.dhis.node.AbstractNodeSerializer;
 import org.hisp.dhis.node.Node;
 import org.hisp.dhis.node.types.CollectionNode;
@@ -43,11 +44,9 @@
 import org.springframework.context.annotation.ScopedProxyMode;
 import org.springframework.stereotype.Component;
 
-import java.io.OutputStream;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
-import java.util.TimeZone;
+import com.google.common.collect.Lists;
+import com.lowagie.text.Document;
+import com.lowagie.text.pdf.PdfPTable;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-06-04 09:25:32 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-08-25 08:45:42 +0000
@@ -210,7 +210,6 @@
     }
 
     // TODO verify parameters length
-    @SuppressWarnings( "unchecked" )
     private Criterion getHibernateCriterion( Schema schema, Restriction restriction )
     {
         if ( restriction == null || restriction.getOperator() == null )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/listener/ProgramStageDataEntrySMSListener.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/listener/ProgramStageDataEntrySMSListener.java	2015-06-23 15:59:19 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/sms/listener/ProgramStageDataEntrySMSListener.java	2015-08-25 08:45:42 +0000
@@ -116,6 +116,8 @@
 
         Date date = lookForDate( message );
         
+        //TODO and?
+        
         String senderPhoneNumber = StringUtils.replace( sms.getOriginator(), "+", "" );
         Collection<OrganisationUnit> orgUnits = getOrganisationUnitsByPhoneNumber( senderPhoneNumber );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceCategoryOptionGroupTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceCategoryOptionGroupTest.java	2015-06-14 12:54:38 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceCategoryOptionGroupTest.java	2015-08-25 08:45:42 +0000
@@ -683,6 +683,7 @@
         System.out.println();
     }
 
+    @SuppressWarnings("unused")
     private void generateAllApprovalsAndPermissions()
     {
         generateUserApprovalsAndPermissions( superUser, dataSetA, periodA, null );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementServiceTest.java	2015-08-25 07:43:48 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementServiceTest.java	2015-08-25 08:45:42 +0000
@@ -34,7 +34,9 @@
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.hisp.dhis.DhisSpringTest;
 import org.junit.Test;
@@ -55,7 +57,6 @@
 
     @Test
     public void testAddDataElement()
-        throws Exception
     {
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
@@ -91,7 +92,6 @@
 
     @Test
     public void testUpdateDataElement()
-        throws Exception
     {
         DataElement dataElementA = createDataElement( 'A' );
         
@@ -111,7 +111,6 @@
 
     @Test
     public void testDeleteAndGetDataElement()
-        throws Exception
     {
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
@@ -152,7 +151,6 @@
 
     @Test
     public void testGetDataElementByCode()
-        throws Exception
     {
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
@@ -182,7 +180,6 @@
 
     @Test
     public void testGetDataElementByName()
-        throws Exception
     {
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
@@ -205,7 +202,6 @@
 
     @Test
     public void testGetDataElementByShortName()
-        throws Exception
     {
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
@@ -228,7 +224,6 @@
 
     @Test
     public void testGetAllDataElements()
-        throws Exception
     {
         assertEquals( 0, dataElementService.getAllDataElements().size() );
 
@@ -287,7 +282,6 @@
 
     @Test
     public void testGetDataElementsByAggregationOperator()
-        throws Exception
     {
         assertEquals( 0, dataElementService.getDataElementsByAggregationOperator(
             DataElement.AGGREGATION_OPERATOR_AVERAGE_SUM ).size() );
@@ -368,7 +362,6 @@
 
     @Test
     public void testAddDataElementGroup()
-        throws Exception
     {
         DataElementGroup dataElementGroupA = new DataElementGroup( "DataElementGroupA" );
         DataElementGroup dataElementGroupB = new DataElementGroup( "DataElementGroupB" );
@@ -396,7 +389,6 @@
 
     @Test
     public void testUpdateDataElementGroup()
-        throws Exception
     {
         DataElementGroup dataElementGroupA = new DataElementGroup( "DataElementGroupA" );
         DataElementGroup dataElementGroupB = new DataElementGroup( "DataElementGroupB" );
@@ -432,7 +424,6 @@
 
     @Test
     public void testDeleteAndGetDataElementGroup()
-        throws Exception
     {
         DataElementGroup dataElementGroupA = new DataElementGroup( "DataElementGroupA" );
         DataElementGroup dataElementGroupB = new DataElementGroup( "DataElementGroupB" );
@@ -476,7 +467,6 @@
 
     @Test
     public void testGetDataElementGroupByName()
-        throws Exception
     {
         DataElementGroup dataElementGroupA = new DataElementGroup( "DataElementGroupA" );
         DataElementGroup dataElementGroupB = new DataElementGroup( "DataElementGroupB" );
@@ -499,4 +489,37 @@
         DataElementGroup dataElementGroupC = dataElementService.getDataElementGroupByName( "DataElementGroupC" );
         assertNull( dataElementGroupC );
     }
+    
+    @Test
+    public void testGetDataElementsByZeroIsSignificantAndGroup()
+    {
+        DataElementGroup dataElementGroupA = new DataElementGroup( "DataElementGroupA" );
+        
+        DataElement dataElementA = createDataElement( 'A' );
+        dataElementA.setZeroIsSignificant( true );
+        DataElement dataElementB = createDataElement( 'B' );
+        dataElementB.setZeroIsSignificant( false );
+        DataElement dataElementC = createDataElement( 'C' );
+        dataElementC.setZeroIsSignificant( true );
+        DataElement dataElementD = createDataElement( 'D' );
+        dataElementD.setZeroIsSignificant( false );
+        
+        dataElementGroupA.addDataElement( dataElementA );
+        dataElementGroupA.addDataElement( dataElementB );
+        dataElementGroupA.addDataElement( dataElementC );
+        dataElementGroupA.addDataElement( dataElementD );
+
+        dataElementService.addDataElement( dataElementA );
+        dataElementService.addDataElement( dataElementB );
+        dataElementService.addDataElement( dataElementC );
+        dataElementService.addDataElement( dataElementD );
+
+        dataElementService.addDataElementGroup( dataElementGroupA );
+        
+        Set<DataElement> expected = new HashSet<>();
+        expected.add( dataElementA );
+        expected.add( dataElementC );
+        
+        assertEquals( expected, dataElementService.getDataElementsByZeroIsSignificantAndGroup( true, dataElementGroupA ) );
+    }
 }