public void updateDayProgramsFor(Channel channel, String level)
throws UpdateException
{
Date date = new Date().addDays(-1);
int daysWithNoData = 0;
while (daysWithNoData < MAX_DAYS_WITHOUT_DATA) {
String completeFileName = DayProgramFile.getProgramFileName(date,
channel.getCountry(), channel.getId(), level);
// Check which version is on the source
// version -1 means there is no version
int versionAtSource = -1;
if (mDataSource.fileExists(completeFileName)) {
versionAtSource = 1;
boolean finished = false;
do {
String updateFileName = DayProgramFile.getProgramFileName(date,
channel.getCountry(), channel.getId(), level, versionAtSource);
if (mDataSource.fileExists(updateFileName)) {
// There is an update file for the current version
// -> The version of the complete file must be at least one higher
versionAtSource++;
} else {
finished = true;
}
} while (! finished);
}
if (versionAtSource == -1) {
daysWithNoData++;
} else {
daysWithNoData = 0;
// Check which version is on the mirror
// version -1 means there is no version
int versionOnMirror = -1;
if (fileIsOnMirror(completeFileName)) {
versionOnMirror = 1;
boolean finished = false;
do {
String updateFileName = DayProgramFile.getProgramFileName(date,
channel.getCountry(), channel.getId(), level, versionOnMirror);
if (fileIsOnMirror(updateFileName)) {
// There is an update file for the current version
// -> The version of the complete file must be at least one higher
versionOnMirror++;
} else {
finished = true;
}
} while (! finished);
}
// Check whether the mirror needs an update
if (versionOnMirror < versionAtSource) {
String additionalFileName = null;
if (versionOnMirror == -1) {
mLog.fine("Adding version " + versionAtSource
+ ": " + completeFileName);
} else {
mLog.fine("Updating from version "
+ versionOnMirror + " to " + versionAtSource
+ ": " + completeFileName);
}
int index = completeFileName.indexOf("_update_");
if(index != -1) {
additionalFileName = completeFileName.substring(0,index) + completeFileName.substring(index+8,completeFileName.indexOf(".prog.gz")) + "_additional.prog.gz";
} else {
additionalFileName = completeFileName.substring(0,completeFileName.indexOf(".prog.gz")) + "_additional.prog.gz";
}
mLog.log(Level.INFO, "Checking for old additional file: " + additionalFileName);
for(String value : mTargetFileArr) {
if(value.compareTo(additionalFileName) == 0) {
mLog.fine("Deleting old additional file: " + additionalFileName);
mDataTarget.deleteFile(additionalFileName);
break;
}
}
if(mDataSource.fileExists(additionalFileName)) {
mLog.fine("Adding new additional file: " + additionalFileName);
byte[] data = mDataSource.loadFile(additionalFileName);
mDataTarget.writeFile(additionalFileName, data);
}
// Update the mirror
byte[] data = mDataSource.loadFile(completeFileName);
mDataTarget.writeFile(completeFileName, data);
for (int version = 0; version < versionAtSource; version++) {
String updateFileName = DayProgramFile.getProgramFileName(date,
channel.getCountry(), channel.getId(), level, version);
data = mDataSource.loadFile(updateFileName);
mDataTarget.writeFile(updateFileName, data);
}
} else {
mLog.fine("File already up to date (version " + versionOnMirror + "): "
+ completeFileName);
}
}
// Go on with the next day
date = date.addDays(1);
}
}