From 049f96dff28e8c13c2c1b6f5a697c4b792a14e4d Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 25 Aug 2020 11:10:58 +0200 Subject: [PATCH] Optimized the business logic to show the preview of files by Get Info (as reported at https://support.d4science.org/issues/19600#note-7) --- .../view/windows/DialogGetInfoBootstrap.java | 53 ++++++++++++++----- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogGetInfoBootstrap.java b/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogGetInfoBootstrap.java index 3f7221c..6d7d768 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogGetInfoBootstrap.java +++ b/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogGetInfoBootstrap.java @@ -62,6 +62,9 @@ import com.google.gwt.user.client.ui.Widget; public class DialogGetInfoBootstrap extends Composite { private static final int PREVIEW_WAITING_TIME = 9000; //9 sec + private static final int PREVIEW_MAX_RETRIES = 3; + private int showPreviewAttempt = 0; + private long loaderPreviewStartTime = 0; private static DialogGetInfoBootstrapUiBinder uiBinder = GWT.create(DialogGetInfoBootstrapUiBinder.class); @@ -396,12 +399,14 @@ public class DialogGetInfoBootstrap extends Composite { return; } - String googleDocViewerURL = "https://docs.google.com/viewer?url=" + final String googleDocViewerURL = "https://docs.google.com/viewer?url=" + URL.encode(result.getCompleteURL()) + "&embedded=true"; iFrameGDV = instanceFrame(googleDocViewerURL, loadingPreviewHTML); - final long startTime = new Date().getTime(); + loaderPreviewStartTime = new Date().getTime(); + showPreviewAttempt = 1; + timerGDV = new Timer() { @Override @@ -414,22 +419,42 @@ public class DialogGetInfoBootstrap extends Composite { return; } long checkTime = new Date().getTime(); - long diff = checkTime - startTime; - if (diff > PREVIEW_WAITING_TIME) {// is greater than 10 sec - try { - GWT.log("iFrameGoogleDocViewer not loaded within "+PREVIEW_WAITING_TIME+" sec, cancelling timer, removing iframe"); - cancel(); - removePlaceHolder(loadingPreviewHTML); - htmlPanelFilePreview.add(noPreviewAvailable); - iFrameGDV.setVisible(false); - htmlPanelFilePreview.remove(iFrameGDV); - } catch (Exception e) { - // Silent + long diff = checkTime - loaderPreviewStartTime; + if (diff > PREVIEW_WAITING_TIME) { // is greater than PREVIEW_WAITING_TIME, performs a new attempt or terminate + if(showPreviewAttempt >= PREVIEW_MAX_RETRIES) { + try { + GWT.log("iFrameGoogleDocViewer not loaded within "+PREVIEW_WAITING_TIME+" sec and max retries "+PREVIEW_MAX_RETRIES+", cancelling timer, removing iframe"); + cancel(); + removePlaceHolder(loadingPreviewHTML); + htmlPanelFilePreview.add(noPreviewAvailable); + iFrameGDV.setVisible(false); + htmlPanelFilePreview.remove(iFrameGDV); + } catch (Exception e) { + // Silent + } + }else { + GWT.log("iFrameGoogleDocViewer not loaded on attempt "+showPreviewAttempt+", instancing new iFrame"); + //new retry by instancing the iFrame again... + iFrameGDV = instanceFrame(googleDocViewerURL, loadingPreviewHTML); + try { + //removing old iFrame + htmlPanelFilePreview.remove(iFrameGDV); + }catch (Exception e) { + //silent + } + //adding the new one + htmlPanelFilePreview.add(iFrameGDV); + //new attempt so incrementing it + showPreviewAttempt++; + GWT.log("Retry, go to attempt "+showPreviewAttempt); + //resetting start time for new attempt + loaderPreviewStartTime = new Date().getTime(); } } } }; - timerGDV.scheduleRepeating(PREVIEW_WAITING_TIME/3); + int attemptRepeater = PREVIEW_WAITING_TIME / 3; + timerGDV.scheduleRepeating(attemptRepeater); htmlPanelFilePreview.add(iFrameGDV); }