← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6980: wip, import/export of attributevalues are back in... still needs work to be a bit more generic

 

------------------------------------------------------------
revno: 6980
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-05-17 21:54:00 +0200
message:
  wip, import/export of attributevalues are back in... still needs work to be a bit more generic
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/indicator/Indicator.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.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/dataelement/DataElement.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java	2012-05-17 09:11:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElement.java	2012-05-17 19:54:00 +0000
@@ -582,12 +582,10 @@
         this.numberType = numberType;
     }
 
-    /*
     @JsonProperty( value = "attributes" )
     @JsonView( {DetailedView.class, ExportView.class} )
     @JacksonXmlElementWrapper( localName = "attributes", namespace = Dxf2Namespace.NAMESPACE )
     @JacksonXmlProperty( localName = "attribute", namespace = Dxf2Namespace.NAMESPACE )
-    */
     public Set<AttributeValue> getAttributeValues()
     {
         return attributeValues;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/indicator/Indicator.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/indicator/Indicator.java	2012-05-17 09:11:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/indicator/Indicator.java	2012-05-17 19:54:00 +0000
@@ -341,12 +341,10 @@
         this.dataSets = dataSets;
     }
 
-    /*
     @JsonProperty( value = "attributes" )
     @JsonView( {DetailedView.class, ExportView.class} )
     @JacksonXmlElementWrapper( localName = "attributes", namespace = Dxf2Namespace.NAMESPACE )
     @JacksonXmlProperty( localName = "attribute", namespace = Dxf2Namespace.NAMESPACE )
-    */
     public Set<AttributeValue> getAttributeValues()
     {
         return attributeValues;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2012-05-17 09:11:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2012-05-17 19:54:00 +0000
@@ -814,12 +814,10 @@
         this.users = users;
     }
 
-    /*
     @JsonProperty( value = "attributes" )
     @JsonView( {DetailedView.class, ExportView.class} )
     @JacksonXmlElementWrapper( localName = "attributes", namespace = Dxf2Namespace.NAMESPACE )
     @JacksonXmlProperty( localName = "attribute", namespace = Dxf2Namespace.NAMESPACE )
-    */
     public Set<AttributeValue> getAttributeValues()
     {
         return attributeValues;

=== 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	2012-05-17 09:11:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/User.java	2012-05-17 19:54:00 +0000
@@ -343,12 +343,10 @@
         this.organisationUnits = organisationUnits;
     }
 
-    /*
     @JsonProperty( value = "attributes" )
     @JsonView( {DetailedView.class, ExportView.class} )
     @JacksonXmlElementWrapper( localName = "attributes", namespace = Dxf2Namespace.NAMESPACE )
     @JacksonXmlProperty( localName = "attribute", namespace = Dxf2Namespace.NAMESPACE )
-    */
     public Set<AttributeValue> getAttributeValues()
     {
         return attributeValues;
@@ -378,11 +376,7 @@
             attributeValues.addAll( user.getAttributeValues() );
 
             removeAllOrganisationUnits();
-
-            for ( OrganisationUnit organisationUnit : user.getOrganisationUnits() )
-            {
-                addOrganisationUnit( organisationUnit );
-            }
+            organisationUnits.addAll( user.getOrganisationUnits() );
         }
     }
 }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-05-17 11:28:33 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-05-17 19:54:00 +0000
@@ -97,6 +97,13 @@
 
         log.info( "User '" + currentUserService.getCurrentUsername() + "' started import at " + startDate );
 
+        doImport( metaData.getSqlViews(), importOptions, importSummary );
+        doImport( metaData.getConcepts(), importOptions, importSummary );
+        doImport( metaData.getConstants(), importOptions, importSummary );
+        doImport( metaData.getDocuments(), importOptions, importSummary );
+        doImport( metaData.getOptionSets(), importOptions, importSummary );
+        doImport( metaData.getAttributeTypes(), importOptions, importSummary );
+
         doImport( metaData.getOrganisationUnits(), importOptions, importSummary );
         doImport( metaData.getOrganisationUnitLevels(), importOptions, importSummary );
         doImport( metaData.getOrganisationUnitGroups(), importOptions, importSummary );
@@ -106,13 +113,6 @@
         // doImport( metaData.getUserGroups(), importOptions, importSummary );
         // doImport( metaData.getUserAuthorityGroups(), importOptions, importSummary );
 
-        doImport( metaData.getSqlViews(), importOptions, importSummary );
-        doImport( metaData.getConcepts(), importOptions, importSummary );
-        doImport( metaData.getConstants(), importOptions, importSummary );
-        doImport( metaData.getDocuments(), importOptions, importSummary );
-        doImport( metaData.getAttributeTypes(), importOptions, importSummary );
-        doImport( metaData.getOptionSets(), importOptions, importSummary );
-
         doImport( metaData.getCategoryOptions(), importOptions, importSummary );
         doImport( metaData.getCategories(), importOptions, importSummary );
         doImport( metaData.getCategoryCombos(), importOptions, importSummary );

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-05-17 14:29:39 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-05-17 19:54:00 +0000
@@ -30,11 +30,14 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
-import org.hisp.dhis.attribute.AttributeStore;
+import org.hisp.dhis.attribute.Attribute;
+import org.hisp.dhis.attribute.AttributeService;
+import org.hisp.dhis.attribute.AttributeValue;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.common.annotation.Scanned;
+import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportCount;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
@@ -42,13 +45,14 @@
 import org.hisp.dhis.dxf2.metadata.ObjectBridge;
 import org.hisp.dhis.dxf2.utils.OrganisationUnitUtils;
 import org.hisp.dhis.importexport.ImportStrategy;
+import org.hisp.dhis.indicator.Indicator;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitComparator;
 import org.hisp.dhis.period.Period;
-import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodStore;
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.system.util.ReflectionUtils;
+import org.hisp.dhis.user.User;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import java.lang.reflect.Field;
@@ -69,13 +73,10 @@
     //-------------------------------------------------------------------------------------------------------
 
     @Autowired
-    private PeriodService periodService;
-
-    @Autowired
     private PeriodStore periodStore;
 
     @Autowired
-    private AttributeStore attributeStore;
+    private AttributeService attributeService;
 
     @Autowired
     private ObjectBridge objectBridge;
@@ -210,6 +211,15 @@
 
         for ( T object : objects )
         {
+            log.info( "Object: " + object + " (" + object.getClass().getSimpleName() + ")" );
+
+            Set<AttributeValue> attributeValues = getAttributeValues( object );
+
+            if ( attributeValues.size() > 0 )
+            {
+                setAttributeValues( object, new HashSet<AttributeValue>() );
+            }
+
             List<ImportConflict> conflicts = importObjectLocal( object, options );
 
             if ( !options.isDryRun() )
@@ -217,6 +227,29 @@
                 sessionFactory.getCurrentSession().flush();
             }
 
+            if ( attributeValues.size() > 0 )
+            {
+                updateAttributeValues( attributeValues );
+
+                for ( AttributeValue attributeValue : attributeValues )
+                {
+                    attributeService.addAttributeValue( attributeValue );
+                }
+
+                if ( !options.isDryRun() )
+                {
+                    sessionFactory.getCurrentSession().flush();
+                }
+
+                setAttributeValues( object, attributeValues );
+
+                if ( !options.isDryRun() )
+                {
+                    sessionFactory.getCurrentSession().flush();
+                }
+            }
+
+
             if ( !conflicts.isEmpty() )
             {
                 importConflicts.addAll( conflicts );
@@ -226,6 +259,52 @@
         return importConflicts;
     }
 
+    private void setAttributeValues( T object, Set<AttributeValue> attributeValues )
+    {
+        ReflectionUtils.invokeSetterMethod( "attributeValues", object, attributeValues );
+    }
+
+    private Set<AttributeValue> getAttributeValues( T object )
+    {
+        if ( DataElement.class.isInstance( object ) )
+        {
+            return ((DataElement) object).getAttributeValues();
+        }
+        else if ( Indicator.class.isInstance( object ) )
+        {
+            return ((Indicator) object).getAttributeValues();
+        }
+        else if ( OrganisationUnit.class.isInstance( object ) )
+        {
+            return ((OrganisationUnit) object).getAttributeValues();
+        }
+        else if ( User.class.isInstance( object ) )
+        {
+            return ((User) object).getAttributeValues();
+        }
+
+        return new HashSet<AttributeValue>();
+    }
+
+    private void updateAttributeValues( Set<AttributeValue> attributeValues )
+    {
+        for ( AttributeValue attributeValue : attributeValues )
+        {
+            Attribute attribute = objectBridge.getObject( attributeValue.getAttribute() );
+
+            log.info( "Attribute: " + attribute );
+
+            if ( attribute == null )
+            {
+                log.warn( "Unknown reference to " + attributeValue.getAttribute() + " on object " + attributeValue );
+                continue;
+            }
+
+            attributeValue.setId( 0 );
+            attributeValue.setAttribute( attribute );
+        }
+    }
+
     @Override
     public List<ImportConflict> importObject( T object, ImportOptions options )
     {