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