← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2103: Removed ActivityPlanProvider.java again.

 

------------------------------------------------------------
revno: 2103
committer: Jo Størset <storset@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2010-11-18 17:02:14 +0530
message:
  Removed ActivityPlanProvider.java again.
  Generalized media type consumation with an AbstractDataSerializableConsumer.java (should maybe be possible to do even more)
removed:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityPlanProvider.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/AbstractDataSerializableConsumer.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityValueConsumer.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataSetValueConsumer.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataStreamSerializableProvider.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
=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/AbstractDataSerializableConsumer.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/AbstractDataSerializableConsumer.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/AbstractDataSerializableConsumer.java	2010-11-18 11:32:14 +0000
@@ -0,0 +1,87 @@
+package org.hisp.dhis.web.api.provider;
+
+/*
+ * Copyright (c) 2004-2010, 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.DataInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.Provider;
+
+import org.hisp.dhis.web.api.model.DataStreamSerializable;
+import org.hisp.dhis.web.api.resources.DhisMediaType;
+
+import com.sun.jersey.spi.resource.Singleton;
+
+/**
+ * An abstract class mapping from the {@link DhisMediaType.MOBILE_SERIALIZED} to implementations of {@link DataStreamSerializable}
+ * 
+ * <p>Implementations only need to define T and specify the relevant annotations
+ * 
+ * @param <T> The concrete implementation of {@link DataStreamSerializable} this consumer creates and populates
+ */
+public abstract class AbstractDataSerializableConsumer<T extends DataStreamSerializable> implements MessageBodyReader<T>
+{
+
+
+    @Override
+    public boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType )
+    {
+        System.out.println("****" + type.getName() + " - " + this.getClass().getName());
+        return DataStreamSerializable.class.isAssignableFrom( type );
+    }
+
+    @Override
+    public T readFrom( Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType,
+        MultivaluedMap<String, String> httpHeaders, InputStream entityStream )
+        throws IOException, WebApplicationException
+    {
+        T t;
+        try
+        {
+            t = type.newInstance();
+            t.deSerialize( new DataInputStream( entityStream ) );
+            return t;
+        }
+        catch ( InstantiationException e )
+        {
+            throw new IOException("Can't instantiate class " + type.getName(), e);
+        }
+        catch ( IllegalAccessException e )
+        {
+            throw new IOException("Not allowed to instantiate class " + type.getName(), e);
+        }
+    }
+}

=== removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityPlanProvider.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityPlanProvider.java	2010-11-18 09:29:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityPlanProvider.java	1970-01-01 00:00:00 +0000
@@ -1,77 +0,0 @@
-package org.hisp.dhis.web.api.provider;
-
-import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
-import javax.ws.rs.Produces;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyWriter;
-import javax.ws.rs.ext.Provider;
-import org.hisp.dhis.web.api.model.ActivityPlan;
-import com.jcraft.jzlib.JZlib;
-import com.jcraft.jzlib.ZOutputStream;
-import com.sun.jersey.spi.resource.Singleton;
-
-@Provider
-@Singleton
-@Produces( "application/vnd.org.dhis2.activityplan+serialized" )
-public class ActivityPlanProvider
-    implements MessageBodyWriter<ActivityPlan>
-{
-    @Override
-    public long getSize( ActivityPlan arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4 )
-    {
-        return -1;
-    }
-
-    @Override
-    public boolean isWriteable( Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3 )
-    {
-        return true;
-    }
-
-    @Override
-    public void writeTo( ActivityPlan arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
-        MultivaluedMap<String, Object> arg5, OutputStream stream )
-        throws IOException, WebApplicationException
-    {
-        serializeZipped( arg0, stream );
-    }
-
-    public void serializeZipped( ActivityPlan activityPlan, OutputStream os )
-        throws IOException
-    {
-        ByteArrayOutputStream baos = serializePersistent( activityPlan );
-        ZOutputStream gzip = new ZOutputStream( os, JZlib.Z_BEST_COMPRESSION );
-        DataOutputStream dos = new DataOutputStream( gzip );
-
-        try
-        {
-            dos.write( baos.toByteArray() );
-        }
-        finally
-        {
-            dos.flush();
-            gzip.finish();
-        }
-    }
-
-    public ByteArrayOutputStream serializePersistent( ActivityPlan activityPlan )
-        throws IOException
-
-    {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        DataOutputStream out = new DataOutputStream( baos );
-
-        activityPlan.serialize( out );
-        out.flush();
-
-        return baos;
-    }
-
-}
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityValueConsumer.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityValueConsumer.java	2010-11-16 16:28:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/ActivityValueConsumer.java	2010-11-18 11:32:14 +0000
@@ -27,43 +27,18 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.DataInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
-
 import javax.ws.rs.Consumes;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.Provider;
 
 import org.hisp.dhis.web.api.model.ActivityValue;
+import org.hisp.dhis.web.api.resources.DhisMediaType;
 
 import com.sun.jersey.spi.resource.Singleton;
 
 @Provider
 @Singleton
-@Consumes( "application/vnd.org.dhis2.activityvaluelist+serialized" )
+@Consumes( DhisMediaType.MOBILE_SERIALIZED )
 public class ActivityValueConsumer
-    implements MessageBodyReader<ActivityValue>
+    extends AbstractDataSerializableConsumer<ActivityValue>
 {
-
-    @Override
-    public boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType )
-    {
-        return true;
-    }
-
-    @Override
-    public ActivityValue readFrom( Class<ActivityValue> type, Type genericType, Annotation[] annotations,
-        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream )
-        throws IOException, WebApplicationException
-    {
-        ActivityValue activityValue = new ActivityValue();
-        activityValue.deSerialize( new DataInputStream( entityStream ) );
-        return activityValue;
-    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataSetValueConsumer.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataSetValueConsumer.java	2010-11-16 16:28:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataSetValueConsumer.java	2010-11-18 11:32:14 +0000
@@ -27,44 +27,18 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.io.DataInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
-
 import javax.ws.rs.Consumes;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyReader;
 import javax.ws.rs.ext.Provider;
 
 import org.hisp.dhis.web.api.model.DataSetValue;
+import org.hisp.dhis.web.api.resources.DhisMediaType;
 
 import com.sun.jersey.spi.resource.Singleton;
 
 @Provider
 @Singleton
-@Consumes( "application/vnd.org.dhis2.datasetvalue+serialized" )
+@Consumes( DhisMediaType.MOBILE_SERIALIZED )
 public class DataSetValueConsumer
-    implements MessageBodyReader<DataSetValue>
+    extends AbstractDataSerializableConsumer<DataSetValue>
 {
-
-    @Override
-    public boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType )
-    {
-        return true;
-    }
-
-    @Override
-    public DataSetValue readFrom( Class<DataSetValue> type, Type genericType, Annotation[] annotations,
-        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream )
-        throws IOException, WebApplicationException
-    {
-        DataSetValue dsValue = new DataSetValue();
-        dsValue.deSerialize( new DataInputStream( entityStream ) );
-        return dsValue;
-    }
-
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataStreamSerializableProvider.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataStreamSerializableProvider.java	2010-11-18 06:08:29 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/provider/DataStreamSerializableProvider.java	2010-11-18 11:32:14 +0000
@@ -49,7 +49,7 @@
 
 @Provider
 @Singleton
-@Produces( "application/vnd.org.dhis2.mobile+serialized" )
+@Produces( org.hisp.dhis.web.api.resources.DhisMediaType.MOBILE_SERIALIZED )
 public class DataStreamSerializableProvider
     implements MessageBodyWriter<DataStreamSerializable>
 {

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/DhisMediaType.java	2010-11-18 11:32:14 +0000
@@ -0,0 +1,14 @@
+package org.hisp.dhis.web.api.resources;
+
+public class DhisMediaType
+{
+    public static final String ACTIVITYVALUELIST_SERIALIZED = "application/vnd.org.dhis2.activityvaluelist+serialized";
+
+    public static final String DATASETVALUE_SERIALIZED = "application/vnd.org.dhis2.datasetvalue+serialized";
+
+    /**
+     * The application/vnd.org.dhis2.mobile+serialized media type
+     */
+    public static final String MOBILE_SERIALIZED = "application/vnd.org.dhis2.mobile+serialized";
+
+}

=== removed file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java	2010-11-16 18:26:11 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MediaType.java	1970-01-01 00:00:00 +0000
@@ -1,13 +0,0 @@
-package org.hisp.dhis.web.api.resources;
-
-public class MediaType
-{
-    public static final String ACTIVITYVALUELIST_SERIALIZED = "application/vnd.org.dhis2.activityvaluelist+serialized";
-
-    public static final String DATASETVALUE_SERIALIZED = "application/vnd.org.dhis2.datasetvalue+serialized";
-
-    public static final String ACTIVITYPLAN_SERIALIZED = "application/vnd.org.dhis2.activityplan+serialized";
-
-    public static final String MOBILE_SERIALIZED = "application/vnd.org.dhis2.mobile+serialized";
-
-}

=== 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-18 06:08:29 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/MobileResource.java	2010-11-18 11:32:14 +0000
@@ -27,17 +27,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -51,6 +47,7 @@
 import com.sun.jersey.api.core.ResourceContext;
 
 @Path( "/" )
+@Produces( DhisMediaType.MOBILE_SERIALIZED )
 public class MobileResource
 {
 
@@ -67,7 +64,6 @@
     private ResourceContext rc;
 
     @GET
-    @Produces( MediaType.MOBILE_SERIALIZED )
     @Path( "mobile" )
     public OrgUnits getOrgUnitsForUser()
     {

=== 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-11-18 06:08:29 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/web/api/resources/OrgUnitResource.java	2010-11-18 11:32:14 +0000
@@ -6,6 +6,7 @@
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
 
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.web.api.model.ActivityPlan;
@@ -17,6 +18,8 @@
 import org.hisp.dhis.web.api.service.IProgramService;
 import org.springframework.beans.factory.annotation.Required;
 
+@Produces( DhisMediaType.MOBILE_SERIALIZED )
+@Consumes( DhisMediaType.MOBILE_SERIALIZED )
 public class OrgUnitResource
 {
 
@@ -35,7 +38,6 @@
 
     @GET
     @Path( "all" )
-    @Produces( MediaType.MOBILE_SERIALIZED )
     public MobileModel getAllDataForUser( @HeaderParam( "accept-language" ) String locale )
     {
         MobileModel mobileModel = new MobileModel();
@@ -49,7 +51,6 @@
 
     @GET
     @Path( "activitiyplan" )
-    @Produces( MediaType.ACTIVITYPLAN_SERIALIZED )
     public ActivityPlan getCurrentActivityPlan( @HeaderParam( "accept-language" ) String locale )
     {
         return activityReportingService.getCurrentActivityPlan( unit, locale );
@@ -57,8 +58,7 @@
 
     @POST
     @Path( "dataSets" )
-    @Consumes( MediaType.DATASETVALUE_SERIALIZED )
-    @Produces( "application/xml" )
+    @Produces(MediaType.TEXT_PLAIN)
     public String saveDataSetValues( DataSetValue dataSetValue )
     {
         return facilityReportingService.saveDataSetValues( unit, dataSetValue );
@@ -66,8 +66,7 @@
 
     @POST
     @Path( "activities" )
-    @Consumes( MediaType.ACTIVITYVALUELIST_SERIALIZED )
-    @Produces( "application/xml" )
+    @Produces(MediaType.TEXT_PLAIN)
     public String saveActivityReport( ActivityValue activityValue )
     {
         return activityReportingService.saveActivityReport( unit, activityValue );

=== 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-11-18 09:29:24 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/resources/META-INF/dhis/beans.xml	2010-11-18 11:32:14 +0000
@@ -48,9 +48,6 @@
   <bean id="org.hisp.dhis.web.api.provider.DataSerializableProvider" class="org.hisp.dhis.web.api.provider.DataStreamSerializableProvider"
     scope="singleton" />
 	
-	<bean id="org.hisp.dhis.web.api.provider.ActivityPlanProvider"
-                class="org.hisp.dhis.web.api.provider.ActivityPlanProvider" scope="singleton"/>
-	
   <bean id="org.hisp.dhis.web.api.consumer.DataSetValueConsumer" class="org.hisp.dhis.web.api.provider.DataSetValueConsumer"
     scope="singleton" />