Fix: download the latest version regardless of the number, documentation & small refactor

This commit is contained in:
Juan Ferrer 2020-03-03 12:08:47 +01:00
parent e4dfb1b147
commit eee4ba89d9
1 changed files with 142 additions and 99 deletions

View File

@ -9,7 +9,7 @@ var Conf = {
,defaultModule: 'tpv'
,defaultLocale: 'es'
,dbName: 'vn2008'
,maxSize: 500
,maxCorruptSize: 600
};
var Locale = {
@ -208,12 +208,10 @@ var App = {
onEnterClick: function() {
this.disableUi(true, _('Loading'));
setTimeout(function() { App.login(); }, 0);
setTimeout(function() { App.login(); });
},
login: function() {
var downloadVersion;
try {
var user = this.$('user').value;
var password = this.$('password').value;
@ -226,13 +224,22 @@ var App = {
this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ');
this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ');
if (hasToUpdate.call(this)) {
var version = this.fetchVersion();
if (version) {
this.disableUi(true, _('Updating'));
var remoteFile = Conf.remoteUrl;
remoteFile += downloadVersion
? '/.archive/'+ this.module +'/'+ downloadVersion
: '/'+ this.module;
remoteFile += '.7z';
var remoteFile;
if (version.code == 'last') {
remoteFile = this.module +'.7z';
remoteFile += version.number
? 'v'+ version.number
: new Date().getTime();
} else {
remoteFile = '.archive/'+ this.module +'/'+ version.number;
}
remoteFile = Conf.remoteUrl +'/'+ remoteFile;
var request = new ActiveXObject('MSXML2.XMLHTTP');
request.open('GET', remoteFile, true);
@ -245,93 +252,107 @@ var App = {
} catch (err) {
this.catchError(err);
}
},
function hasToUpdate() {
// Gets the last version number using the MySQL ODBC connection
var mysqlConn = new ActiveXObject('ADODB.Connection');
try {
mysqlConn.open(Conf.dsName);
} catch (err) {
var dbErrors = mysqlConn.errors;
if (dbErrors.count > 0) {
var newErr;
var dbError = dbErrors.item(0);
switch (dbError.NativeError) {
case 1045: // Access denied
clearPassword = true;
newErr = new Error(_('Bad login'));
newErr.code = 'BadLogin';
break;
case 2003: // Can't connect
newErr = new Error(_('Server can\'t be reached'));
break;
default:
errorMsg = dbError.description;
}
dbErrors.clear();
throw newErr;
} else
throw err;
}
/**
* Gets information about the version to download.
*
* @return {Object} Version information or %null if local is up-to-date
*/
fetchVersion: function() {
// Gets the last version number using the MySQL ODBC connection
var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
var rs = mysqlConn.execute(sql);
downloadVersion = rs.EOF ? 0 : parseInt(rs.fields(0).value);
rs.close();
mysqlConn.close();
if (this.$('previous-version').checked && downloadVersion > 1)
downloadVersion -= 1;
// Checks if it's already open
if (this.fso.fileExists(this.lockFile))
try {
this.fso.deleteFile(this.lockFile);
return true;
} catch (e) {
throw new Error(_('Application it\'s already open'));
}
// Checks if MDB exists
if (!this.fso.fileExists(this.mdbFile))
return true;
// If it's abnormaly bigger, maybe is corrupted, so force download
var file = this.fso.getFile(this.mdbFile);
if (file.size > Conf.maxSize * 1024 * 1024)
return true;
// Obtains the local version number from the MDB file
var localVersion = -1;
var mysqlConn = new ActiveXObject('ADODB.Connection');
try {
var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';');
var oRs = new ActiveXObject('ADODB.Recordset');
oRs.Open('SELECT Version FROM tblVariables', mdbConn);
localVersion = oRs.EOF ? -1 : parseInt(oRs('Version'));
oRs.close();
mdbConn.close();
} catch (e) {}
// Compares the local version with the requested version
try {
mysqlConn.open(Conf.dsName);
} catch (err) {
var dbErrors = mysqlConn.errors;
return localVersion != downloadVersion;
if (dbErrors.count > 0) {
var newErr;
var dbError = dbErrors.item(0);
switch (dbError.NativeError) {
case 1045: // Access denied
clearPassword = true;
newErr = new Error(_('Bad login'));
newErr.code = 'BadLogin';
break;
case 2003: // Can't connect
newErr = new Error(_('Server can\'t be reached'));
break;
default:
errorMsg = dbError.description;
}
dbErrors.clear();
throw newErr;
} else
throw err;
}
var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
var rs = mysqlConn.execute(sql);
var lastVersion = rs.EOF ? null : parseInt(rs.fields(0).value);
var version = {
code: 'last',
number: lastVersion
};
rs.close();
mysqlConn.close();
if (this.$('previous-version').checked && lastVersion > 1)
version = {
code: 'previous',
number: lastVersion - 1
};
// Checks if it's already open
if (this.fso.fileExists(this.lockFile))
try {
this.fso.deleteFile(this.lockFile);
return version;
} catch (e) {
throw new Error(_('Application it\'s already open'));
}
// Checks if MDB exists
if (!this.fso.fileExists(this.mdbFile))
return version;
// If it's abnormaly bigger, maybe is corrupted, so force download
var file = this.fso.getFile(this.mdbFile);
if (file.size > Conf.maxCorruptSize * 1024 * 1024)
return version;
// Obtains the local version number from the MDB file
var localVersion;
try {
var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open('Provider=Microsoft.Jet.OLEDB.4.0; Data Source='+ this.mdbFile +';');
var oRs = new ActiveXObject('ADODB.Recordset');
oRs.Open('SELECT Version FROM tblVariables', mdbConn);
localVersion = oRs.EOF ? -1 : parseInt(oRs('Version'));
oRs.close();
mdbConn.close();
} catch (e) {}
// Compares the local version with the requested version
return !localVersion || localVersion != version.number
? version
: null;
},
onRequestReady: function(request) {
@ -396,8 +417,20 @@ var App = {
this.showMessage(err.message, 'error');
this.resetForm(clearPassword);
},
/**
* Displays a non-intrusive message.
*
* @param {String} message Message to display
* @param {String<error|notice>} className Message type
*/
showMessage: function(message, className) {
setTimeout(function() {
App.showMessageAsync(message, className);
});
},
showMessageAsync: function(message, className) {
if (this.messageTimeout)
clearTimeout(this.messageTimeout);
@ -405,13 +438,18 @@ var App = {
messageDiv.className = className;
messageDiv.innerHTML = message;
messageDiv.style.display = 'block';
this.messageTimeout = setTimeout(function() { App.hideMessage(); }, 10000);
this.messageTimeout = setTimeout(function() {
App.hideMessage();
}, 10000);
},
onBodyClick: function() {
this.hideMessage();
},
/**
* Hides the last displayed non-intrusive message.
*/
hideMessage: function() {
if (this.messageTimeout) {
this.$('message').style.display = 'none';
@ -420,10 +458,11 @@ var App = {
}
},
onUnload: function() {
this.disableUi(false);
},
/**
* Obtains a DOM element by it's identifier.
*
* @param {String} id The element id
*/
$: function(id) {
return document.getElementById(id);
},
@ -456,6 +495,10 @@ var App = {
regWrites: function(path, type, values) {
for(var key in values)
this.regWrite(path, key, values[key], type);
},
onUnload: function() {
this.disableUi(false);
}
};