You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
391 lines
13 KiB
391 lines
13 KiB
6 months ago
|
import { ref, computed } from 'vue';
|
||
|
import { axios, Environment, $t, Tools, EnumTools, Options } from 'platform-core';
|
||
|
import { Processor } from '../Processor';
|
||
|
import { PlaceHolder } from '@/utils/PlaceHolder';
|
||
|
|
||
|
class HttpRequest extends Processor {
|
||
|
#editorDialogWidth: number = 1280;
|
||
|
#HttpMethodTypeEnum: any;
|
||
|
#httpMethodTypeOptionsRef = ref(<any>[]);
|
||
|
#parameterOptionsRef = ref(<any>[]);
|
||
|
#sqlQueryResultFieldsRef = ref(<any>[]);
|
||
|
#editorForm: any;
|
||
|
|
||
|
constructor(targetType: string, context?: any) {
|
||
|
super(targetType, context);
|
||
|
EnumTools.fetch(['io.sc.engine.rule.core.enums.HttpMethodType']).then((value) => {
|
||
|
this.#HttpMethodTypeEnum = value.HttpMethodType;
|
||
|
this.#httpMethodTypeOptionsRef = Options.enum(this.#HttpMethodTypeEnum);
|
||
|
});
|
||
|
if (this.targetType === Processor.PARAMETER) {
|
||
|
axios.get(Environment.apiContextPath('/api/re/model/parameter/listParemtersByParameterId/' + this.context.target.id)).then((response) => {
|
||
|
this.#parameterOptionsRef.value = Tools.objects2Options(response.data, 'name', 'code');
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public getToolbarAction(): any {
|
||
|
return {
|
||
|
extend: 'add',
|
||
|
name: 'http',
|
||
|
label: $t('io.sc.engine.rule.core.enums.ProcessorType.HTTP_REQUEST'),
|
||
|
icon: 'sync_alt',
|
||
|
enableIf: (args: any) => {
|
||
|
const type = this.context.target.type;
|
||
|
return type !== 'RULE_RESULT' && type !== 'SINGLE_RULE_RESULT';
|
||
|
},
|
||
|
afterClick: (args: any) => {
|
||
|
args.grid.getEditorForm().setFieldValue('type', 'HTTP_REQUEST');
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
public format(row: any): any {
|
||
|
return {
|
||
|
componentType: 'w-code-mirror',
|
||
|
attrs: {
|
||
|
lang: 'sql',
|
||
|
rows: 4,
|
||
|
editable: false,
|
||
|
modelValue: row.sql,
|
||
|
placeholder: true,
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
public getEditorFields(): any {
|
||
|
return [
|
||
|
{
|
||
|
colSpan: 2,
|
||
|
name: 'httpMethod',
|
||
|
label: $t('re.resources.designer.processor.grid.entity.httpMethod'),
|
||
|
type: 'w-select',
|
||
|
clearable: true,
|
||
|
options: this.#httpMethodTypeOptionsRef,
|
||
|
rows: 1,
|
||
|
showIf: (args: any) => {
|
||
|
return 'HTTP_REQUEST' === args.form.getFieldValue('type');
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
colSpan: 5,
|
||
|
name: 'sql',
|
||
|
label: $t('re.resources.designer.processor.grid.entity.httpUrl'),
|
||
|
type: 'w-text',
|
||
|
showIf: (args: any) => {
|
||
|
return 'HTTP_REQUEST' === args.form.getFieldValue('type');
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
colSpan: 3,
|
||
|
name: 'sqlParameterValues',
|
||
|
label: $t('re.resources.designer.processor.grid.entity.sqlParameterValues'),
|
||
|
showIf: (args: any) => {
|
||
|
return 'SQL' === args.form.getFieldValue('type');
|
||
|
},
|
||
|
type: 'w-grid',
|
||
|
height: 150,
|
||
|
title: $t('re.resources.designer.processor.grid.entity.sqlParameterValues'),
|
||
|
autoFetchData: false,
|
||
|
dense: true,
|
||
|
dbClickOperation: 'edit',
|
||
|
dndMode: 'local',
|
||
|
pageable: false,
|
||
|
configButton: false,
|
||
|
toolbarConfigure: { noIcon: false },
|
||
|
toolbarActions: [
|
||
|
{
|
||
|
name: 'analyze',
|
||
|
label: $t('analyze'),
|
||
|
icon: 'bi-tag',
|
||
|
click: (args: any) => {
|
||
|
const sql = args.grid.getEditorForm().getFieldValue('sql');
|
||
|
const regex = /\$\{[\u0000-\uFFFF]+?\}/g;
|
||
|
const array: any[] = sql.match(regex);
|
||
|
const rows: any[] = [];
|
||
|
array.forEach((item) => {
|
||
|
rows.push({ name: item, value: '' });
|
||
|
});
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlParameterValues');
|
||
|
grid.setLocalData(rows);
|
||
|
},
|
||
|
},
|
||
|
'separator',
|
||
|
'add',
|
||
|
'edit',
|
||
|
[
|
||
|
{
|
||
|
extend: 'remove',
|
||
|
click: (args: any) => {
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlParameterValues');
|
||
|
grid.removeRows(args.selecteds);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
extend: 'remove',
|
||
|
name: 'removeAll',
|
||
|
label: $t('deleteAll'),
|
||
|
click: (args: any) => {
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlParameterValues');
|
||
|
grid.setLocalData([]);
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
],
|
||
|
columns: [
|
||
|
{
|
||
|
width: '50%',
|
||
|
name: 'name',
|
||
|
label: $t('name'),
|
||
|
align: 'left',
|
||
|
sortable: false,
|
||
|
format: (value: any) => {
|
||
|
return PlaceHolder.replace(value);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
width: '100%',
|
||
|
name: 'value',
|
||
|
label: $t('value'),
|
||
|
sortable: false,
|
||
|
},
|
||
|
],
|
||
|
editor: {
|
||
|
dialog: {
|
||
|
width: '600px',
|
||
|
},
|
||
|
form: {
|
||
|
colsNum: 1,
|
||
|
fields: [
|
||
|
{
|
||
|
name: 'name',
|
||
|
label: $t('name'),
|
||
|
type: 'w-code-mirror',
|
||
|
toolbar: false,
|
||
|
lang: 'java',
|
||
|
rows: 1,
|
||
|
placeholder: true,
|
||
|
autoCompletion: this.autoCompletionManager.autoCompletion(),
|
||
|
},
|
||
|
{
|
||
|
name: 'value',
|
||
|
label: $t('value'),
|
||
|
type: 'w-text',
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
},
|
||
|
onBeforeEditorDataSubmit: (args: any) => {
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlParameterValues');
|
||
|
const form = grid.getEditorForm();
|
||
|
if ('add' == form.getStatus() || 'clone' == form.getStatus()) {
|
||
|
grid.addLocalData(args.data);
|
||
|
args.callback(false);
|
||
|
} else if ('edit' == form.getStatus()) {
|
||
|
grid.updateLocalData(args.data);
|
||
|
args.callback(false);
|
||
|
}
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
colSpan: 4,
|
||
|
name: 'sqlQueryResult',
|
||
|
label: $t('re.resources.designer.processor.grid.entity.sqlQueryResult'),
|
||
|
showIf: (args: any) => {
|
||
|
return 'SQL' === args.form.getFieldValue('type');
|
||
|
},
|
||
|
type: 'w-grid',
|
||
|
height: 250,
|
||
|
autoFetchData: false,
|
||
|
title: $t('re.resources.designer.processor.grid.entity.sqlQueryResult'),
|
||
|
dense: true,
|
||
|
dndMode: 'local',
|
||
|
pageable: false,
|
||
|
checkboxSelection: false,
|
||
|
configButton: false,
|
||
|
toolbarConfigure: { noIcon: false },
|
||
|
toolbarActions: [
|
||
|
{
|
||
|
name: 'execute',
|
||
|
label: $t('execute'),
|
||
|
icon: 'bi-caret-right',
|
||
|
click: (args: any) => {
|
||
|
const sql = this.#editorForm.getFieldValue('sql');
|
||
|
const grid = this.#editorForm.getFieldComponent('sqlParameterValues');
|
||
|
const sqlParameterValues = grid?.getRows();
|
||
|
axios
|
||
|
.post(Environment.apiContextPath('/api/re/model/parameter/processor/executeSql'), {
|
||
|
sql: sql,
|
||
|
sqlParameterValues: sqlParameterValues,
|
||
|
})
|
||
|
.then((response) => {
|
||
|
const fieldMetaDatas: any[] = response.data.fieldMetaDatas;
|
||
|
fieldMetaDatas.forEach((field) => {
|
||
|
field.value = field.name;
|
||
|
field.label = field.name;
|
||
|
});
|
||
|
const data = response.data.data;
|
||
|
const grid = this.#editorForm.getFieldComponent('sqlQueryResult');
|
||
|
this.#sqlQueryResultFieldsRef.value = fieldMetaDatas;
|
||
|
grid.setLocalData(data);
|
||
|
});
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
columns: computed(() => {
|
||
|
return this.#sqlQueryResultFieldsRef.value;
|
||
|
}),
|
||
|
},
|
||
|
{
|
||
|
colSpan: 3,
|
||
|
name: 'sqlFieldMapping',
|
||
|
label: $t('re.resources.designer.processor.grid.entity.sqlFieldMapping'),
|
||
|
showIf: (args: any) => {
|
||
|
return 'SQL' === args.form.getFieldValue('type');
|
||
|
},
|
||
|
type: 'w-grid',
|
||
|
height: 250,
|
||
|
width: '100%',
|
||
|
autoFetchData: false,
|
||
|
title: $t('re.resources.designer.processor.grid.entity.sqlFieldMapping'),
|
||
|
dense: true,
|
||
|
dbClickOperation: 'edit',
|
||
|
dndMode: 'local',
|
||
|
pageable: false,
|
||
|
configButton: false,
|
||
|
toolbarConfigure: { noIcon: false },
|
||
|
toolbarActions: [
|
||
|
'add',
|
||
|
'edit',
|
||
|
[
|
||
|
{
|
||
|
extend: 'remove',
|
||
|
click: (args: any) => {
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlFieldMapping');
|
||
|
grid.removeLocalData(args.selecteds);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
extend: 'remove',
|
||
|
name: 'removeAll',
|
||
|
label: $t('deleteAll'),
|
||
|
click: (args: any) => {
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlFieldMapping');
|
||
|
grid.setLocalData([]);
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
],
|
||
|
columns: [
|
||
|
{
|
||
|
width: '50%',
|
||
|
name: 'parameter',
|
||
|
label: $t('parameterName'),
|
||
|
sortable: false,
|
||
|
format: (value: any) => {
|
||
|
return PlaceHolder.replace(value);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
width: '50%',
|
||
|
name: 'field',
|
||
|
label: $t('fieldName'),
|
||
|
align: 'left',
|
||
|
sortable: false,
|
||
|
},
|
||
|
],
|
||
|
editor: {
|
||
|
dialog: {
|
||
|
width: '600px',
|
||
|
},
|
||
|
form: {
|
||
|
colsNum: 1,
|
||
|
fields: [
|
||
|
{
|
||
|
name: 'parameter',
|
||
|
label: $t('parameterName'),
|
||
|
type: 'w-select',
|
||
|
options: this.#parameterOptionsRef.value,
|
||
|
},
|
||
|
{
|
||
|
name: 'field',
|
||
|
label: $t('fieldName'),
|
||
|
type: 'w-select',
|
||
|
options: this.#sqlQueryResultFieldsRef.value,
|
||
|
},
|
||
|
],
|
||
|
},
|
||
|
},
|
||
|
onBeforeEditorDataSubmit: (args: any) => {
|
||
|
const grid = args.grid.getEditorForm().getFieldComponent('sqlFieldMapping');
|
||
|
const form = grid.getEditorForm();
|
||
|
if ('add' == form.getStatus() || 'clone' == form.getStatus()) {
|
||
|
grid.addLocalData(args.data);
|
||
|
args.callback(false);
|
||
|
} else if ('edit' == form.getStatus()) {
|
||
|
grid.updateLocalData(args.data);
|
||
|
args.callback(false);
|
||
|
}
|
||
|
},
|
||
|
},
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public getViewerFields(): any {
|
||
|
return [
|
||
|
{ name: 'sqlDatasourceName', label: $t('re.resources.designer.processor.grid.entity.sqlDatasourceName') },
|
||
|
{ name: 'sql', label: $t('re.resources.designer.processor.grid.entity.sql') },
|
||
|
{ name: 'sqlParameterValues', label: $t('re.resources.designer.processor.grid.entity.sqlParameterValues') },
|
||
|
{ name: 'sqlFieldMapping', label: $t('re.resources.designer.processor.grid.entity.sqlFieldMapping') },
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public beforeEditorDataSubmit(args: any): void {
|
||
|
const form = args.grid.getEditorForm();
|
||
|
const sqlParameterValuesGrid = form.getFieldComponent('sqlParameterValues');
|
||
|
const sqlParameterValuesLocalData: any[] = sqlParameterValuesGrid.getRows();
|
||
|
const sqlParameterValues: any[] = [];
|
||
|
sqlParameterValuesLocalData.forEach((item) => {
|
||
|
sqlParameterValues.push({
|
||
|
name: item.name,
|
||
|
value: item.value,
|
||
|
});
|
||
|
});
|
||
|
args.data.sqlParameterValues = Tools.object2Json(sqlParameterValues);
|
||
|
|
||
|
const sqlFieldMappingGrid = form.getFieldComponent('sqlFieldMapping');
|
||
|
const sqlFieldMappingLocalData: any[] = sqlFieldMappingGrid.getRows();
|
||
|
const sqlFieldMapping: any[] = Tools.objects2Objects(sqlFieldMappingLocalData, { field: 'field', parameter: 'parameter' });
|
||
|
args.data.sqlFieldMapping = Tools.object2Json(sqlFieldMapping);
|
||
|
}
|
||
|
|
||
|
public afterEditorOpen(args: any): void {
|
||
|
args.grid.getEditorDialog().setWidth(this.#editorDialogWidth);
|
||
|
this.initAutoCompletionManager();
|
||
|
this.initUserDefinedFunctionsManager();
|
||
|
|
||
|
this.#editorForm = args.grid.getEditorForm();
|
||
|
const form = args.grid.getEditorForm();
|
||
|
const row = args.data;
|
||
|
|
||
|
axios.get(Environment.apiContextPath('api/re/model/parameter/findParametersByParameterId?parameterId=' + this.context.target.id)).then((response) => {
|
||
|
this.#parameterOptionsRef.value = Tools.objects2Objects(response.data, {
|
||
|
label: 'name',
|
||
|
value: (parameter: any) => {
|
||
|
return '${' + parameter.name + '}';
|
||
|
},
|
||
|
});
|
||
|
});
|
||
|
|
||
|
const sqlParameterValuesGrid = form.getFieldComponent('sqlParameterValues');
|
||
|
const sqlParameterValuesRows = Tools.json2Object(row.sqlParameterValues);
|
||
|
sqlParameterValuesGrid.setLocalData(sqlParameterValuesRows);
|
||
|
|
||
|
const sqlFieldMappingGrid = form.getFieldComponent('sqlFieldMapping');
|
||
|
const sqlFieldMappingRows = Tools.json2Object(row.sqlFieldMapping);
|
||
|
sqlFieldMappingGrid.setLocalData(sqlFieldMappingRows);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export { HttpRequest };
|