PageRenderTime 25ms CodeModel.GetById 9ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

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