getBrowse method

Future<BrowseHead> getBrowse({
  1. String? code,
  2. int p = 1,
  3. int r = 20,
  4. String q = '',
  5. String f = '',
  6. String o = '',
  7. int s = 0,
})

Implementation

Future<BrowseHead> getBrowse({
  String? code,
  int p = 1,
  int r = 20,
  String q = '',
  String f = '',
  String o = '',
  int s = 0,
}) async {
  BrowseHead _head = BrowseHead(code: code, rows: {});
  _msg = '';
  if (code != '') {
    var search = (q != '') ? '&bsearchtext=' + q.replaceAll('+', '%2B') : '';
    var sqlfilter = (f != '') ? '&sqlfilter=' + f : '';
    var sqlorder = (o != '') ? '&sortorder=' + o : '';
    var pgno = (p > 1) ? '&bpageno=' + p.toString() : '';
    var nbrows = (r != 20) ? '&brows=' + r.toString() : '';
    var sts = (s > 0) ? '&stateid=' + s.toString() : '';

    //await httpSvc.loadAccount(code: code);
    //if (await verifyHost()) {
    //if (Oph.curPreset.hostguid != null && Oph.curPreset.hostguid != '' && Oph.curPreset.isLogin) {
    await httpSvc.loadAccount(code!, hostguid: Oph.curPreset.hostguid!);
    if (Oph.curPreset.hostguid != null &&
        Oph.curPreset.hostguid != '' &&
        Oph.curPreset.isLogin!) {
      var url = Oph.curPreset.serverURL! +
          Oph.curPreset.rootAccountId! +
          '/' +
          Oph.curPreset.apiURL! +
          '?suba=' +
          Oph.curPreset.accountId! +
          '&mode=browse&code=' +
          code +
          pgno +
          nbrows +
          search +
          sqlfilter +
          sqlorder +
          sts;

      Map<String, String> body = {'hostguid': Oph.curPreset.hostguid!};
      isLoading = true;
      String value = await httpSvc.getXML(url, body: body);
      isLoading = false;
      if (value != '') {
        XmlDocument xmlDoc = XmlDocument.parse(value);
        //menu
        _msg = xmlDoc.findAllElements('message').toString();
        if (_msg.indexOf('You are not authorized') > 0) {
          Oph.curPreset.isLogin = false;
        } else if (_msg != '') {
          _getUser(xmlDoc);
          _head.menu = _getMenu(xmlDoc);
          _head.state = _getState(xmlDoc);
          //browse
          var l1 = xmlDoc.findAllElements("row").toList();
          for (var f in l1) {
            String? docstat = f
                .findAllElements("docStatus")
                .toList()[0]
                .getAttribute("title");
            var l2 = f.findAllElements("field").toList();
            var guid = f.getAttribute("GUID").toString();
            Map<String, Field> _field = {};
            for (var i in l2) {
              var title = i.getAttribute("title").toString();
              var caption = i.getAttribute("caption").toString();
              var mandatory = i.getAttribute("mandatory").toString();
              var editor = i.getAttribute("editor").toString();
              var rawval = editor == 'datepicker'
                  ? i.getAttribute("date").toString()
                  : editor == 'select2'
                      ? i.getAttribute("guid").toString()
                      : i.text.toString();
              var val = i.text.toString();
              _field[caption] = Field(
                  title: title,
                  //caption: caption,
                  mandatory: int.parse(mandatory),
                  rawVal: rawval,
                  val: val);
            }
            BrowseRow row = BrowseRow(fields: _field, docStatus: docstat);
            row.frmSvc = FormService();
            row.frmSvc!.init(_head.code, guid);
            _head.rows[guid] = row;
          }
        } else {
          isLoading = false;
          _msg = 'Empty ' + url + ' ' + httpSvc.httpError.toString();
          print('empty $code $_msg');
          _errorback!();
        }
        print(code + ' loaded.');
      } else {
        if (_msg == '')
          _msg = "Unauthorized: " + url + ' ' + Oph.curPreset.hostguid!;
        print(_msg);
      }
    }
  }
  return _head;
}