/alaspatial/src/main/java/org/ala/spatial/analysis/user/DownloadAnalysis.java

http://alageospatialportal.googlecode.com/ · Java · 104 lines · 69 code · 28 blank · 7 comment · 6 complexity · 4d1127e570c1863202bf8073035bb139 MD5 · raw file

  1. package org.ala.spatial.analysis.user;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.Enumeration;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.Vector;
  10. import java.util.zip.ZipEntry;
  11. import java.util.zip.ZipFile;
  12. import java.util.zip.ZipInputStream;
  13. import org.ala.spatial.util.CitationService;
  14. import org.ala.spatial.util.TabulationSettings;
  15. /**
  16. * Analysis the user downloaded zip files and sends the data
  17. * to the ALA Logger
  18. *
  19. * @author ajay
  20. */
  21. public class DownloadAnalysis {
  22. private static final int BUFFER = 2048;
  23. public DownloadAnalysis() {
  24. }
  25. private void analyseZip(String path) {
  26. try {
  27. ZipFile zf = new ZipFile(path);
  28. ZipInputStream in = new ZipInputStream(new FileInputStream(path));
  29. File theZip = new File(path);
  30. String outputdir = theZip.getAbsolutePath().substring(0, theZip.getAbsolutePath().lastIndexOf("/") + 1);
  31. // iterate till we find a csv file
  32. Enumeration entries = zf.entries();
  33. while (entries.hasMoreElements()) {
  34. ZipEntry entry = (ZipEntry) entries.nextElement();
  35. String ename = entry.getName();
  36. ename = outputdir + ename;
  37. System.out.println("File is csv: " + ename + " > " + (ename.endsWith(".csv")));
  38. if (ename.equalsIgnoreCase("samples.csv")) {
  39. System.out.println("outputdir: " + outputdir);
  40. int len = 0;
  41. byte data[] = new byte[BUFFER];
  42. BufferedOutputStream out = new BufferedOutputStream(
  43. new FileOutputStream(ename), BUFFER);
  44. while ((len = in.read(data, 0, BUFFER)) != -1) {
  45. out.write(data, 0, len);
  46. }
  47. out.close();
  48. analyseFile(ename);
  49. Vector drList = CitationService.getDataResources(ename, TabulationSettings.occurrences_dr_uid);
  50. StringBuilder sbDp = new StringBuilder();
  51. sbDp.append("[");
  52. for (int i = 0; i < drList.size(); i++) {
  53. if (i > 0) {
  54. sbDp.append(",");
  55. }
  56. sbDp.append("\"").append((String) drList.get(i)).append("\"");
  57. }
  58. sbDp.append("]");
  59. Map params = new HashMap();
  60. CitationService.postInfo(TabulationSettings.ala_logger_url, params, true);
  61. break;
  62. }
  63. }
  64. in.close();
  65. } catch (Exception e) {
  66. System.out.println("Unable to analyse zip file:");
  67. e.printStackTrace(System.out);
  68. }
  69. }
  70. private void analyseFile(String path) {
  71. }
  72. public static void main(String[] args) {
  73. DownloadAnalysis da = new DownloadAnalysis();
  74. da.analyseZip("/Users/ajay/projects/tmp/Sample_20101017_1287292213876.zip");
  75. }
  76. }