← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15554: minor api changes to NodeService

 

------------------------------------------------------------
revno: 15554
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-06-04 18:22:56 +0200
message:
  minor api changes to NodeService
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeDeserializer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeSerializer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/DefaultNodeService.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/node/NodeDeserializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeDeserializer.java	2014-06-04 11:18:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeDeserializer.java	2014-06-04 16:22:56 +0000
@@ -60,11 +60,14 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface NodeDeserializer
 {
+    List<String> contentTypes();
+
     RootNode deserialize( InputStream inputStream ) throws IOException;
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeSerializer.java	2014-06-01 11:39:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeSerializer.java	2014-06-04 16:22:56 +0000
@@ -32,13 +32,14 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface NodeSerializer
 {
+    List<String> contentTypes();
+
     void serialize( RootNode rootNode, OutputStream outputStream ) throws IOException;
-
-    String contentType();
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeService.java	2014-06-01 13:52:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeService.java	2014-06-04 16:22:56 +0000
@@ -31,15 +31,19 @@
 import org.hisp.dhis.node.types.RootNode;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface NodeService
 {
-    List<NodeSerializer> getSerializers();
+    NodeSerializer getNodeSerializer( String contentType );
 
     void serialize( RootNode rootNode, String contentType, OutputStream outputStream ) throws IOException;
+
+    NodeDeserializer getNodeDeserializer( String contentType );
+
+    RootNode deserialize( RootNode rootNode, String contentType, InputStream inputStream ) throws IOException;
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java	2014-06-04 11:18:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/JacksonJsonNodeSerializer.java	2014-06-04 16:22:56 +0000
@@ -32,6 +32,7 @@
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import com.google.common.collect.Lists;
 import org.hisp.dhis.node.Node;
 import org.hisp.dhis.node.NodeSerializer;
 import org.hisp.dhis.node.types.CollectionNode;
@@ -42,6 +43,7 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -54,9 +56,9 @@
     private final ObjectMapper objectMapper = new ObjectMapper();
 
     @Override
-    public String contentType()
+    public List<String> contentTypes()
     {
-        return CONTENT_TYPE;
+        return Lists.newArrayList( CONTENT_TYPE );
     }
 
     public JacksonJsonNodeSerializer()

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java	2014-06-04 11:18:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/serializers/StAXNodeSerializer.java	2014-06-04 16:22:56 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  */
 
+import com.google.common.collect.Lists;
 import org.hisp.dhis.node.Node;
 import org.hisp.dhis.node.NodeSerializer;
 import org.hisp.dhis.node.types.CollectionNode;
@@ -42,6 +43,7 @@
 import javax.xml.stream.XMLStreamWriter;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -54,9 +56,9 @@
     private final XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance();
 
     @Override
-    public String contentType()
+    public List<String> contentTypes()
     {
-        return CONTENT_TYPE;
+        return Lists.newArrayList( CONTENT_TYPE );
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/DefaultNodeService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/DefaultNodeService.java	2014-06-01 13:52:28 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/node/DefaultNodeService.java	2014-06-04 16:22:56 +0000
@@ -35,6 +35,7 @@
 
 import javax.annotation.PostConstruct;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.List;
 import java.util.Map;
@@ -47,32 +48,78 @@
     @Autowired( required = false )
     private List<NodeSerializer> nodeSerializers = Lists.newArrayList();
 
+    @Autowired(required = false)
+    private List<NodeDeserializer> nodeDeserializers = Lists.newArrayList();
+
     private Map<String, NodeSerializer> nodeSerializerMap = Maps.newHashMap();
 
+    private Map<String, NodeDeserializer> nodeDeserializerMap = Maps.newHashMap();
+
     @PostConstruct
     private void init()
     {
         for ( NodeSerializer nodeSerializer : nodeSerializers )
         {
-            nodeSerializerMap.put( nodeSerializer.contentType(), nodeSerializer );
+            for ( String contentType : nodeSerializer.contentTypes() )
+            {
+                nodeSerializerMap.put( contentType, nodeSerializer );
+            }
+        }
+
+        for ( NodeDeserializer nodeDeserializer : nodeDeserializers )
+        {
+            for ( String contentType : nodeDeserializer.contentTypes() )
+            {
+                nodeDeserializerMap.put( contentType, nodeDeserializer );
+            }
         }
     }
 
     @Override
-    public List<NodeSerializer> getSerializers()
+    public NodeSerializer getNodeSerializer( String contentType )
     {
-        return nodeSerializers;
+        if ( nodeSerializerMap.containsKey( contentType ) )
+        {
+            nodeSerializerMap.get( contentType );
+        }
+
+        return null;
     }
 
     @Override
     public void serialize( RootNode rootNode, String contentType, OutputStream outputStream ) throws IOException
     {
-        if ( !nodeSerializerMap.containsKey( contentType ) )
+        NodeSerializer nodeSerializer = getNodeSerializer( contentType );
+
+        if ( nodeSerializer == null )
         {
-            return;
+            return; // TODO throw exception?
         }
 
-        NodeSerializer nodeSerializer = nodeSerializerMap.get( contentType );
         nodeSerializer.serialize( rootNode, outputStream );
     }
+
+    @Override
+    public NodeDeserializer getNodeDeserializer( String contentType )
+    {
+        if ( nodeDeserializerMap.containsKey( contentType ) )
+        {
+            return nodeDeserializerMap.get( contentType );
+        }
+
+        return null;
+    }
+
+    @Override
+    public RootNode deserialize( RootNode rootNode, String contentType, InputStream inputStream ) throws IOException
+    {
+        NodeDeserializer nodeDeserializer = getNodeDeserializer( contentType );
+
+        if ( nodeDeserializer == null )
+        {
+            return null; // TODO throw exception?
+        }
+
+        return nodeDeserializer.deserialize( inputStream );
+    }
 }