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'
,defaultModule: 'tpv'
,defaultLocale: 'es'
,dbName: 'vn2008'
,maxSize: 500
};
var Locale = {
@ -22,6 +24,8 @@ var Locale = {
"Actualizando"
,"Bad login":
"Usuario o contraseña incorrectos"
,"Application it's already open":
"La aplicación ya está abierta"
,"Loading":
"Cargando"
,"Error while updating":
@ -30,7 +34,9 @@ var Locale = {
"Microsoft Access 2003 no está instalado en el sistema"
,"MDB file not found":
"No se encontro el fichero MDB"
}
,"Cache deleted":
"Caché borrada"
}
};
var App = {
@ -45,7 +51,7 @@ var App = {
window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
},
_onLoad: function() {
onLoad: function() {
// Initializes the global variables
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
@ -60,6 +66,7 @@ var App = {
this.remoteFile = Conf.remoteUrl +'/'+ this.module +'.7z';
this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.mdbFile = this.moduleDir +'\\'+ this.module +'.mdb';
this.lockFile = this.moduleDir +'\\'+ this.module +'.ldb';
// Creates the necessary registry entries
@ -89,7 +96,7 @@ var App = {
'Driver' : driverPath,
'DESCRIPTION' : Conf.appName,
'SERVER' : Conf.dbHost,
'DATABASE' : 'vn2008',
'DATABASE' : Conf.dbName,
'SSLCA' : this.appDir +'\\cacert.pem',
'SSLVERIFY' : 1,
'AUTO_RECONNECT' : 1
@ -118,7 +125,7 @@ var App = {
if (remember && password) {
this.$('password').value = password;
this.$('remember').checked = true;
this._onEnterClick();
this.onEnterClick();
} else
this.resetForm(true);
},
@ -139,9 +146,9 @@ var App = {
dsName, driverName, 'REG_SZ');
},
_disableUi: function(disabled, loadMessage) {
disableUi: function(disabled, loadMessage) {
if (disabled)
this._hideMessage();
this.hideMessage();
else
loadMessage = '';
@ -156,11 +163,25 @@ var App = {
this.$('background').style.display = display;
this.$('spinner').style.display = display;
},
_onKeyPress: function(e) {
switch (e.keyCode) {
onCleanCacheClick: function() {
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
this._onEnterPress(e);
this.onEnterPress(event);
break;
case 27: // Esc
window.close();
@ -168,7 +189,7 @@ var App = {
}
},
_onEnterPress: function(event) {
onEnterPress: function(event) {
var target = event.target || event.srcElement;
if (target && target.id == 'user' && this.$('password').value == '') {
@ -176,17 +197,15 @@ var App = {
return;
}
this._onEnterClick();
this.onEnterClick();
},
_onEnterClick: function() {
this._disableUi(true, _('Loading'));
setTimeout(function() {App._login();}, 0);
onEnterClick: function() {
this.disableUi(true, _('Loading'));
setTimeout(function() {App.login();}, 0);
},
_login: function() {
var clearPassword;
login: function() {
try {
var user = this.$('user').value;
var password = this.$('password').value;
@ -199,86 +218,112 @@ var App = {
this.regWrite(Conf.dsPath, 'UID', user, 'REG_SZ');
this.regWrite(Conf.dsPath, 'PWD', password, 'REG_SZ');
// Gets the last version number using the MySQL ODBC connection
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'));
if (this.hasToUpdate()) {
this.disableUi(true, _('Updating'));
var queryString = lastVersion
? 'v'+ lastVersion
: new Date().getTime();
var request = new ActiveXObject('MSXML2.XMLHTTP');
request.open('GET', this.remoteFile +'?'+ queryString, true);
request.onreadystatechange = function() {
App._onRequestReady(request);
App.onRequestReady(request);
};
request.send();
} else
this.openMdb();
} catch (e) {
this._catchError(e, clearPassword);
} catch (err) {
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)
return;
@ -312,7 +357,7 @@ var App = {
this.openMdb();
} catch (e) {
this._catchError(e);
this.catchError(e);
}
},
@ -330,12 +375,14 @@ var App = {
window.close();
},
_catchError: function(error, clearPassword) {
catchError: function(err) {
var clearPassword = err.code == 'BadLogin';
if (!this.$('remember').checked || clearPassword)
this.regWrite(Conf.dsPath, 'PWD', '', 'REG_SZ');
this._disableUi(false);
this.showMessage(error.message);
this.disableUi(false);
this.showMessage(err.message);
this.resetForm(clearPassword);
},
@ -346,14 +393,14 @@ var App = {
var messageDiv = this.$('message');
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();
onBodyClick: function() {
this.hideMessage();
},
_hideMessage: function() {
hideMessage: function() {
if (this.messageTimeout) {
this.$('message').style.display = 'none';
clearTimeout(this.messageTimeout);
@ -361,8 +408,8 @@ var App = {
}
},
_onUnload: function() {
this._disableUi(false);
onUnload: function() {
this.disableUi(false);
},
$: function(id) {

View File

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

View File

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