← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20567: Minor: simplify expressions

 

------------------------------------------------------------
revno: 20567
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-10-07 15:43:35 +0200
message:
  Minor: simplify expressions
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java
  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/JCloudsFileResourceContentStore.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.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-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java	2015-10-06 22:00:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/fileresource/FileResourceContentStore.java	2015-10-07 13:43:35 +0000
@@ -30,6 +30,7 @@
 
 import com.google.common.io.ByteSource;
 
+import java.io.IOException;
 import java.net.URI;
 
 /**
@@ -54,6 +55,8 @@
      */
     String saveFileResourceContent( String key, ByteSource content, long size, String contentMd5 );
 
+    String saveFileResourceContent2( String key, ByteSource content, long size, String contentMd5 ) throws IOException;
+
     /**
      * Delete the content bytes of a file resource.
      * @param key the key.

=== 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-10-06 22:00:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/DefaultFileResourceService.java	2015-10-07 13:43:35 +0000
@@ -36,6 +36,7 @@
 
 import com.google.common.io.ByteSource;
 
+import java.io.IOException;
 import java.net.URI;
 import java.util.List;
 
@@ -87,8 +88,22 @@
     {
         String storageKey = getRelativeStorageKey( fileResource );
 
-        String key = fileResourceContentStore.saveFileResourceContent(
-            storageKey, content, fileResource.getContentLength(), fileResource.getContentMd5() );
+//        String key = fileResourceContentStore.saveFileResourceContent(
+//            storageKey, content, fileResource.getContentLength(), fileResource.getContentMd5() );
+
+        //
+        String key = null;
+
+        try
+        {
+            key = fileResourceContentStore.saveFileResourceContent2(
+                storageKey, content, fileResource.getContentLength(), fileResource.getContentMd5() );
+        }
+        catch ( IOException e )
+        {
+            e.printStackTrace();
+        }
+        //
 
         if ( key == null )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java	2015-10-06 22:00:49 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/fileresource/JCloudsFileResourceContentStore.java	2015-10-07 13:43:35 +0000
@@ -29,13 +29,17 @@
  */
 
 import com.google.common.hash.HashCode;
+import com.google.common.io.ByteSink;
 import com.google.common.io.ByteSource;
+import com.google.common.io.FileWriteMode;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.NullInputStream;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.external.location.LocationManager;
 import org.hisp.dhis.hibernate.HibernateConfigurationProvider;
+import org.hisp.dhis.system.scheduling.Scheduler;
 import org.jclouds.ContextBuilder;
 import org.jclouds.blobstore.BlobRequestSigner;
 import org.jclouds.blobstore.BlobStore;
@@ -48,16 +52,22 @@
 import org.jclouds.filesystem.reference.FilesystemConstants;
 import org.jclouds.http.HttpRequest;
 import org.joda.time.Minutes;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.concurrent.ListenableFuture;
+import org.springframework.util.concurrent.ListenableFutureCallback;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.concurrent.Callable;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
@@ -128,6 +138,9 @@
         this.configurationProvider = configurationProvider;
     }
 
+    @Autowired
+    private Scheduler scheduler;
+
     // -------------------------------------------------------------------------
     // Life cycle management
     // -------------------------------------------------------------------------
@@ -266,6 +279,49 @@
     }
 
     @Override
+    public String saveFileResourceContent2( String key, ByteSource content, long size, String contentMd5 ) throws IOException
+    {
+        Path tempDir = Files.createTempDirectory( null );
+        Path tmpFile = Files.createTempFile( tempDir, null, null );
+
+        ByteSink byteSink = com.google.common.io.Files.asByteSink( tmpFile.toFile(), FileWriteMode.APPEND );
+        IOUtils.copy( content.openStream(), byteSink.openStream() );
+
+        ListenableFuture<String> uploadResult = scheduler.executeTask( () -> {
+            Blob blob = blobStore.blobBuilder( key )
+                .payload( tmpFile.toFile() )
+                .contentLength( size )
+                .build();
+
+            return putBlob( blob );
+        } );
+
+        uploadResult.addCallback( new ListenableFutureCallback<String>()
+        {
+            @Override
+            public void onFailure( Throwable ex )
+            {
+                log.error( ex );
+            }
+
+            @Override
+            public void onSuccess( String result )
+            {
+                if ( result != null )
+                {
+                    log.info( "File uploaded: " + result );
+                }
+                else
+                {
+                    log.info( "Upload failed" );
+                }
+            }
+        } );
+
+        return key;
+    }
+
+    @Override
     public void deleteFileResourceContent( String key )
     {
         deleteBlob( key );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java	2015-10-07 13:25:14 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/scheduling/SpringScheduler.java	2015-10-07 13:43:35 +0000
@@ -109,8 +109,8 @@
         if ( key != null )
         {
             ScheduledFuture<?> future = futures.get( key );
-            
-            boolean result = future != null ? future.cancel( true ) : false;
+
+            boolean result = future != null && future.cancel( true );
             
             futures.remove( key );
             
@@ -133,7 +133,7 @@
             
             ScheduledFuture<?> future = futures.get( key );
             
-            boolean result = future != null ? future.cancel( true ) : false;
+            boolean result = future != null && future.cancel( true );
             
             keys.remove();
             

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java	2015-10-06 17:11:19 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/FileResourceController.java	2015-10-07 13:43:35 +0000
@@ -31,6 +31,7 @@
 import com.google.common.hash.Hashing;
 import com.google.common.io.ByteSource;
 import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.NullInputStream;
 import org.apache.commons.lang3.StringUtils;
 import org.hisp.dhis.dxf2.webmessage.WebMessage;