//CYCLONE MATRIX HOST NODE APPLICATION
//Hermann L. Johnson, 2019
//Free for unmodified distribution and non-commercial use.

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

public class DataManager {

   public DataManager(String root_directory) {

   }


//Use special methods to stop data files from being corrupted in case
//of shutdown.

//THE SERVER'S DATAMANAGER CANNOT REORDER ITS DATA IN THE MIDST OF A CLIENT
//CONNECTION, at least as far as that client is concerned.




   public Vector<String> getIndex() {

       Vector<String> index = new Vector<String>();

index.addElement("text index entry");
       return index;

   }

   //Does not want an extension.
   //Provides header hashes as well.
   public Vector<String> getHashList(String dataFile) {
       Vector<String> results = new Vector<String>();

System.err.println("Data manager is supposed to yield all hashes of "+dataFile);

results.addElement("hashlist for "+dataFile);

       return results;
   }

   //Does not want an extension.
   public Vector<String> getHeaderHashList(String dataFile) {
       Vector<String> results = new Vector<String>();
results.addElement("header hash list for "+dataFile);


       return results;
   }

   //Returns 1 if the exact datafile is on hand.
   //Returns 2 if the datafile is on hand, but is not exact.
   //Returns 0 if the datafile is non-existent.
   public int hasExactDataFile(String abbrLocation, String entriesDate,
                                   String fileSha) {

System.err.println("Data manager: "+abbrLocation+" "+entriesDate+" "+fileSha+" is being queried, we are claiming that it is on hand, but not exact.");
return 2;

   }

   //Does not want an extension.
   //This would also yield a header entry that is requested by hash.
   public String getDataEntry(String dataFileName, String identifyingHash) {
System.err.println("Data manager is supposed to yield entry: "+identifyingHash);
return "This is a test line being pulled by getDataEntry!!!!!!!!%LBR%";
   }

   //Does not want an extension.
   //This also includes header entries.
   public Vector<String> getDataEntries(String dataFileName,
                                        String hashSpan) {

       int dashLocation = 0;
       String firstHash = new String("");
       String secondHash = new String("");
       Vector<String> results = new Vector<String>();

       //If it was called with no dash, return nothing.
       if(!hashSpan.contains("-")) return results;

       dashLocation = hashSpan.indexOf("-");
       firstHash = hashSpan.substring(0,dashLocation);
       secondHash = hashSpan.substring(dashLocation+1,hashSpan.length());

System.err.println("data manager is supposed to yield hash span request: "+firstHash+" - "+secondHash);
//Get the entries..
results.addElement("dataentries for "+dataFileName+" hashspan: "+hashSpan);

       return results;
   }

   //This also accepts header entries.
   public void takeDataLine(String line) {
System.err.println("Data manager taking: "+line);
   }

//NOTE: If a date-time is not found where it should be, but is found in the
//next location as to imply that the hash
//was included, remove the hash from said inbound entry.

//ALSO! Headers must be considered, their format is different!

   public static boolean isValidDateTime(String date) {
       if(date.length()<24) return false;
       if(!isValidDate(date.substring(0,10))) return false;
       if(date.charAt(10)!='T') return false;
       if(!Character.isDigit(date.charAt(11))) return false;
       if(!Character.isDigit(date.charAt(12))) return false;
       if(date.charAt(13)!=':') return false;
       if(!Character.isDigit(date.charAt(14))) return false;
       if(!Character.isDigit(date.charAt(15))) return false;
       if(date.charAt(16)!=':') return false;
       if(!Character.isDigit(date.charAt(17))) return false;
       if(!Character.isDigit(date.charAt(18))) return false;
       if((date.charAt(19)!='+') && (date.charAt(19)!='-')) return false;
       if(!Character.isDigit(date.charAt(20))) return false;
       if(!Character.isDigit(date.charAt(21))) return false;
       if(date.charAt(22)!=':') return false;
       if(!Character.isDigit(date.charAt(23))) return false;
       if(!Character.isDigit(date.charAt(24))) return false;

       return true;
   }

   public static boolean isValidDate(String date) {
       String current_part;
       try {
           StringTokenizer sttok = new StringTokenizer(date,"-");
           current_part = sttok.nextToken();
           if(current_part.length()!=4) return false;
           for(int count=0;count<current_part.length();count++) {
               if(!Character.isDigit(current_part.charAt(count)))
                   return false;
           }
           current_part = sttok.nextToken();
           if(current_part.length()!=2) return false;
           for(int count=0;count<current_part.length();count++) {
               if(!Character.isDigit(current_part.charAt(count)))
                   return false;
           }
           current_part = sttok.nextToken();
           if(current_part.length()!=2) return false;
           for(int count=0;count<current_part.length();count++) {
               if(!Character.isDigit(current_part.charAt(count)))
                   return false;
           }
       } catch(Exception e) {
           return false;
       }
       return true;
   }

}