
/*** Contact Info *****
 * 
 * Veeresh Rudrappa 
 * Colorado State University
 * email: veeru.jss@gmail.com
 * 
 * Dilum Bandara
 * Colorado State University
 * email: dilumb@engr.colostate.edu
 * 
 */

package com.project.schedule;
import com.project.extract.SystemInfo;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;



public class UploadWithPhp {
  public static void upload() throws Exception {
    
	  String uploadWebLocation="";
 
	  String userHome=System.getProperty("user.home");
	  
	  Calendar today = new GregorianCalendar();
	  int yesterday = today.get(Calendar.DATE) -1;
	  
	    BufferedReader fileInput =  new BufferedReader(new FileReader(userHome+"/ResourceTracker/"+"ConfigFile.txt"));
	      try {
	    	  uploadWebLocation  = fileInput.readLine(); 
	    	  fileInput.close();
	      }
	      catch(Exception e)
	      {
	    	  
	      }
	  
	  if(today.get(Calendar.DATE) == 1){
		  
		if(today.get(Calendar.MONTH)==1 || today.get(Calendar.MONTH)==3|| today.get(Calendar.MONTH)==5|| today.get(Calendar.MONTH)==8|| today.get(Calendar.MONTH)==10 )
		{
			yesterday=31;
		}
		  
		else if(today.get(Calendar.MONTH)==2){
			
			if(today.get(Calendar.YEAR) % 4==0){
			yesterday = 28;
			}
			else{
				yesterday = 29;
			}
		}
		
		else{
			
			yesterday = 30;
			
		}
	  }
	  
	  // One day Before
	  
	  String UploadFileName = SystemInfo.MacFileName + "_"+yesterday + ".txt";
	  
	  File f = new File(userHome+"/ResourceTracker/"+UploadFileName);
	  if(f.exists()) 
	  {   
		  HttpClient httpclient = new DefaultHttpClient();
		  httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		  HttpPost httppost = new HttpPost(uploadWebLocation);
		  File file = new File(userHome+"/ResourceTracker/"+UploadFileName);
		  MultipartEntity mpEntity = new MultipartEntity();
		  ContentBody cbFile = new FileBody(file);
		  mpEntity.addPart("userfile", cbFile);
		  httppost.setEntity(mpEntity);
		  System.out.println("executing request " + httppost.getRequestLine());
		  HttpResponse response = httpclient.execute(httppost);
		  HttpEntity resEntity = response.getEntity(); 
		  if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK"))
		  {
		  
		  	  boolean bool =f.delete();
			
		  	  if(!bool)
		  	  {
				  System.out.println("Deletion Failed");
			  }
				 
		    	
		  }
		  System.out.println(response.getStatusLine());
		  if (resEntity != null) 
		  {
		      System.out.println(EntityUtils.toString(resEntity));
		  }
		  if (resEntity != null) {
		      resEntity.consumeContent();
		    }
		
		  httpclient.getConnectionManager().shutdown();
		  
	  }
	  else
	  {
		  System.out.println("The Upload File Not Yet Ready");
	  }

	  yesterday = today.get(Calendar.DATE) -2;
	  
	  
	  if(today.get(Calendar.DATE) == 1){
		  
		if(today.get(Calendar.MONTH)==1 || today.get(Calendar.MONTH)==3|| today.get(Calendar.MONTH)==5|| today.get(Calendar.MONTH)==8|| today.get(Calendar.MONTH)==10 )
		{
			
			yesterday=31;
		}
		  
		else if(today.get(Calendar.MONTH)==2){
			
			if(today.get(Calendar.YEAR) % 4==0){
			yesterday = 28;
			}
			else{
				yesterday = 29;
			}
		}
		
		else{
			yesterday = 30;

		}
	  }
	  // two days Before
	  
	  UploadFileName = SystemInfo.MacFileName + "_"+yesterday + ".txt";
	  
	 f = new File(userHome+"/ResourceTracker/"+UploadFileName);
	if(f.exists()) 
	{ 
		 HttpClient httpclient = new DefaultHttpClient();
		 httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		 HttpPost httppost = new HttpPost(uploadWebLocation);
		 File file = new File(userHome+"/ResourceTracker/"+UploadFileName);
		 MultipartEntity mpEntity = new MultipartEntity();
		 ContentBody cbFile = new FileBody(file);
		 mpEntity.addPart("userfile", cbFile);
		 httppost.setEntity(mpEntity);
		 System.out.println("executing request " + httppost.getRequestLine());
		 HttpResponse response = httpclient.execute(httppost);
		 HttpEntity resEntity = response.getEntity(); 

		 if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){

			 System.out.println("Absolute Path "+f.getAbsolutePath());
			 boolean bool =f.delete();
				  if(!bool){
				  }
		 }

		 System.out.println(response.getStatusLine());

		 if (resEntity != null) {
		      System.out.println(EntityUtils.toString(resEntity));
		    }
		    if (resEntity != null) {
		      resEntity.consumeContent();
		    }

		    httpclient.getConnectionManager().shutdown();

	}
	else{
		System.out.println("The Upload File Not Yet Ready");
		}

	yesterday = today.get(Calendar.DATE) -3;

	if(today.get(Calendar.DATE) == 1){

		if(today.get(Calendar.MONTH)==1 || today.get(Calendar.MONTH)==3|| today.get(Calendar.MONTH)==5|| today.get(Calendar.MONTH)==8|| today.get(Calendar.MONTH)==10 )
		{
			yesterday=31;
		}
		  
		else if(today.get(Calendar.MONTH)==2){
			
			if(today.get(Calendar.YEAR) % 4==0){
			yesterday = 28;
			}
			else{
				yesterday = 29;
			}
		}
		else{
			
			yesterday = 30;
		}
	  }
	  // 3 days Before
	  
	UploadFileName = SystemInfo.MacFileName + "_"+yesterday + ".txt";
	f = new File(userHome+"/ResourceTracker/"+UploadFileName);

	if(f.exists()) 
	{ 
		 HttpClient httpclient = new DefaultHttpClient();
		 httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		 HttpPost httppost = new HttpPost(uploadWebLocation);
		 File file = new File(userHome+"/ResourceTracker/"+UploadFileName);
		 MultipartEntity mpEntity = new MultipartEntity();
		 ContentBody cbFile = new FileBody(file);
		 mpEntity.addPart("userfile", cbFile);
		 httppost.setEntity(mpEntity);
		 System.out.println("executing request " + httppost.getRequestLine());
		 HttpResponse response = httpclient.execute(httppost);
		 HttpEntity resEntity = response.getEntity(); 

		 if((response.getStatusLine().toString()).equals("HTTP/1.1 200 OK")){
	
			 boolean bool =f.delete();

			 if(!bool)
			 {
				 System.out.println("Deletion Failed");
			 }
		 }

		 System.out.println(response.getStatusLine());

		 if (resEntity != null) {
		      System.out.println(EntityUtils.toString(resEntity));
		 }

		 if (resEntity != null) {
		      resEntity.consumeContent();
		 }
	    httpclient.getConnectionManager().shutdown();

	}
	else{
		System.out.println("The Upload File Not Yet Ready");
	}
  }
  
}
