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.delete;
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.*;
41  import static cz.zcu.mre.sparkle.tools.sparqlParser.SparqlParserUtils.getTreeChild;
42  
43  /**
44   * The panel controller for a DELETE query.
45   *
46   * @author Josef Kazak
47   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
48   */
49  public final class DeleteQueryFormPane
50          extends QueryFormPane<Boolean> {
51  
52      public static final String XML_QUERY_TYPE_VALUE = "delete"; //$NON-NLS-1$
53  
54      private GroupGraphPatternPane deleteClausePane;
55  
56      public DeleteQueryFormPane(final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
57              String paneType) {
58  
59          super(mainForm, dataAgent, localStorage, paneType);
60      }
61  
62      public DeleteQueryFormPane(final MainForm mainForm, final DataAgent dataAgent, final Storage localStorage,
63              final Object root, String paneType)
64              throws Saveable.LoadException {
65  
66          super(mainForm, dataAgent, localStorage, root, paneType);
67      }
68  
69      @Override
70      public final void load(final Object root) throws Saveable.LoadException {
71          ObservableSet<String> deleteClauseVariables = FXCollections.observableSet();
72  
73          deleteClausePane = new DeleteClausePane(this);
74          deleteClausePane.addVariablesCollector(createCollector(deleteClauseVariables));
75  
76          final ObservableList<Node> children = getChildren();
77          children.add(new CustomTitledPane(Messages.getString("DELETE_QUERY_FRAGMENT"), deleteClausePane)); //$NON-NLS-1$
78  
79          if (root != null) {
80              if (root instanceof Element) {
81                  // Loading of a saved query
82                  Element rootNode = (Element) root;
83                  loadElement(rootNode, deleteClausePane);
84              } else if (root instanceof ParseTree) {
85                  setVariables((ParseTree) root);
86              }
87          }
88  
89          watch(deleteClausePane);
90      }
91  
92      @Override
93      protected void setVariables(ParseTree root) throws Saveable.LoadException {
94          Stack<SparqlParserNode> nodeStack = new Stack<>(); //current path in parse tree
95          nodeStack.push(new SparqlParserNode(root));
96  
97          while (!nodeStack.isEmpty()) {
98              SparqlParserNode currentNode = nodeStack.pop();
99              ParseTree nodeData = currentNode.getNodeData();
100 
101             if (nodeData instanceof SparqlParser.QuadDataContext) { //where clause
102                 new SparqlParserWhereClauseWalker(nodeData, deleteClausePane)
103                         .processWhereClauseDFS();
104             } else { //uninsteresting node -> get next node
105                 nodeStack.push(currentNode);
106                 getTreeChild(currentNode, nodeStack);
107             }
108         }
109     }
110 
111     @Override
112     public final String getQueryPart() {
113         final StringBuilder sb = new StringBuilder();
114         String deleteClausePaneValue = deleteClausePane.getQueryPart();
115         sb.append("DELETE DATA "); //$NON-NLS-1$
116 
117         if (!deleteClausePaneValue.startsWith("{")) { //$NON-NLS-1$
118             sb.append("{ "); //$NON-NLS-1$
119         }
120         sb.append(deleteClausePaneValue);
121         if (!deleteClausePaneValue.startsWith("{")) { //$NON-NLS-1$
122             sb.append("}"); //$NON-NLS-1$
123         }
124         return sb.toString();
125     }
126 
127     @Override
128     public final List<SubSelectPane> getSubSelectPanes() {
129         return Collections.emptyList();
130     }
131 
132     @Override
133     public final Set<String> getPrefixesUsed(final boolean appendDelimiter) {
134         return deleteClausePane.getPrefixesUsed(appendDelimiter);
135     }
136 
137     @Override
138     public final void save(final Element root) {
139         Saveable.defaultSave(root, deleteClausePane);
140     }
141 
142     @Override
143     protected final String getXMLQueryType() {
144         return XML_QUERY_TYPE_VALUE;
145     }
146 
147 }