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.queryTypes.insert;
21  
22  import cz.zcu.mre.sparkle.Messages;
23  import cz.zcu.mre.sparkle.data.DataAgent;
24  import cz.zcu.mre.sparkle.data.Storage;
25  import cz.zcu.mre.sparkle.gui.mainForm.MainForm;
26  import cz.zcu.mre.sparkle.gui.query.CustomTitledPane;
27  import cz.zcu.mre.sparkle.gui.query.QueryFormPane;
28  import cz.zcu.mre.sparkle.gui.query.modifiers.GroupGraphPatternPane;
29  import cz.zcu.mre.sparkle.gui.query.queryTypes.subselect.SubSelectPane;
30  import cz.zcu.mre.sparkle.tools.Saveable;
31  import cz.zcu.mre.sparkle.tools.SparqlParser;
32  import cz.zcu.mre.sparkle.tools.sparqlParser.SparqlParserNode;
33  import cz.zcu.mre.sparkle.tools.sparqlParser.SparqlParserWhereClauseWalker;
34  import javafx.collections.FXCollections;
35  import javafx.collections.ObservableList;
36  import javafx.collections.ObservableSet;
37  import javafx.scene.Node;
38  import org.antlr.v4.runtime.tree.ParseTree;
39  import org.w3c.dom.Element;
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Set;
43  import java.util.Stack;
44  import static cz.zcu.mre.sparkle.tools.sparqlParser.SparqlParserUtils.getTreeChild;
45  
46  /**
47   * The panel controller for a INSERT query.
48   *
49   * @author Josef Kazak
50   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
51   */
52  public final class InsertQueryFormPane
53          extends QueryFormPane<Boolean> {
54  
55      public static final String XML_QUERY_TYPE_VALUE = "insert"; //$NON-NLS-1$
56  
57      private GroupGraphPatternPane insertClausePane;
58      private ObservableSet<String> insertClauseVariables;
59  
60      public InsertQueryFormPane(final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
61              String paneType) {
62          super(mainForm, dataAgent, localStorage, paneType);
63      }
64  
65      public InsertQueryFormPane(final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
66              final Object root, String paneType)
67              throws Saveable.LoadException {
68  
69          super(mainForm, dataAgent, localStorage, root, paneType);
70      }
71  
72      @Override
73      public final void load(final Object root) throws Saveable.LoadException {
74          insertClauseVariables = FXCollections.observableSet();
75          initializeQueryFormParts();
76          addPartsTitles();
77          loadForm(root);
78  
79          watch(insertClausePane);
80      }
81  
82      private void loadForm(Object root) throws LoadException {
83          if (root != null) {
84              if (root instanceof Element) {
85                  // Loading of a saved query
86                  Element rootNode = (Element) root;
87                  loadElement(rootNode, insertClausePane);
88              } else if (root instanceof ParseTree) {
89                  setVariables((ParseTree) root);
90              }
91          }
92      }
93  
94      private void addPartsTitles() {
95          final ObservableList<Node> children = getChildren();
96          children.add(new CustomTitledPane(Messages.getString("INSERT_QUERY_FRAGMENT"), insertClausePane)); //$NON-NLS-1$
97      }
98  
99      private void initializeQueryFormParts() {
100         insertClausePane = new InsertClausePane(this);
101         insertClausePane.addVariablesCollector(createCollector(insertClauseVariables));
102     }
103 
104     @Override
105     protected void setVariables(ParseTree root) throws Saveable.LoadException {
106         Stack<SparqlParserNode> nodeStack = new Stack<>(); //current path in parse tree
107         nodeStack.push(new SparqlParserNode(root));
108         while (!nodeStack.isEmpty()) {
109             SparqlParserNode currentNode = nodeStack.pop();
110             ParseTree nodeData = currentNode.getNodeData();
111             if (nodeData instanceof SparqlParser.QuadDataContext) { //where clause
112                 new SparqlParserWhereClauseWalker(nodeData, insertClausePane)
113                         .processWhereClauseDFS();
114 
115             } else { //uninsteresting node -> get next node
116                 nodeStack.push(currentNode);
117                 getTreeChild(currentNode, nodeStack);
118             }
119         }
120     }
121 
122     @Override
123     public final String getQueryPart() {
124         final StringBuilder sb = new StringBuilder();
125         String insertClausePaneValue = insertClausePane.getQueryPart();
126         sb.append("INSERT DATA "); //$NON-NLS-1$
127         if (!insertClausePaneValue.startsWith("{")) { //$NON-NLS-1$
128             sb.append("{ "); //$NON-NLS-1$
129         }
130         sb.append(insertClausePaneValue);
131         if (!insertClausePaneValue.startsWith("{")) { //$NON-NLS-1$
132             sb.append("}"); //$NON-NLS-1$
133         }
134         return sb.toString();
135     }
136 
137     @Override
138     public final Set<String> getPrefixesUsed(final boolean appendDelimiter) {
139         return insertClausePane.getPrefixesUsed(appendDelimiter);
140     }
141 
142     @Override
143     public final void save(final Element root) {
144         Saveable.defaultSave(root, insertClausePane);
145     }
146 
147     @Override
148     public final List<SubSelectPane> getSubSelectPanes() {
149         return Collections.emptyList();
150     }
151 
152     @Override
153     protected final String getXMLQueryType() {
154         return XML_QUERY_TYPE_VALUE;
155     }
156 
157 }