/**
*
*/
package gederedem.data;
import gederedem.Gederedem;
import gederedem.Trace;
import gederedem.storage.HsqldbStorage;
import gederedem.storage.Storage;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
/**
* Collection of datas
* @author chrelaix
*
*/
public class DataSet {
// Public constants
//******************
// Internal objects
//******************
/**
* Where the profile datas are saved.
*/
private String dataDir = Gederedem.DEFAULT_DATA_DIR;
/**
* How and where the data are saved.
*/
private Storage physicalStorage = null;
/**
* Flag to know if the dataset was changed since last load.
*/
public boolean isUnsaved = true;
/**
* logger object
*/
private static Trace log = null;
/**
* String used to store log messages
*/
private String logMsg = null;
private String name = null;
public ArrayList<Job> jobArray = new ArrayList<Job>();
// Constructors
//--------------------------
/**
* Constructor of an empty set.
*
* @param name : name of the data collection
*/
public DataSet(String name) {
super();
this.name = name;
// TODO : create a default logger
// TODO : load automatically if storage exist.
}
/**
* Constructor of an empty set.
*
* @param name : name of the data collection
*/
public DataSet(String name, Trace logger) {
this(name);
setLog(logger);
}
// Methods
//--------------------------
/**
* @param log the log to set
*/
public void setLog(Trace newlog) {
log = newlog;
}
/**
* @return the dataDir
*/
public String getDataDir() {
return dataDir;
}
public void load() {
log.entering(this.getClass().getName(), "load()");
String filePath = dataDir + "/" + name;
logMsg = MessageFormat.format(log.getMsg("logs.data.dataset.filepath_profile"), filePath);
log.info(logMsg);
ResultSet dbDatas = null;
// Test if the file exist
final File savedProfile = new File(filePath);
if (savedProfile.exists() == true) {
try {
physicalStorage = new HsqldbStorage(dataDir, name, log);
physicalStorage.open();
} catch (InstantiationException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
}
String SQLCommand = Job.GET_TABLE;
logMsg = MessageFormat.format(log.getMsg("logs.data.callSQL"), SQLCommand);
log.debug(logMsg);
dbDatas = physicalStorage.getData(SQLCommand);
try {
while(dbDatas.next()){
logMsg = MessageFormat.format(log.getMsg("logs.data.dataset.entryFound"),
dbDatas.getInt(Job.ID),
dbDatas.getString(Job.TITLE),
dbDatas.getString(Job.ENTERPRISE),
dbDatas.getString(Job.DESCRIPTION),
Job.df.format(dbDatas.getDate(Job.PUBLICATION_DATE)),
Job.df.format(dbDatas.getDate(Job.JOB_BEGIN_DATE)),
dbDatas.getString(Job.NOTE));
log.debug(logMsg);
Job savedJob = new Job(dbDatas.getInt(Job.ID),
dbDatas.getString(Job.TITLE),
dbDatas.getString(Job.ENTERPRISE),
dbDatas.getString(Job.DESCRIPTION),
dbDatas.getDate(Job.PUBLICATION_DATE),
dbDatas.getDate(Job.JOB_BEGIN_DATE),
dbDatas.getString(Job.NOTE),
dbDatas.getString(Job.URL)
);
addJob(savedJob);
}
isUnsaved = false;
} catch (SQLException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
}
physicalStorage.close();
physicalStorage = null;
} else {
log.info("logs.data.noSavedFile");
}
log.exiting(this.getClass().getName(), "load()");
}
/**
* @param dataDir the dataDir to set
*/
public void setDataDir(String dataDir) {
log.entering(this.getClass().getName(), "setDataDir(" + dataDir + ")");
this.dataDir = dataDir;
log.exiting(this.getClass().getName(), "setDataDir()");
}
public Job getJob(int index) {
log.entering(this.getClass().getName(), "getJob(" + index + ")");
// TODO Add test to avoid exception
int nextId = 0;
int maxSize = jobArray.size();
Job tmpJob;
while (nextId < maxSize) {
tmpJob = jobArray.get(nextId);
if (tmpJob.getId() == index) {
log.exiting(this.getClass().getName(), "getJob()");
return tmpJob;
}
nextId++;
}
logMsg = MessageFormat.format(log.getMsg("logs.data.noJobFound"), index);
log.info(logMsg);
log.exiting(this.getClass().getName(), "getJob()");
return null;
}
public void addJob(Job newJob) {
log.entering(this.getClass().getName(), "addJob(" + newJob.getId() + ")");
// TODO : add a try/catch for the function
// TODO : seek if a job exist with the same index
jobArray.add(newJob);
isUnsaved = true;
log.exiting(this.getClass().getName(), "addJob()");
}
public boolean changeJob(Job newJob) {
log.entering(this.getClass().getName(), "changeJob(" + newJob.getId() + ")");
int nextId = 0;
int maxSize = jobArray.size();
Job tmpJob;
// TODO : add a try/catch for the function
while (nextId < maxSize) {
tmpJob = jobArray.get(nextId);
if (tmpJob.getId() == newJob.getId()) {
jobArray.set(nextId, newJob);
isUnsaved = true;
log.exiting(this.getClass().getName(), "changeJob()", true);
return true;
}
nextId++;
}
log.exiting(this.getClass().getName(), "changeJob()", false);
return false;
}
public boolean removeJob(int indexOfjobToDelete) {
log.entering(this.getClass().getName(), "removeJob(" + indexOfjobToDelete + ")");
// TODO : add a try/catch for the function
int nextId = 0;
int maxSize = jobArray.size();
Job tmpJob;
while (nextId < maxSize) {
tmpJob = jobArray.get(nextId);
if (tmpJob.getId() == indexOfjobToDelete) {
tmpJob.setDeletedState(true);
isUnsaved = true;
log.exiting(this.getClass().getName(), "removeJob()", true);
return true;
}
nextId++;
}
log.exiting(this.getClass().getName(), "removeJob()", false);
return false;
}
public boolean Save() {
log.entering(this.getClass().getName(), "Save()");
if (isUnsaved == true) {
log.debug("logs.data.needSave");
String filePath = dataDir + "/" + name;
String msg = MessageFormat.format(log.getMsg("logs.data.dataset.filepath_profile"), filePath);
log.debug(msg);
final File savedProfile = new File(filePath);
if (savedProfile.exists() == true) {
try {
physicalStorage = new HsqldbStorage(dataDir, name, log);
physicalStorage.open();
} catch (InstantiationException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
}
updateProfile();
// TODO : warning in case of multiple save.
physicalStorage.close();
physicalStorage = null;
log.debug("logs.data.dbSaved");
} else {
// file didn't exist
log.info("logs.data.noSavedFile");
}
} else {
// no change recorded
log.info("logs.data.noSaveNeeded");
}
log.exiting(this.getClass().getName(), "save()", true);
return true;
}
private void updateProfile() {
log.entering(this.getClass().getName(), "updateProfile()");
int nextId = 0;
// TODO : test the empty indexes
int maxSize = jobArray.size();
Job tmpJob;
String msg = MessageFormat.format(log.getMsg("logs.data.dataset.seekDataToSave"), maxSize);
log.debug(msg);
while (nextId < maxSize) {
jobArray.get(nextId);
tmpJob = jobArray.get(nextId);
if (tmpJob.getChangedState() == true) {
String SQLCommand = tmpJob.GetItem();
logMsg = MessageFormat.format(log.getMsg("logs.data.callSQL"), SQLCommand);
log.debug(logMsg);
ResultSet resultQuery = physicalStorage.getData(SQLCommand);
try {
if (resultQuery.next() == false) {
log.debug("logs.data.dataset.noResult");
SQLCommand = tmpJob.AddItem();
} else {
log.debug("logs.data.dataset.resultFound");
SQLCommand = tmpJob.UpdateItem();
if (resultQuery.next() == true) {
log.severe("logs.data.dataset.multipleResult");
// TODO : Handle error
}
}
} catch (SQLException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
}
logMsg = MessageFormat.format(log.getMsg("logs.data.callSQL"), SQLCommand);
log.debug(logMsg);
int ret=physicalStorage.callSQLWithNoResult(SQLCommand);
if (ret !=1) {
// TODO : Handle error
logMsg = MessageFormat.format(log.getMsg("logs.data.dataset.returnSQL"), ret);
log.debug(logMsg);
} else {
// Ok, tell the data it's done
tmpJob.setChangedState(false);
}
}
nextId++;
}
log.exiting(this.getClass().getName(), "updateProfile()");
}
/**
* Creation of a new profile of datas
*/
public void CreateProfile() {
log.entering(this.getClass().getName(), "CreateProfile()");
// TODO : Test all the needed information to do the creation
// If all the informations are OK, then create with the correct connection type.
try {
// TODO use of multiple Storage (SQLite)
physicalStorage = new HsqldbStorage(dataDir, name, log);
physicalStorage.open();
} catch (InstantiationException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
log.warning(e.getLocalizedMessage());
e.printStackTrace();
}
String SQLCommand = Job.CREATE_TABLE;
logMsg = MessageFormat.format(log.getMsg("logs.data.callSQL"), SQLCommand);
log.debug(logMsg);
physicalStorage.callSQLWithNoResult(SQLCommand);
physicalStorage.close();
physicalStorage = null;
log.exiting(this.getClass().getName(), "CreateProfile()");
}
}