Remove cache, detect corruption, detect it's open, refactor

This commit is contained in:
Juan Ferrer 2020-02-28 15:18:58 +01:00
parent 58b753f706
commit 6f0d4c4acd
3 changed files with 156 additions and 102 deletions

View File

@ -8,6 +8,8 @@ var Conf = {
,dbHost: 'db.verdnatura.es' ,dbHost: 'db.verdnatura.es'
,defaultModule: 'tpv' ,defaultModule: 'tpv'
,defaultLocale: 'es' ,defaultLocale: 'es'
,dbName: 'vn2008'
,maxSize: 500
}; };
var Locale = { var Locale = {
@ -22,6 +24,8 @@ var Locale = {
"Actualizando" "Actualizando"
,"Bad login": ,"Bad login":
"Usuario o contraseña incorrectos" "Usuario o contraseña incorrectos"
,"Application it's already open":
"La aplicación ya está abierta"
,"Loading": ,"Loading":
"Cargando" "Cargando"
,"Error while updating": ,"Error while updating":
@ -30,7 +34,9 @@ var Locale = {
"Microsoft Access 2003 no está instalado en el sistema" "Microsoft Access 2003 no está instalado en el sistema"
,"MDB file not found": ,"MDB file not found":
"No se encontro el fichero MDB" "No se encontro el fichero MDB"
} ,"Cache deleted":
"Caché borrada"
}
}; };
var App = { var App = {
@ -45,7 +51,7 @@ var App = {
window.moveTo((screen.width - width) / 2, (screen.height - height) / 2); window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
}, },
_onLoad: function() { onLoad: function() {
// Initializes the global variables // Initializes the global variables
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g); var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
@ -60,6 +66,7 @@ var App = {
this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z'; this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z';
this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z'; this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb'; this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb';
this.lockFile = this.moduleDir +'\\'+ this.module +'.ldb';
// Creates the necessary registry entries // Creates the necessary registry entries
@ -89,7 +96,7 @@ var App = {
'Driver' : driverPath, 'Driver' : driverPath,
'DESCRIPTION' : Conf.appName, 'DESCRIPTION' : Conf.appName,
'SERVER' : Conf.dbHost, 'SERVER' : Conf.dbHost,
'DATABASE' : 'vn2008', 'DATABASE' : Conf.dbName,
'SSLCA' : this.appDir +'\\cacert.pem', 'SSLCA' : this.appDir +'\\cacert.pem',
'SSLVERIFY' : 1, 'SSLVERIFY' : 1,
'AUTO_RECONNECT' : 1 'AUTO_RECONNECT' : 1
@ -118,7 +125,7 @@ var App = {
if (remember && password) { if (remember && password) {
this.$('password').value = password; this.$('password').value = password;
this.$('remember').checked = true; this.$('remember').checked = true;
this._onEnterClick(); this.onEnterClick();
} else } else
this.resetForm(true); this.resetForm(true);
}, },
@ -139,9 +146,9 @@ var App = {
dsName, driverName, 'REG_SZ'); dsName, driverName, 'REG_SZ');
}, },
_disableUi: function(disabled, loadMessage) { disableUi: function(disabled, loadMessage) {
if (disabled) if (disabled)
this._hideMessage(); this.hideMessage();
else else
loadMessage = ''; loadMessage = '';
@ -156,11 +163,25 @@ var App = {
this.$('background').style.display = display; this.$('background').style.display = display;
this.$('spinner').style.display = display; this.$('spinner').style.display = display;
}, },
_onKeyPress: function(e) { onCleanCacheClick: function() {
switch (e.keyCode) { var file;
var folder = this.fso.getFolder(this.moduleDir);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext()) {
var file = files.item();
if (/\.mdb$/.test(file.name) && file.name != 'config.mdb')
file.delete(file.name);
}
this.showMessage(_('Cache deleted'));
},
onKeyPress: function(event) {
switch (event.keyCode) {
case 13: // Enter case 13: // Enter
this._onEnterPress(e); this.onEnterPress(event);
break; break;
case 27: // Esc case 27: // Esc
window.close(); window.close();
@ -168,7 +189,7 @@ var App = {
} }
}, },
_onEnterPress: function(event) { onEnterPress: function(event) {
var target = event.target || event.srcElement; var target = event.target || event.srcElement;
if (target && target.id == 'user' && this.$('password').value == '') { if (target && target.id == 'user' && this.$('password').value == '') {
@ -176,17 +197,15 @@ var App = {
return; return;
} }
this._onEnterClick(); this.onEnterClick();
}, },
_onEnterClick: function() { onEnterClick: function() {
this._disableUi(true, _('Loading')); this.disableUi(true, _('Loading'));
setTimeout(function() {App._login();}, 0); setTimeout(function() {App.login();}, 0);
}, },
_login: function() { login: function() {
var clearPassword;
try { try {
var user = this.$('user').value; var user = this.$('user').value;
var password = this.$('password').value; var password = this.$('password').value;
@ -199,86 +218,112 @@ 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');
// Gets the last version number using the MySQL ODBC connection if (this.hasToUpdate()) {
this.disableUi(true, _('Updating'));
var mysqlConn = new ActiveXObject('ADODB.Connection');
try {
mysqlConn.open(Conf.dsName);
} catch (e) {
var dbErrors = mysqlConn.errors;
if (dbErrors.count > 0) {
var errorMsg;
var dbError = dbErrors.item(0);
switch (dbError.NativeError) {
case 1045: // Access denied
clearPassword = true;
errorMsg = _('Bad login');
break;
case 2003: // Can't connect
errorMsg = _('Server can\'t be reached');
break;
default:
errorMsg = dbError.description;
}
dbErrors.clear();
throw new Error(errorMsg);
} else
throw e;
}
var sql = 'SELECT version FROM versiones WHERE programa = \''+ this.module +'\'';
var rs = mysqlConn.execute(sql);
var lastVersion = rs.EOF ? 0 : parseInt(rs.fields(0).value);
rs.close();
mysqlConn.close();
// Obtains the local version number from the MDB file
var localVersion = -1;
if (this.fso.fileExists(this.mdbFile))
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 las version
if (localVersion < lastVersion) {
this._disableUi(true, _('Updating'));
var queryString = lastVersion var queryString = lastVersion
? 'v'+ lastVersion ? 'v'+ lastVersion
: new Date().getTime(); : new Date().getTime();
var request = new ActiveXObject('MSXML2.XMLHTTP'); var request = new ActiveXObject('MSXML2.XMLHTTP');
request.open('GET', this.remoteFile +'?'+ queryString, true); request.open('GET', this.remoteFile +'?'+ queryString, true);
request.onreadystatechange = function() { request.onreadystatechange = function() {
App._onRequestReady(request); App.onRequestReady(request);
}; };
request.send(); request.send();
} else } else
this.openMdb(); this.openMdb();
} catch (e) { } catch (err) {
this._catchError(e, clearPassword); this.catchError(err);
} }
}, },
hasToUpdate: function() {
// 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;
}
_onRequestReady: function(request) var sql = "SELECT version FROM versiones WHERE programa = '"+ this.module +"'";
{ var rs = mysqlConn.execute(sql);
var lastVersion = rs.EOF ? 0 : parseInt(rs.fields(0).value);
rs.close();
mysqlConn.close();
// Checks if the MDB exists
if (!this.fso.fileExists(this.mdbFile))
return true;
// Checks wether the MDB 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 wether the MDB is abnormaly bigger so maybe it's corrupted
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;
if (this.fso.fileExists(this.mdbFile) && !forcedlyClosed)
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 last version
return localVersion < lastVersion;
},
onRequestReady: function(request) {
if (request.readyState !== 4) if (request.readyState !== 4)
return; return;
@ -312,7 +357,7 @@ var App = {
this.openMdb(); this.openMdb();
} catch (e) { } catch (e) {
this._catchError(e); this.catchError(e);
} }
}, },
@ -330,12 +375,14 @@ var App = {
window.close(); window.close();
}, },
_catchError: function(error, clearPassword) { catchError: function(err) {
var clearPassword = err.code == 'BadLogin';
if (!this.$('remember').checked || clearPassword) if (!this.$('remember').checked || clearPassword)
this.regWrite(Conf.dsPath, 'PWD', '', 'REG_SZ'); this.regWrite(Conf.dsPath, 'PWD', '', 'REG_SZ');
this._disableUi(false); this.disableUi(false);
this.showMessage(error.message); this.showMessage(err.message);
this.resetForm(clearPassword); this.resetForm(clearPassword);
}, },
@ -346,14 +393,14 @@ var App = {
var messageDiv = this.$('message'); var messageDiv = this.$('message');
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();
}, },
_hideMessage: function() { hideMessage: function() {
if (this.messageTimeout) { if (this.messageTimeout) {
this.$('message').style.display = 'none'; this.$('message').style.display = 'none';
clearTimeout(this.messageTimeout); clearTimeout(this.messageTimeout);
@ -361,8 +408,8 @@ var App = {
} }
}, },
_onUnload: function() { onUnload: function() {
this._disableUi(false); this.disableUi(false);
}, },
$: function(id) { $: function(id) {

View File

@ -46,7 +46,8 @@ input[type='password'] {
margin-top: .5em; margin-top: .5em;
} }
#checkbox, #checkbox,
#submit { #submit,
#clean {
text-align: center; text-align: center;
} }
button { button {

View File

@ -22,10 +22,10 @@
windowsstate="normal"/> windowsstate="normal"/>
</head> </head>
<body <body
onload="App._onLoad(event)" onload="App.onLoad(event)"
onunload="App._onUnload(event)" onunload="App.onUnload(event)"
onkeypress="App._onKeyPress(event)" onkeypress="App.onKeyPress(event)"
onclick="App._onBodyClick(event)"> onclick="App.onBodyClick(event)">
<img <img
id="logo" id="logo"
src="verdnatura.png" src="verdnatura.png"
@ -42,15 +42,21 @@
</div> </div>
<div id="checkbox"> <div id="checkbox">
<input id="remember" type="checkbox"/> <input id="remember" type="checkbox"/>
<label for="remember">Recuerdame</label> <label for="remember">Recu&eacute;rdame</label>
</div> </div>
<div id="submit"> <div id="submit">
<button <button
id="enter" id="enter"
onclick="App._onEnterClick(event)"> onclick="App.onEnterClick()">
Entrar Entrar
</button> </button>
</div> </div>
<div id="clean">
<a
onclick="App.onCleanCacheClick()">
Limpiar cach&eacute;
</a>
</div>
<div id="background"> <div id="background">
</div> </div>
<div id="spinner"> <div id="spinner">