← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9914: Web api, added custom serializer for some report table associations

 

------------------------------------------------------------
revno: 9914
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-02-27 12:10:13 +0100
message:
  Web api, added custom serializer for some report table associations
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonMapListIdentifiableObjectSerializer.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.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
=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonMapListIdentifiableObjectSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonMapListIdentifiableObjectSerializer.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonMapListIdentifiableObjectSerializer.java	2013-02-27 11:10:13 +0000
@@ -0,0 +1,71 @@
+package org.hisp.dhis.common.adapter;
+
+/*
+ * Copyright (c) 2004-2012, 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 java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import org.hisp.dhis.common.IdentifiableObject;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class JacksonMapListIdentifiableObjectSerializer
+    extends JsonSerializer<Map<String, List<IdentifiableObject>>>
+{
+    @Override
+    public void serialize( Map<String, List<IdentifiableObject>> value, JsonGenerator jgen, SerializerProvider provider )
+        throws IOException
+    {
+        if ( value != null )
+        {
+            jgen.writeStartObject();
+            
+            for ( String key : value.keySet() )
+            {
+                jgen.writeArrayFieldStart( key );
+                
+                for ( IdentifiableObject object : value.get( key ) )
+                {
+                    jgen.writeStartObject();
+                    jgen.writeStringField( "id", object.getUid() );
+                    jgen.writeEndObject();
+                }
+                
+                jgen.writeEndArray();                
+            }
+            
+            jgen.writeEndObject();
+        }
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java	2011-12-20 12:46:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameter.java	2013-02-27 11:10:13 +0000
@@ -1,12 +1,38 @@
 package org.hisp.dhis.common.adapter;
 
+/*
+ * Copyright (c) 2004-2012, 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 javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlType;
 
 @XmlType(propOrder={"value", "key"})
 public class Parameter
 {
-
     private String key;
 
     private String value;
@@ -42,5 +68,4 @@
     {
         this.value = value;
     }
-
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java	2011-12-20 12:46:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/Parameters.java	2013-02-27 11:10:13 +0000
@@ -1,12 +1,38 @@
 package org.hisp.dhis.common.adapter;
 
+/*
+ * Copyright (c) 2004-2012, 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 java.util.List;
 
 import javax.xml.bind.annotation.XmlElement;
 
 public class Parameters
 {
-
     List<Parameter> parameters;
 
     public Parameters()
@@ -27,7 +53,5 @@
     public void setParameters( List<Parameter> parameters )
     {
         this.parameters = parameters;
-    }
-
-    
+    }    
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java	2011-12-20 12:46:37 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/ParametersMapXmlAdapter.java	2013-02-27 11:10:13 +0000
@@ -1,36 +1,66 @@
 package org.hisp.dhis.common.adapter;
 
+/*
+ * Copyright (c) 2004-2012, 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 java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 
-
-public class ParametersMapXmlAdapter extends XmlAdapter<Parameters, Map<String,String>> {
-
+public class ParametersMapXmlAdapter
+    extends XmlAdapter<Parameters, Map<String, String>>
+{
     @Override
     public Parameters marshal( Map<String, String> v )
         throws Exception
     {
         ArrayList<Parameter> list = new ArrayList<Parameter>();
-        
-        for ( Map.Entry<String,String> e : v.entrySet() ) {
-
-            list.add( new Parameter(e.getKey(), e.getValue()) );
+
+        for ( Map.Entry<String, String> e : v.entrySet() )
+        {
+
+            list.add( new Parameter( e.getKey(), e.getValue() ) );
         }
-        return new Parameters(list);
+        return new Parameters( list );
     }
 
     @Override
-    public Map<String, String> unmarshal( Parameters v)
+    public Map<String, String> unmarshal( Parameters v )
         throws Exception
     {
-        Map<String,String> map = new HashMap<String,String>();
-        for ( Parameter p : v.getParameters() ) {
-            map.put(p.getKey(), p.getValue());
+        Map<String, String> map = new HashMap<String, String>();
+        
+        for ( Parameter p : v.getParameters() )
+        {
+            map.put( p.getKey(), p.getValue() );
         }
+        
         return map;
     }
-
 }
\ No newline at end of file

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2013-02-27 10:13:13 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/reporttable/ReportTable.java	2013-02-27 11:10:13 +0000
@@ -37,6 +37,7 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.common.*;
+import org.hisp.dhis.common.adapter.JacksonMapListIdentifiableObjectSerializer;
 import org.hisp.dhis.common.adapter.JacksonPeriodDeserializer;
 import org.hisp.dhis.common.adapter.JacksonPeriodSerializer;
 import org.hisp.dhis.common.annotation.Scanned;
@@ -555,7 +556,7 @@
         
         for ( OrganisationUnitGroup group : organisationUnitGroups )
         {
-            ougs.putValue( group.getUid(), group );
+            ougs.putValue( group.getGroupSet().getUid(), group );
         }
         
         dataElementGroupSets.clear();
@@ -1337,9 +1338,10 @@
     // Get- and set-methods for presentation properties
     // -------------------------------------------------------------------------
 
-    @JsonProperty( value = "dataElementGroupSets" )
-    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    @JsonProperty
+    @JsonSerialize( using = JacksonMapListIdentifiableObjectSerializer.class )
     @JsonView( {DetailedView.class, ExportView.class} )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0)
     public Map<String, List<DataElementGroup>> getDataElementGroupSets()
     {
         return dataElementGroupSets;
@@ -1350,9 +1352,10 @@
         this.dataElementGroupSets = dataElementGroupSets;
     }
 
-    @JsonProperty( value = "organisationUnitGroupSets" )
-    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    @JsonProperty
+    @JsonSerialize( using = JacksonMapListIdentifiableObjectSerializer.class )
     @JsonView( {DetailedView.class, ExportView.class} )
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0)
     public Map<String, List<OrganisationUnitGroup>> getOrganisationUnitGroupSets()
     {
         return organisationUnitGroupSets;