← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18214: added NodeUtils class, creates common node constructs (metadata, pager, etc)

 

------------------------------------------------------------
revno: 18214
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-02-10 16:04:29 +0700
message:
  added NodeUtils class, creates common node constructs (metadata, pager, etc)
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeUtils.java
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DimensionController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/IndexController.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/node/NodeUtils.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeUtils.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/NodeUtils.java	2015-02-10 09:04:29 +0000
@@ -0,0 +1,83 @@
+package org.hisp.dhis.node;
+
+/*
+ * Copyright (c) 2004-2015, 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 org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.Pager;
+import org.hisp.dhis.node.types.ComplexNode;
+import org.hisp.dhis.node.types.RootNode;
+import org.hisp.dhis.node.types.SimpleNode;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public final class NodeUtils
+{
+    public static RootNode createRootNode( Node node )
+    {
+        RootNode rootNode = new RootNode( node );
+        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
+        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
+
+        return rootNode;
+    }
+
+    public static RootNode createMetadata()
+    {
+        RootNode rootNode = new RootNode( "metadata" );
+        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
+        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
+
+        return rootNode;
+    }
+
+    public static RootNode createMetadata( Node child )
+    {
+        RootNode rootNode = createMetadata();
+        rootNode.addChild( child );
+
+        return rootNode;
+    }
+
+    public static Node createPager( Pager pager )
+    {
+        ComplexNode pagerNode = new ComplexNode( "pager" );
+        pagerNode.addChild( new SimpleNode( "page", pager.getPage() ) );
+        pagerNode.addChild( new SimpleNode( "pageCount", pager.getPageCount() ) );
+        pagerNode.addChild( new SimpleNode( "total", pager.getTotal() ) );
+        pagerNode.addChild( new SimpleNode( "nextPage", pager.getNextPage() ) );
+        pagerNode.addChild( new SimpleNode( "prevPage", pager.getPrevPage() ) );
+
+        return pagerNode;
+    }
+
+    private NodeUtils()
+    {
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-02-02 09:26:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AbstractCrudController.java	2015-02-10 09:04:29 +0000
@@ -34,7 +34,6 @@
 import com.google.common.collect.Lists;
 import org.hisp.dhis.acl.AclService;
 import org.hisp.dhis.common.BaseIdentifiableObject;
-import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.Pager;
@@ -50,9 +49,9 @@
 import org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.node.Node;
+import org.hisp.dhis.node.NodeUtils;
 import org.hisp.dhis.node.config.InclusionStrategy;
 import org.hisp.dhis.node.types.CollectionNode;
-import org.hisp.dhis.node.types.ComplexNode;
 import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.node.types.SimpleNode;
 import org.hisp.dhis.schema.Property;
@@ -234,20 +233,12 @@
 
         linkService.generatePagerLinks( pager, getEntityClass() );
 
-        RootNode rootNode = new RootNode( "metadata" );
-        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
-        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
-
+        RootNode rootNode = NodeUtils.createMetadata();
         rootNode.getConfig().setInclusionStrategy( getInclusionStrategy( rpParameters.get( "inclusionStrategy" ) ) );
 
         if ( pager != null )
         {
-            ComplexNode pagerNode = rootNode.addChild( new ComplexNode( "pager" ) );
-            pagerNode.addChild( new SimpleNode( "page", pager.getPage() ) );
-            pagerNode.addChild( new SimpleNode( "pageCount", pager.getPageCount() ) );
-            pagerNode.addChild( new SimpleNode( "total", pager.getTotal() ) );
-            pagerNode.addChild( new SimpleNode( "nextPage", pager.getNextPage() ) );
-            pagerNode.addChild( new SimpleNode( "prevPage", pager.getPrevPage() ) );
+            rootNode.addChild( NodeUtils.createPager( pager ) );
         }
 
         rootNode.addChild( fieldFilterService.filter( getEntityClass(), entityList, fields ) );
@@ -370,21 +361,14 @@
 
         if ( options.isTrue( "useWrapper" ) || entities.size() > 1 )
         {
-            RootNode rootNode = new RootNode( "metadata" );
-            rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
-            rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
-            rootNode.addChild( collectionNode );
-
+            RootNode rootNode = NodeUtils.createMetadata( collectionNode );
             rootNode.getConfig().setInclusionStrategy( getInclusionStrategy( parameters.get( "inclusionStrategy" ) ) );
 
             return rootNode;
         }
         else
         {
-            RootNode rootNode = new RootNode( collectionNode.getChildren().get( 0 ) );
-            rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
-            rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
-
+            RootNode rootNode = NodeUtils.createRootNode( collectionNode.getChildren().get( 0 ) );
             rootNode.getConfig().setInclusionStrategy( getInclusionStrategy( parameters.get( "inclusionStrategy" ) ) );
 
             return rootNode;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DimensionController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DimensionController.java	2015-02-02 09:26:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DimensionController.java	2015-02-10 09:04:29 +0000
@@ -38,6 +38,7 @@
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.node.AbstractNode;
 import org.hisp.dhis.node.Node;
+import org.hisp.dhis.node.NodeUtils;
 import org.hisp.dhis.node.types.CollectionNode;
 import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.webapi.utils.ContextUtils;
@@ -109,9 +110,7 @@
         items = objectFilterService.filter( items, filters );
         Collections.sort( items, IdentifiableObjectNameComparator.INSTANCE );
 
-        RootNode rootNode = new RootNode( "metadata" );
-        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
-        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
+        RootNode rootNode = NodeUtils.createMetadata();
 
         CollectionNode collectionNode = rootNode.addChild( fieldFilterService.filter( getEntityClass(), items, fields ) );
         collectionNode.setName( "items" );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/IndexController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/IndexController.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/IndexController.java	2015-02-10 09:04:29 +0000
@@ -30,6 +30,7 @@
 
 import org.apache.commons.lang.StringUtils;
 import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.node.NodeUtils;
 import org.hisp.dhis.node.types.CollectionNode;
 import org.hisp.dhis.node.types.ComplexNode;
 import org.hisp.dhis.node.types.RootNode;
@@ -86,10 +87,7 @@
 
     private RootNode createRootNode()
     {
-        RootNode rootNode = new RootNode( "metadata" );
-        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
-        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
-
+        RootNode rootNode = NodeUtils.createMetadata();
         CollectionNode collectionNode = rootNode.addChild( new CollectionNode( "resources" ) );
 
         for ( Schema schema : schemaService.getSchemas() )