
/**
 * DataSnoop.java
 * class for searhing through data saved in the sdl format
 *
 * Created: May 25, 2004
 *
 * @author Nathan Stevens
 * @version 0.21 3/9/06
 */

import java.util.*;
import java.io.File;

public class DataSnoop {
  private File dataDirectory;
  private DataSnoopSearchList[] dataSnoopSearchList;
  private final String SDL_EXTENSION = ".sdl";
  
  public DataSnoop(File dataDirectory) {
    this.dataDirectory = dataDirectory;
  }
  
  // Method to set the data directory
  public void setDataDirectory(File dataDirectory) {
    this.dataDirectory = dataDirectory;
    dataSnoopSearchList = null;
  }
  
  // Method to retrieve the search list
  public DataSnoopSearchList[] getDataSearchList() {
    File[] filelist = getSDLFileList();
    generateSearchList(filelist);
    
    return dataSnoopSearchList;
  }
  
  // Method to get the list of sdl files in the data directory
  private File[] getSDLFileList() {
    DirectoryLister directoryLister = new DirectoryLister();
    directoryLister.listDirectory(dataDirectory, SDL_EXTENSION);
    return directoryLister.getFileList();
  }
  
  // Method to generate the SDL search list
  private void generateSearchList(File[] filelist) {
    dataSnoopSearchList = new DataSnoopSearchList[filelist.length];
    
    for(int i = 0; i < filelist.length; i++) {
      dataSnoopSearchList[i] = new DataSnoopSearchList(filelist[i]);
      
      // now add search objects for each data file
      addSearchObjects(dataSnoopSearchList[i]);
    }
  }
  
  // Method to add the search objects to data snoop search list
  private void addSearchObjects(DataSnoopSearchList slist) {
    String[] header = null;
    String[] meta = null;
    String name = null;
    String type = null;
    String date = null;
    String notes = null;
    String page = null;
    int index = 0;
    
    // open the spectra data list file
    Object object = BinaryFileManager.getObjectFromFile(slist.getFile());
    ExperimentData experimentData = (ExperimentData)object;
    Vector sdv = experimentData.getData();
    
    // add information to the DataSnoopSearchList object now
    meta = experimentData.getMetaInfo();
    type = new String(experimentData.getType());
    date = new String(meta[4]);
    notes = new String(experimentData.getNotes());
    
    slist.setInformation(type, date, notes);
    
    for (Enumeration e = sdv.elements(); e.hasMoreElements(); ) {
      SpectraData sd = (SpectraData)e.nextElement();
      name = sd.getName();
      header = sd.getHeader();
      meta = sd.getMetaInfo();
      
      type = getSpectraDataType(sd.getType());
      date = new String(meta[5]);
      page = new String(meta[6]);
      notes = new String(header[header.length -1]);
      
      DataSnoopSearchObject sobject = new DataSnoopSearchObject(name, type, date, 
                                                                                             notes, page, slist.getFile(), index);
      slist.add(sobject);
      index++;
    }
  }
  
  // Method to get the spectra data type
  private String getSpectraDataType(String exptype) {
    String type = null;
    if(exptype.equals("transmission") || exptype.equals("reflectance")) {
      type = "absorption";
    }
    else if(exptype.equals("Em") || exptype.equals("Ex")) {
      type = "emission";
    }
    else if(exptype.equals("macro") || exptype.equals("micro") || exptype.equals("FT")) {
      type = "raman";
    }
    else if(exptype.equals("streak")) {
      type = "streak";
    }
    else if(exptype.equals("pump-probe")) {
      type = "pump-probe";
    }
    else if(exptype.equals("oodata")) {
      type = "oodata";
    }
    else if(exptype.equals("xydata")) {
      type = "xydata";
    }
    else {
      System.err.println("Unkown experiment type " + exptype + " in DataSnoop");
    }
    
    return type;
  }
  
  // Method to search through the sdl list for a particular
  public DataSnoopSearchList[] searchThroughList(String searchby, String searchfor,
                                                                                       boolean[] etypes) {
    if(dataSnoopSearchList == null) {
      getDataSearchList();
    }
    
    searchfor = searchfor.toLowerCase();
    DataSnoopSearchObject searchObject;
    boolean show = false; // see whether to display this data object
    
    for(int i = 0; i < dataSnoopSearchList.length; i++) {
      for (Enumeration e = dataSnoopSearchList[i].elements(); e.hasMoreElements(); ) {
        searchObject = (DataSnoopSearchObject)e.nextElement();
        String type = searchObject.getType();
        
        // decide what to search for
        String searchterm = "";
        
        if(searchby.equals("name")) {
          searchterm = searchObject.getName().toLowerCase();
        }
        else if(searchby.equals("note")) {
          searchterm = searchObject.getNotes().toLowerCase();
        }
        else if(searchby.equals("date")) {
          searchterm = searchObject.getDate();
        }
        else if(searchby.equals("page")) {
          searchterm = searchObject.getPage();
        }
        
        // now start the searching
        if(etypes[1] && type.equals("absorption")) {
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else if(etypes[2] && type.equals("emission")) {
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else if(etypes[3] && type.equals("raman")) {
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else if(etypes[4] && type.equals("streak")) {
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else if(etypes[5] && type.equals("pump-probe")) {
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else if(etypes[6] && type.equals("oodata")) {
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else if(etypes[7] && type.equals("xydata")) { // should be using XY data type in this search, but
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
        }
        else { // no specific data to search for
          if(searchterm.indexOf(searchfor) != -1) {
            searchObject.setShown(true);
            show = true;
          }
          else {
            searchObject.setShown(false);
          }
        }
      }
      
      dataSnoopSearchList[i].setShown(show);
      show = false;
    }
    
    return dataSnoopSearchList;
  }
  
  // Method to search for data of a particlular type
  public DataSnoopSearchList[]  searchForAll(boolean[] etypes) {
    if(dataSnoopSearchList == null) {
      getDataSearchList();
    }
    
    // now add nodes to the tree now
    DataSnoopSearchObject searchObject;
    boolean show = false; // see whether to display this data object
    
    for(int i = 0; i < dataSnoopSearchList.length; i++) {
      for (Enumeration e = dataSnoopSearchList[i].elements(); e.hasMoreElements(); ) {
        searchObject = (DataSnoopSearchObject)e.nextElement();
        String type = searchObject.getType();
        
        if(etypes[1] && type.equals("absorption")) {
          searchObject.setShown(true);
          show = true;
        }
        else if(etypes[2] && type.equals("emission")) {
           searchObject.setShown(true);
           show = true;
        }
        else if(etypes[3] && type.equals("raman")) {
           searchObject.setShown(true);
           show = true;
        }
        else if(etypes[4] && type.equals("streak")) {
           searchObject.setShown(true);
           show = true;
        }
        else if(etypes[5] && type.equals("pump-probe")) {
           searchObject.setShown(true);
           show = true;
        }
        else if(etypes[6] && type.equals("oodata")) {
           searchObject.setShown(true);
           show = true;
        }
        else if(etypes[7] && type.equals("xydata")) {
           searchObject.setShown(true);
           show = true;
        }
        else {
          searchObject.setShown(false);
          show = false;
        }
      }
      dataSnoopSearchList[i].setShown(show);
      show = false;
    }
    
    return dataSnoopSearchList;
  }
} //--end class DataSnoop
