View Javadoc
1   /*
2    * Copyright 2013-2023 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * This file is part of Sparkle project.
7    *
8    * Sparkle is free software: you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License.
11   *
12   * Sparkle is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with Sparkle. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package cz.zcu.mre.sparkle.gui.query.modifiers;
21  
22  import cz.zcu.mre.sparkle.gui.query.QueryFormPane;
23  import cz.zcu.mre.sparkle.gui.query.clause.ClauseVariablePane;
24  import cz.zcu.mre.sparkle.gui.query.clause.SingleClausePane;
25  import cz.zcu.mre.sparkle.gui.query.clause.SingleClauseRulePane;
26  import cz.zcu.mre.sparkle.gui.query.helpers.ObjectRelatedActionEvent;
27  import cz.zcu.mre.sparkle.gui.query.helpers.PartialQueryGenerator;
28  import cz.zcu.mre.sparkle.tools.Definitions;
29  import javafx.collections.ObservableList;
30  import javafx.scene.Node;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.NodeList;
33  
34  /**
35   * Kontroler kontejnerové komponenty reprezentující v dotazu klauzuli ORDER BY.
36   * Obsahuje komponenty typu {@link OrderByClauseRulePane}.
37   *
38   * @author Jan Smucr
39   * @author Klara Hlavacova
40   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
41   */
42  public final class OrderByClausePane extends ClauseVariablePane {
43  
44      private static final String XML_ELEMENT_NAME = "OrderBy"; //$NON-NLS-1$
45  
46      public OrderByClausePane(final QueryFormPane<?> parentQueryFormPane) {
47          super(parentQueryFormPane);
48      }
49  
50      @Override
51      protected final OrderByClauseRulePane addNewRule() {
52          final OrderByClauseRulePane pane = new OrderByClauseRulePane(parentQueryFormPane);
53          final ObservableList<Node> children = getChildren();
54          children.add(children.indexOf(addRuleButton), pane);
55  
56          pane.addVariablesCollector(this);
57          pane.setOnRemovalRequested(keeper.toWeak((final ObjectRelatedActionEvent<SingleClausePane> e) -> {
58              children.remove(e.relatedObject);
59              ((SingleClauseRulePane) e.relatedObject).getVariables().stream().forEach(this::onVariableRemoved);
60          }));
61  
62          return pane;
63      }
64  
65      @Override
66      public final String getQueryPart() {
67          final StringBuilder sb = new StringBuilder();
68  
69          getChildren().stream().filter((node) -> (node instanceof PartialQueryGenerator)).forEach((node) -> sb.append(((PartialQueryGenerator) node).getQueryPart()));
70  
71          if (sb.length() > 0) {
72              sb.insert(0, "ORDER BY "); //$NON-NLS-1$
73          }
74  
75          sb.append(Definitions.SPACE);
76  
77          return sb.toString();
78      }
79  
80      @Override
81      public final void load(final Object e) throws LoadException {
82          if (e instanceof Element) {
83              final NodeList nodes = ((Element) e).getElementsByTagName(OrderByClauseRulePane.XML_ELEMENT_NAME);
84              final int nodesCount = nodes.getLength();
85              for (int nodeIndex = 0; nodeIndex < nodesCount; nodeIndex++) {
86                  final org.w3c.dom.Node childNode = nodes.item(nodeIndex);
87                  if (childNode instanceof Element) {
88                      addNewRule().load(childNode);
89                  }
90              }
91          } else {
92              addNewRule().load(e);
93          }
94      }
95  
96      @Override
97      public final String getXMLElementName() {
98          return XML_ELEMENT_NAME;
99      }
100 }