PageRenderTime 115ms CodeModel.GetById 98ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

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