import { Injectable } from '@angular/core'; import { AppConf } from '../app-conf/app-conf'; import { DbSvc } from '../db/db'; import { RemoteService } from '../remote/remote'; import { AppUtil } from '../app-util/app-util'; @Injectable() export class AD_SysConfig { constructor(public appConf: AppConf, public db: DbSvc, public remoteService: RemoteService, public appUtil: AppUtil) { } public list = []; public tableName = "ad_sysconfig"; public idColumn = "ad_sysconfig_uu"; public nameColumn = "name"; public init() { let promise = new Promise((resolve, reject) => { this.db.query("Select * from " + this.tableName).then((result) => { this.list = this.db.fetchAll(result); if (this.list.length < 1) { this.addArray(this.appConf.getDefaultSysConf()); } else { this.appConf.setDefaultSysCong(this.list); } resolve(this.list); }, (reject) => { reject(reject); }); }); return promise; } public all() { return this.list; } public filterByConfigType() { let j = 0; let filterSysConfigList = []; if (this.appConf.isLogin()) { for (let i = 0; i < this.list.length; i++) { if (this.list[i].configtype === "AU") { filterSysConfigList[j++] = this.list[i]; } else { continue; } } return filterSysConfigList; } else { for (let i = 0; i < this.list.length; i++) { if (this.list[i].configtype === "AC") { filterSysConfigList[j++] = this.list[i]; } else { continue; } } return filterSysConfigList; } } public remove(obj) { let sql = "Delete from " + this.tableName + " WHERE " + this.idColumn + " = ? "; this.db.query(sql, [obj[this.idColumn]]).then((result) => { this.list.splice(this.list.indexOf(obj), 1); }); } public get(uuid) { for (let i = 0; i < this.list.length; i++) { if (this.list[i][this.idColumn] == uuid) { return this.list[i]; } } return null; } public getByName(name) { for (let i = 0; i < this.list.length; i++) { if (this.list[i][this.nameColumn] == name) { return this.list[i]['value']; } } return null; } public add(row) { var promise = new Promise((resolve, reject) => { this.db.addTableRow(this.tableName, row, true).then((result) => { //because of updation as default commit 21 pass true as demo or avoid error this.list.push(row); resolve(result); }, (res) => { reject(res); }); }) return promise; } public update(row) { var promise = new Promise((resolve, reject) => { let obj = row if (obj) { this.db.updateTableRow(this.tableName, obj).then((result) => { for (let i = 0; i < this.list.length; i++) { if (this.list[i][this.idColumn] == obj[this.idColumn]) { this.list[i] = obj; break; } } resolve(result); }, (res) => { reject(res); }); } }); return promise; } public addArray(objectList) { var promise = new Promise((resolve, reject) => { let loopPromises = []; objectList.forEach(object => { var prom = new Promise((resolve, reject) => { if (this.get(object[this.idColumn]) == null) { this.add(object).then((res) => { resolve(); }, (res) => { reject(); }); } else { delete object.description; this.update(object).then((res) => { resolve(); }, (res) => { reject(); }); } }); loopPromises.push(prom); }); Promise.all(loopPromises).then((res) => { resolve(res); }, (res) => { resolve(res); }); }); return promise; }; /* * Check for this method is Applicable or Removed. */ public inActiveInstance() { var promise = new Promise((resolve, reject) => { let actualDat = { IMEI: this.appConf.getIMEI(), AppName: this.appConf.getAppName() }; actualDat.IMEI = this.appConf.getIMEI(); actualDat.AppName = this.appConf.getAppName(); let data = { "ModelRunProcessRequest": { "ModelRunProcess": { "serviceType": "InActiveAppInstance", "ParamValues": { "field": [{ "@column": "IMEI", "val": this.appUtil.encodeJsonTOBase64(actualDat.IMEI) }, { "@column": "AppName", "val": this.appUtil.encodeJsonTOBase64(actualDat.AppName) }] } } } }; let request = { method: "POST", actionType: "/run_process" }; this.remoteService.invokeRemoteAction(request, data).then((response) => { let error = response["@IsError"]; let res = { Error: '' }; if (error == "true") { res.Error = response['Error']; } else { let jObject = this.appUtil.decodeBase64ToJson(response['Summary']); if (jObject.Error) { res.Error = jObject.Error; } else { if (jObject.Data.length > 0) { } else { resolve(res); } } } if (res.Error) { reject(res); } }, (err) => { this.appUtil.alert("Failure : Could not Inactive Instance " + err, "Alert"); }); }); return promise; }; }