@abaplint/runtime
Version:
Transpiler - Runtime
98 lines • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageStatement = void 0;
const types_1 = require("../types");
function replace(text, w) {
for (let i = 0; i < 6; i++) {
const search = "&" + (i + 1);
let replace = "";
if (w && w[i]) {
const j = w[i];
if (typeof j === "string") {
replace = j;
}
else {
// todo: some formatting missing here,
replace = (j.get() + "").trimEnd();
}
}
const field = "msgv" + (i + 1);
if (i <= 3) {
abap.builtin.sy.get()[field].set(replace);
}
text = text.replace(search, replace);
}
return text.trim();
}
async function findText(context, arbgb, msgnr, msgty) {
let text = undefined;
if (arbgb && msgnr) {
try {
// todo, sql injection?
const select = `SELECT * FROM t100 WHERE sprsl='E' AND arbgb='${arbgb}' AND msgnr='${msgnr}' LIMIT 1`;
const { rows: result } = await context.defaultDB().select({ select });
if (result[0]) {
text = result[0]["text"];
}
}
catch {
// use fallback text
}
}
if (text === undefined) {
// @ts-ignore
text = abap.MSAG[arbgb?.trimEnd().toUpperCase()]?.[msgnr];
}
if (text === undefined) {
// fallback
text = msgty + ":" + arbgb?.trim() + ":" + msgnr + " &1 &2 &3 &4";
}
return text;
}
class MessageStatement {
context;
constructor(context) {
this.context = context;
}
async message(options) {
let arbgb = options.id;
if (arbgb !== undefined && typeof arbgb !== "string") {
arbgb = arbgb.get();
}
arbgb = arbgb?.toUpperCase();
let msgty = options.type;
if (msgty !== undefined && typeof msgty !== "string") {
msgty = msgty.get();
}
msgty = msgty?.toUpperCase();
abap.builtin.sy.get().msgid.set(arbgb || "");
let msgnr = options.number;
if (msgnr !== undefined && typeof msgnr !== "string") {
msgnr = msgnr.get();
}
abap.builtin.sy.get().msgno.set(msgnr || "");
abap.builtin.sy.get().msgty.set(msgty);
let replaced = "";
if (options.exceptionOrText) {
if (options.exceptionOrText instanceof types_1.ABAPObject) {
replaced = await options.exceptionOrText.get().if_message$get_text();
}
else {
replaced = options.exceptionOrText.get();
}
}
else {
const text = await findText(this.context, arbgb, msgnr, msgty);
replaced = replace(text, options.with);
}
if (options.into) {
options.into.set(replaced);
}
else {
// hmm, add option on how/if to write messages to console? or it should be the abap.console() ?
console.log(replaced);
}
}
}
exports.MessageStatement = MessageStatement;
//# sourceMappingURL=message.js.map