← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20486: Load file meta data along with dataset for file resource data elements

 

------------------------------------------------------------
revno: 20486
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-10-03 00:42:59 +0200
message:
  Load file meta data along with dataset for file resource data elements
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataValuesForDataSetAction.java
  dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/responseDataValues.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
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java	2015-09-19 08:23:14 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceService.java	2015-10-02 22:42:59 +0000
@@ -30,12 +30,16 @@
 
 import com.google.common.io.ByteSource;
 
+import java.util.List;
+
 /**
  * @author Halvdan Hoem Grelland
  */
 public interface FileResourceService
 {
     FileResource getFileResource( String uid );
+
+    List<FileResource> getFileResources( List<String> uids );
     
     String saveFileResource( FileResource fileResource, ByteSource content );
     

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java	2015-09-21 10:09:44 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java	2015-10-02 22:42:59 +0000
@@ -36,6 +36,8 @@
 
 import com.google.common.io.ByteSource;
 
+import java.util.List;
+
 /**
  * @author Halvdan Hoem Grelland
  */
@@ -72,6 +74,12 @@
         return fileResourceStore.getByUid( uid );
     }
 
+    @Override
+    public List<FileResource> getFileResources( List<String> uids )
+    {
+        return fileResourceStore.getByUid( uids );
+    }
+
     @Transactional
     @Override
     public String saveFileResource( FileResource fileResource, ByteSource content )

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataValuesForDataSetAction.java'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataValuesForDataSetAction.java	2015-10-01 11:35:44 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/action/GetDataValuesForDataSetAction.java	2015-10-02 22:42:59 +0000
@@ -31,6 +31,7 @@
 import com.opensymphony.xwork2.Action;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataset.CompleteDataSetRegistration;
 import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
@@ -38,6 +39,8 @@
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.datavalue.DataValue;
 import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.fileresource.FileResource;
+import org.hisp.dhis.fileresource.FileResourceService;
 import org.hisp.dhis.minmax.MinMaxDataElement;
 import org.hisp.dhis.minmax.MinMaxDataElementService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -50,7 +53,11 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * @author Lars Helge Overland
@@ -99,6 +106,13 @@
         this.organisationUnitService = organisationUnitService;
     }
 
+    private FileResourceService fileResourceService;
+
+    public void setFileResourceService( FileResourceService fileResourceService )
+    {
+        this.fileResourceService = fileResourceService;
+    }
+
     @Autowired
     private InputUtils inputUtils;
 
@@ -199,6 +213,13 @@
         return storedBy;
     }
 
+    private Map<String, FileResource> dataValueFileResourceMap = new HashMap<>();
+
+    public Map<String, FileResource> getDataValueFileResourceMap()
+    {
+        return dataValueFileResourceMap;
+    }
+
     // -------------------------------------------------------------------------
     // Action implementation
     // -------------------------------------------------------------------------
@@ -255,6 +276,18 @@
         }
 
         // ---------------------------------------------------------------------
+        // File resource meta-data
+        // ---------------------------------------------------------------------
+
+        List<String> fileResourceUids = dataValues.stream()
+            .filter( dv -> dv.getDataElement().isFileType() )
+            .map( DataValue::getValue )
+            .collect( Collectors.toList() );
+
+        dataValueFileResourceMap.putAll( fileResourceService.getFileResources( fileResourceUids ).stream()
+            .collect( Collectors.toMap( BaseIdentifiableObject::getUid, f -> f ) ) );
+
+        // ---------------------------------------------------------------------
         // Data set completeness info
         // ---------------------------------------------------------------------
 

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml	2015-06-22 15:21:26 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/resources/META-INF/dhis/beans.xml	2015-10-02 22:42:59 +0000
@@ -22,6 +22,7 @@
     <property name="dataSetService" ref="org.hisp.dhis.dataset.DataSetService" />
     <property name="registrationService" ref="org.hisp.dhis.dataset.CompleteDataSetRegistrationService" />
     <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+    <property name="fileResourceService" ref="org.hisp.dhis.fileresource.FileResourceService" />
   </bean>
 
   <bean id="org.hisp.dhis.de.action.LoadFormAction" class="org.hisp.dhis.de.action.LoadFormAction" scope="prototype">

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js	2015-10-02 12:01:15 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.fileresource.js	2015-10-02 22:42:59 +0000
@@ -127,6 +127,12 @@
             $button.button( 'enable' );
         };
 
+        var updateProgress = function( loaded, total ) {
+            var percent = parseInt( loaded / total * 100, 10 );
+            $progressBar.css( 'width', percent + '%' );
+            $progressInfo.text( percent + '%' );
+        };
+
         var disableField = function() {
             $button.button( 'disable' );
             $field.toggleClass( 'entryfileresource-disabled', true );
@@ -163,14 +169,12 @@
             },
             progress: function( e, data )
             {
-                var percent = parseInt( data.loaded / data.total * 100, 10 );
-                $progressBar.css( 'width', percent + '%' );
-                $progressInfo.text( percent + '%' );
+                updateProgress( data.loaded, data.total );
             },
             fail: function( e, data )
             {
                 setHeaderDelayMessage( i18n_file_upload_failed );
-                console.log( data.errorThrown );
+                console.error( data.errorThrown );
                 setButtonUpload();
             },
             done: function( e, data )

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2015-10-01 18:46:36 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/form.js	2015-10-02 22:42:59 +0000
@@ -1673,26 +1673,17 @@
             }
             else if ( $( fieldId ).attr( 'name' ) == 'entryoptionset' )
             {
-            	dhis2.de.setOptionNameInField( fieldId, value );            	
+                dhis2.de.setOptionNameInField( fieldId, value );
             }
             else if ( $( fieldId ).attr( 'class' ) == 'entryfileresource' )
             {
-                // TODO Consider pre-fetching with dataset
                 $( fieldId ).data( 'value', value.val );
 
-                $.ajax( {
-                    url: '../api/fileResources/' + value.val,
-                    success: function( data ) {
-                        var name = data.name, size = '(' + filesize( data.contentLength ) + ')';
-
-                        $( fieldId ).find( '.upload-fileinfo-name' ).text( name );
-                        $( fieldId ).find( '.upload-fileinfo-size' ).text( size );
-                    },
-                    error: function( data ) {
-                        $( fieldId ).find( '.upload-fileinfo-name' ).text( 'Failed loading file meta-data' );
-                        $( fieldId ).find( '.upload-fileinfo-size' ).text( + '' );
-                    }
-                } );
+                if ( value.fileMeta )
+                {
+                    $( fieldId ).find( '.upload-fileinfo-name' ).text( value.fileMeta.name );
+                    $( fieldId ).find( '.upload-fileinfo-size' ).text( '(' + filesize( value.fileMeta.size ) + ')' );
+                }
             }
             else 
             {

=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/responseDataValues.vm'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/responseDataValues.vm	2014-06-16 20:30:48 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/responseDataValues.vm	2015-10-02 22:42:59 +0000
@@ -8,6 +8,13 @@
     #else
     "id":"${value.dataElement.uid}-${value.optionCombo.uid}",
     #end
+    #if( $dataValueFileResourceMap.containsKey( $!{value.value} ) )
+      #set( $fr = $dataValueFileResourceMap.get( $!{value.value} ) )
+    "fileMeta":{
+      "name":"$!encoder.jsonEncode( $!{fr.name} )",
+      "size":${fr.contentLength}
+    },
+    #end
     "val":"$!encoder.jsonEncode( ${value.value} )",
     "com":"${value.hasComment()}"
   }#if( $velocityCount < $size1 ),#end