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.queryMainParts;
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.clause.SingleFromClausePane;
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.SparqlParser;
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 seskupující v dotazu všechny klauzule FROM.
36   * Obsahuje položky typu {@link SingleFromClausePane}.
37   *
38   * @author Jan Smucr
39   * @author Klara Hlavacova
40   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
41   */
42  public final class FromClausePane
43          extends ClausePane {
44  
45      private static final String XML_ELEMENT_NAME = "FromGroup"; //$NON-NLS-1$
46  
47      public FromClausePane(final QueryFormPane<?> parentQueryFormPane) {
48          super(parentQueryFormPane);
49      }
50  
51      @Override
52      protected final SingleFromClausePane addNewRule() {
53          final SingleFromClausePane pane = new SingleFromClausePane(parentQueryFormPane);
54          final ObservableList<Node> children = getChildren();
55          children.add(children.indexOf(addRuleButton), pane);
56  
57          pane.setOnRemovalRequested(keeper.toWeak((final ObjectRelatedActionEvent<SingleClausePane> e) -> children.remove(e.relatedObject)));
58  
59          return pane;
60      }
61  
62      @Override
63      public final String getQueryPart() {
64          final StringBuilder sb = new StringBuilder();
65  
66          getChildren().stream().filter((node) -> (node instanceof PartialQueryGenerator)).forEach((node) -> sb.append(((PartialQueryGenerator) node).getQueryPart()));
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(SingleFromClausePane.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 if (e instanceof SparqlParser.DatasetClauseContext) {
83              addNewRule().load(e);
84          }
85      }
86  
87      @Override
88      public final String getXMLElementName() {
89          return XML_ELEMENT_NAME;
90      }
91  }