PageRenderTime 64ms CodeModel.GetById 46ms app.highlight 10ms RepoModel.GetById 5ms app.codeStats 0ms

/alaspatial/src/main/java/org/ala/spatial/analysis/index/DatasetMonitor.java

http://alageospatialportal.googlecode.com/
Java | 147 lines | 87 code | 25 blank | 35 comment | 13 complexity | e3095adf3cc2cd8298f02950206958a1 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.index;
  6
  7import java.io.BufferedReader;
  8import java.io.File;
  9import java.io.FileReader;
 10import java.text.SimpleDateFormat;
 11import java.util.ArrayList;
 12import java.util.Date;
 13import java.util.HashMap;
 14import org.ala.spatial.analysis.service.ShapeIntersectionService;
 15import org.ala.spatial.util.TabulationSettings;
 16
 17/**
 18 * monitor TabulationSettings.occurrences_config_path for new occurrences files
 19 *
 20 * Events:
 21 * - add dataset
 22 * - remove dataset
 23 * - update dataset
 24 * 
 25 * @author Adam
 26 */
 27public class DatasetMonitor extends Thread {
 28
 29    ArrayList<String> dataset_files;
 30    File path;
 31    boolean end = false;
 32
 33    public DatasetMonitor() {
 34        TabulationSettings.load();
 35
 36        setPriority(MIN_PRIORITY);
 37    }
 38
 39    /**
 40     * Get all dataset headers.
 41     *
 42     * Auto update, load and enable most recent versions.
 43     *
 44     */
 45    public void initDatasetFiles() {        
 46        dataset_files = new ArrayList<String>();
 47        path = new File(TabulationSettings.occurrences_config_path);
 48
 49        HashMap<String, Dataset> names = new HashMap<String, Dataset>();
 50
 51        //add DatasetLoadedPoints
 52        try {
 53            dataset_files.add("loaded points");
 54            Dataset d = new DatasetLoadedPoints("loaded points", "loaded points", new Date(0), false, false, false);
 55            names.put("loaded points", d);
 56            OccurrencesCollection.datasets.add(d);
 57        } catch (Exception e) {
 58            e.printStackTrace();
 59        }
 60
 61        for (String f : path.list()) {
 62            dataset_files.add(path.getPath() + File.separator + f);
 63            Dataset d = loadDatasetFile(path.getPath() + File.separator + f, false, false, false);
 64
 65            //identify most recent version of this dataset
 66            if (d != null) {
 67                Dataset ds = names.get(d.name);
 68                if (ds == null) {
 69                    names.put(d.name, d);
 70                    ds = d;
 71                }
 72
 73                if (ds.date.before(d.date)) {
 74                    names.put(d.name, d);
 75                }
 76            }
 77        }
 78
 79        for (Dataset d : names.values()) {
 80            //keep index up to date
 81            d.updateOccurrencesIndex(false);
 82            d.updateSamplingIndex(false);
 83
 84            ///load index
 85            d.isEnabled = true;     //cannot load until this dataset is enabled
 86            if (d.load()) {
 87                System.out.println("Dataset loaded: " + d.getUniqueName());
 88            } else {
 89                System.out.println("Failed Dataset load: " + d.getUniqueName());
 90            }
 91        }
 92    }
 93
 94    public void run() {
 95        initDatasetFiles();
 96
 97        try {
 98            while (!end) {
 99                wait(30000);
100
101                //TODO: insert checking
102            }
103        } catch (Exception e) {
104        }
105    }
106
107    public void end() {
108        end = true;
109        this.interrupt();
110    }
111
112    /**
113     * dataset files contain \n separated records
114     *
115     * line 1: dataset name
116     * line 2: date/time as dd/mm/yyyy hh:mm:ss
117     * line 3: path to occurrences
118     *
119     * @param filename
120     * @return loaded Dataset, or null
121     */
122    private Dataset loadDatasetFile(String filename, boolean forceUpdate, boolean enabled, boolean load) {
123        try {
124            BufferedReader r = new BufferedReader(new FileReader(filename));
125
126            String[] s = new String[3];
127            int i = 0;
128            while (i < 3 && (s[i] = r.readLine()) != null) {
129                i = i + 1;
130            }
131
132            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
133
134            Dataset d = new Dataset(s[2], s[0], df.parse(s[1]), forceUpdate, enabled, load);
135
136            OccurrencesCollection.datasets.add(d);
137
138            r.close();
139
140            return d;
141        } catch (Exception e) {
142            e.printStackTrace();
143        }
144
145        return null;
146    }
147}