← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13049: add custom serializer for writing out children part of orgUnit. Adds 'haveChildren' attribute to ...

 

------------------------------------------------------------
revno: 13049
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-11-28 14:52:05 +0100
message:
  add custom serializer for writing out children part of orgUnit. Adds 'haveChildren' attribute to xml, and 'haveChildren' property to json.
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonOrganisationUnitChildrenSerializer.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitGroup.vm


--
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/JacksonOrganisationUnitChildrenSerializer.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonOrganisationUnitChildrenSerializer.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/adapter/JacksonOrganisationUnitChildrenSerializer.java	2013-11-28 13:52:05 +0000
@@ -0,0 +1,104 @@
+package org.hisp.dhis.common.adapter;
+
+/*
+ * Copyright (c) 2004-2013, 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 com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class JacksonOrganisationUnitChildrenSerializer extends JsonSerializer<OrganisationUnit>
+{
+    private static DateFormat format = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" );
+
+    @Override
+    public void serialize( OrganisationUnit value, JsonGenerator jgen, SerializerProvider provider ) throws IOException, JsonProcessingException
+    {
+        if ( ToXmlGenerator.class.isAssignableFrom( jgen.getClass() ) )
+        {
+            ToXmlGenerator xmlGenerator = (ToXmlGenerator) jgen;
+
+            try
+            {
+                XMLStreamWriter staxWriter = xmlGenerator.getStaxWriter();
+
+                staxWriter.writeStartElement( DxfNamespaces.DXF_2_0, "child" );
+                staxWriter.writeAttribute( "id", value.getUid() );
+                staxWriter.writeAttribute( "name", value.getName() );
+                staxWriter.writeAttribute( "created", format.format( value.getCreated() ) );
+                staxWriter.writeAttribute( "lastUpdated", format.format( value.getLastUpdated() ) );
+
+                if ( value.getHref() != null )
+                {
+                    staxWriter.writeAttribute( "href", value.getHref() );
+                }
+
+                staxWriter.writeAttribute( "haveChildren", String.valueOf( value.hasChild() ) );
+                staxWriter.writeEndElement();
+            }
+            catch ( XMLStreamException e )
+            {
+                e.printStackTrace();
+            }
+        }
+        else
+        {
+            jgen.writeStartObject();
+
+            jgen.writeStringField( "id", value.getUid() );
+            jgen.writeStringField( "name", value.getName() );
+            jgen.writeFieldName( "created" );
+            provider.defaultSerializeDateValue( value.getCreated(), jgen );
+
+            jgen.writeFieldName( "lastUpdated" );
+            provider.defaultSerializeDateValue( value.getLastUpdated(), jgen );
+
+            if ( value.getHref() != null )
+            {
+                jgen.writeStringField( "href", value.getHref() );
+            }
+
+            jgen.writeBooleanField( "haveChildren", value.hasChild() );
+
+            jgen.writeEndObject();
+        }
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2013-10-16 13:35:17 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java	2013-11-28 13:52:05 +0000
@@ -41,6 +41,7 @@
 import org.hisp.dhis.common.BaseNameableObject;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.adapter.JacksonOrganisationUnitChildrenSerializer;
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.common.view.ExportView;
@@ -699,7 +700,8 @@
     }
 
     @JsonProperty
-    @JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    //@JsonSerialize( contentAs = BaseIdentifiableObject.class )
+    @JsonSerialize( contentUsing = JacksonOrganisationUnitChildrenSerializer.class)
     @JsonView( { DetailedView.class } )
     @JacksonXmlElementWrapper( localName = "children", namespace = DxfNamespaces.DXF_2_0 )
     @JacksonXmlProperty( localName = "child", namespace = DxfNamespaces.DXF_2_0 )

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitGroup.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitGroup.vm	2013-10-10 15:32:40 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-organisationunit/src/main/webapp/dhis-web-maintenance-organisationunit/organisationUnitGroup.vm	2013-11-28 13:52:05 +0000
@@ -23,41 +23,41 @@
 			<table class="listTable" id="listTable">
 				<col/>
 				<col width="145px"/>
-                <thead>				
-				<tr>
-					<th>$i18n.getString( "name" )</th>
-					<th class="{sorter: false}">$i18n.getString( "operations" )</th>
-				</tr>
-                </thead>
+        <thead>
+          <tr>
+            <th>$i18n.getString( "name" )</th>
+            <th class="{sorter: false}">$i18n.getString( "operations" )</th>
+          </tr>
+        </thead>
 				<tbody id="list">
 				#foreach( $organisationUnitGroup in $organisationUnitGroups )
 				<tr id="tr${organisationUnitGroup.id}">
 					<td onclick="showOrganisationUnitGroupDetails( $organisationUnitGroup.id )">$encoder.htmlEncode( $organisationUnitGroup.displayName )</td>
 					<td style="text-align:right">
 
-                        #if ( $security.canManage( $organisationUnitGroup ) )
-                        <a href="javascript:showSharingDialog('organisationUnitGroup', '$organisationUnitGroup.uid');" title="$i18n.getString( 'sharing_settings' )"><img src="../images/relationship.png" alt="$i18n.getString( 'sharing_settings' )"></a>
-                        #else
-                        <img src="../images/relationship-denied.png">
-                        #end
-
-                        #if( $security.canUpdate( $organisationUnitGroup ) )
-                        <a href="openUpdateOrganisationUnitGroup.action?id=$organisationUnitGroup.id" title="$i18n.getString( 'edit' )"><img src="../images/edit.png" alt="$i18n.getString( 'edit' )"/></a>
-                        #else
-                        <img src="../images/edit-denied.png">
-                        #end
-
-                        #if( $security.canUpdate( $organisationUnitGroup ) )
-                        <a href="javascript:translate( 'OrganisationUnitGroup', '$organisationUnitGroup.id' )"><img src="../images/i18n.png" alt="$i18n.getString( 'translation_translate' )"/></a>
-                        #else
-                        <img src="../images/edit-denied.png">
-                        #end
-
-                        #if( $security.canDelete( $organisationUnitGroup ) )
-                        <a href="javascript:removeOrganisationUnitGroup( $organisationUnitGroup.id, '$encoder.jsEncode( $organisationUnitGroup.displayName )' )" title="$i18n.getString( 'remove' )"><img src="../images/delete.png" alt="$i18n.getString( 'remove' )"/></a>
-                        #else
-                        <img src="../images/delete-denied.png">
-                        #end
+            #if ( $security.canManage( $organisationUnitGroup ) )
+            <a href="javascript:showSharingDialog('organisationUnitGroup', '$organisationUnitGroup.uid');" title="$i18n.getString( 'sharing_settings' )"><img src="../images/relationship.png" alt="$i18n.getString( 'sharing_settings' )"></a>
+            #else
+            <img src="../images/relationship-denied.png">
+            #end
+
+            #if( $security.canUpdate( $organisationUnitGroup ) )
+            <a href="openUpdateOrganisationUnitGroup.action?id=$organisationUnitGroup.id" title="$i18n.getString( 'edit' )"><img src="../images/edit.png" alt="$i18n.getString( 'edit' )"/></a>
+            #else
+            <img src="../images/edit-denied.png">
+            #end
+
+            #if( $security.canUpdate( $organisationUnitGroup ) )
+            <a href="javascript:translate( 'OrganisationUnitGroup', '$organisationUnitGroup.id' )"><img src="../images/i18n.png" alt="$i18n.getString( 'translation_translate' )"/></a>
+            #else
+            <img src="../images/edit-denied.png">
+            #end
+
+            #if( $security.canDelete( $organisationUnitGroup ) )
+            <a href="javascript:removeOrganisationUnitGroup( $organisationUnitGroup.id, '$encoder.jsEncode( $organisationUnitGroup.displayName )' )" title="$i18n.getString( 'remove' )"><img src="../images/delete.png" alt="$i18n.getString( 'remove' )"/></a>
+            #else
+            <img src="../images/delete-denied.png">
+            #end
 
 						<a href="javascript:showOrganisationUnitGroupDetails( $organisationUnitGroup.id )" title="$i18n.getString( 'show_details' )"><img src="../images/information.png" alt="$i18n.getString( 'show_details' )"/></a>
 					</td>
@@ -69,24 +69,24 @@
 			#parse( "/dhis-web-commons/paging/paging.vm" )
 
 		</td>
+
 		<td id="detailsData">
-
-            <div id="detailsArea">
-                <div id="hideDetailsArea">
-					<a href="javascript:hideDetails()" title="$i18n.getString( 'hide_details' )"><img src="../images/hide.png" alt="$i18n.getString( 'hide_details' )"/></a>
-				</div>				
+      <div id="detailsArea">
+        <div id="hideDetailsArea">
+          <a href="javascript:hideDetails()" title="$i18n.getString( 'hide_details' )"><img src="../images/hide.png" alt="$i18n.getString( 'hide_details' )"/></a>
+        </div>
 				<p><label>$i18n.getString( "name" ):</label><br/><span id="nameField"></span></p>
 				<p><label>$i18n.getString( "short_name" ):</label><br/><span id="shortNameField"></span></p>
 				<p><label>$i18n.getString( "code" ):</label><br/><span id="codeField"></span></p>
 				<p><label>$i18n.getString( "number_of_group_members" ):</label><br/><span id="memberCountField"></span></p>
 			</div>
 			
-            <div id="warningArea">
-                <div id="hideDetailsArea">
-                    <a href="javascript:hideWarning()" title="$i18n.getString( 'hide_warning' )"><img src="../images/hide.png" alt="$i18n.getString( 'hide_warning' )"/></a>
-                </div>
-                <p><span id="warningField"></span></p>
-            </div>
+      <div id="warningArea">
+        <div id="hideDetailsArea">
+          <a href="javascript:hideWarning()" title="$i18n.getString( 'hide_warning' )"><img src="../images/hide.png" alt="$i18n.getString( 'hide_warning' )"/></a>
+        </div>
+        <p><span id="warningField"></span></p>
+      </div>
 			
 		</td>
 	</tr>