/*
 * EnronFileReading.java
 *
 * Created on November 15, 2005, 10:34 PM
 */

package DDMT.Nodes;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Vector;
import java.util.Hashtable;
import java.io.IOException;
import java.util.*;
import java.lang.*;

public class EnronFileReading 
{
    String dir;       //root directory of mail folders
    Vector MailList = new Vector();
    Vector SenderIDList = new Vector();
    float loadFactor = (float)0.5;
    public EnronFileReading(String dir) 
    {
        this.dir = dir;
//        this.ReadFile();
        this.GenerateNameID();
    }
    
    public void ReadFile()
    {
        File dir = new File(this.dir);
        File[] senders = dir.listFiles();
        int i=senders.length-1;
        System.out.println(i);
        
        //populate the initial sender list
//        while(i>=0)
//        {
//            if (senders[i].isDirectory())
//            {
//                System.out.println(senders[i].getName());
//                if (senders[i)
//            }
//            i--;
//        }
        
        try
        {
            while(i>=0)
            {
                if (senders[i].isDirectory())
                {
                    System.out.println("Sender : "+senders[i].getName());
                    File[] mailfolders = senders[i].listFiles();
                    for (int j=0;j<mailfolders.length;j++)
                    {
                        String mailFolder = mailfolders[j].getName();
                        if (mailFolder.equals("_sent_mail")||mailFolder.equals("sent_items")||mailFolder.equals("sent"))
                        {
//                            System.out.println(mailfolders[j].getName());
                            //read the individual files
                            File[] SentFiles = mailfolders[j].listFiles();
                            for (int k=0;k<SentFiles.length;k++)
                            {
                                GregorianCalendar gc = new GregorianCalendar();
                                String Sender = "";
                                String Receiver = "";
                                boolean NoSender = false;
                                boolean NoDate = false;
                                boolean NoRecvr = false;
                                MailStruct ms = new MailStruct();
//                                System.out.println("File Name :"+SentFiles[k].getName());
                                FileReader fr = new FileReader(SentFiles[k]);
                                BufferedReader reader = new BufferedReader(fr);
                                String lines = reader.readLine();
                                while(lines!=null)
                                {
                                    lines = reader.readLine();
                                    if (lines.startsWith("Date: "))
                                    {
                                        //read a date
                                        String[] temp = lines.split("Date: ");
                                        String date = temp[temp.length-1];
                                        String[] dateComponents = date.split(" ");
                                        int day = (Integer.parseInt(dateComponents[1]));
                                        int month = -1;
                                        if (dateComponents[2].equals("Jan"))
                                            month = 0;
                                        if (dateComponents[2].equals("Feb"))
                                            month = 1;
                                        if (dateComponents[2].equals("Mar"))
                                            month = 2;
                                        if (dateComponents[2].equals("Apr"))
                                            month = 3;
                                        if (dateComponents[2].equals("May"))
                                            month = 4;
                                        if (dateComponents[2].equals("Jun"))
                                            month = 5;
                                        if (dateComponents[2].equals("Jul"))
                                            month = 6;
                                        if (dateComponents[2].equals("Aug"))
                                            month = 7;
                                        if (dateComponents[2].equals("Sep"))
                                            month = 8;
                                        if (dateComponents[2].equals("Oct"))
                                            month = 9;
                                        if (dateComponents[2].equals("Nov"))
                                            month = 10;
                                        if (dateComponents[2].equals("Dec"))
                                            month = 11;
                                            
                                        int year = (Integer.parseInt(dateComponents[3]));
                                        String[] TimeComponents = dateComponents[4].split(":");
                                        int hour = Integer.parseInt(TimeComponents[0]);
                                        int min = Integer.parseInt(TimeComponents[1]);
                                        int sec = Integer.parseInt(TimeComponents[2]);
                                        gc.set(year,month,day,hour,min,sec);
                                    }
                                    lines = reader.readLine();
                                    if (lines.startsWith("From:"))
                                    {       
                                        String[] temp = lines.split("From: ");
                                        if (temp.length==0)
                                            NoSender = true;
                                        else
                                            Sender = temp[temp.length-1];
                                    }
//                                        System.out.println("Sender :"+Sender);
//                                        System.out.println("Date :"+gc.getTime().toString());
                                    lines = reader.readLine();
                                    if (!lines.startsWith("To: "))
                                    {
//                                        System.out.println("No sender");
                                        NoSender = true;
//                                            System.exit(0);
                                    }
                                    if (!NoSender)
                                    {
                                        String[] temp = lines.split("To: ");
                                        String listofReceivers = "";
                                        if (temp.length==0)
                                            NoSender = true;
                                        else
                                        {
					    listofReceivers = temp[temp.length-1];
                                            String[] receiver = listofReceivers.split(", ");
                                            //split the receiver to extract the e-mail id
                                            for (int m=0;m<receiver.length;m++)
                                            {
                                                if (receiver[m].indexOf("enron")!=-1)
                                                {
                                                    //get rid of the ,
                                                    temp = receiver[m].split(",");
                                                    Receiver = temp[0];
    //                                                    System.out.println("Receiver "+m+": "+Receiver);
                                                    ms = new MailStruct(Sender,Receiver,gc);
                                                    this.AddElement(this.MailList, ms);
                                                }
                                            }

                                            //get the other receivers
                                            lines = reader.readLine();
                                            while(!lines.startsWith("Subject: "))
                                            {
                                                //there are more receivers
                                                listofReceivers = lines.trim();
                                                receiver = listofReceivers.split(", ");
                                                //split the receiver to extract the e-mail id
                                                for (int m=0;m<receiver.length;m++)
                                                {
                                                    if (receiver[m].indexOf("enron")!=-1)
                                                    {
                                                        temp = receiver[m].split(",");
                                                        Receiver = temp[0];
    //                                                        System.out.println("Receiver "+m+": "+Receiver);
                                                        ms = new MailStruct(Sender,Receiver,gc);
                                                        this.AddElement(this.MailList, ms);
                                                    }
                                                }
                                                lines = reader.readLine();
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                i--;
            }
            File f = new File("D:\\EnronFile\\Email_list.txt");
            if (f.exists())
                f.delete();
            this.AppendFile("D:\\EnronFile\\Email_list.txt",MailList);
        }
        catch(FileNotFoundException e)
        {
            
        }
        catch(IOException e)
        {
            
        }
    }
    
    public void GenerateNameID()
    {
        
        int id = 0;
        try
        {
          String ifilename = "D:\\EnronFile\\Email_list.txt";
          
          File file = new File(ifilename);
          byte buffer[] = new byte[(int)file.length()];
          BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
          input.read(buffer,0,buffer.length);
          input.close();					 
          String fileData = new String(buffer);
          String[] result = fileData.split("\n");          
          
          String ofilename = "D:\\EnronFile\\Email_listID.txt";
          File f = new File(ofilename);
          if (f.exists())
            f.delete();
          
          int oldIndex = -1;
          int FileNum = -1;
          for (int i=0;i<result.length;i++)
          {
              System.out.println("Line no : "+i);
              String Sender = result[i].split("#")[0];
              String Receiver = result[i].split("#")[1];
              
              int indexS = this.SenderRecvrID(Sender);
              if (indexS==-1)
              {
                  SenderRecvrID s = new SenderRecvrID(Sender,id++);
                  this.SenderIDList.add(s);
                  indexS = id-1;
              }
              
              int indexR = this.SenderRecvrID(Receiver);
              if (indexR==-1)
              {
                  SenderRecvrID s = new SenderRecvrID(Receiver,id++);
                  this.SenderIDList.add(s);
                  indexR = id-1;
              }
              this.AppendFile(ofilename, ""+indexS+"#"+indexR+"#"+result[i].split("#")[2]+"\n");
              String specificfilename = "D:\\EnronFile\\SenderFiles\\"+indexS+".txt";
              File t = new File(specificfilename);
              this.AppendFile(specificfilename, ""+indexS+"#"+indexR+"#"+result[i].split("#")[2]+"\n");              
          }
          
          for (int i=0;i<this.SenderIDList.size();i++)
          {
              SenderRecvrID sri = (SenderRecvrID)this.SenderIDList.elementAt(i);
              this.AppendFile("D:\\EnronFile\\NameID.txt",sri.Name+" "+sri.id+"\n");
          }
//          
          //rename file        
          String Filename = "D:\\EnronFile\\SenderFiles";
          f = new File(Filename);
          File[] ff = f.listFiles();
          System.out.println(ff.length);
          for (int i=0;i<ff.length;i++)
          {
              buffer = new byte[(int)ff[i].length()];
              input = new BufferedInputStream(new FileInputStream(ff[i]));
              input.read(buffer,0,buffer.length);
              input.close();					 
              fileData = new String(buffer);  
              this.AppendFile("D:\\EnronFile\\SenderFilesNew"+"\\"+i+".txt",fileData);
              System.out.println(ff[i].getAbsolutePath()+" "+"D:\\EnronFile\\SenderFilesNew"+"\\"+i+".txt");
          }
      }
      catch(IOException ioe)
      {
          System.out.println("Error is : " + ioe);
      }
    }
    
    public int SenderRecvrID(String Name)
    {
        int index = -1;
        for (int i=0;i<this.SenderIDList.size();i++)
        {
            SenderRecvrID sri = (SenderRecvrID)this.SenderIDList.elementAt(i);
            if (sri.Name.equals(Name))
            {
                index = i;
                break;
            }
            else
                continue;
        }
        return index;
    }
    
    public void AddElement(Vector List,MailStruct ms)
    {
        //list is a vector of time in millisecs
        int i=0;
        for (i=0;i<List.size();i++)
        {
            MailStruct Time = (MailStruct)List.elementAt(i);
            if (Time.time.getTimeInMillis()<=ms.time.getTimeInMillis())
                continue;
            else
                break;
        }
        this.MailList.add(i, ms);
    }
    
    
    
    public  void AppendFile(String filename,Vector x) 
    {
        try 
        {
            System.out.println("Size :"+x.size());
            BufferedWriter out = new BufferedWriter(new FileWriter(filename,false));

            
            for (int i=0;i<x.size();i++)
            {
                System.out.println(i);
                String Mail_list = "";
                MailStruct ms = (MailStruct)x.elementAt(i);
                Mail_list = Mail_list.concat(ms.Sender+"#");
                Mail_list = Mail_list.concat(ms.Receiver+"#");
                Mail_list = Mail_list.concat(ms.time.getTime().toString()+"\n");
                out.write(Mail_list);
            }
            
            out.close();
        }
        catch (IOException e) 
        {
            System.out.println("Writing exception");
        }
    }
    
    public  void AppendFile(String filename,String x) 
    {
        try 
        {
            BufferedWriter out = new BufferedWriter(new FileWriter(filename,true));
            out.write(x);
            out.close();
        }
        catch (IOException e) 
        {
            System.out.println("Writing exception");
        }
    }
    
    public static void main(String[] args) 
    {
          EnronFileReading en = new EnronFileReading("d:\\maildir\\");
    }
    
}
