File: loadappui.cpp

package info (click to toggle)
postbooks-updater 2.2.5-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 944 kB
  • ctags: 479
  • sloc: cpp: 5,424; sh: 351; makefile: 23; sql: 7
file content (153 lines) | stat: -rw-r--r-- 5,276 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2010 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "loadappui.h"

#include <QDomDocument>
#include <QSqlError>
#include <QVariant>     // used by XSqlQuery::bindValue()
#include <limits.h>

#include "xsqlquery.h"

#define DEBUG false

LoadAppUI::LoadAppUI(const QString &name, const int order,
                     const bool system, const bool enabled,
                     const QString & comment, const QString &filename)
  : Loadable("loadappui", name, order, system, comment, filename)
{
  _enabled = enabled;
  _pkgitemtype = "U";
}

LoadAppUI::LoadAppUI(const QDomElement &elem, const bool system,
                     QStringList &msg, QList<bool> &fatal)
  : Loadable(elem, system, msg, fatal)
{
  _pkgitemtype = "U";

  if (elem.nodeName() != "loadappui")
  {
    msg.append(TR("Creating a LoadAppUI element from a %1 node.")
                         .arg(elem.nodeName()));
    fatal.append(false);
  }

  if (elem.hasAttribute("grade"))
  {
    msg.append(TR("Node %1 '%2' has a 'grade' attribute but "
                      "should use 'order' instead.")
                     .arg(elem.nodeName()).arg(elem.attribute("name")));
    fatal.append(false);
  }

  if (elem.hasAttribute("order"))
  {
    if (elem.attribute("order").contains("highest", Qt::CaseInsensitive))
      _grade = INT_MAX;
    else if (elem.attribute("order").contains("lowest", Qt::CaseInsensitive))
      _grade = INT_MIN;
    else
      _grade = elem.attribute("order").toInt();
  }

  _enabled = true;
  if (elem.hasAttribute("enabled"))
  {
    if (elem.attribute("enabled").contains(trueRegExp))
      _enabled = true;
    else if (elem.attribute("enabled").contains(falseRegExp))
      _enabled = false;
    else
    {
      msg.append(TR("Node %1 '%2' has an 'enabled' attribute that is "
                        "neither 'true' nor 'false'. Using '%3'.")
                         .arg(elem.nodeName()).arg(elem.attribute("name"))
                         .arg(_enabled ? "true" : "false"));
      fatal.append(false);
    }
  }
}

int LoadAppUI::writeToDB(const QByteArray &pdata, const QString pkgname, QString &errMsg)
{
  int errLine = 0;
  int errCol = 0;
  QDomDocument doc;
  if (! doc.setContent(pdata, &errMsg, &errLine, &errCol))
  {
    errMsg = TR("Error parsing file %1: %2 on line %3 column %4")
                          .arg(_filename).arg(errMsg).arg(errLine).arg(errCol);
    return -1;
  }

  QDomElement root = doc.documentElement();
  if (root.tagName() != "ui")
  {
    errMsg = TR("XML Document %1 does not have root node of 'ui'")
              .arg(_filename);
    return -2;
  }

  if (DEBUG)
    qDebug("LoadAppUI::writeToDB() name before looking for class node: %s",
           qPrintable(_name));
  QDomElement n = root.firstChildElement("class");
  if (n.isNull())
  {
    errMsg = TR("XML Document %1 does not name its class and is not a valid "
                "UI Form.")
                .arg(_filename);
    return -3;
  }
  _name = n.text();
  if (DEBUG)
    qDebug("LoadAppUI::writeToDB() name after looking for class node: %s",
           qPrintable(_name));

  _minMql = new MetaSQLQuery("SELECT MIN(uiform_order) AS min "
                   "FROM uiform "
                   "WHERE (uiform_name=<? value('name') ?>);");

  _maxMql = new MetaSQLQuery("SELECT MAX(uiform_order) AS max "
                   "FROM uiform "
                   "WHERE (uiform_name=<? value('name') ?>);");

  _selectMql = new MetaSQLQuery("SELECT uiform_id, -1, -1"
                      "  FROM <? literal('tablename') ?> "
                      " WHERE ((uiform_name=<? value('name') ?>)"
                      "   AND  (uiform_order=<? value('grade') ?>));");

  _updateMql = new MetaSQLQuery("UPDATE <? literal('tablename') ?> "
                      "   SET uiform_order=<? value('grade') ?>, "
                      "       uiform_enabled=<? value('enabled') ?>,"
                      "       uiform_source=E<? value('source') ?>,"
                      "       uiform_notes=<? value('notes') ?> "
                      " WHERE (uiform_id=<? value('id') ?>) "
                      "RETURNING uiform_id AS id;");

  _insertMql = new MetaSQLQuery("INSERT INTO <? literal('tablename') ?> ("
                      "    uiform_id, uiform_name,"
                      "    uiform_order, uiform_enabled, "
                      "    uiform_source, uiform_notes"
                      ") VALUES ("
                      "    DEFAULT, <? value('name') ?>,"
                      "    <? value('grade') ?>, <? value('enabled') ?>,"
                      "    E<? value('source') ?>,"
                      "    <? value('notes') ?>) "
                      "RETURNING uiform_id AS id;");

  ParameterList params;
  params.append("enabled",   QVariant(_enabled));
  params.append("tablename", "uiform");

  return Loadable::writeToDB(pdata, pkgname, errMsg, params);
}