← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16211: FormUtils, meta data

 

------------------------------------------------------------
revno: 16211
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2014-07-22 19:57:36 +0200
message:
  FormUtils, meta data
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/FormUtils.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/form/Group.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/BaseNameableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java	2014-07-22 17:57:36 +0000
@@ -85,14 +85,26 @@
         this.name = name;
     }
 
-    public BaseNameableObject( int id, String uid, String name, String shortName,
-        String code, String description )
+    public BaseNameableObject( int id, String uid, String name, String shortName, String code, String description )
     {
         super( id, uid, name );
         this.shortName = shortName;
         this.code = code;
         this.description = description;
     }
+    
+    public BaseNameableObject( NameableObject object )
+    {
+        super( object.getId(), object.getUid(), object.getName() );
+        this.shortName = object.getShortName();
+        this.code = object.getCode();
+        this.description = object.getDescription();
+    }
+
+    // -------------------------------------------------------------------------
+    // hashCode and equals
+    // -------------------------------------------------------------------------
+
 
     @Override
     public int hashCode()
@@ -144,6 +156,10 @@
         return true;
     }
 
+    // -------------------------------------------------------------------------
+    // Getters and setters
+    // -------------------------------------------------------------------------
+
     @JsonProperty
     @JsonView( { ShortNameView.class, DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( isAttribute = true )

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java	2014-07-21 15:28:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/NameableObjectUtils.java	2014-07-22 17:57:36 +0000
@@ -31,7 +31,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedHashMap;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -137,15 +137,35 @@
     }
 
     /**
-     * Returns a mapping between the uid and the nameable objects. The order of
-     * the objects are preserved.
+     * Returns a list of BaseNameableObjects based on the given list of NameableObjects.
+     * 
+     * @param objects the list of NameableObjects.
+     * @return a list of BaseNameableObejcts.
+     */
+    public static List<NameableObject> getAsNameableObjects( List<? extends NameableObject> objects )
+    {
+        List<NameableObject> list = new ArrayList<>();
+        
+        for ( NameableObject object : objects )
+        {
+            if ( object != null )
+            {
+                list.add( new BaseNameableObject( object ) );
+            }
+        }
+        
+        return list;
+    }
+
+    /**
+     * Returns a mapping between the uid and the nameable objects.
      *
      * @param objects the nameable objects.
-     * @return ordered mapping between the uid and the nameable objects.
+     * @return mapping between the uid and the nameable objects.
      */
     public static Map<String, NameableObject> getUidObjectMap( List<? extends NameableObject> objects )
     {
-        Map<String, NameableObject> map = new LinkedHashMap<String, NameableObject>();
+        Map<String, NameableObject> map = new HashMap<String, NameableObject>();
 
         if ( objects != null )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/FormUtils.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/FormUtils.java	2014-07-21 15:28:57 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/utils/FormUtils.java	2014-07-22 17:57:36 +0000
@@ -60,23 +60,27 @@
  */
 public class FormUtils
 {
+    private static final String KEY_PERIOD_TYPE = "periodType";
+    private static final String KEY_ALLOW_FUTURE_PERIODS = "allowFuturePeriods";
+    private static final String KEY_DATA_ELEMENTS = "dataElements";
+    
     public static Form fromDataSet( DataSet dataSet, boolean metaData )
     {
         Form form = new Form();
         form.setLabel( dataSet.getDisplayName() );
         form.setSubtitle( dataSet.getDisplayShortName() );
 
-        form.getOptions().put( "periodType", dataSet.getPeriodType().getName() );
-        form.getOptions().put( "allowFuturePeriods", dataSet.isAllowFuturePeriods() );
+        form.getOptions().put( KEY_PERIOD_TYPE, dataSet.getPeriodType().getName() );
+        form.getOptions().put( KEY_ALLOW_FUTURE_PERIODS, dataSet.isAllowFuturePeriods() );
 
         if ( dataSet.getSections().size() > 0 )
         {
-            List<Section> sections = new ArrayList<Section>( dataSet.getSections() );
+            List<Section> sections = new ArrayList<>( dataSet.getSections() );
             Collections.sort( sections, SectionOrderComparator.INSTANCE );
             
             for ( Section section : sections )
             {
-                List<Field> fields = inputsFromDataElements( new ArrayList<DataElement>( section.getDataElements() ), new ArrayList<DataElementOperand>( section.getGreyedFields() ) );
+                List<Field> fields = inputsFromDataElements( new ArrayList<>( section.getDataElements() ), new ArrayList<>( section.getGreyedFields() ) );
 
                 Group group = new Group();
                 group.setLabel( section.getDisplayName() );
@@ -86,7 +90,7 @@
                 
                 if ( metaData )
                 {
-                    group.setMetaData( NameableObjectUtils.getUidObjectMap( section.getDataElements() ) );
+                    group.getMetaData().put( KEY_DATA_ELEMENTS, NameableObjectUtils.getAsNameableObjects( section.getDataElements() ) );
                 }
                 
                 form.getGroups().add( group );
@@ -94,7 +98,7 @@
         }
         else
         {
-            List<Field> fields = inputsFromDataElements( new ArrayList<DataElement>( dataSet.getDataElements() ) );
+            List<Field> fields = inputsFromDataElements( new ArrayList<>( dataSet.getDataElements() ) );
 
             Group group = new Group();
             group.setLabel( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
@@ -104,7 +108,7 @@
 
             if ( metaData )
             {
-                group.setMetaData( NameableObjectUtils.getUidObjectMap( new ArrayList<DataElement>( dataSet.getDataElements() ) ) );
+                group.getMetaData().put( KEY_DATA_ELEMENTS, NameableObjectUtils.getAsNameableObjects( new ArrayList<>( dataSet.getDataElements() ) ) );
             }
             
             form.getGroups().add( group );
@@ -113,7 +117,6 @@
         return form;
     }
 
-
     public static Form fromProgram( Program program )
     {
         Assert.notNull( program );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/form/Group.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/form/Group.java	2014-07-21 15:28:57 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/webdomain/form/Group.java	2014-07-22 17:57:36 +0000
@@ -29,15 +29,13 @@
  */
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.hisp.dhis.common.BaseNameableObject;
 import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.NameableObject;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
-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;
@@ -56,7 +54,7 @@
 
     private List<Field> fields = new ArrayList<Field>();
 
-    private Map<String, NameableObject> metaData;
+    private Map<Object, Object> metaData = new HashMap<Object, Object>();
 
     public Group()
     {
@@ -112,15 +110,14 @@
     }
 
     @JsonProperty
-    @JsonSerialize( contentAs = BaseNameableObject.class )
     @JacksonXmlElementWrapper( localName = "metaData", namespace = DxfNamespaces.DXF_2_0 )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
-    public Map<String, NameableObject> getMetaData()
+    public Map<Object, Object> getMetaData()
     {
         return metaData;
     }
     
-    public void setMetaData( Map<String, NameableObject> metaData )
+    public void setMetaData( Map<Object, Object> metaData )
     {
         this.metaData = metaData;
     }