← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13821: csd: sort attributes in outgoing xml

 

------------------------------------------------------------
revno: 13821
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-01-23 16:32:35 +0700
message:
  csd: sort attributes in outgoing xml
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeValueSortOrderComparator.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeSortOrderComparator.java
  dhis-2/dhis-web/dhis-web-ohie/src/main/java/org/hisp/dhis/web/ohie/csd/webapi/CsdController.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/comparator/AttributeSortOrderComparator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeSortOrderComparator.java	2014-01-19 06:27:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeSortOrderComparator.java	2014-01-23 09:32:35 +0000
@@ -33,7 +33,7 @@
 import java.util.Comparator;
 
 /**
- * @author mortenoh
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public class AttributeSortOrderComparator
     implements Comparator<Attribute>

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeValueSortOrderComparator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeValueSortOrderComparator.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/attribute/comparator/AttributeValueSortOrderComparator.java	2014-01-23 09:32:35 +0000
@@ -0,0 +1,57 @@
+package org.hisp.dhis.attribute.comparator;
+
+/*
+ * Copyright (c) 2004-2013, 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 org.hisp.dhis.attribute.AttributeValue;
+
+import java.util.Comparator;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class AttributeValueSortOrderComparator
+    implements Comparator<AttributeValue>
+{
+    public static final Comparator<AttributeValue> INSTANCE = new AttributeValueSortOrderComparator();
+
+    @Override
+    public int compare( AttributeValue o1, AttributeValue o2 )
+    {
+        if ( o1.getAttribute().getSortOrder() == null || o2.getAttribute().getSortOrder() == 0 )
+        {
+            return o1.getAttribute().getName().compareTo( o2.getAttribute().getName() );
+        }
+        if ( o2.getAttribute().getSortOrder() == null || o2.getAttribute().getSortOrder() == 0 )
+        {
+            return o1.getAttribute().getName().compareTo( o2.getAttribute().getName() );
+        }
+
+        return o1.getAttribute().getSortOrder() - o2.getAttribute().getSortOrder();
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-ohie/src/main/java/org/hisp/dhis/web/ohie/csd/webapi/CsdController.java'
--- dhis-2/dhis-web/dhis-web-ohie/src/main/java/org/hisp/dhis/web/ohie/csd/webapi/CsdController.java	2014-01-22 19:51:07 +0000
+++ dhis-2/dhis-web/dhis-web-ohie/src/main/java/org/hisp/dhis/web/ohie/csd/webapi/CsdController.java	2014-01-23 09:32:35 +0000
@@ -30,7 +30,10 @@
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.attribute.AttributeValue;
+import org.hisp.dhis.attribute.comparator.AttributeValueSortOrderComparator;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
@@ -73,11 +76,10 @@
 import javax.xml.bind.Unmarshaller;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -89,7 +91,7 @@
     private static final Log log = LogFactory.getLog( CsdController.class );
 
     private static final String SOAP_CONTENT_TYPE = "application/soap+xml";
-    
+
     // Name of group
     private static final String FACILITY_TYPE_DISCRIMINATOR = "Health Facility";
 
@@ -232,20 +234,22 @@
         for ( OrganisationUnit organisationUnit : organisationUnits )
         {
             boolean isFacility = false;
-            for (OrganisationUnitGroup group : organisationUnit.getGroups())
+
+            for ( OrganisationUnitGroup group : organisationUnit.getGroups() )
             {
-                if( group.getName().equals( FACILITY_TYPE_DISCRIMINATOR))
+                if ( group.getName().equals( FACILITY_TYPE_DISCRIMINATOR ) )
                 {
                     isFacility = true;
                     break;
                 }
             }
+
             // skip if orgunit is not a health facility
-            if (!isFacility)
+            if ( !isFacility )
             {
                 continue;
             }
-            
+
             Facility facility = new Facility();
             facility.setOid( "No oid, please provide facility_oid attribute value" );
 
@@ -259,12 +263,12 @@
             }
 
             facility.getOtherID().add( new OtherID( organisationUnit.getUid(), "dhis2-uid" ) );
-            
-            if (organisationUnit.getCode() != null)
+
+            if ( organisationUnit.getCode() != null )
             {
-                facility.getOtherID().add( new OtherID( organisationUnit.getCode(), "dhis2-code" ) );                
+                facility.getOtherID().add( new OtherID( organisationUnit.getCode(), "dhis2-code" ) );
             }
-            
+
             facility.setPrimaryName( organisationUnit.getDisplayName() );
 
             if ( organisationUnit.getContactPerson() != null )
@@ -287,11 +291,11 @@
                 {
                     continue;
                 }
-                
+
                 CodedType codedType = new CodedType();
                 codedType.setCode( organisationUnitGroup.getCode() );
-                
-                codedType.setCodingSchema("Unknown" );                
+
+                codedType.setCodingSchema( "Unknown" );
                 for ( AttributeValue attributeValue : organisationUnitGroup.getAttributeValues() )
                 {
                     if ( attributeValue.getAttribute().getName().equals( "code_system" ) )
@@ -368,7 +372,10 @@
 
             Map<String, List<AddressLine>> addressLines = Maps.newHashMap();
 
-            for ( AttributeValue attributeValue : organisationUnit.getAttributeValues() )
+            List<AttributeValue> attributeValues = new ArrayList<AttributeValue>( organisationUnit.getAttributeValues() );
+            Collections.sort( attributeValues, AttributeValueSortOrderComparator.INSTANCE );
+
+            for ( AttributeValue attributeValue : attributeValues )
             {
                 if ( attributeValue.getAttribute().getName().startsWith( "Address_" ) )
                 {