← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19203: Centralized isInUserHierarchy method. Using callable for period in data import.

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 19203 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-05-29 18:31:27 +0200
message:
  Centralized isInUserHierarchy method. Using callable for period in data import.
added:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/callable/PeriodCallable.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.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/user/User.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2015-04-21 11:30:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2015-05-29 15:01:41 +0000
@@ -241,6 +241,33 @@
         return IdentifiableObjectUtils.join( organisationUnits );
     }
 
+    /**
+     * Indicates whether the given organisation unit is part of the hierarchy
+     * of the organisation units of this user.
+     * 
+     * @param organisationUnit the organisation unit.
+     * @return true if the given organisation unit is part of the hierarchy.
+     */
+    public boolean isInUserHierarchy( OrganisationUnit organisationUnit )
+    {
+        if ( organisationUnits == null )
+        {
+            return false;
+        }
+        
+        while ( organisationUnit != null )
+        {
+            if ( organisationUnits.contains( organisationUnit ) )
+            {
+                return true;
+            }
+            
+            organisationUnit = organisationUnit.getParent();
+        }
+        
+        return false;
+    }
+    
     public String getUsername()
     {
         return userCredentials != null ? userCredentials.getUsername() : null;

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-05-28 18:21:56 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/organisationunit/DefaultOrganisationUnitService.java	2015-05-29 15:01:41 +0000
@@ -760,29 +760,7 @@
     {
         User user = currentUserService.getCurrentUser();
         
-        if ( user == null )
-        {
-            return false;
-        }
-        
-        Set<OrganisationUnit> userRootUnits = user.getOrganisationUnits();
-        
-        if ( userRootUnits == null )
-        {
-            return false;
-        }
-        
-        while ( organisationUnit != null )
-        {
-            if ( userRootUnits.contains( organisationUnit ) )
-            {
-                return true;
-            }
-            
-            organisationUnit = organisationUnit.getParent();
-        }
-        
-        return false;
+        return user != null ? user.isInUserHierarchy( organisationUnit ) : false;
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2015-05-28 14:33:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/organisationunit/OrganisationUnitServiceTest.java	2015-05-29 15:01:41 +0000
@@ -43,10 +43,13 @@
 import java.util.List;
 
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.user.User;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import com.google.common.collect.Sets;
+
 /**
  * @author Kristian Nordal
  */
@@ -938,7 +941,7 @@
     }
 
     @Test
-    public void getMaxLevels()
+    public void testGetMaxLevels()
     {
         assertEquals( 0, organisationUnitService.getMaxOfOrganisationUnitLevels() );
 
@@ -950,4 +953,43 @@
 
         assertEquals( 2, organisationUnitService.getMaxOfOrganisationUnitLevels() );
     }
+    
+    @Test
+    public void testIsInUserHierarchy()
+    {
+        OrganisationUnit ouA = createOrganisationUnit( 'A' );
+        OrganisationUnit ouB = createOrganisationUnit( 'B', ouA );
+        OrganisationUnit ouC = createOrganisationUnit( 'C', ouA );
+        OrganisationUnit ouD = createOrganisationUnit( 'D', ouB );
+        OrganisationUnit ouE = createOrganisationUnit( 'E', ouB );
+        OrganisationUnit ouF = createOrganisationUnit( 'F', ouC );
+        OrganisationUnit ouG = createOrganisationUnit( 'G', ouC );
+
+        ouA.getChildren().add( ouB );
+        ouA.getChildren().add( ouC );
+        ouB.getChildren().add( ouD );
+        ouB.getChildren().add( ouE );
+        ouC.getChildren().add( ouF );
+        ouC.getChildren().add( ouG );
+
+        organisationUnitService.addOrganisationUnit( ouA );
+        organisationUnitService.addOrganisationUnit( ouB );
+        organisationUnitService.addOrganisationUnit( ouC );
+        organisationUnitService.addOrganisationUnit( ouD );
+        organisationUnitService.addOrganisationUnit( ouE );
+        organisationUnitService.addOrganisationUnit( ouF );
+        organisationUnitService.addOrganisationUnit( ouG );
+
+        User user = createUser( 'A' );
+        user.setOrganisationUnits( Sets.newHashSet( ouB ) );
+
+        assertTrue( user.isInUserHierarchy( ouB ) );
+        assertTrue( user.isInUserHierarchy( ouD ) );
+        assertTrue( user.isInUserHierarchy( ouE ) );
+        
+        assertFalse( user.isInUserHierarchy( ouA ) );
+        assertFalse( user.isInUserHierarchy( ouC ) );
+        assertFalse( user.isInUserHierarchy( ouF ) );
+        assertFalse( user.isInUserHierarchy( ouG ) );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2015-05-28 20:17:44 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2015-05-29 15:24:03 +0000
@@ -33,16 +33,15 @@
 import static org.hisp.dhis.common.IdentifiableProperty.UUID;
 import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
 import static org.hisp.dhis.system.notification.NotificationLevel.INFO;
-import static org.hisp.dhis.util.ConversionUtils.wrap;
 import static org.hisp.dhis.system.util.DateUtils.getDefaultDate;
 import static org.hisp.dhis.system.util.DateUtils.parseDate;
+import static org.hisp.dhis.util.ConversionUtils.wrap;
 
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -89,12 +88,13 @@
 import org.hisp.dhis.scheduling.TaskId;
 import org.hisp.dhis.system.callable.CategoryOptionComboAclCallable;
 import org.hisp.dhis.system.callable.IdentifiableObjectCallable;
+import org.hisp.dhis.system.callable.PeriodCallable;
 import org.hisp.dhis.system.notification.Notifier;
 import org.hisp.dhis.system.util.DateUtils;
 import org.hisp.dhis.system.util.ValidationUtils;
+import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.util.CachingMap;
 import org.hisp.dhis.util.DebugUtils;
-import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import com.csvreader.CsvReader;
@@ -629,7 +629,7 @@
         CachingMap<String, DataElement> dataElementMap = new CachingMap<>();
         CachingMap<String, OrganisationUnit> orgUnitMap = new CachingMap<>();
         CachingMap<String, DataElementCategoryOptionCombo> optionComboMap = new CachingMap<>();
-        Map<String, Period> periodMap = new HashMap<>();
+        CachingMap<String, Period> periodMap = new CachingMap<>();
 
         //----------------------------------------------------------------------
         // Load meta-data maps
@@ -648,6 +648,8 @@
             identifiableObjectManager, OrganisationUnit.class, orgUnitIdScheme, trimToNull( dataValueSet.getOrgUnit() ) );
         IdentifiableObjectCallable<DataElementCategoryOptionCombo> optionComboCallable = new CategoryOptionComboAclCallable( 
             categoryService, idScheme, null );
+        IdentifiableObjectCallable<Period> periodCallable = new PeriodCallable( 
+            periodService, null, trimToNull( dataValueSet.getPeriod() ) );
         
         //----------------------------------------------------------------------
         // Get outer meta-data
@@ -657,7 +659,7 @@
         
         Date completeDate = getDefaultDate( dataValueSet.getCompleteDate() );
 
-        Period outerPeriod = PeriodType.getPeriodFromIsoString( trimToNull( dataValueSet.getPeriod() ) );
+        Period outerPeriod = periodMap.get( trimToNull( dataValueSet.getPeriod() ), periodCallable );
 
         OrganisationUnit outerOrgUnit = orgUnitMap.get( trimToNull( dataValueSet.getOrgUnit() ), orgUnitCallable );
 
@@ -732,7 +734,8 @@
             totalCount++;
 
             DataElement dataElement = dataElementMap.get( trimToNull( dataValue.getDataElement() ), dataElementCallable.setId( trimToNull( dataValue.getDataElement() ) ) );
-            Period period = outerPeriod != null ? outerPeriod : PeriodType.getPeriodFromIsoString( trimToNull( dataValue.getPeriod() ) );
+            Period period = outerPeriod != null ? outerPeriod : 
+                periodMap.get( trimToNull( dataValue.getPeriod() ), periodCallable.setId( trimToNull( dataValue.getPeriod() ) ) );
             OrganisationUnit orgUnit = outerOrgUnit != null ? outerOrgUnit : 
                 orgUnitMap.get( trimToNull( dataValue.getOrgUnit() ), orgUnitCallable.setId( trimToNull( dataValue.getOrgUnit() ) ) );
             DataElementCategoryOptionCombo categoryOptionCombo = optionComboMap.get( trimToNull( dataValue.getCategoryOptionCombo() ), 
@@ -805,16 +808,6 @@
                 continue;
             }
 
-            if ( periodMap.containsKey( dataValue.getPeriod() ) )
-            {
-                period = periodMap.get( dataValue.getPeriod() );
-            }
-            else
-            {
-                period = periodService.reloadPeriod( period );
-                periodMap.put( dataValue.getPeriod(), period );
-            }
-
             internalValue.setDataElement( dataElement );
             internalValue.setPeriod( period );
             internalValue.setSource( orgUnit );

=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/callable/PeriodCallable.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/callable/PeriodCallable.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/callable/PeriodCallable.java	2015-05-29 15:24:03 +0000
@@ -0,0 +1,64 @@
+package org.hisp.dhis.system.callable;
+
+/*
+ * Copyright (c) 2004-2015, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import java.util.concurrent.ExecutionException;
+
+import org.hisp.dhis.common.IdentifiableProperty;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodService;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class PeriodCallable
+    extends IdentifiableObjectCallable<Period>
+{
+    private PeriodService periodService;
+    
+    public PeriodCallable( PeriodService periodService, IdentifiableProperty property, String id )
+    {
+        super( null, Period.class, property, id );
+        this.periodService = periodService;
+    }
+    
+    @Override
+    public Period call()
+        throws ExecutionException
+    {
+        return periodService.reloadIsoPeriod( id );
+    }
+    
+    @Override
+    public PeriodCallable setId( String id )
+    {
+        this.id = id;
+        return this;
+    }
+}