← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17611: wip, fixes for dataElementOperands

 

------------------------------------------------------------
revno: 17611
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-11-29 12:00:47 +0700
message:
  wip, fixes for dataElementOperands
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseMetaDataCollectionObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementOperand.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/Section.java
  dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/DataSet.hbm.xml
  dhis-2/dhis-services/dhis-service-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/common/BaseMetaDataCollectionObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseMetaDataCollectionObject.java	2014-10-25 21:58:41 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseMetaDataCollectionObject.java	2014-11-29 05:00:47 +0000
@@ -129,8 +129,7 @@
     }
 
     @JsonProperty
-    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
-    @JsonView( DetailedView.class )
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlElementWrapper( localName = "dataElementOperands", namespace = DxfNamespaces.DXF_2_0 )
     @JacksonXmlProperty( localName = "dataElementOperand", namespace = DxfNamespaces.DXF_2_0 )
     public List<DataElementOperand> getDataElementOperands()

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementOperand.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementOperand.java	2014-11-18 11:35:52 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementOperand.java	2014-11-29 05:00:47 +0000
@@ -31,6 +31,8 @@
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.common.BaseIdentifiableObject;
@@ -149,6 +151,12 @@
     // -------------------------------------------------------------------------
 
     @Override
+    public boolean haveUniqueNames()
+    {
+        return false;
+    }
+
+    @Override
     public String getUid()
     {
         if ( uid != null )
@@ -254,7 +262,7 @@
     public Integer getRelevantAggregationLevel( int organisationUnitLevel )
     {
         List<Integer> levels = new ArrayList<>( aggregationLevels );
-        
+
         Collections.sort( levels );
 
         for ( final Integer aggregationLevel : levels )
@@ -426,6 +434,7 @@
     @JsonProperty
     @JsonSerialize( as = BaseIdentifiableObject.class )
     @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public DataElement getDataElement()
     {
         return dataElement;
@@ -439,6 +448,7 @@
     @JsonProperty
     @JsonSerialize( as = BaseIdentifiableObject.class )
     @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public DataElementCategoryOptionCombo getCategoryOptionCombo()
     {
         return categoryOptionCombo;
@@ -451,6 +461,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getDataElementId()
     {
         return dataElementId;
@@ -463,6 +474,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getOptionComboId()
     {
         return optionComboId;
@@ -475,6 +487,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getOperandId()
     {
         return operandId;
@@ -487,6 +500,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getOperandName()
     {
         return operandName;
@@ -499,6 +513,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getValueType()
     {
         return valueType;
@@ -511,6 +526,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getAggregationOperator()
     {
         return aggregationOperator;
@@ -523,6 +539,8 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlElementWrapper( localName = "aggregationLevels", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "aggregationLevel", namespace = DxfNamespaces.DXF_2_0 )
     public List<Integer> getAggregationLevels()
     {
         return aggregationLevels;
@@ -535,6 +553,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public int getFrequencyOrder()
     {
         return frequencyOrder;
@@ -547,6 +566,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getOperandType()
     {
         return operandType;
@@ -559,6 +579,7 @@
 
     @JsonProperty
     @JsonView( { DetailedView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public boolean isHasAggregationLevels()
     {
         return hasAggregationLevels;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/Section.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/Section.java	2014-09-30 09:08:21 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/Section.java	2014-11-29 05:00:47 +0000
@@ -28,11 +28,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonView;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
@@ -44,14 +45,12 @@
 import org.hisp.dhis.dataelement.DataElementOperand;
 import org.hisp.dhis.indicator.Indicator;
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
-@JacksonXmlRootElement(localName = "section", namespace = DxfNamespaces.DXF_2_0)
+@JacksonXmlRootElement( localName = "section", namespace = DxfNamespaces.DXF_2_0 )
 public class Section
     extends BaseIdentifiableObject
 {
@@ -61,7 +60,7 @@
     private static final long serialVersionUID = -4657657995917502852L;
 
     private String description;
-    
+
     private DataSet dataSet;
 
     @Scanned
@@ -156,7 +155,7 @@
                     {
                         return true;
                     }
-    
+
                     categoryCombo = element.getCategoryCombo();
                 }
             }
@@ -164,7 +163,7 @@
 
         return false;
     }
-    
+
     public boolean hasDataElements()
     {
         return dataElements != null && !dataElements.isEmpty();
@@ -181,22 +180,22 @@
     // -------------------------------------------------------------------------
 
     @JsonProperty
-    @JsonView({ DetailedView.class, ExportView.class })
+    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public String getDescription()
     {
         return description;
     }
-    
+
     public void setDescription( String description )
     {
         this.description = description;
     }
-    
+
     @JsonProperty
-    @JsonSerialize(as = BaseIdentifiableObject.class)
-    @JsonView({ DetailedView.class, ExportView.class })
-    @JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
+    @JsonSerialize( as = BaseIdentifiableObject.class )
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public DataSet getDataSet()
     {
         return dataSet;
@@ -208,10 +207,10 @@
     }
 
     @JsonProperty
-    @JsonSerialize(contentAs = BaseIdentifiableObject.class)
-    @JsonView({ DetailedView.class, ExportView.class })
-    @JacksonXmlElementWrapper(localName = "dataElements", namespace = DxfNamespaces.DXF_2_0)
-    @JacksonXmlProperty(localName = "dataElement", namespace = DxfNamespaces.DXF_2_0)
+    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlElementWrapper( localName = "dataElements", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "dataElement", namespace = DxfNamespaces.DXF_2_0 )
     public List<DataElement> getDataElements()
     {
         return dataElements;
@@ -223,10 +222,10 @@
     }
 
     @JsonProperty
-    @JsonSerialize(contentAs = BaseIdentifiableObject.class)
-    @JsonView({ DetailedView.class, ExportView.class })
-    @JacksonXmlElementWrapper(localName = "indicators", namespace = DxfNamespaces.DXF_2_0)
-    @JacksonXmlProperty(localName = "indicator", namespace = DxfNamespaces.DXF_2_0)
+    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlElementWrapper( localName = "indicators", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "indicator", namespace = DxfNamespaces.DXF_2_0 )
     public List<Indicator> getIndicators()
     {
         return indicators;
@@ -248,9 +247,9 @@
     }
 
     @JsonProperty
-    @JsonView({ DetailedView.class, ExportView.class })
-    @JacksonXmlElementWrapper(localName = "greyedFields", namespace = DxfNamespaces.DXF_2_0)
-    @JacksonXmlProperty(localName = "greyedField", namespace = DxfNamespaces.DXF_2_0)
+    @JsonView( { DetailedView.class, ExportView.class } )
+    @JacksonXmlElementWrapper( localName = "greyedFields", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "greyedField", namespace = DxfNamespaces.DXF_2_0 )
     public Set<DataElementOperand> getGreyedFields()
     {
         return greyedFields;

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/DataSet.hbm.xml'
--- dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/DataSet.hbm.xml	2014-10-13 13:58:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/dataset/hibernate/DataSet.hbm.xml	2014-11-29 05:00:47 +0000
@@ -38,7 +38,7 @@
     </set>
 
     <set name="compulsoryDataElementOperands" table="datasetoperands" cascade="all-delete-orphan">
-      <key column="datasetid" foreign-key="fk_datasetoperands_datasetid" />
+g      <key column="datasetid" foreign-key="fk_datasetoperands_datasetid" />
       <many-to-many class="org.hisp.dhis.dataelement.DataElementOperand" column="dataelementoperandid"
         foreign-key="fk_dataset_dataelementoperandid" />
     </set>

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2014-11-24 14:15:14 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2014-11-29 05:00:47 +0000
@@ -131,7 +131,7 @@
     @Autowired
     private SchemaService schemaService;
 
-    @Autowired(required = false)
+    @Autowired( required = false )
     private List<ObjectHandler<T>> objectHandlers;
 
     @Autowired
@@ -674,6 +674,12 @@
     private Map<Field, Object> detachFields( final Object object )
     {
         final Map<Field, Object> fieldMap = Maps.newHashMap();
+
+        if ( object == null )
+        {
+            return fieldMap;
+        }
+
         final Collection<Field> fieldCollection = ReflectionUtils.collectFields( object.getClass(), idObjects );
 
         for ( Field field : fieldCollection )
@@ -832,6 +838,7 @@
 
         private Set<DataElementOperand> compulsoryDataElementOperands = Sets.newHashSet();
         private Set<DataElementOperand> greyedFields = Sets.newHashSet();
+        private List<DataElementOperand> dataElementOperands = Lists.newArrayList();
 
         private Collection<ProgramStageDataElement> programStageDataElements = Lists.newArrayList();
         private List<ProgramTrackedEntityAttribute> programTrackedEntityAttributes = new ArrayList<>();
@@ -842,8 +849,9 @@
             leftSide = extractExpression( object, "leftSide" );
             rightSide = extractExpression( object, "rightSide" );
             dataEntryForm = extractDataEntryForm( object, "dataEntryForm" );
-            compulsoryDataElementOperands = extractDataElementOperands( object, "compulsoryDataElementOperands" );
-            greyedFields = extractDataElementOperands( object, "greyedFields" );
+            compulsoryDataElementOperands = Sets.newHashSet( extractDataElementOperands( object, "compulsoryDataElementOperands" ) );
+            greyedFields = Sets.newHashSet( extractDataElementOperands( object, "greyedFields" ) );
+            dataElementOperands = Lists.newArrayList( extractDataElementOperands( object, "dataElementOperands" ) );
             programStageDataElements = extractProgramStageDataElements( object );
             programTrackedEntityAttributes = extractProgramTrackedEntityAttributes( object );
         }
@@ -860,9 +868,10 @@
                 if ( options.getImportStrategy().isDelete() )
                 {
                     deleteDataElementOperands( object, "compulsoryDataElementOperands" );
+                    deleteDataElementOperands( object, "dataElementOperands" );
+                    deleteDataElementOperands( object, "greyedFields" );
                 }
 
-                deleteDataElementOperands( object, "greyedFields" );
                 deleteProgramStageDataElements( object );
                 deleteProgramTrackedEntityAttributes( object );
             }
@@ -876,6 +885,7 @@
             saveDataEntryForm( object, "dataEntryForm", dataEntryForm );
             saveDataElementOperands( object, "compulsoryDataElementOperands", compulsoryDataElementOperands );
             saveDataElementOperands( object, "greyedFields", greyedFields );
+            saveDataElementOperands( object, "dataElementOperands", dataElementOperands );
             saveProgramStageDataElements( object, programStageDataElements );
             saveProgramTrackedEntityAttributes( object, programTrackedEntityAttributes );
         }
@@ -944,20 +954,18 @@
             return expression;
         }
 
-        private Set<DataElementOperand> extractDataElementOperands( T object, String fieldName )
+        private Collection<DataElementOperand> extractDataElementOperands( T object, String fieldName )
         {
-            Set<DataElementOperand> dataElementOperands = Sets.newHashSet();
+            Collection<DataElementOperand> dataElementOperands = Sets.newHashSet();
 
             if ( ReflectionUtils.findGetterMethod( fieldName, object ) != null )
             {
-                Set<DataElementOperand> detachedDataElementOperands = ReflectionUtils.invokeGetterMethod( fieldName, object );
-                dataElementOperands = Sets.newHashSet( detachedDataElementOperands );
+                Collection<DataElementOperand> detachedDataElementOperands = ReflectionUtils.invokeGetterMethod( fieldName, object );
 
-                if ( detachedDataElementOperands.size() > 0 )
-                {
-                    detachedDataElementOperands.clear();
-                    ReflectionUtils.invokeSetterMethod( fieldName, object, Sets.newHashSet() );
-                }
+                dataElementOperands = ReflectionUtils.newCollectionInstance( detachedDataElementOperands.getClass() );
+                dataElementOperands.addAll( detachedDataElementOperands );
+                detachedDataElementOperands.clear();
+                // ReflectionUtils.invokeSetterMethod( fieldName, object, ReflectionUtils.newCollectionInstance( detachedDataElementOperands.getClass() ) );
             }
 
             return dataElementOperands;
@@ -994,38 +1002,28 @@
             }
         }
 
-        private void saveDataElementOperands( T object, String fieldName, Set<DataElementOperand> dataElementOperands )
+        private void saveDataElementOperands( T object, String fieldName, Collection<DataElementOperand> dataElementOperands )
         {
-            if ( dataElementOperands.size() > 0 )
-            {
-                // need special handling for compulsoryDataElementOperands since they cascade with all-delete-orphan
-                if ( "compulsoryDataElementOperands".equals( fieldName ) )
-                {
-                    for ( DataElementOperand dataElementOperand : dataElementOperands )
-                    {
-                        Map<Field, Object> identifiableObjects = detachFields( dataElementOperand );
-                        reattachFields( dataElementOperand, identifiableObjects );
-                    }
-
-                    Set<DataElementOperand> detachedDataElementOperands = ReflectionUtils.invokeGetterMethod( fieldName, object );
-                    detachedDataElementOperands.clear();
-                    detachedDataElementOperands.addAll( dataElementOperands );
-                    sessionFactory.getCurrentSession().flush();
-                }
-                else
-                {
-                    for ( DataElementOperand dataElementOperand : dataElementOperands )
-                    {
-                        Map<Field, Object> identifiableObjects = detachFields( dataElementOperand );
-                        reattachFields( dataElementOperand, identifiableObjects );
-
-                        dataElementOperand.setId( 0 );
-                        dataElementOperandService.addDataElementOperand( dataElementOperand );
-                    }
-
-                    ReflectionUtils.invokeSetterMethod( fieldName, object, dataElementOperands );
-                }
-            }
+            for ( DataElementOperand dataElementOperand : dataElementOperands )
+            {
+                Map<Field, Object> identifiableObjects = detachFields( dataElementOperand );
+                reattachFields( dataElementOperand, identifiableObjects );
+
+                dataElementOperandService.addDataElementOperand( dataElementOperand );
+                sessionFactory.getCurrentSession().flush();
+            }
+
+            Collection<DataElementOperand> detachedDataElementOperands = ReflectionUtils.invokeGetterMethod( fieldName, object );
+
+            if ( detachedDataElementOperands == null )
+            {
+                detachedDataElementOperands = ReflectionUtils.newCollectionInstance( dataElementOperands.getClass() );
+                ReflectionUtils.invokeSetterMethod( fieldName, object, detachedDataElementOperands );
+            }
+
+            detachedDataElementOperands.clear();
+            detachedDataElementOperands.addAll( dataElementOperands );
+            sessionFactory.getCurrentSession().flush();
         }
 
         private void deleteAttributeValues( T object )
@@ -1080,7 +1078,7 @@
 
         private void deleteDataElementOperands( T object, String fieldName )
         {
-            Set<DataElementOperand> dataElementOperands = extractDataElementOperands( object, fieldName );
+            Collection<DataElementOperand> dataElementOperands = extractDataElementOperands( object, fieldName );
 
             for ( DataElementOperand dataElementOperand : dataElementOperands )
             {