/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Core;
import Writers.CSVWriter;
import Writers.FileWriter;
import Writers.PDFWriter;
import Writers.WSSWriter;
import Writers.XLSWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
*This class manages the calculation, presentation and storage of the calculation
* results
* @author Amir
*/
public class ResultManager {
Result result = new Result();
Data data;
File outputFile;
boolean pageSetup;
boolean coverPage;
String author;
String projectDesc;
CalCore calculationCore;
GraphResultDrawer graphDrawer;
TabularResultDrawer tableDrawer;
/**
* Constructor of class Result manager.
* @param d
* Data to be used for calculation
*/
public ResultManager(Data d) {
data = d;
calculationCore = new CalCore();
result = calculationCore.calculate(data);
graphDrawer = new GraphResultDrawer(result);
tableDrawer = new TabularResultDrawer(result);
}
/**
* Constructor of class Result manager.
* @param r
* Result to be managed
*/
public ResultManager(Result r) {
data = new Data();
result = r;
graphDrawer = new GraphResultDrawer(result);
tableDrawer = new TabularResultDrawer(result);
}
/**
* Changes the calculation dataset and causes a new calculation process
* @param d
* New dataset
*/
public void setData(Data d){
data = d;
result = calculationCore.calculate(data);
graphDrawer = new GraphResultDrawer(result);
tableDrawer = new TabularResultDrawer(result);
}
/**
* Resturns the result of the last calculation
* @return
* result of the last calculation
*/
public Result getResult(){
return result;
}
/**
* Sets the destination of the result output file
* @param path
* Full path of the new result file
* @throws Exception
* If the given path is not a writable file
*/
private void setOutputFile(String path) throws Exception{
File newFile = new File(path);
try{
if(!newFile.exists())
newFile.createNewFile();
outputFile = newFile;
}
catch(Exception ex){
throw new Exception("Can not write to destination. You may not have" +
"permission to save the file in the selected\ndirectory OR the " +
"file might have been removed/renamed.\nDescription:\n"+ex.getLocalizedMessage());
}
}
/**
* Sets the page layout of the output file
* @param choice
* User's choice of page layout
*/
public void setPage(boolean choice){
pageSetup = choice;
}
/**
* Sets the page cover page existance of the output file
* @param choice
* User's choice of the cover page existance
*/
public void enabledCoverPage(boolean choice){
coverPage = choice;
}
/**
* Sets the page author name who creates the output file
* @param theAuthor
* Author's name
*/
public void setAuthor(String theAuthor) {
author = theAuthor;
}
/**
* Sets the project description of the output file (report)
* @param theProjectDesc Project Description
*/
public void setProjectDesc(String theProjectDesc) {
projectDesc = theProjectDesc;
}
/**
* Saves the calculation results to the given file
* @param filePath
* Full path of the new result file
* @throws FileNotFoundException
* @throws IOException
* @throws Exception
*/
public void saveToFile(String filePath) throws FileNotFoundException, IOException, Exception{
filePath = filePath.toLowerCase();
setOutputFile(filePath);
FileWriter writer = null;
if(filePath.endsWith("csv")){
writer = new CSVWriter(data, result, outputFile);
}
if(filePath.endsWith("xls")){
writer = new XLSWriter(data, result, outputFile);
}
if(filePath.endsWith("wss")){
writer = new WSSWriter(data, result, outputFile);
}
if(filePath.endsWith("pdf")){
writer = new PDFWriter(data, result, outputFile, pageSetup, coverPage, author, projectDesc);
}
if (writer!=null) {
writer.writeAll();
}else
throw new Exception("Unsupported File Format");
}
/**
* Returns the graphical result drawer component
* @return
* Graphical result drawer component
*/
public GraphResultDrawer getGraphDrawer(){
return graphDrawer;
}
/**
* Returns the Tabular result drawer component
* @return
* Tabular result drawer component
*/
public TabularResultDrawer getTabularResultDrawer(){
return tableDrawer;
}
}