File: exportData.cpp

package info (click to toggle)
postbooks 4.9.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 103,120 kB
  • sloc: cpp: 288,269; sh: 607; xml: 214; awk: 104; makefile: 49
file content (247 lines) | stat: -rw-r--r-- 7,852 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 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 "exportData.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QSqlError>

#include <metasql.h>
#include <mqlutil.h>
#include <parameteredit.h>
#include <selectmql.h>

#include "exporthelper.h"
#include "storedProcErrorLookup.h"

#define DEBUG true

QString exportData::exportFileDir = QString::null;

exportData::exportData(QWidget *parent, const char *name, Qt::WindowFlags fl)
  : XWidget(parent, name, fl)
{
  setupUi(this);

  _paramedit = new ParameterEdit(this);
  QGridLayout *lyt = qobject_cast<QGridLayout*>(layout());
  if (lyt)
    lyt->addWidget(_paramedit, 4, 0, 1, -1);

  connect(_otherXML,    SIGNAL(toggled(bool)), this, SLOT(sHandleButtons()));
  connect(_deleteQuerySet,SIGNAL(clicked()),   this, SLOT(sDeleteQuerySet()));
  connect(_editQuerySet,SIGNAL(clicked()),     this, SLOT(sEditQuerySet()));
  connect(_export,      SIGNAL(clicked()),     this, SLOT(sExport()));
  connect(_exportList,  SIGNAL(newId(int)),    this, SLOT(sHandleButtons()));
  connect(_newQuerySet, SIGNAL(clicked()),     this, SLOT(sNewQuerySet()));
  connect(_qrySetList,  SIGNAL(newId(int)),    this, SLOT(sHandleButtons()));
  connect(_xtupleXML,   SIGNAL(toggled(bool)), this, SLOT(sHandleButtons()));

  _exportList->addColumn(tr("Name"),             -1, Qt::AlignLeft, true, "xsltmap_name");
  _exportList->addColumn(tr("Document Type"),    -1, Qt::AlignLeft, true, "xsltmap_doctype");
  _exportList->addColumn(tr("System Identifier"),-1, Qt::AlignLeft, false,"xsltmap_system");
  _exportList->addColumn(tr("Export XSLT File"), -1, Qt::AlignLeft, false,"xsltmap_export");

  _qrySetList->addColumn(tr("Name"),        -1, Qt::AlignLeft, true, "qryhead_name");
  _qrySetList->addColumn(tr("Description"), -1, Qt::AlignLeft, true, "qryhead_descrip");

  _exportList->populate("SELECT xsltmap_id, xsltmap_name, xsltmap_doctype,"
                        "       xsltmap_system, xsltmap_export"
                        "  FROM xsltmap"
                        " WHERE (LENGTH(xsltmap_export) > 0)"
                        " ORDER BY xsltmap_name;");
  sFillList();
  sHandleButtons();

  _queryset = new QuerySet(0, Qt::Dialog);
  if (_queryset)
  {
    _queryset->setWindowModality(Qt::WindowModal);
    connect(_queryset, SIGNAL(saved(int)), this, SLOT(sFillList()));
  }
}

exportData::~exportData()
{
  // no need to delete child widgets, Qt does it all for us
  if (_queryset)
    _queryset->deleteLater();
  _queryset = 0;
}

void exportData::languageChange()
{
  retranslateUi(this);
}

ParameterEdit *exportData::getParameterEdit() const
{
  return _paramedit;
}

void exportData::sDeleteQuerySet()
{
  if (QMessageBox::question(this, tr("Delete Query Set?"),
                            tr("<p>Are you sure you want to delete this "
                               "Query Set?"),
                            QMessageBox::Yes | QMessageBox::No,
                            QMessageBox::No) == QMessageBox::Yes)
  {
    XSqlQuery delq;
    delq.prepare("SELECT deleteQryhead(:id) AS result;");
    delq.bindValue(":id", _qrySetList->id());
    if (delq.exec())
    {
      int result = delq.value("result").toInt();
      if (result < 0)
      {
        systemError(this, storedProcErrorLookup("deleteQryhead", result),
                    __FILE__, __LINE__);
        return;
      }
    }
    if (delq.lastError().type() != QSqlError::NoError)
    {
      systemError(this, delq.lastError().text(), __FILE__, __LINE__);
      return;
    }

    sFillList();
  }
}

void exportData::sEditQuerySet()
{
  if (_queryset)
  {
    _queryset->setId(_qrySetList->id());
    _queryset->show();
  }
}

void exportData::sExport()
{
  if (DEBUG)
    qDebug("exportData::sExport() entered with exportFileDir '%s'",
           qPrintable(exportFileDir));
  if (exportFileDir.isEmpty())
  {
#if defined Q_OS_MAC
    exportFileDir = _metrics->value("XMLExportDefaultDirMac");
#elif defined Q_OS_WIN
    exportFileDir = _metrics->value("XMLExportDefaultDirWindows");
#elif defined Q_OS_LINUX
    exportFileDir = _metrics->value("XMLExportDefaultDirLinux");
#endif
  }

  QString filename = QFileDialog::getSaveFileName(0, tr("Export Output File"),
              exportFileDir + QDir::separator()
              + _qrySetList->currentItem()->rawValue("qryhead_name").toString()
              + ".xml",
              "XML files (*.xml *.txt)");
  if (filename.isEmpty())
    return;
  QString tmpfilename = QFileInfo(filename).dir().absolutePath();

  exportFileDir = tmpfilename;
  QString errmsg;

  ParameterList params = _paramedit->getParameterList();
  bool success = ExportHelper::exportXML(_qrySetList->id(), params,
                                         filename,          errmsg,
                                         (_otherXML->isChecked() ?
                                                    _exportList->id() : -1));
  if (success)
    QMessageBox::information(this, tr("Processing Complete"),
                             tr("The export to %1 is complete").arg(filename));
  else
    QMessageBox::critical(this, tr("Processing Error"), errmsg);
}

void exportData::sFillList()
{
  _qrySetList->populate("SELECT qryhead_id, qryhead_name, qryhead_descrip"
                        "  FROM qryhead"
                        " ORDER BY qryhead_name;");
}

void exportData::sHandleButtons()
{
  static int prevqrysetid = -1;

  _export->setEnabled(_qrySetList->id() > 0 &&
                        (_xtupleXML->isChecked() ||
                         (_otherXML->isChecked() && _exportList->id() > 0)));

  // populate _paramedit with parameters from all of the query items in the set
  if (_qrySetList->id() != prevqrysetid)
  {
    _paramedit->clear();

    if (_qrySetList->id() < 0)
      prevqrysetid = -1;
    else
    {
      QString xml = "<report>";

      QStringList paramlist;
      XSqlQuery qiq;
      qiq.prepare("SELECT qryitem_src, qryitem_group, qryitem_detail"
                  "  FROM qryitem"
                  " WHERE ((qryitem_qryhead_id=:id)"
                  "    AND (qryitem_src!='REL'))"
                  " ORDER BY qryitem_order;");
      qiq.bindValue(":id", _qrySetList->id());
      qiq.exec();
      while (qiq.next())
      {
        QString qryitem_src = qiq.value("qryitem_src").toString();
        if (qryitem_src == "MQL")
        {
          bool valid = false;
          QString errmsg;
          QString tmpqry;
          tmpqry = MQLUtil::mqlLoad(qiq.value("qryitem_group").toString(),
                                    qiq.value("qryitem_detail").toString(),
                                    errmsg, &valid);
          if (valid)
            paramlist.append(MQLEdit::getParamsFromMetaSQLText(tmpqry));
        }
        else if (qryitem_src == "CUSTOM")
          paramlist.append(MQLEdit::getParamsFromMetaSQLText(qiq.value("qryitem_detail").toString()));
      }
      if (qiq.lastError().type() != QSqlError::NoError)
      {
        systemError(this, qiq.lastError().text(), __FILE__, __LINE__);
        return;
      }
      paramlist.sort();
      for (int i = 0; i < paramlist.size(); i++)
        xml += "\n <parameter name='" + paramlist.at(i) + "'/>";

      xml += "\n</report>";
      QDomDocument doc;
      doc.setContent(xml);
      _paramedit->setDocument(doc);
      prevqrysetid = _qrySetList->id();
    }
  }
}

void exportData::sNewQuerySet()
{
  if (_queryset)
  {
    _queryset->setId(-1);
    _queryset->show();
  }
}