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.having;
21  
22  import cz.zcu.mre.sparkle.gui.query.QueryFormPane;
23  import cz.zcu.mre.sparkle.gui.query.clause.ClausePane;
24  import cz.zcu.mre.sparkle.gui.query.clause.SingleClausePane;
25  import cz.zcu.mre.sparkle.gui.query.helpers.ObjectRelatedActionEvent;
26  import cz.zcu.mre.sparkle.gui.query.helpers.PartialQueryGenerator;
27  import javafx.collections.ObservableList;
28  import javafx.scene.Node;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.NodeList;
31  
32  /**
33   * Component controller creating a HAVING clause.
34   *
35   * @author Josef Kazak
36   * @author Klara Hlavacova
37   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
38   */
39  public class HavingClausePane extends ClausePane {
40  
41      private static final String XML_ELEMENT_NAME = "Having"; //$NON-NLS-1$
42  
43      public HavingClausePane(final QueryFormPane<?> parentQueryFormPane) {
44          super(parentQueryFormPane);
45      }
46  
47      @Override
48      protected final HavingClauseConstraintPane addNewRule() {
49          final HavingClauseConstraintPane pane = new HavingClauseConstraintPane(parentQueryFormPane);
50          final ObservableList<Node> children = getChildren();
51          children.add(children.indexOf(addRuleButton), pane);
52  
53          pane.setOnRemovalRequested(keeper.toWeak((final ObjectRelatedActionEvent<SingleClausePane> e) -> children.remove(e.relatedObject)));
54  
55          return pane;
56      }
57  
58      @Override
59      public final String getQueryPart() {
60          final StringBuilder sb = new StringBuilder();
61  
62          getChildren().stream().filter((node) -> (node instanceof PartialQueryGenerator)).forEach((node) -> sb.append(((PartialQueryGenerator) node).getQueryPart()));
63  
64          if (sb.length() > 0) {
65              sb.insert(0, "HAVING "); //$NON-NLS-1$
66          }
67  
68          return sb.toString();
69      }
70  
71      @Override
72      public final void load(final Object e) throws LoadException {
73          if (e instanceof Element) {
74              final NodeList nodes = ((Element) e).getElementsByTagName(HavingClauseConstraintPane.XML_ELEMENT_NAME);
75              final int nodesCount = nodes.getLength();
76              for (int nodeIndex = 0; nodeIndex < nodesCount; nodeIndex++) {
77                  final org.w3c.dom.Node childNode = nodes.item(nodeIndex);
78                  if (childNode instanceof Element) {
79                      addNewRule().load(childNode);
80                  }
81              }
82          } else {
83              addNewRule().load(e);
84          }
85      }
86  
87      @Override
88      public final String getXMLElementName() {
89          return XML_ELEMENT_NAME;
90      }
91  }