← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2307: Added basic json support

 

------------------------------------------------------------
revno: 2307
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2010-12-08 11:15:56 +0530
message:
  Added basic json support
modified:
  dhis-2/dhis-web/dhis-web-api/pom.xml
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnits.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/OrgUnitResource.java
  dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml


--
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-web/dhis-web-api/pom.xml'
--- dhis-2/dhis-web/dhis-web-api/pom.xml	2010-11-15 18:00:33 +0000
+++ dhis-2/dhis-web/dhis-web-api/pom.xml	2010-12-08 05:45:56 +0000
@@ -45,6 +45,11 @@
       <version>1.4</version>
     </dependency>
     <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-json</artifactId>
+      <version>1.4</version>
+    </dependency>
+    <dependency>
       <groupId>com.sun.jersey.contribs</groupId>
       <artifactId>jersey-spring</artifactId>
       <version>1.4</version>
@@ -83,7 +88,7 @@
       <artifactId>dhis-web-commons-resources</artifactId>
       <type>war</type>
     </dependency>
-   <dependency>
+    <dependency>
       <groupId>org.hisp.dhis</groupId>
       <artifactId>dhis-api</artifactId>
     </dependency>

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnits.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnits.java	2010-11-26 11:00:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/model/OrgUnits.java	2010-12-08 05:45:56 +0000
@@ -9,17 +9,27 @@
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
-
 @XmlRootElement
-public class OrgUnits implements DataStreamSerializable
+public class OrgUnits
+    implements DataStreamSerializable
 {
     private List<OrgUnit> orgUnits = new ArrayList<OrgUnit>();
-    
-    @XmlElement(name="orgUnit")
-    public List<OrgUnit> getOrgUnits() {
+
+    public OrgUnits()
+    {
+    }
+
+    public OrgUnits( List<OrgUnit> unitList )
+    {
+        this.orgUnits = unitList;
+    }
+
+    @XmlElement( name = "orgUnit" )
+    public List<OrgUnit> getOrgUnits()
+    {
         return orgUnits;
     }
-    
+
     public void setOrgUnits( List<OrgUnit> orgUnits )
     {
         this.orgUnits = orgUnits;

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java	2010-11-26 11:00:40 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java	2010-12-08 05:45:56 +0000
@@ -45,12 +45,13 @@
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.web.api.model.OrgUnit;
 import org.hisp.dhis.web.api.model.OrgUnits;
+import org.hisp.dhis.web.api.service.NotAllowedException;
 import org.springframework.beans.factory.annotation.Required;
 
 import com.sun.jersey.api.core.ResourceContext;
 
 @Path( "/" )
-@Produces( { DhisMediaType.MOBILE_SERIALIZED, MediaType.APPLICATION_XML } )
+@Produces( { DhisMediaType.MOBILE_SERIALIZED, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } )
 public class MobileResource
 {
 
@@ -66,60 +67,61 @@
     @Context
     private ResourceContext rc;
 
+    /**
+     * Get the units associated with the currently logged in user
+     * 
+     * @throws NotAllowedException if no user is logged in
+     */
     @GET
     @Path( "mobile" )
     public OrgUnits getOrgUnitsForUser()
+        throws NotAllowedException
     {
         User user = currentUserService.getCurrentUser();
 
+        if ( user == null )
+        {
+            throw new NotAllowedException( "NO_USER", "No user is logged in." );
+        }
+
         Collection<OrganisationUnit> units = user.getOrganisationUnits();
 
-        OrgUnits orgUnits = new OrgUnits();
-
         List<OrgUnit> unitList = new ArrayList<OrgUnit>();
-        
         for ( OrganisationUnit unit : units )
         {
             unitList.add( getOrgUnit( unit ) );
         }
 
-        orgUnits.setOrgUnits( unitList );
-
-        return orgUnits;
+        return new OrgUnits( unitList );
     }
 
     @Path( "orgUnits/{id}" )
     public OrgUnitResource getOrgUnit( @PathParam( "id" ) int id )
     {
-
         OrgUnitResource resource = rc.getResource( OrgUnitResource.class );
-
         resource.setOrgUnit( organisationUnitService.getOrganisationUnit( id ) );
-
         return resource;
     }
 
     private OrgUnit getOrgUnit( OrganisationUnit unit )
     {
-        OrgUnit m = new OrgUnit();
-
-        m.setId( unit.getId() );
-        m.setName( unit.getShortName() );
-
-        m.setDownloadAllUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "all" ).build( unit.getId() )
+        OrgUnit orgUnit = new OrgUnit();
+
+        orgUnit.setId( unit.getId() );
+        orgUnit.setName( unit.getShortName() );
+
+        orgUnit.setDownloadAllUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "all" ).build( unit.getId() )
             .toString() );
-        m.setDownloadActivityPlanUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "activitiyplan" )
-            .build( unit.getId() ).toString() );
-        m.setUploadFacilityReportUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "dataSets" )
-            .build( unit.getId() ).toString() );
-        m.setUploadActivityReportUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "activities" )
+        orgUnit.setDownloadActivityPlanUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "activitiyplan" )
+            .build( unit.getId() ).toString() );
+        orgUnit.setUploadFacilityReportUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "dataSets" )
+            .build( unit.getId() ).toString() );
+        orgUnit.setUploadActivityReportUrl( uriInfo.getBaseUriBuilder().path( "/orgUnits/{id}" ).path( "activities" )
             .build( unit.getId() ).toString() );
 
-        return m;
+        return orgUnit;
     }
 
-    // Setters...
-
     @Required
     public void setCurrentUserService( CurrentUserService currentUserService )
     {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/OrgUnitResource.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/OrgUnitResource.java	2010-12-02 10:53:14 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/OrgUnitResource.java	2010-12-08 05:45:56 +0000
@@ -22,8 +22,8 @@
 import org.hisp.dhis.web.api.service.NotAllowedException;
 import org.springframework.beans.factory.annotation.Required;
 
-@Produces( { DhisMediaType.MOBILE_SERIALIZED, MediaType.APPLICATION_XML } )
-@Consumes( { DhisMediaType.MOBILE_SERIALIZED, MediaType.APPLICATION_XML } )
+@Produces( { DhisMediaType.MOBILE_SERIALIZED, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } )
+@Consumes( { DhisMediaType.MOBILE_SERIALIZED, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } )
 public class OrgUnitResource
 {
 
@@ -45,9 +45,12 @@
         this.unit = unit;
     }
 
+    /** Get activity plan, program forms and facility forms wrapped in a {@link MobileModel}
+     * @param locale - localize for the given locale
+     */
     @GET
     @Path( "all" )
-    public MobileModel getAllDataForUser( @HeaderParam( "accept-language" ) String locale )
+    public MobileModel getAllDataForOrgUnit( @HeaderParam( "accept-language" ) String locale )
     {
         MobileModel mobileModel = new MobileModel();
 
@@ -61,6 +64,9 @@
         return mobileModel;
     }
 
+    /**
+     * Get a localized representation of the current activity plan 
+     */
     @GET
     @Path( "activitiyplan" )
     public ActivityPlan getCurrentActivityPlan( @HeaderParam( "accept-language" ) String locale )
@@ -68,6 +74,11 @@
         return activityReportingService.getCurrentActivityPlan( unit, locale );
     }
 
+    /**
+     * Save a facility report for unit 
+     * @param dataSetValue - the report to save
+     * @throws NotAllowedException if the {@link DataSetValue} is invalid
+     */
     @POST
     @Path( "dataSets" )
     public void saveDataSetValues( DataSetValue dataSetValue ) throws NotAllowedException
@@ -75,6 +86,11 @@
         facilityReportingService.saveDataSetValues( unit, dataSetValue );
     }
 
+    /**
+     * Save activity report for unit
+     * @param activityValue - the report to save
+     * @throws NotAllowedException if the {@link ActivityValue activity value} is invalid
+     */
     @POST
     @Path( "activities" )
     public void saveActivityReport( ActivityValue activityValue ) throws NotAllowedException
@@ -82,8 +98,6 @@
         activityReportingService.saveActivityReport( unit, activityValue );
     }
 
-    // Setters...
-
     @Required
     public void setProgramService( IProgramService programService )
     {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2010-12-02 10:53:14 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2010-12-08 05:45:56 +0000
@@ -44,7 +44,7 @@
     <property name="i18nService" ref="org.hisp.dhis.i18n.I18nService" />
   </bean>
 
-  <!-- Jax-RS serialization writers and readers -->
+  <!-- Jax-RS providers for (de-)serialization, exception mapping, json configuration -->
 
   <bean id="org.hisp.dhis.web.api.mapping.DataSerializableProvider" class="org.hisp.dhis.web.api.mapping.DataStreamSerializableProvider"
     scope="singleton" />
@@ -55,9 +55,10 @@
   <bean id="org.hisp.dhis.web.api.mapping.ActivityValueConsumer" class="org.hisp.dhis.web.api.mapping.ActivityValueConsumer"
     scope="singleton" />
 
-  <bean id="org.hisp.dhis.web.api.mapping.StringSerializedProvider" class="org.hisp.dhis.web.api.mapping.NotAllowedExceptionMapper"
+  <bean id="org.hisp.dhis.web.api.mapping.NotAllowedExceptionMapper" class="org.hisp.dhis.web.api.mapping.NotAllowedExceptionMapper"
     scope="singleton" />
 
+  <bean id="JacksonJaxbJsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" scope="singleton" />
 
   <!-- ImportDataValue beans -->