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.groupBy;
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 cz.zcu.mre.sparkle.tools.Saveable;
30  import javafx.collections.ObservableList;
31  import javafx.scene.Node;
32  import org.w3c.dom.Element;
33  import org.w3c.dom.NodeList;
34  
35  /**
36   * Component controller creating a GROUP BY clause.
37   *
38   * @author Josef Kazak
39   * @author Klara Hlavacova
40   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
41   */
42  public class GroupByClausePane
43          extends ClauseVariablePane {
44  
45      private static final String XML_ELEMENT_NAME = "GroupBy"; //$NON-NLS-1$
46  
47      public GroupByClausePane(final QueryFormPane<?> parentQueryFormPane) {
48          super(parentQueryFormPane);
49      }
50  
51      @Override
52      protected final GroupByClauseRulePane addNewRule() {
53          final GroupByClauseRulePane pane = new GroupByClauseRulePane(parentQueryFormPane);
54          final ObservableList<Node> children = getChildren();
55          children.add(children.indexOf(addRuleButton), pane);
56  
57          pane.addVariablesCollector(this);
58          pane.setOnRemovalRequested(keeper.toWeak((final ObjectRelatedActionEvent<SingleClausePane> e) -> {
59              children.remove(e.relatedObject);
60              ((SingleClauseRulePane) e.relatedObject).getVariables().stream().forEach(this::onVariableRemoved);
61          }));
62  
63          return pane;
64      }
65  
66      @Override
67      public final String getQueryPart() {
68          final StringBuilder sb = new StringBuilder();
69  
70          getChildren().stream().filter((node) -> (node instanceof PartialQueryGenerator)).forEach((node) -> sb.append(((PartialQueryGenerator) node).getQueryPart()));
71  
72          if (sb.length() > 0) {
73              sb.insert(0, "GROUP BY "); //$NON-NLS-1$
74          }
75  
76          sb.append(Definitions.SPACE);
77  
78          return sb.toString();
79      }
80  
81      @Override
82      public final void load(final Object e) throws Saveable.LoadException {
83          if (e instanceof Element) {
84              final NodeList nodes = ((Element) e).getElementsByTagName(GroupByClauseRulePane.XML_ELEMENT_NAME);
85              final int nodesCount = nodes.getLength();
86              for (int nodeIndex = 0; nodeIndex < nodesCount; nodeIndex++) {
87                  final org.w3c.dom.Node childNode = nodes.item(nodeIndex);
88                  if (childNode instanceof Element) {
89                      addNewRule().load(childNode);
90                  }
91              }
92          } else {
93              addNewRule().load(e);
94          }
95      }
96  
97      @Override
98      public final String getXMLElementName() {
99          return XML_ELEMENT_NAME;
100     }
101 }