← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14920: Moved code for generating data value set templates from web api to service layer. Moved resource ...

 

------------------------------------------------------------
revno: 14920
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2014-04-20 21:17:42 +0200
message:
  Moved code for generating data value set templates from web api to service layer. Moved resource from /dvs to /dataValueSet
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2014-04-20 19:17:42 +0000
@@ -28,14 +28,18 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
+import org.hisp.dhis.period.Period;
 import org.hisp.dhis.scheduling.TaskId;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Writer;
 import java.util.Date;
+import java.util.List;
 import java.util.Set;
 
 public interface DataValueSetService
@@ -49,7 +53,10 @@
     void writeDataValueSetJson( Set<String> dataSet, Date startDate, Date endDate, Set<String> ous, OutputStream outputStream );
 
     void writeDataValueSetCsv( Set<String> dataSets, Date startDate, Date endDate, Set<String> orgUnits, Writer writer );
-
+    
+    void writeDataValueSetTemplate( OutputStream out, DataSet dataSet, Period period, List<String> orgUnits,
+        boolean comment, String orgUnitIdScheme, String dataElementIdScheme ) throws IOException;
+    
     ImportSummary saveDataValueSet( InputStream in );
 
     ImportSummary saveDataValueSetJson( InputStream in );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-04-20 19:17:42 +0000
@@ -28,13 +28,36 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.csvreader.CsvReader;
+import static org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty.UUID;
+import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
+import static org.hisp.dhis.system.notification.NotificationLevel.INFO;
+import static org.hisp.dhis.system.util.ConversionUtils.wrap;
+import static org.hisp.dhis.system.util.DateUtils.getDefaultDate;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.nio.charset.Charset;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
 import org.amplecode.quick.BatchHandler;
 import org.amplecode.quick.BatchHandlerFactory;
 import org.amplecode.staxwax.factory.XMLFactory;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dataelement.DataElement;
@@ -67,22 +90,8 @@
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Writer;
-import java.nio.charset.Charset;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import static org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty.UUID;
-import static org.hisp.dhis.system.notification.NotificationLevel.ERROR;
-import static org.hisp.dhis.system.notification.NotificationLevel.INFO;
-import static org.hisp.dhis.system.util.ConversionUtils.wrap;
-import static org.hisp.dhis.system.util.DateUtils.getDefaultDate;
+import com.csvreader.CsvReader;
+import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
 
 /**
  * @author Lars Helge Overland
@@ -229,6 +238,116 @@
         dataValueSetStore.writeDataValueSetCsv( getDataElements( dataSets ), periods, getOrgUnits( orgUnits ), writer );
     }
 
+    public void writeDataValueSetTemplate( OutputStream out, DataSet dataSet, Period period, List<String> orgUnits,
+        boolean comment, String orgUnitIdScheme, String dataElementIdScheme ) throws IOException
+    {
+        ToXmlGenerator generator = (ToXmlGenerator) JacksonUtils.getXmlMapper().getJsonFactory().createJsonGenerator( out );
+            
+        try
+        {
+            XMLStreamWriter staxWriter = generator.getStaxWriter();
+    
+            if ( comment )
+            {
+                staxWriter.writeComment( "Data set: " + dataSet.getDisplayName() + " (" + dataSet.getUid() + ")" );
+            }
+    
+            staxWriter.writeStartElement( "", "dataValueSet", DxfNamespaces.DXF_2_0 );
+    
+            if ( orgUnits.isEmpty() )
+            {
+                for ( DataElement dataElement : dataSet.getDataElements() )
+                {
+                    writeDataValue( dataElement, dataElementIdScheme, null, orgUnitIdScheme, period, comment, staxWriter );
+                }
+            }
+            else
+            {
+                for ( String orgUnit : orgUnits )
+                {
+                    OrganisationUnit organisationUnit = identifiableObjectManager.search( OrganisationUnit.class, orgUnit );
+    
+                    if ( organisationUnit == null )
+                    {
+                        continue;
+                    }
+    
+                    if ( comment )
+                    {
+                        if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( orgUnitIdScheme.toLowerCase() ) )
+                        {
+                            staxWriter.writeComment( "Org unit: " + organisationUnit.getDisplayName() + " (" + organisationUnit.getCode() + ")" );
+                        }
+                        else
+                        {
+                            staxWriter.writeComment( "Org unit: " + organisationUnit.getDisplayName() + " (" + organisationUnit.getUid() + ")" );
+                        }    
+                    }
+    
+                    for ( DataElement dataElement : dataSet.getDataElements() )
+                    {
+                        writeDataValue( dataElement, dataElementIdScheme, organisationUnit, orgUnitIdScheme, period, comment, staxWriter );
+                    }
+                }
+            }
+    
+            staxWriter.writeEndElement();
+            staxWriter.flush();
+        }
+        catch ( XMLStreamException ignored )
+        {
+            ignored.printStackTrace();
+        }
+    }
+    
+    private void writeDataValue( DataElement dataElement, String deScheme, OrganisationUnit organisationUnit, String ouScheme, Period period, boolean comment, XMLStreamWriter staxWriter ) throws XMLStreamException
+    {
+        for ( DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getCategoryCombo().getSortedOptionCombos() )
+        {
+            String label = dataElement.getDisplayName();
+    
+            if ( !categoryOptionCombo.isDefault() )
+            {
+                label += " " + categoryOptionCombo.getDisplayName();
+            }
+    
+            if ( comment )
+            {
+                staxWriter.writeComment( "Data element: " + label );
+            }
+    
+            staxWriter.writeStartElement( "", "dataValue", DxfNamespaces.DXF_2_0 );
+    
+            if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( deScheme.toLowerCase() ) )
+            {
+                staxWriter.writeAttribute( "dataElement", dataElement.getCode() );
+            }
+            else
+            {
+                staxWriter.writeAttribute( "dataElement", dataElement.getUid() );
+            }
+    
+            staxWriter.writeAttribute( "categoryOptionCombo", categoryOptionCombo.getUid() );
+            
+            staxWriter.writeAttribute( "period", period != null ? period.getIsoDate() : "" );
+    
+            if ( organisationUnit != null )
+            {
+                if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( ouScheme.toLowerCase() ) )
+                {
+                    staxWriter.writeAttribute( "orgUnit", organisationUnit.getCode() == null ? "" : organisationUnit.getCode() );
+                }
+                else
+                {
+                    staxWriter.writeAttribute( "orgUnit", organisationUnit.getUid() == null ? "" : organisationUnit.getUid() );
+                }
+            }
+    
+            staxWriter.writeAttribute( "value", "" );
+            staxWriter.writeEndElement();
+        }
+    }
+    
     public ImportSummary saveDataValueSet( InputStream in )
     {
         return saveDataValueSet( in, ImportOptions.getDefaultImportOptions(), null );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2014-04-20 18:58:48 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataSetController.java	2014-04-20 19:17:42 +0000
@@ -28,28 +28,42 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.FormUtils;
 import org.hisp.dhis.api.view.ClassPathUriResolver;
 import org.hisp.dhis.api.webdomain.form.Form;
-import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.view.ExportView;
-import org.hisp.dhis.dataelement.DataElement;
-import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataentryform.DataEntryForm;
 import org.hisp.dhis.dataentryform.DataEntryFormService;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.dxf2.datavalueset.DataValueSetService;
 import org.hisp.dhis.dxf2.metadata.ExportService;
 import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.i18n.I18nService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.period.PeriodType;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
@@ -62,24 +76,6 @@
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -106,6 +102,12 @@
 
     @Autowired
     private DataValueService dataValueService;
+    
+    @Autowired
+    private DataValueSetService dataValueSetService;
+    
+    @Autowired
+    private PeriodService periodService;
 
     @Autowired
     private I18nService i18nService;
@@ -175,7 +177,7 @@
         JacksonUtils.toJson( response.getOutputStream(), form );
     }
 
-    @RequestMapping( value = "/{uid}/dvs", method = RequestMethod.GET, produces = { "application/xml", "text/xml" } )
+    @RequestMapping( value = "/{uid}/dataValueSet", method = RequestMethod.GET, produces = { "application/xml", "text/xml" } )
     public void getDvs( @PathVariable( "uid" ) String uid,
         @RequestParam( value = "orgUnitIdScheme", defaultValue = "ID", required = false ) String orgUnitIdScheme,
         @RequestParam( value = "dataElementIdScheme", defaultValue = "ID", required = false ) String dataElementIdScheme,
@@ -192,114 +194,11 @@
             return;
         }
 
-        ToXmlGenerator generator = (ToXmlGenerator) JacksonUtils.getXmlMapper().getJsonFactory()
-            .createJsonGenerator( response.getOutputStream() );
-
+        Period pe = periodService.getPeriod( period );
+        
         response.setContentType( MediaType.APPLICATION_XML_VALUE );
 
-        try
-        {
-            XMLStreamWriter staxWriter = generator.getStaxWriter();
-
-            if ( comment )
-            {
-                staxWriter.writeComment( "DataSet: " + dataSet.getDisplayName() + " (" + dataSet.getUid() + ")" );
-            }
-
-            staxWriter.writeStartElement( "", "dataValueSet", DxfNamespaces.DXF_2_0 );
-
-            if ( orgUnits.isEmpty() )
-            {
-                for ( DataElement dataElement : dataSet.getDataElements() )
-                {
-                    writeDataValue( dataElement, dataElementIdScheme, null, orgUnitIdScheme, period, comment, staxWriter );
-                }
-            }
-            else
-            {
-                for ( String orgUnit : orgUnits )
-                {
-                    OrganisationUnit organisationUnit = manager.search( OrganisationUnit.class, orgUnit );
-
-                    if ( organisationUnit == null )
-                    {
-                        continue;
-                    }
-
-                    if ( comment )
-                    {
-                        if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( orgUnitIdScheme.toLowerCase() ) )
-                        {
-                            staxWriter.writeComment( "OrgUnit: " + organisationUnit.getDisplayName() + " (" + organisationUnit.getCode() + ")" );
-                        }
-                        else
-                        {
-                            staxWriter.writeComment( "OrgUnit: " + organisationUnit.getDisplayName() + " (" + organisationUnit.getUid() + ")" );
-                        }
-
-                    }
-
-                    for ( DataElement dataElement : dataSet.getDataElements() )
-                    {
-                        writeDataValue( dataElement, dataElementIdScheme, organisationUnit, orgUnitIdScheme, period, comment, staxWriter );
-                    }
-                }
-            }
-
-            staxWriter.writeEndElement();
-            staxWriter.flush();
-        }
-        catch ( XMLStreamException ignored )
-        {
-            ignored.printStackTrace();
-        }
-    }
-
-    private void writeDataValue( DataElement dataElement, String deScheme, OrganisationUnit organisationUnit, String ouScheme, String period, boolean comment, XMLStreamWriter staxWriter ) throws XMLStreamException
-    {
-        for ( DataElementCategoryOptionCombo categoryOptionCombo : dataElement.getCategoryCombo().getSortedOptionCombos() )
-        {
-            String label = dataElement.getDisplayName();
-
-            if ( !categoryOptionCombo.isDefault() )
-            {
-                label += " " + categoryOptionCombo.getDisplayName();
-            }
-
-            if ( comment )
-            {
-                staxWriter.writeComment( "DataElement: " + label );
-            }
-
-            staxWriter.writeStartElement( "", "dataValue", DxfNamespaces.DXF_2_0 );
-
-            if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( deScheme.toLowerCase() ) )
-            {
-                staxWriter.writeAttribute( "dataElement", dataElement.getCode() );
-            }
-            else
-            {
-                staxWriter.writeAttribute( "dataElement", dataElement.getUid() );
-            }
-
-            staxWriter.writeAttribute( "categoryOptionCombo", categoryOptionCombo.getUid() );
-            staxWriter.writeAttribute( "period", period );
-
-            if ( organisationUnit != null )
-            {
-                if ( IdentifiableObject.IdentifiableProperty.CODE.toString().toLowerCase().equals( ouScheme.toLowerCase() ) )
-                {
-                    staxWriter.writeAttribute( "orgUnit", organisationUnit.getCode() == null ? "" : organisationUnit.getCode() );
-                }
-                else
-                {
-                    staxWriter.writeAttribute( "orgUnit", organisationUnit.getUid() == null ? "" : organisationUnit.getUid() );
-                }
-            }
-
-            staxWriter.writeAttribute( "value", "" );
-            staxWriter.writeEndElement();
-        }
+        dataValueSetService.writeDataValueSetTemplate( response.getOutputStream(), dataSet, pe, orgUnits, comment, orgUnitIdScheme, dataElementIdScheme );
     }
 
     @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = { "application/xml", "text/xml" } )