4974-datasources-and-improvements #4
199
src/main.js
199
src/main.js
|
@ -5,15 +5,15 @@ var Conf = {
|
|||
dsName: 'verdnatura',
|
||||
dsPath: odbcPath + 'verdnatura',
|
||||
regPath: 'HKCU\\SOFTWARE\\Verdnatura\\vn-access',
|
||||
remoteUrl: 'https://cdn.verdnatura.es/vn-access',
|
||||
defaultModule: 'vn',
|
||||
defaultLocale: 'es',
|
||||
defaultBranch: 'master',
|
||||
defaultDatasource: 'production',
|
||||
defaultRemoteURL: 'https://salix.verdnatura.es',
|
||||
dbName: 'vn2008',
|
||||
maxCorruptSize: 600,
|
||||
odbcDriver: 'MySQL ODBC 8.0 Unicode Driver',
|
||||
driverPath: '\\MySQL\\Connector ODBC 8.0\\myodbc8w.dll',
|
||||
defaultDatasource: 'production',
|
||||
version: 4,
|
||||
cdnURL: 'https://cdn.verdnatura.es/vn-access',
|
||||
datasources: {
|
||||
|
@ -26,8 +26,8 @@ var Conf = {
|
|||
test: {
|
||||
DESCRIPTION: 'test',
|
||||
SERVER: 'test-db.verdnatura.es',
|
||||
PORT: '3307',
|
||||
SSLMODE: 'DISABLED'
|
||||
PORT: '3306',
|
||||
SSLMODE: 'VERIFY_IDENTITY'
|
||||
},
|
||||
dev: {
|
||||
DESCRIPTION: 'dev',
|
||||
|
@ -78,7 +78,7 @@ var App = {
|
|||
init: function() {
|
||||
// Specify the size of window
|
||||
var width = 420;
|
||||
var height = 550;
|
||||
var height = 570;
|
||||
window.resizeTo(width, height);
|
||||
window.moveTo((screen.width - width) / 2, (screen.height - height) / 2);
|
||||
},
|
||||
|
@ -104,7 +104,6 @@ var App = {
|
|||
this.certFile = this.appDir +'\\cacert.pem';
|
||||
|
||||
// Creates the necessary registry entries
|
||||
|
||||
var myVersion = this.regRead(Conf.regPath, 'version');
|
||||
|
||||
if (!myVersion || myVersion != Conf.version) {
|
||||
|
@ -180,14 +179,19 @@ var App = {
|
|||
|
||||
createODBC: function(path, dsName, driverName) {
|
||||
var driverPath = this.getEnv('ProgramFiles') + Conf.driverPath;
|
||||
var datasource = Conf.defaultDatasource;
|
||||
var serverURL, sslMode;
|
||||
|
||||
serverURL = Conf.datasources[datasource].SERVER
|
||||
sslMode = Conf.datasources[datasource].SSLMODE
|
||||
|
||||
var params = {
|
||||
Driver: driverPath,
|
||||
DESCRIPTION: Conf.appName,
|
||||
SERVER: Conf.datasources.production.SERVER,
|
||||
SERVER: serverURL,
|
||||
DATABASE: Conf.dbName,
|
||||
SSLCA: this.certFile,
|
||||
SSLMODE: 'VERIFY_IDENTITY',
|
||||
SSLMODE: sslMode,
|
||||
SSLCIPHER: 'AES256-SHA',
|
||||
AUTO_RECONNECT: 1,
|
||||
NO_PROMPT: 1,
|
||||
|
@ -199,20 +203,16 @@ var App = {
|
|||
},
|
||||
|
||||
updateODBC: function(path, dsName, datasource) {
|
||||
var params;
|
||||
var params = Conf.datasources[datasource]
|
||||
|
||||
// ToDo → Transform variable to property
|
||||
if (datasource == 'production')
|
||||
params = Conf.datasources.production
|
||||
var datasourcePath = this.getDatasourcePath();
|
||||
var myUID = this.regRead(datasourcePath, 'UID');
|
||||
var myPWD = this.regRead(datasourcePath, 'PWD');
|
||||
|
||||
if (datasource == 'test')
|
||||
params = Conf.datasources.test
|
||||
|
||||
if (datasource == 'dev')
|
||||
params = Conf.datasources.dev
|
||||
|
||||
if (datasource == 'localhost')
|
||||
params = Conf.datasources.localhost
|
||||
if (myUID && myPWD) {
|
||||
params['UID'] = myUID;
|
||||
params['PWD'] = myPWD;
|
||||
}
|
||||
|
||||
this.regWrites(path + dsName, params);
|
||||
},
|
||||
|
@ -288,8 +288,7 @@ var App = {
|
|||
);
|
||||
}
|
||||
|
||||
var curDatasource = this.regRead(Conf.regPath, 'currentDatasource');
|
||||
var datasourcePath = Conf.regPath + '\\datasources\\' + curDatasource;
|
||||
var datasourcePath = this.getDatasourcePath();
|
||||
var myUID = this.regRead(datasourcePath, 'UID');
|
||||
var myPWD = this.regRead(datasourcePath, 'PWD');
|
||||
|
||||
|
@ -356,7 +355,10 @@ var App = {
|
|||
this.regWrite(Conf.dsPath, 'UID', user);
|
||||
this.regWrite(Conf.dsPath, 'PWD', password);
|
||||
|
||||
// Check the cretentials and return the last version number
|
||||
var version = this.fetchVersion();
|
||||
|
||||
// Check if there is a new version, and if there is, download it
|
||||
if (version !== null) {
|
||||
this.disableUi(true, _('Updating'));
|
||||
var remoteFile = version
|
||||
|
@ -383,37 +385,27 @@ var App = {
|
|||
* fetch 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');
|
||||
var datasource = this.getDatasource();
|
||||
var serverURL;
|
||||
var sslMode;
|
||||
var user = this.$('user').value;
|
||||
var password = this.$('password').value;
|
||||
var serverURL = Conf.datasources[datasource].SERVER;
|
||||
var sslMode = Conf.datasources[datasource].SSLMODE;
|
||||
|
||||
// ToDo → Transform variable to property
|
||||
if (datasource == 'production')
|
||||
// ToDo: Can't login to dev-db
|
||||
if (datasource == 'dev') {
|
||||
serverURL = Conf.datasources.production.SERVER
|
||||
sslMode = Conf.datasources.production.SSLMODE
|
||||
|
||||
if (datasource == 'test')
|
||||
serverURL = Conf.datasources.test.SERVER
|
||||
sslMode = Conf.datasources.test.SSLMODE
|
||||
|
||||
if (datasource == 'dev')
|
||||
serverURL = Conf.datasources.dev.SERVER
|
||||
sslMode = Conf.datasources.dev.SSLMODE
|
||||
|
||||
if (datasource == 'localhost')
|
||||
serverURL = Conf.datasources.localhost.SERVER
|
||||
sslMode = Conf.datasources.localhost.SSLMODE
|
||||
sslMode = Conf.datasources.production.SERVER
|
||||
};
|
||||
|
||||
// Check credentials
|
||||
try {
|
||||
mysqlConn.open(this.getODBCString({
|
||||
Driver: '{'+ Conf.odbcDriver +'}',
|
||||
SERVER: serverURL,
|
||||
DATABASE: Conf.dbName,
|
||||
UID: this.$('user').value,
|
||||
PWD: this.$('password').value,
|
||||
UID: user,
|
||||
PWD: password,
|
||||
SSLCA: this.certFile,
|
||||
SSLMODE: sslMode,
|
||||
SSLCIPHER: 'AES256-SHA',
|
||||
|
@ -424,7 +416,6 @@ var App = {
|
|||
|
||||
if (dbErrors && dbErrors.count > 0) {
|
||||
var dbError = dbErrors.item(0);
|
||||
|
||||
switch (dbError.NativeError) {
|
||||
case 1045: // Access denied
|
||||
clearPassword = true;
|
||||
|
@ -437,41 +428,48 @@ var App = {
|
|||
default:
|
||||
err = new Error(dbError.description);
|
||||
}
|
||||
|
||||
dbErrors.clear();
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
||||
var sql = "SELECT version " +
|
||||
" FROM vn.mdbVersion " +
|
||||
" WHERE app = '"+ this.module +"' " +
|
||||
" AND branchFk = '" + this.$('branch').value + "'";
|
||||
var rs = mysqlConn.execute(sql);
|
||||
var version = rs.EOF ? false : parseInt(rs.fields(0).value);
|
||||
|
||||
rs.close();
|
||||
mysqlConn.close();
|
||||
|
||||
// Request to obtain the token and lastest version of this module
|
||||
try {
|
||||
var request = new ActiveXObject("MSXML2.ServerXMLHTTP");
|
||||
urlLoginMethod = this.getRemoteURL() + '/api/Accounts/login'
|
||||
request.open('POST', urlLoginMethod, false);
|
||||
request.setRequestHeader('Content-Type', 'application/json')
|
||||
var params = JSON.stringify({
|
||||
"user": user,
|
||||
"password": password
|
||||
});
|
||||
request.onreadystatechange = function() {
|
||||
App.onRequestTokenReady(request);
|
||||
}
|
||||
request.send(params);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
};
|
||||
|
||||
// Checks if it's already open
|
||||
if (this.fso.fileExists(this.lockFile))
|
||||
try {
|
||||
this.fso.deleteFile(this.lockFile);
|
||||
return version;
|
||||
return this.lastVersion;
|
||||
} catch (e) {
|
||||
throw new Error(_('Application it\'s already open'));
|
||||
}
|
||||
|
||||
// Checks if MDB exists
|
||||
if (!this.fso.fileExists(this.mdbFile))
|
||||
return version;
|
||||
return this.lastVersion;
|
||||
|
||||
// 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;
|
||||
return this.lastVersion;
|
||||
|
||||
// Obtains the local version number from the MDB file
|
||||
var localVersion = this.mdbGetValue(
|
||||
|
@ -482,8 +480,8 @@ var App = {
|
|||
localVersion = false;
|
||||
|
||||
// Determines if should download
|
||||
return !localVersion || version === false || localVersion != version
|
||||
? version
|
||||
return !localVersion || this.lastVersion === false || localVersion != this.lastVersion
|
||||
? this.lastVersion
|
||||
: null;
|
||||
},
|
||||
mdbGetValue: function(query, field, parseFn) {
|
||||
|
@ -553,6 +551,50 @@ var App = {
|
|||
this.catchError(e);
|
||||
}
|
||||
},
|
||||
onRequestTokenReady: function(request) {
|
||||
if (request.readyState !== 4)
|
||||
return;
|
||||
|
||||
try {
|
||||
if (request.status !== 200 )
|
||||
throw new Error('HTTP: '+ request.statusText + ' ' + request.status);
|
||||
|
||||
this.response = JSON.parse(request.responseText)
|
||||
|
||||
if (this.response) {
|
||||
var request = new ActiveXObject("MSXML2.ServerXMLHTTP");
|
||||
var filter = '&filter=' + JSON.stringify({
|
||||
"fields":["version"],
|
||||
"where":{
|
||||
"app": this.module,
|
||||
"branchFk": this.$('branch').value
|
||||
},
|
||||
});
|
||||
var urlVersionMethod = this.getRemoteURL() + '/api/MdbVersions?access_token=' + this.response.token + filter
|
||||
request.open('GET', urlVersionMethod, false);
|
||||
request.onreadystatechange = function() {
|
||||
App.onRequestVersionReady(request);
|
||||
};
|
||||
request.send();
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
onRequestVersionReady: function(request) {
|
||||
if (request.readyState !== 4)
|
||||
return;
|
||||
|
||||
try {
|
||||
if (request.status !== 200 )
|
||||
throw new Error('HTTP: '+ request.statusText + ' .' + request.status + '.');
|
||||
|
||||
var response = JSON.parse(request.responseText);
|
||||
this.lastVersion = response[0].version
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
},
|
||||
openMdb: function() {
|
||||
var notSignOut = !!this.$('notSignOut').checked;
|
||||
this.regWrite(Conf.regPath, 'notSignOut', notSignOut);
|
||||
|
@ -678,6 +720,11 @@ var App = {
|
|||
);
|
||||
return branch || Conf.defaultBranch;
|
||||
},
|
||||
/**
|
||||
* Get the current datasource used
|
||||
*
|
||||
* @return {string} Datasource name
|
||||
*/
|
||||
getDatasource: function() {
|
||||
var datasource = this.regRead(
|
||||
'HKCU\\SOFTWARE\\Verdnatura\\vn-access',
|
||||
|
@ -685,9 +732,39 @@ var App = {
|
|||
);
|
||||
return datasource || Conf.defaultDatasource;
|
||||
},
|
||||
/**
|
||||
* Get the remote URL
|
||||
*
|
||||
* @return {string} Remote server
|
||||
*/
|
||||
getRemoteURL: function() {
|
||||
var datasource = this.regRead(
|
||||
'HKCU\\SOFTWARE\\Verdnatura\\vn-access',
|
||||
'remoteURL'
|
||||
);
|
||||
return datasource || Conf.defaultRemoteURL;
|
||||
},
|
||||
/**
|
||||
* Get the current datasource path
|
||||
*
|
||||
* @return {string} Datasource path
|
||||
*/
|
||||
getDatasourcePath: function() {
|
||||
var datasourcePath = Conf.regPath + '\\datasources\\' + this.getDatasource();
|
||||
|
||||
return datasourcePath;
|
||||
},
|
||||
/**
|
||||
* Update the datasource
|
||||
*
|
||||
* @param {String} datasource The datasource
|
||||
*/
|
||||
updateDatasource: function(datasource) {
|
||||
this.regWrite(Conf.regPath, 'currentDatasource', datasource);
|
||||
},
|
||||
/**
|
||||
* Create all datasources structure
|
||||
*/
|
||||
createDatasources: function() {
|
||||
var params = {
|
||||
'UID': null,
|
||||
|
|
|
@ -103,7 +103,7 @@ button:hover {
|
|||
/* Branch */
|
||||
|
||||
#branchOptions {
|
||||
margin-top: 1em;
|
||||
margin-top: 2em;
|
||||
}
|
||||
#branchLogo {
|
||||
cursor: pointer;
|
||||
|
@ -156,7 +156,7 @@ select.dev {
|
|||
/* Datasource */
|
||||
|
||||
#datasourceOptions {
|
||||
margin-top: 1em;
|
||||
margin-top: 2em;
|
||||
margin-left: 17.3em;
|
||||
}
|
||||
#datasourceLogo {
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
<button id="enter" onclick="App.onEnterClick()">Iniciar sesión</button>
|
||||
</div>
|
||||
<div id="cleanDiv">
|
||||
<a id="clean" href="#" onclick="App.onCleanCacheClick()">Limpiar caché </a>
|
||||
<a id="clean" href="#" onclick="App.onCleanCacheClick()">Limpiar caché</a>
|
||||
</div>
|
||||
<div id="branchOptions">
|
||||
<div id="branchButton">
|
||||
|
|
Loading…
Reference in New Issue