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' ,defaultModule: 'tpv'
,defaultLocale: 'es' ,defaultLocale: 'es'
,dbName: 'vn2008' ,dbName: 'vn2008'
,maxSize: 500 ,maxCorruptSize: 600
}; };
var Locale = { var Locale = {
@ -208,12 +208,10 @@ var App = {
onEnterClick: function() { onEnterClick: function() {
this.disableUi(true, _('Loading')); this.disableUi(true, _('Loading'));
setTimeout(function() { App.login(); }, 0); setTimeout(function() { App.login(); });
}, },
login: function() { login: function() {
var downloadVersion;
try { try {
var user = this.$('user').value; var user = this.$('user').value;
var password = this.$('password').value; var password = this.$('password').value;
@ -226,13 +224,22 @@ var App = {
this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ'); this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ');
this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ'); this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ');
if (hasToUpdate.call(this)) { var version = this.fetchVersion();
if (version) {
this.disableUi(true, _('Updating')); this.disableUi(true, _('Updating'));
var remoteFile = Conf.remoteUrl; var remoteFile;
remoteFile += downloadVersion
? '/.archive/'+ this.module +'/'+ downloadVersion if (version.code == 'last') {
: '/'+ this.module; remoteFile = this.module +'.7z';
remoteFile += '.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'); var request = new ActiveXObject('MSXML2.XMLHTTP');
request.open('GET', remoteFile, true); request.open('GET', remoteFile, true);
@ -245,93 +252,107 @@ var App = {
} catch (err) { } catch (err) {
this.catchError(err); this.catchError(err);
} }
},
function hasToUpdate() { /**
// Gets the last version number using the MySQL ODBC connection * 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 mysqlConn = new ActiveXObject('ADODB.Connection'); var mysqlConn = new ActiveXObject('ADODB.Connection');
try { try {
mysqlConn.open(Conf.dsName); mysqlConn.open(Conf.dsName);
} catch (err) { } catch (err) {
var dbErrors = mysqlConn.errors; var dbErrors = mysqlConn.errors;
if (dbErrors.count > 0) { if (dbErrors.count > 0) {
var newErr; var newErr;
var dbError = dbErrors.item(0); var dbError = dbErrors.item(0);
switch (dbError.NativeError) { switch (dbError.NativeError) {
case 1045: // Access denied case 1045: // Access denied
clearPassword = true; clearPassword = true;
newErr = new Error(_('Bad login')); newErr = new Error(_('Bad login'));
newErr.code = 'BadLogin'; newErr.code = 'BadLogin';
break; break;
case 2003: // Can't connect case 2003: // Can't connect
newErr = new Error(_('Server can\'t be reached')); newErr = new Error(_('Server can\'t be reached'));
break; break;
default: default:
errorMsg = dbError.description; errorMsg = dbError.description;
} }
dbErrors.clear(); dbErrors.clear();
throw newErr; throw newErr;
} else } else
throw err; throw err;
}
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;
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 != downloadVersion;
} }
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) { onRequestReady: function(request) {
@ -397,7 +418,19 @@ var App = {
this.resetForm(clearPassword); 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) { showMessage: function(message, className) {
setTimeout(function() {
App.showMessageAsync(message, className);
});
},
showMessageAsync: function(message, className) {
if (this.messageTimeout) if (this.messageTimeout)
clearTimeout(this.messageTimeout); clearTimeout(this.messageTimeout);
@ -405,13 +438,18 @@ var App = {
messageDiv.className = className; messageDiv.className = className;
messageDiv.innerHTML = message; messageDiv.innerHTML = message;
messageDiv.style.display = 'block'; messageDiv.style.display = 'block';
this.messageTimeout = setTimeout(function() { App.hideMessage(); }, 10000); this.messageTimeout = setTimeout(function() {
App.hideMessage();
}, 10000);
}, },
onBodyClick: function() { onBodyClick: function() {
this.hideMessage(); this.hideMessage();
}, },
/**
* Hides the last displayed non-intrusive message.
*/
hideMessage: function() { hideMessage: function() {
if (this.messageTimeout) { if (this.messageTimeout) {
this.$('message').style.display = 'none'; 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) { $: function(id) {
return document.getElementById(id); return document.getElementById(id);
}, },
@ -456,6 +495,10 @@ var App = {
regWrites: function(path, type, values) { regWrites: function(path, type, values) {
for(var key in values) for(var key in values)
this.regWrite(path, key, values[key], type); this.regWrite(path, key, values[key], type);
},
onUnload: function() {
this.disableUi(false);
} }
}; };