loadForm method

Future<void> loadForm()

Implementation

Future<void> loadForm() async {
  //_guid = guid;

  _frm!.fields = [];
  _frm!.pages = [];
  if (_frm!.code != '') {
    await httpSvc.loadAccount(_code!, hostguid: Oph.curPreset.hostguid!);
    if (Oph.curPreset.hostguid != null &&
        Oph.curPreset.hostguid != '' &&
        Oph.curPreset.isLogin!) {
      String url = Oph.curPreset.serverURL! +
          Oph.curPreset.rootAccountId! +
          '/' +
          Oph.curPreset.apiURL! +
          '?suba=' +
          Oph.curPreset.accountId! +
          '&mode=form&code=' +
          _frm!.code +
          '&guid=' +
          _frm!.guid;
      _frm!.isLoaded = false;
      Map<String, String> body = {'hostguid': Oph.curPreset.hostguid!};
      _frm!.isLoaded = false;
      String value = await httpSvc.getXML(url, body: body);
      if (value != '') {
        XmlDocument xmlDoc = XmlDocument.parse(value);
        //_msg = xmlDoc.findAllElements('message');
        List<XmlElement> mx = xmlDoc.findAllElements("message").toList();
        if (mx.length == 0) {
          List<FrmPage> fp = [];
          List<XmlElement> px = xmlDoc.findAllElements("formPage").toList();
          for (XmlElement p in px) {
            List<FrmSection> fs = [];
            List<XmlElement> sx = p.findAllElements("formSection").toList();
            int fpno = int.parse(p.getAttribute("pageNo")!);
            for (XmlElement s in sx) {
              List<FrmCol> fc = [];
              List<XmlElement> cx = s.findAllElements("formCol").toList();
              int fsno = int.parse(s.getAttribute("sectionNo")!);
              for (XmlElement c in cx) {
                List<FrmRow> fr = [];
                List<XmlElement> rx = c.findAllElements("formRow").toList();
                int fcno = int.parse(c.getAttribute("colNo")!);
                for (XmlElement r in rx) {
                  List<FrmField> ff = [];
                  List<XmlElement> fx = r.findAllElements("field").toList();
                  for (XmlElement f in fx) {
                    int no = 0;
                    String fieldName = f.getAttribute("fieldName").toString();
                    int isEditable =
                        int.parse((f.getAttribute("isEditable") ?? '0'));
                    bool isNullable =
                        (f.getAttribute("isNullable") ?? '1').toString() ==
                                '0'
                            ? false
                            : true;
                    int primaryCol =
                        int.parse((f.getAttribute("mandatory") ?? '0'));
                    int maxLength =
                        int.parse((f.getAttribute("rows") ?? '0'));
                    String boxType = 'hiddenBox';
                    String? value, caption, wf1, wf2, combovalue;
                    AutosuggestBoxPar? autosuggestBoxPar;
                    List<XmlElement> tBox =
                        f.findAllElements("textBox").toList();
                    if (tBox.length > 0) //textbox
                    {
                      boxType = 'textBox';
                      value =
                          tBox[0].findAllElements("value").toList().length ==
                                  0
                              ? ''
                              : tBox[0].findAllElements("value").first.text;
                      caption = tBox[0]
                                  .findAllElements("titlecaption")
                                  .toList()
                                  .length ==
                              0
                          ? ''
                          : tBox[0]
                              .findAllElements("titlecaption")
                              .first
                              .text;
                    } else {
                      List<XmlElement> asBox =
                          f.findAllElements("autoSuggestBox").toList();
                      if (asBox.length > 0) {
                        String comboCode =
                            asBox[0].getAttribute("comboCode").toString();
                        int allowAdd = int.parse(
                            asBox[0].getAttribute("allowAdd").toString());
                        int allowEdit = int.parse(
                            asBox[0].getAttribute("allowEdit").toString());
                        wf1 =
                            asBox[0].findAllElements("wf1").toList().length >
                                    0
                                ? asBox[0].findAllElements("wf1").first.text
                                : '';
                        wf2 =
                            asBox[0].findAllElements("wf2").toList().length >
                                    0
                                ? asBox[0].findAllElements("wf2").first.text
                                : '';
                        value = asBox[0]
                                    .findAllElements("value")
                                    .toList()
                                    .length ==
                                0
                            ? ''
                            : asBox[0].findAllElements("value").first.text;
                        combovalue = asBox[0]
                                    .findAllElements("combovalue")
                                    .toList()
                                    .length ==
                                0
                            ? ''
                            : asBox[0]
                                .findAllElements("combovalue")
                                .first
                                .text;
                        caption = asBox[0]
                            .findAllElements("titlecaption")
                            .first
                            .text;
                        boxType = 'autosuggestBox';
                        autosuggestBoxPar = AutosuggestBoxPar(
                            code: comboCode,
                            isAllowAdd: allowAdd,
                            isAllowEdit: allowEdit,
                            wf1: wf1,
                            wf2: wf2);

                        autosuggestBoxPar.list = [];
                      } else {
                        List<XmlElement> chBox =
                            f.findAllElements("checkBox").toList();
                        if (chBox.length > 0) {
                          boxType = 'checkBox';
                          caption = chBox[0]
                              .findAllElements("titlecaption")
                              .first
                              .text;
                          value = chBox[0]
                                      .findAllElements("value")
                                      .toList()
                                      .length ==
                                  0
                              ? '0'
                              : chBox[0].findAllElements("value").first.text;
                        } else {
                          List<XmlElement> pBox =
                              f.findAllElements("profileBox").toList();
                          if (pBox.length > 0) {
                            boxType = 'profileBox';
                            caption = pBox[0]
                                .findAllElements("titlecaption")
                                .first
                                .text;
                            value = pBox[0]
                                        .findAllElements("value")
                                        .toList()
                                        .length ==
                                    0
                                ? ''
                                : pBox[0].findAllElements("value").first.text;
                          } else {
                            List<XmlElement> sgpsBox =
                                f.findAllElements("setGPSBox").toList();
                            if (sgpsBox.length > 0) //textbox
                            {
                              boxType = 'setGPSBox';
                              caption = sgpsBox[0]
                                  .findAllElements("titlecaption")
                                  .first
                                  .text;
                              value = sgpsBox[0]
                                          .findAllElements("value")
                                          .toList()
                                          .length ==
                                      0
                                  ? ''
                                  : sgpsBox[0]
                                      .findAllElements("value")
                                      .first
                                      .text;
                            }
                          }
                        }
                      }
                    }
                    ff.add(FrmField(
                        no: no,
                        fieldName: fieldName,
                        isEditable: isEditable,
                        isNullable: isNullable,
                        primaryCol: primaryCol,
                        value: value!,
                        combovalue: combovalue ?? '',
                        caption: caption!,
                        boxType: boxType,
                        maxLength: maxLength,
                        pageNo: fpno,
                        sectionNo: fsno,
                        autosuggestBoxPar: autosuggestBoxPar));
                  }
                  int frno = int.parse(r.getAttribute("rowNo")!);
                  fr.add(FrmRow(no: frno, fields: ff));
                }
                fc.add(FrmCol(no: fcno, rows: fr));
              }
              String fstitle = s.getAttribute("sectionTitle") ?? '';
              fs.add(FrmSection(no: fsno, title: fstitle, cols: fc));
            }
            String fptitle = p.getAttribute("pageTitle") ?? '';
            fp.add(FrmPage(no: fpno, title: fptitle, sections: fs));
          }

          //_frm!.guid = guid;
          _frm!.pages = fp;
          List<XmlElement> ff = xmlDoc.findAllElements("form").toList();
          List<XmlElement> ffi = ff[0].findAllElements("info").toList();
          String docno = ffi[0].findAllElements("docNo").toList().length > 0
              ? ffi[0].findAllElements("docNo").toList()[0].text
              : '';
          _frm!.docNo = docno;
          String docRefNo =
              ffi[0].findAllElements("docRefNo").toList().length > 0
                  ? ffi[0].findAllElements("docRefNo").toList()[0].text
                  : '';
          _frm!.docRefNo = docRefNo;

          List<XmlElement> fip = ff[0].findAllElements("permission").toList();
          int allowBrowse = int.parse(
              fip[0].findAllElements("allowBrowse").toList().length > 0
                  ? fip[0].findAllElements("allowBrowse").toList()[0].text
                  : '0');
          int allowAdd = int.parse(
              fip[0].findAllElements("allowAdd").toList().length > 0
                  ? fip[0].findAllElements("allowAdd").toList()[0].text
                  : '0');
          int allowEdit = int.parse(
              fip[0].findAllElements("allowEdit").toList().length > 0
                  ? fip[0].findAllElements("allowEdit").toList()[0].text
                  : '0');
          int allowDelete = int.parse(
              fip[0].findAllElements("allowDelete").toList().length > 0
                  ? fip[0].findAllElements("allowDelete").toList()[0].text
                  : '0');
          _frm!.permission = Permission(
              allowAdd: allowAdd,
              allowBrowse: allowBrowse,
              allowDelete: allowDelete,
              allowEdit: allowEdit);
          if (_frm != null) {
            _frm!.fields!.clear();
            _frm!.pages!.forEach((p) {
              p.sections.forEach((s) {
                s.cols.forEach((c) {
                  c.rows.forEach((r) {
                    //if (_frm!.fields.where((f)=>f.caption==).toList().length==0)
                    _frm!.fields!.addAll(r.fields);
                  });
                });
              });
            });
            _frm!.fields!.forEach((f) {
              f.controller = TextEditingController();
              if (f.boxType == 'autosuggestBox' && f.value != '')
                autosuggest(f.fieldName, dv: f.value!).then((dv) {
                  print(dv);
                  f.controller!.text = dv[0]['text'];
                  if (_callback != null) _callback!();
                });
              else
                f.controller!.text =
                    f.boxType != 'autosuggestBox' ? f.value! : '';
            });
          } //else
          _frm!.isLoaded = true;
          _frm!.children = [];
          List<XmlElement> hx = xmlDoc.findAllElements("child").toList();
          hx.forEach((h) {
            String code = h.findAllElements("code").toList().length > 0
                ? h.findAllElements("code").toList()[0].text.toString()
                : '';
            String title = h.findAllElements("childTitle").toList().length > 0
                ? h.findAllElements("childTitle").toList()[0].text.toString()
                : '';
            String parentKey =
                h.findAllElements("parentkey").toList().length == 0
                    ? ''
                    : h
                        .findAllElements("parentkey")
                        .toList()[0]
                        .text
                        .toString();
            int allowBrowse = int.parse(
                h.findAllElements("allowBrowse").toList().length == 0
                    ? '0'
                    : h.findAllElements("allowBrowse").toList()[0].text);
            int allowAdd = int.parse(
                h.findAllElements("allowAdd").toList().length == 0
                    ? '0'
                    : h.findAllElements("allowAdd").toList()[0].text);
            int allowEdit = int.parse(
                h.findAllElements("allowEdit").toList().length == 0
                    ? '0'
                    : h.findAllElements("allowEdit").toList()[0].text);
            int allowDelete = int.parse(
                h.findAllElements("allowDelete").toList().length == 0
                    ? '0'
                    : h.findAllElements("allowDelete").toList()[0].text);

            BrowseService childSvc =
                BrowseService(Oph.curPreset.accountId!, code, code);
            String filter = //guid == '00000000-0000-0000-0000-000000000000'
                parentKey + '=\'' + _frm!.guid + '\'';
            _frm!.children!.add(FrmChild(
                code: code,
                title: title,
                parentKey: parentKey,
                permission: Permission(
                    allowAdd: allowAdd,
                    allowBrowse: allowBrowse,
                    allowDelete: allowDelete,
                    allowEdit: allowEdit),
                service: childSvc));
            childSvc.init(code, code, _callback, _errorback, f: filter);
            if (_callback != null) _callback!();
          });
        } else {
          _msg = mx[0].toString();
        }
      }
    } else {
      _msg = "Unauthorized";
      print(_msg);
    }

    //} catch (e) {
    //_msg = e.message;
    //}
  }

  //return _form;
}