karusic/front/src/common/utils/dataInterface.ts

331 lines
8.8 KiB
TypeScript

/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license PROPRIETARY (see license file)
*/
import { NodeData } from '../model';
import { isArray, isNullOrUndefined } from './validator';
export enum TypeCheck {
CONTAINS = 'C',
EQUAL = '==',
NOT_EQUAL = '!=',
LESS = '<',
LESS_EQUAL = '<=',
GREATER = '>',
GREATER_EQUAL = '>=',
}
export interface SelectModel {
check: TypeCheck;
key: string;
value?: number | string | boolean | number[] | string[];
}
/*
{ check: TypeCheck.EQUAL, key: sss, value: null}
*/
/**
* @breif Generic interface to access to the BDD (no BDD, direct file IO)
*/
export class DataInterface {
static extractLimitOne(data: NodeData[], key: string): any[] {
const out = [];
for (let iii = 0; iii < data.length; iii++) {
const value = data[iii][key];
if (DataInterface.existIn(value, out) === false) {
out.push(value);
}
}
return out;
}
static extractLimitOneList(data: NodeData[], key: string): any[] {
const out = [];
for (let iii = 0; iii < data.length; iii++) {
const value = data[iii][key];
for (let jjj = 0; jjj < value.length; jjj++)
if (DataInterface.existIn(value[jjj], out) === false) {
out.push(value[jjj]);
}
}
return out;
}
constructor(private name: string, private bdd: NodeData[]) {}
public getTableIndex(id: number) {
for (let iii = 0; iii < this.bdd.length; iii++) {
if (this.bdd[iii].id === id) {
return iii;
}
}
return undefined;
}
public gets(): NodeData[] {
console.log(`[I] gets ${this.name}`);
return this.bdd;
}
public getsWhere(select: SelectModel[], orderBy?: string[]): NodeData[] {
// console.log("[I] gets_where " + this.name + " select " + _select);
let tmpList = this.getSubList(this.bdd, select);
tmpList = this.orderBy(tmpList, orderBy);
return tmpList;
}
public get(id: number): NodeData {
// console.log("[I] get " + this.name + " " + _id);
for (let iii = 0; iii < this.bdd.length; iii++) {
if (this.bdd[iii].id === id) {
return this.bdd[iii];
}
}
console.log(`[W] not found element{ ${this.bdd.length}`);
return undefined;
}
public getNameLikeAll(name: string): NodeData[] {
let out = undefined;
let nameLower = name.toLowerCase();
for (let iii = 0; iii < this.bdd.length; iii++) {
if (this.bdd[iii].name.toLowerCase() === nameLower) {
out.push(this.bdd[iii]);
}
}
if (out.length === 0) {
return undefined;
}
return out;
}
public getNameLike(name: string): NodeData[] {
let out = [];
let nameLower = name.toLowerCase();
for (let iii = 0; iii < this.bdd.length; iii++) {
// console.log("compare '" + _name + "' ??? '" + this.bdd[iii]['name'] + "'");
if (this.bdd[iii].name.toLowerCase() === nameLower) {
out.push(this.bdd[iii]);
}
}
return out;
}
public set(id: number, value: NodeData): void {
console.log(`[I] Set ${this.name} ${id}`);
for (let iii = 0; iii < this.bdd.length; iii++) {
console.log(` check: ${this.bdd[iii].id}`);
if (this.bdd[iii].id === id) {
console.log(` *** Set specific values: ${id} ${JSON.stringify(value, null, 2)}`);
this.bdd[iii] = value;
}
}
}
public add(value: NodeData): void {
console.log(`[I] Set ${this.name}`);
this.bdd.push(value);
}
public delete(id: number): void {
console.log(`[I] delete ${this.name} ${id}`);
for (let iii = 0; iii < this.bdd.length; iii++) {
if (this.bdd[iii].id === id) {
this.bdd.splice(iii, 1);
}
}
}
public find(listToken, values): NodeData[] {
let out = [];
for (let iii = 0; iii < this.bdd.length; iii++) {
let find = true;
for (let jjj = 0; jjj < listToken.length; jjj++) {
if (this.bdd[iii][listToken[jjj]] !== values[listToken[jjj]]) {
find = false;
break;
}
}
if (find === true) {
out.push(this.bdd[iii]);
}
}
return out;
}
public count(select?: SelectModel[]) {
if (isNullOrUndefined(select)) {
return this.bdd.length;
}
let tmp = this.getSubList(this.bdd, select);
return tmp.length;
}
public static existIn(value, listValues: number | string | boolean | number[] | string[]): boolean {
if (isArray(listValues)) {
for (let iii = 0; iii < listValues.length; iii++) {
if (value === listValues[iii]) {
return true;
}
}
return false;
}
return value === listValues;
}
public static containsOneIn(
value: number | string | boolean | number[] | string[],
listValues: number | string | boolean | number[] | string[]
): boolean {
if (isArray(value)) {
for (let iii = 0; iii < value.length; iii++) {
if (this.existIn(value[iii], listValues) === true) {
return true;
}
}
return false;
}
return this.existIn(value, listValues);
}
public getSubList(values: NodeData[], select?: SelectModel[]): NodeData[] {
let out = [] as NodeData[];
for (let iiiElem = 0; iiiElem < values.length; iiiElem++) {
let find = true;
if (select.length === 0) {
find = false;
}
//console.log("Check : " + JSON.stringify(values[iiiElem], null, 2));
for (let iiiSelect = 0; iiiSelect < select.length; iiiSelect++) {
let control = select[iiiSelect];
let valueElement = values[iiiElem][control.key];
//console.log(" valueElement : " + JSON.stringify(valueElement, null, 2));
if (isArray(control.value)) {
if (control.check === TypeCheck.CONTAINS) {
//console.log("check contains ... " + valueElement + " contains one element in " + control.value);
if (DataInterface.containsOneIn(valueElement, control.value) === false) {
find = false;
break;
}
} else if (control.check === TypeCheck.EQUAL) {
//console.log("check Equals ... " + valueElement + " === " + control.value);
if (DataInterface.existIn(valueElement, control.value) === false) {
find = false;
break;
}
} else if (control.check === TypeCheck.NOT_EQUAL) {
if (DataInterface.existIn(valueElement, control.value) === false) {
find = true;
break;
}
} else {
console.log('[ERROR] Internal Server Error{ unknow comparing type ...');
return undefined;
}
} else {
//console.log(" [" + control.key + "] = " + valueElement);
if (control.check === TypeCheck.CONTAINS) {
//console.log("check Equals ... " + valueElement + " === " + control.value + " #2");
if (DataInterface.containsOneIn(valueElement, control.value) === false) {
find = false;
break;
}
} else if (control.check === TypeCheck.EQUAL) {
if (valueElement !== control.value) {
find = false;
break;
}
} else if (control.check === TypeCheck.NOT_EQUAL) {
if (valueElement === control.value) {
find = false;
break;
}
} else if (control.check === TypeCheck.LESS) {
if (valueElement >= control.value) {
find = false;
break;
}
} else if (control.check === TypeCheck.LESS_EQUAL) {
if (valueElement > control.value) {
find = false;
break;
}
} else if (control.check === TypeCheck.GREATER) {
if (valueElement <= control.value) {
find = false;
break;
}
} else if (control.check === TypeCheck.GREATER_EQUAL) {
if (valueElement < control.value) {
find = false;
break;
}
} else {
console.log('[ERROR] Internal Server Error{ unknow comparing type ...');
return undefined;
}
}
}
if (find === true) {
// console.log(" ==> SELECTED");
out.push(values[iiiElem]);
} else {
// console.log(" ==> NOT SELECTED");
}
}
return out;
}
public orderBy(values: NodeData[], order: string[]): NodeData[] {
if (isNullOrUndefined(order)) {
return values;
}
if (order.length === 0) {
return values;
}
let valueOrder = order[0];
let out = [];
let outUnclassable = [];
for (let iii = 0; iii < values.length; iii++) {
if (values[iii][valueOrder] === undefined) {
outUnclassable.push(values[iii]);
continue;
}
if (values[iii][valueOrder] === null) {
outUnclassable.push(values[iii]);
continue;
}
out.push(values[iii]);
}
// console.log("order in list by : " + value_order);
// out = sorted(out, key=lambda x{ x[value_order])
if (valueOrder === 'name') {
out.sort((aaa, bbb) => {
const name1 = aaa[valueOrder].toLowerCase();
const name2 = bbb[valueOrder].toLowerCase();
if (name1 > name2) {
return 1;
}
if (name1 < name2) {
return -1;
}
return 0;
});
} else {
out.sort((aaa, bbb) => {
if (aaa[valueOrder] > bbb[valueOrder]) {
return 1;
}
if (aaa[valueOrder] < bbb[valueOrder]) {
return -1;
}
return 0;
});
}
if (order.length > 1) {
outUnclassable = this.orderBy(outUnclassable, order.slice(1));
}
for (let jjj = 0; jjj < outUnclassable.length; jjj++) {
out.push(outUnclassable[jjj]);
}
return out;
}
}