← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6999: Fixed expressions in importer

 

------------------------------------------------------------
revno: 6999
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2012-05-22 13:04:59 +0200
message:
  Fixed expressions in importer
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/Expression.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/DefaultObjectBridge.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
  dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml


--
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/expression/Expression.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/Expression.java	2012-03-19 14:58:46 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/Expression.java	2012-05-22 11:04:59 +0000
@@ -41,6 +41,7 @@
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
 import java.io.Serializable;
+import java.util.HashSet;
 import java.util.Set;
 
 /**
@@ -93,12 +94,12 @@
     /**
      * A reference to the DataElements in the Expression.
      */
-    private Set<DataElement> dataElementsInExpression;
+    private Set<DataElement> dataElementsInExpression = new HashSet<DataElement>();
 
     /**
      * A reference to the optionCombos in the Expression.
      */
-    private Set<DataElementCategoryOptionCombo> optionCombosInExpression;
+    private Set<DataElementCategoryOptionCombo> optionCombosInExpression = new HashSet<DataElementCategoryOptionCombo>();
 
     // -------------------------------------------------------------------------
     // Constructors
@@ -119,7 +120,7 @@
      * @param dataElementsInExpression A reference to the DataElements in the Expression.
      */
     public Expression( String expression, String description, Set<DataElement> dataElementsInExpression,
-                       Set<DataElementCategoryOptionCombo> optionCombosInExpression )
+        Set<DataElementCategoryOptionCombo> optionCombosInExpression )
     {
         this.expression = expression;
         this.description = description;
@@ -207,7 +208,7 @@
     }
 
     @JsonProperty
-    @JsonView( {DetailedView.class, ExportView.class} )
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty
     public String getExpression()
     {
@@ -221,7 +222,7 @@
 
     @JsonProperty( value = "dataElements" )
     @JsonSerialize( contentAs = BaseIdentifiableObject.class )
-    @JsonView( {DetailedView.class, ExportView.class} )
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlElementWrapper( localName = "dataElements" )
     @JacksonXmlProperty( localName = "dataElement" )
     public Set<DataElement> getDataElementsInExpression()
@@ -236,7 +237,7 @@
 
     @JsonProperty( value = "categoryOptionCombos" )
     @JsonSerialize( contentAs = BaseIdentifiableObject.class )
-    @JsonView( {DetailedView.class, ExportView.class} )
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlElementWrapper( localName = "categoryOptionCombos" )
     @JacksonXmlProperty( localName = "categoryOptionCombo" )
     public Set<DataElementCategoryOptionCombo> getOptionCombosInExpression()
@@ -250,7 +251,7 @@
     }
 
     @JsonProperty
-    @JsonView( {DetailedView.class, ExportView.class} )
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty
     public String getDescription()
     {

=== 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 19:54:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2012-05-22 11:04:59 +0000
@@ -47,7 +47,6 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @Transactional
-@Service
 public class DefaultImportService
     implements ImportService
 {

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2012-05-17 09:11:30 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultObjectBridge.java	2012-05-22 11:04:59 +0000
@@ -59,6 +59,9 @@
 import org.hisp.dhis.report.Report;
 import org.hisp.dhis.reporttable.ReportTable;
 import org.hisp.dhis.sqlview.SqlView;
+import org.hisp.dhis.user.User;
+import org.hisp.dhis.user.UserAuthorityGroup;
+import org.hisp.dhis.user.UserGroup;
 import org.hisp.dhis.validation.ValidationRule;
 import org.hisp.dhis.validation.ValidationRuleGroup;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -70,8 +73,6 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-@Component
-@Transactional( readOnly = true )
 public class DefaultObjectBridge
     implements ObjectBridge
 {
@@ -188,13 +189,13 @@
     public void destroy()
     {
         masterMap = null;
-
         uidMap = null;
         codeMap = null;
         nameMap = null;
         shortNameMap = null;
-
         periodTypeMap = null;
+
+        writeEnabled = true;
     }
 
     //-------------------------------------------------------------------------------------------------------
@@ -284,7 +285,6 @@
     //-------------------------------------------------------------------------------------------------------
 
     @Override
-    @Transactional( readOnly = false )
     public void saveObject( Object object )
     {
         if ( _typeSupported( object.getClass() ) && IdentifiableObject.class.isInstance( object ) )
@@ -301,7 +301,6 @@
     }
 
     @Override
-    @Transactional( readOnly = false )
     public void updateObject( Object object )
     {
         if ( _typeSupported( object.getClass() ) && IdentifiableObject.class.isInstance( object ) )

=== 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-21 13:28:46 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-05-22 11:04:59 +0000
@@ -37,7 +37,10 @@
 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.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementOperand;
+import org.hisp.dhis.dataelement.DataElementOperandService;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
 import org.hisp.dhis.dxf2.importsummary.ImportCount;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
@@ -45,6 +48,7 @@
 import org.hisp.dhis.dxf2.metadata.ObjectBridge;
 import org.hisp.dhis.dxf2.utils.OrganisationUnitUtils;
 import org.hisp.dhis.expression.Expression;
+import org.hisp.dhis.expression.ExpressionService;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.comparator.OrganisationUnitComparator;
@@ -78,6 +82,12 @@
     private AttributeService attributeService;
 
     @Autowired
+    private ExpressionService expressionService;
+
+    @Autowired
+    private DataElementOperandService dataElementOperandService;
+
+    @Autowired
     private ObjectBridge objectBridge;
 
     @Autowired
@@ -210,26 +220,26 @@
 
         for ( T object : objects )
         {
-            log.info( "Importing object " + object + " (" + object.getClass().getSimpleName() + ")" );
-
             Set<AttributeValue> attributeValues = getAndClearAttributeValues( object );
+            Set<DataElementOperand> compulsoryDataElementOperands = getAndClearDataElementOperands( object, "compulsoryDataElementOperands" );
             Set<DataElementOperand> greyedFields = getAndClearDataElementOperands( object, "greyedFields" );
-            Set<DataElementOperand> compulsoryDataElementOperands = getAndClearDataElementOperands( object, "compulsoryDataElementOperands" );
             Expression leftSide = getAndClearExpression( object, "leftSide" );
             Expression rightSide = getAndClearExpression( object, "rightSide" );
 
             List<ImportConflict> conflicts = importObjectLocal( object, options );
+            importConflicts.addAll( conflicts );
 
             if ( !options.isDryRun() )
             {
                 sessionFactory.getCurrentSession().flush();
-            }
-
-            updateAttributeValues( object, attributeValues, options.isDryRun() );
-
-            if ( !conflicts.isEmpty() )
-            {
-                importConflicts.addAll( conflicts );
+
+                newAttributeValues( object, attributeValues );
+                newExpression( object, "leftSide", leftSide );
+                newExpression( object, "rightSide", rightSide );
+                newDataElementOperands( object, "compulsoryDataElementOperands", compulsoryDataElementOperands );
+                newDataElementOperands( object, "greyedFields", greyedFields );
+
+                sessionFactory.getCurrentSession().flush();
             }
         }
 
@@ -247,7 +257,7 @@
 
             if ( expression != null )
             {
-                ReflectionUtils.invokeSetterMethod( field, object, new Object[]{null} );
+                ReflectionUtils.invokeSetterMethod( field, object, new Object[]{ null } );
             }
         }
 
@@ -290,7 +300,71 @@
         return attributeValues;
     }
 
-    private void updateAttributeValues( T object, Set<AttributeValue> attributeValues, boolean dryRun )
+    private void newExpression( T object, String field, Expression expression )
+    {
+        if ( expression != null )
+        {
+            expression.setId( 0 );
+
+            Set<DataElement> dataElements = new HashSet<DataElement>();
+
+            for ( DataElement dataElement : expression.getDataElementsInExpression() )
+            {
+                DataElement de = objectBridge.getObject( dataElement );
+
+                if ( de != null )
+                {
+                    dataElements.add( de );
+                }
+                else
+                {
+                    // FIXME add warning
+                }
+            }
+
+            Set<DataElementCategoryOptionCombo> dataElementCategoryOptionCombos = new HashSet<DataElementCategoryOptionCombo>();
+
+            for ( DataElementCategoryOptionCombo dataElementCategoryOptionCombo : expression.getOptionCombosInExpression() )
+            {
+                DataElementCategoryOptionCombo optionCombo = objectBridge.getObject( dataElementCategoryOptionCombo );
+
+                if ( optionCombo != null )
+                {
+                    dataElementCategoryOptionCombos.add( dataElementCategoryOptionCombo );
+                }
+                else
+                {
+                    // FIXME add warning
+                }
+            }
+
+            expression.setDataElementsInExpression( dataElements );
+            expression.setOptionCombosInExpression( dataElementCategoryOptionCombos );
+
+            expressionService.addExpression( expression );
+
+            sessionFactory.getCurrentSession().flush();
+
+            ReflectionUtils.invokeSetterMethod( field, object, expression );
+        }
+    }
+
+    private void newDataElementOperands( T object, String field, Set<DataElementOperand> dataElementOperands )
+    {
+        if ( dataElementOperands.size() > 0 )
+        {
+            for ( DataElementOperand dataElementOperand : dataElementOperands )
+            {
+                dataElementOperand.setId( 0 );
+                dataElementOperandService.addDataElementOperand( dataElementOperand );
+                sessionFactory.getCurrentSession().flush();
+            }
+
+            ReflectionUtils.invokeSetterMethod( field, object, dataElementOperands );
+        }
+    }
+
+    private void newAttributeValues( T object, Set<AttributeValue> attributeValues )
     {
         if ( attributeValues.size() > 0 )
         {
@@ -313,17 +387,7 @@
                 attributeService.addAttributeValue( attributeValue );
             }
 
-            if ( !dryRun )
-            {
-                sessionFactory.getCurrentSession().flush();
-            }
-
             ReflectionUtils.invokeSetterMethod( "attributeValues", object, attributeValues );
-
-            if ( !dryRun )
-            {
-                sessionFactory.getCurrentSession().flush();
-            }
         }
     }
 
@@ -585,7 +649,7 @@
                 if ( ref != null )
                 {
                     identifiableObjects.put( field, ref );
-                    ReflectionUtils.invokeSetterMethod( field.getName(), identifiableObject, new Object[]{null} );
+                    ReflectionUtils.invokeSetterMethod( field.getName(), identifiableObject, new Object[]{ null } );
                 }
             }
 

=== modified file 'dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml	2012-05-16 18:08:43 +0000
+++ dhis-2/dhis-dxf2/src/main/resources/META-INF/dhis/beans.xml	2012-05-22 11:04:59 +0000
@@ -1,9 +1,12 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
-    xmlns:context="http://www.springframework.org/schema/context";
-    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd";>
-
-  <context:component-scan base-package="org.hisp.dhis.dxf2" />
+    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd";>
+
+  <bean id="org.hisp.dhis.dxf2.metadata.ExportService" class="org.hisp.dhis.dxf2.metadata.DefaultExportService" />
+
+  <bean id="org.hisp.dhis.dxf2.metadata.ImportService" class="org.hisp.dhis.dxf2.metadata.DefaultImportService" />
+
+  <bean id="org.hisp.dhis.dxf2.metadata.ObjectBridge" class="org.hisp.dhis.dxf2.metadata.DefaultObjectBridge" />
 
   <bean id="org.hisp.dhis.dxf2.datavalueset.DataValueSetService"
       class="org.hisp.dhis.dxf2.datavalueset.DefaultDataValueSetService" />
@@ -122,7 +125,8 @@
         value="org.hisp.dhis.organisationunit.OrganisationUnit" />
   </bean>
 
-  <bean id="organisationUnitLevelImporter" class="org.hisp.dhis.dxf2.metadata.importers.DefaultIdentifiableObjectImporter">
+  <bean id="organisationUnitLevelImporter"
+      class="org.hisp.dhis.dxf2.metadata.importers.DefaultIdentifiableObjectImporter">
     <constructor-arg name="importerClass" type="java.lang.Class"
         value="org.hisp.dhis.organisationunit.OrganisationUnitLevel" />
   </bean>