/webportal/src/main/java/org/ala/spatial/analysis/web/SamplingProgressWCController.java

http://alageospatialportal.googlecode.com/ · Java · 148 lines · 114 code · 20 blank · 14 comment · 26 complexity · 02ef878aae2fc6f41d61e583526edb12 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.ala.spatial.analysis.web;
  6. import au.org.emii.portal.composer.UtilityComposer;
  7. import au.org.emii.portal.menu.MapLayer;
  8. import au.org.emii.portal.settings.SettingsSupplementary;
  9. import java.io.ByteArrayInputStream;
  10. import java.util.ArrayList;
  11. import org.ala.spatial.data.Query;
  12. import org.ala.spatial.util.CommonData;
  13. import org.apache.commons.httpclient.HttpClient;
  14. import org.apache.commons.httpclient.methods.GetMethod;
  15. import org.zkoss.zk.ui.event.Event;
  16. import org.zkoss.zk.ui.event.Events;
  17. import org.zkoss.zul.Filedownload;
  18. import org.zkoss.zul.Label;
  19. import org.zkoss.zul.Messagebox;
  20. import org.zkoss.zul.Progressmeter;
  21. import org.zkoss.zul.Textbox;
  22. import org.zkoss.zul.Timer;
  23. /**
  24. *
  25. * @author ajay
  26. */
  27. public class SamplingProgressWCController extends UtilityComposer {
  28. Thread samplingThread;
  29. byte [] download;
  30. Query query;
  31. String [] layers;
  32. String [] displayNames;
  33. long start;
  34. long estimate;
  35. Label jobstatus;
  36. Progressmeter jobprogress;
  37. Timer timer;
  38. Textbox tbPid;
  39. public String pid = null;
  40. //public SamplingWCController parent = null;
  41. public AddToolSamplingComposer parent = null;
  42. @Override
  43. public void afterCompose() {
  44. super.afterCompose();
  45. timer.stop();
  46. }
  47. public void onTimer$timer(Event e) {
  48. //is it running?
  49. boolean running = samplingThread.isAlive();
  50. //get status
  51. if(!running){
  52. timer.stop();
  53. finish();
  54. } else {
  55. //update progress bar
  56. long now = System.currentTimeMillis();
  57. double pos = Math.min((now - start) / (double)estimate, 0.95);
  58. jobprogress.setValue((int) (pos * 100));
  59. }
  60. }
  61. public void onClick$btnCancel(Event e) {
  62. samplingThread.interrupt();
  63. this.detach();
  64. }
  65. void finish(){
  66. try {
  67. if(download != null) {
  68. Filedownload.save(new ByteArrayInputStream(download), "application/zip", query.getName() + ".zip");
  69. } else {
  70. Messagebox.show("Unable to download sample file.", "ALA Spatial Analysis Toolkit - Sampling", Messagebox.OK, Messagebox.ERROR);
  71. }
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. this.detach();
  76. }
  77. void start(Query q, String[] l) {
  78. if(l == null || l.length == 0) {
  79. download = query.getDownloadBytes(layers, null);
  80. finish();
  81. return;
  82. }
  83. this.query = q;
  84. this.layers = l;
  85. //estimate
  86. double perGrid = getMapComposer().getSettingsSupplementary().getValueAsDouble("sampling_time_per_grid");
  87. double perShapeFile = getMapComposer().getSettingsSupplementary().getValueAsDouble("sampling_time_per_shape_file");
  88. int threadCount = getMapComposer().getSettingsSupplementary().getValueAsInt("sampling_thread_count");
  89. estimate = 0;
  90. for(int i=0;i<l.length;i++) {
  91. if(l[i] == null) {
  92. continue;
  93. }
  94. if(l[i].startsWith("cl")) {
  95. estimate += perShapeFile * 1000 / threadCount;
  96. } else {
  97. estimate += perGrid * 1000 / threadCount;
  98. }
  99. }
  100. if(layers != null && layers.length> 0) {
  101. displayNames = new String[layers.length];
  102. for(int i=0;i<layers.length;i++) {
  103. displayNames[i] = CommonData.getFacetLayerDisplayName(layers[i]);
  104. if(displayNames[i] == null || displayNames[i].equals(layers[i])) {
  105. //attempt to get the name from a mapLayer, for analysis layers
  106. MapLayer ml = getMapComposer().getMapLayer(layers[i]);
  107. if(ml != null) {
  108. displayNames[i] = ml.getDisplayName();
  109. }
  110. }
  111. if(displayNames[i] == null || displayNames[i].equals(layers[i])) {
  112. displayNames[i] = layers[i];
  113. }
  114. }
  115. }
  116. samplingThread = new Thread() {
  117. @Override
  118. public void run() {
  119. download = query.getDownloadBytes(layers, displayNames);
  120. }
  121. };
  122. samplingThread.start();
  123. timer.start();
  124. start = System.currentTimeMillis();
  125. jobstatus.setValue("preparing...");
  126. onTimer$timer(null);
  127. }
  128. }