All files / src/objects message_class.ts

98.03% Statements 100/102
84.14% Branches 69/82
100% Functions 10/10
98.03% Lines 100/102

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 1031x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 56x 56x 56x 56x 258x 258x 56x 56x 2x 2x 2x 56x 56x 6x 6x 6x 6x 6x 56x 56x 12x 12x 12x 56x 56x 10x 10x 10x 56x 56x 61x 61x 61x 56x 56x 46x 46x 46x 46x 56x 56x 11x 11x 11x 14x 10x 10x 14x 1x 1x 56x 56x 56x 56x 81x 42x 42x 39x 39x 39x 39x 39x 39x 39x 39x 39x 81x     39x 81x 81x 81x 81x 81x 6x 6x 81x 42x 42x 33x 81x 81x 31x 31x 81x 2x 2x 2x 56x 56x  
import {AbstractObject} from "./_abstract_object";
import {Message} from "../abap/types/message";
import {xmlToArray, unescape} from "../xml_utils";
 
export type parsedMessageClass = {
  topName: string | undefined,
  description: string | undefined,
  parsedMessages: Message[] | undefined,
  textsTranslations: {language: string, number: string, text?: string}[] | undefined,
};
 
export class MessageClass extends AbstractObject {
  private xml: parsedMessageClass | undefined = undefined;
 
  public getType(): string {
    return "MSAG";
  }
 
  public getDescription(): string | undefined {
    this.parseXML();
    return this.xml?.description;
  }
 
  public getAllowedNaming() {
    return {
      maxLength: 20,
      allowNamespace: true,
    };
  }
 
  public getParsed(): parsedMessageClass | undefined {
    this.parseXML();
    return this.xml;
  }
 
  public getTextsTranslations() {
    this.parseXML();
    return this.xml?.textsTranslations;
  }
 
  public setDirty(): void {
    this.xml = undefined;
    super.setDirty();
  }
 
  public getMessages(): readonly Message[] {
    this.parseXML();
    const msg = this.xml?.parsedMessages;
    return msg ? msg : [];
  }
 
  public getByNumber(num: string): Message | undefined {
    this.parseXML();
    // todo, optimize performance,
    for (const message of this.getMessages()) {
      if (message.getNumber() === num) {
        return message;
      }
    }
    return undefined;
  }
 
/////////////////////////////////
 
  private parseXML() {
    if (this.xml !== undefined) {
      return;
    }
 
    this.xml = {
      topName: undefined,
      description: undefined,
      parsedMessages: [],
      textsTranslations: [],
    };
 
    const parsed = super.parseRaw2();
    if (parsed === undefined) {
      return;
    }
 
    this.xml.topName = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.T100A?.ARBGB;
    this.xml.description = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.T100A?.STEXT;
 
    const t100 = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.T100;
    if (t100 === undefined) {
      return;
    }
    for (const msg of xmlToArray(t100.T100)) {
      this.xml.parsedMessages!.push(new Message(msg.MSGNR, unescape(msg.TEXT), msg.ARBGB));
    }
 
    const t100_texts = parsed?.abapGit?.["asx:abap"]?.["asx:values"]?.T100_TEXTS;
    if (t100_texts === undefined) {
      return;
    }
    for (const item of xmlToArray(t100_texts?.item)) {
      this.xml.textsTranslations!.push({language: item.SPRSL, number: item.MSGNR, text: unescape(item.TEXT)});
    }
  }
 
}