refs #4974 Final commit

This commit is contained in:
Guillermo Bonet 2023-01-26 10:13:49 +01:00
parent 1c65af2343
commit cc12ff328f
2 changed files with 136 additions and 176 deletions

View File

@ -47,7 +47,7 @@ var Locale = {
var App = {
shell: new ActiveXObject('WScript.Shell'),
fso: new ActiveXObject('scripting.filesystemobject'),
init: function() {
// Specify the size of window
var width = 420;
@ -60,12 +60,25 @@ var App = {
},
onLoad: function() {
// Initializes the global variables
this.module = App.getModule();
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (split.length > 1)
this.module = split[1].replace(/^"+|"+$/g, '');
if (!this.module)
this.module = Conf.defaultModule;
this.appDir = this.getEnv('ProgramFiles') +'\\'+ Conf.appName;
this.moduleDir = this.shell.SpecialFolders('AppData') +'\\'+ Conf.appName;
this.mdbPath = this.moduleDir +'\\' + this.module;
this.compressFile = this.getEnv('TEMP') +'\\'+ this.module +'.7z';
this.certFile = this.appDir +'\\cacert.pem';
this.lockFile = this.moduleDir +'\\' + this.module +'.ldb';
this.mdbFile = this.moduleDir +'\\' + this.module +'.mdb';
var defaultDsName = this.formatDatasource(Conf.defaultDatasource);
var mdbDsName = this.mdbGetValue(
'SELECT dsName FROM TblConfig',
'dsName', String
);
this.dsName = mdbDsName || defaultDsName;
// Creates the necessary registry entries
var lastVersion = this.regRead(Conf.regPath, 'lastExecutedVersion');
@ -82,11 +95,7 @@ var App = {
this.regWrite(path + 'Security', 'Level', 1);
// Creates the MySQL ODBC connection
this.createODBC(
Conf.odbcPath,
Conf.defaultDatasource + Conf.identifier,
Conf.odbcDriver
);
this.createODBC(Conf.odbcPath, defaultDsName, Conf.odbcDriver);
this.regWrite(Conf.regPath, 'remoteURL', Conf.defaultRemoteURL);
this.regWrite(Conf.regPath, 'lastExecutedVersion', Conf.version);
@ -112,36 +121,46 @@ var App = {
selectBranch.options.add(option[x])
}
});
this.$('branch').value = this.getBranch();
var branch = this.mdbGetValue(
'SELECT branch FROM tblVariables',
'branch', String
);
if (!branch)
branch = Conf.defaultBranch;
this.$('branch').value = branch;
this.onChangeBranch();
// Datasource options
var selectDatarouce = document.querySelector('#datasource');
var option = [];
var odbcName;
var allOdbc = this.enumValues(Conf.odbcPath +'ODBC Data Sources\\')
var allOdbc = this.enumValues(Conf.odbcPath +'ODBC Data Sources\\');
var idLen = Conf.identifier.length;
for (var y in allOdbc) {
if (allOdbc[y].slice(-3) == Conf.identifier) {
odbcName = allOdbc[y].replace(Conf.identifier, '')
if (allOdbc[y].slice(-idLen) == Conf.identifier) {
var odbcName = allOdbc[y];
option[y] = document.createElement('option');
option[y].text = odbcName
option[y].value = odbcName
option[y].text = this.getDatasource(odbcName);
option[y].value = odbcName;
selectDatarouce.options.add(option[y])
}
}
this.$('datasource').value = this.dsName;
this.$('datasource').value = this.getDatasource();
if (notSignOut && password) {
this.$('password').value = password;
this.$('notSignOut').checked = true;
this.onChangeDatasource(true, false);
this.onChangeDatasource(true);
this.onEnterClick();
} else {
this.resetForm(true);
this.onChangeDatasource(true, true);
this.onChangeDatasource(true);
}
},
formatDatasource: function(datasource) {
return datasource + Conf.identifier;
},
getDatasource: function(dsName) {
return dsName.substr(0, dsName.length - Conf.identifier.length);
},
resetForm: function(clearPassword) {
if (clearPassword)
this.$('password').value = '';
@ -176,9 +195,9 @@ var App = {
if (myUID && myPWD) {
params['UID'] = myUID;
params['PWD'] = myPWD;
} else {
} else {
params['UID'] = this.regRead(odbcPath, 'UID');
params['PWD'] = null;
params['PWD'] = this.regRead(odbcPath, 'PWD');
}
this.regWrites(odbcPath, params);
},
@ -234,10 +253,11 @@ var App = {
* Changes the datasource, and optionally do the following
*
* @param {Boolean} hasUpdate
* @param {Boolean} hasChangeCredentials
*/
onChangeDatasource: function(hasUpdate, hasChangeCredentials) {
var myDatasource = this.$("datasource").value;
onChangeDatasource: function(hasUpdate) {
this.dsName = this.$("datasource").value;
var myDatasource = this.getDatasource(this.dsName);
if (myDatasource == "production"||"test"||"dev") {
this.$("datasourceButton").className = myDatasource;
this.$("datasource").className = myDatasource;
@ -246,21 +266,23 @@ var App = {
this.$("datasource").className = null;
};
if (!hasUpdate) {
this.regWrite(Conf.regPath, 'currentDatasource', myDatasource);
this.updateODBC();
};
if (hasChangeCredentials) {
var odbcPath = this.getOdbcPath();
var myUID = this.regRead(odbcPath, 'remoteUser');
var myPWD = this.regRead(odbcPath, 'remotePass');
if (myUID && myPWD) {
this.$('user').value = myUID;
this.$('password').value = myPWD;
} else {
this.$('user').value = this.regRead(odbcPath, 'UID')
var odbcPath = this.getOdbcPath();
var myUID = this.regRead(odbcPath, 'remoteUser');
var myPWD = this.regRead(odbcPath, 'remotePass');
var notSignOut = this.regRead(Conf.regPath, 'notSignOut');
if (myUID && myPWD && !notSignOut) {
this.$('user').value = myUID;
this.$('password').value = myPWD;
} else {
this.$('user').value = this.regRead(odbcPath, 'UID')
if (!notSignOut)
this.resetForm(true)
else
this.$('password').value = this.regRead(odbcPath, 'PWD')
}
};
}
this.$('user').focus();
},
cleanCache: function() {
@ -295,7 +317,6 @@ var App = {
this.$('password').focus();
return;
}
this.onEnterClick();
},
onEnterClick: function() {
@ -314,29 +335,11 @@ var App = {
this.regWrite(this.getOdbcPath(), 'UID', user);
this.regWrite(this.getOdbcPath(), 'PWD', password);
var mysqlConn = new ActiveXObject('ADODB.Connection');
var datasource = this.getDatasource();
// FIXME: Can't login in dev-db
if (datasource == 'dev') {
datasource = Conf.defaultDatasource
};
// Check credentials
try {
var odbcPath = this.getOdbcPath()
mysqlConn.open(this.getODBCString({
Driver: '{'+ Conf.odbcDriver +'}',
SERVER: this.regRead(odbcPath, 'SERVER'),
DATABASE: Conf.dbName,
UID: user,
PWD: password,
SSLCA: this.certFile,
SSLMODE: this.regRead(odbcPath, 'SSLMODE'),
SSLCIPHER: 'AES256-SHA',
ENABLE_CLEARTEXT_PLUGIN : 1
}));
mysqlConn.open(this.dsName)
} catch (err) {
var dbErrors = mysqlConn && mysqlConn.errors;
@ -383,71 +386,71 @@ var App = {
},
}
};
this.request('GET', 'MdbVersions/findOne', params, function(err, res) {
if (err)
throw new Error ('Version could not be retrieved: '+ err.message +': ');
this.request('GET', 'MdbVersions/findOne', params,
function(err, res) { App.onVersionRequest(err, res); }
);
},
onVersionRequest: function(err, res) {
if (err)
throw new Error ('Version could not be retrieved: '+ err.message +': ');
// Checks if it's already open
if (App.fso.fileExists(App.lockFile()))
try {
App.fso.deleteFile(App.lockFile());
this.version = res.version;
} catch (e) {
throw new Error(_('Application it\'s already open'));
}
var lastVersion;
// Checks if it's already open
if (this.fso.fileExists(this.lockFile))
try {
this.fso.deleteFile(this.lockFile);
lastVersion = res.version;
} catch (e) {
throw new Error(_('Application it\'s already open'));
}
// Checks if MDB exists
if (!App.fso.fileExists(App.mdbFile()))
this.version = res.version;
// Checks if MDB exists
if (!this.fso.fileExists(this.mdbFile))
lastVersion = res.version;
// If it's abnormaly bigger, maybe is corrupted, so force download
if (!this.version) {
var file = App.fso.getFile(App.mdbFile());
if (file.size > Conf.maxCorruptSize * 1024 * 1024)
this.version = res.version;
// If it's abnormaly bigger, maybe is corrupted, so force download
if (!lastVersion) {
var file = this.fso.getFile(this.mdbFile);
if (file.size > Conf.maxCorruptSize * 1024 * 1024)
lastVersion = res.version;
// Obtains the local version number from the MDB file
var localVersion = App.mdbGetValue(
'SELECT Version FROM tblVariables',
'Version', parseInt
);
if (!localVersion)
localVersion = false;
// Determines if should download
!localVersion || res.version === false || localVersion != res.version
? this.version = res.version
: this.version = null;
}
if (this.version) {
// Check if there is a new version, and if there is, download it
if (version) {
App.disableUi(true, _('Updating'));
var module = App.getModule();
var remoteFile = this.version
? '.archive/'+ module +'/'+ this.version +'.7z'
: module +'.7z?'+ new Date().getTime();
remoteFile = Conf.cdnURL +'/'+ remoteFile;
var request = new ActiveXObject('MSXML2.XMLHTTP.6.0');
request.open('GET', remoteFile, true);
request.onreadystatechange = function() {
App.onRequestReady(request);
};
request.send();
} else
this.openMdb();
}
});
// Obtains the local version number from the MDB file
var localVersion = this.mdbGetValue(
'SELECT Version FROM tblVariables',
'Version', parseInt
);
if (!localVersion)
localVersion = false;
// Determines if should download
!localVersion || res.version === false || localVersion != res.version
? lastVersion = res.version
: lastVersion = null;
}
if (lastVersion) {
// Check if there is a new version, and if there is, download it
this.disableUi(true, _('Updating'));
var remoteFile = lastVersion
? '.archive/'+ this.module +'/'+ lastVersion +'.7z'
: this.module +'.7z?'+ new Date().getTime();
remoteFile = Conf.cdnURL +'/'+ remoteFile;
var request = new ActiveXObject('MSXML2.XMLHTTP.6.0');
request.open('GET', remoteFile, true);
request.onreadystatechange = function() {
App.onRequestReady(request);
};
request.send();
} else
App.openMdb();
},
mdbGetValue: function(query, field, parseFn) {
var value;
try {
if (this.fso.fileExists(this.mdbFile())) {
if (this.fso.fileExists(this.mdbFile)) {
var mdbConn = new ActiveXObject('ADODB.Connection');
mdbConn.open(this.getODBCString({
'Provider': 'Microsoft.Jet.OLEDB.4.0',
'Data Source': this.mdbFile()
'Data Source': this.mdbFile
}));
try {
@ -488,8 +491,8 @@ var App = {
stream.saveToFile(this.compressFile, 2);
stream.close();
if (this.fso.fileExists(this.mdbFile()))
this.fso.deleteFile(this.mdbFile());
if (this.fso.fileExists(this.mdbFile))
this.fso.deleteFile(this.mdbFile);
this.run('7za e "'+ this.compressFile +'" -o"'+ this.moduleDir +'"', true);
this.fso.deleteFile(this.compressFile);
@ -498,7 +501,7 @@ var App = {
}
try {
if (!this.fso.fileExists(this.mdbFile()))
if (!this.fso.fileExists(this.mdbFile))
throw new Error(_('MDB file not found'));
this.openMdb();
@ -515,15 +518,19 @@ var App = {
if (!this.fso.fileExists(accessBin))
throw new Error(_('Microsoft Access 2003 is not installed'));
this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile() +'"');
// Open the mdb and pass it the argument
this.shell.exec('"'+ accessBin +'" "'+ this.mdbFile +'" /cmd "'+ this.dsName +'"');
window.close();
},
catchError: function(err) {
var clearPassword = err.name == 'BadLogin';
if (!this.$('notSignOut').checked || clearPassword)
if (this.$('notSignOut').checked) {
this.regWrite(this.getOdbcPath(), 'PWD', '');
this.regWrite(Conf.regPath, 'notSignOut', 0)
this.$('notSignOut').checked = false
}
this.disableUi(false);
this.showMessage(err.message, 'error');
@ -555,6 +562,10 @@ var App = {
onBodyClick: function() {
this.hideMessage();
},
openOptions: function() {
this.onShowBranchOptionsClick();
this.onShowDatasourceOptionsClick();
},
/**
* Hides the last displayed non-intrusive message.
*/
@ -620,7 +631,11 @@ var App = {
} catch (e) {}
},
request: function (method, url, data, cb) {
var fullUrl = this.getRemoteURL() +'/api/'+ url;
var remoteURL = this.regRead(Conf.regPath,'remoteURL');
if (!remoteURL)
remoteURL = Conf.defaultRemoteURL;
var fullUrl = remoteURL +'/api/'+ url;
var isGet = method == 'GET';
if (isGet) {
@ -664,53 +679,13 @@ var App = {
cb(err);
}
},
/**
* Gets information about the branch config in access
*
* @return {string} Branch name, master if cannot
*/
getBranch: function() {
var branch = this.mdbGetValue(
'SELECT branch FROM tblVariables',
'branch', String
);
return branch || Conf.defaultBranch;
},
/**
* Get the current datasource used
*
* @return {string} Datasource name
*/
getDatasource: function() {
var datasource = this.regRead(
Conf.regPath,
'currentDatasource'
);
return datasource || Conf.defaultDatasource;
},
/**
* Get the remote URL
*
* @return {string} Remote server
*/
getRemoteURL: function() {
var remoteURL = this.regRead(
Conf.regPath,
'remoteURL'
);
return remoteURL || Conf.defaultRemoteURL;
},
/**
* Get the odbc path
*
* @return {string} Remote server
*/
getOdbcPath: function() {
var odbcPath = Conf.odbcPath +
(this.$("datasource").value || Conf.defaultDatasource) +
Conf.identifier
return odbcPath;
return Conf.odbcPath + this.dsName;
},
enumValues: function(RegKey) {
var RootKey = new Object()
@ -732,19 +707,6 @@ var App = {
return p_Out.sNames.toArray();
}
},
getModule: function() {
var split = Verdnatura.commandLine.match(/(?:[^\s"]+|"[^"]*")+/g);
if (split.length > 1)
return split[1].replace(/^"+|"+$/g, '');
if (!this.module)
return Conf.defaultModule;
},
lockFile: function() {
return this.mdbPath +'.ldb';
},
mdbFile: function() {
return this.mdbPath +'.mdb';
}
};
App.init();

View File

@ -57,8 +57,7 @@
<img
id="branchLogo"
src="branch.png"
onclick="App.onShowBranchOptionsClick(),
App.onShowDatasourceOptionsClick()"
onclick="App.openOptions()"
alt="Change branch"/>
<div id="branchSelector">
<select id="branch" onchange="App.onChangeBranch()"></select>
@ -70,11 +69,10 @@
<img
id="datasourceLogo"
src="datasource.png"
onclick="App.onShowDatasourceOptionsClick(),
App.onShowBranchOptionsClick()"
onclick="App.openOptions()"
alt="Change datasource"/>
<div id="datasourceSelector">
<select id="datasource" onchange="App.onChangeDatasource(false, true)">
<select id="datasource" onchange="App.onChangeDatasource(false)">
</select>
</div>
</div>