PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/IncrementalMaintenanceOfRDFViews/src/rdb2rdfmappingbuilder/MainController.java

https://github.com/luiseufrasio/rdb2rdfMB
Java | 1036 lines | 851 code | 113 blank | 72 comment | 165 complexity | 93e1460b673c1b1c568aeaae75618b26 MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package rdb2rdfmappingbuilder;
  6. import br.ufc.mcc.arida.rdb2rdfmb.db.DbConnection;
  7. import br.ufc.mcc.arida.rdb2rdfmb.mapping.R2RMLGen;
  8. import br.ufc.mcc.arida.rdb2rdfmb.mapping.ViewsGen;
  9. import br.ufc.mcc.arida.rdb2rdfmb.model.CA;
  10. import br.ufc.mcc.arida.rdb2rdfmb.model.CCA;
  11. import br.ufc.mcc.arida.rdb2rdfmb.model.MappingConfiguration;
  12. import br.ufc.mcc.arida.rdb2rdfmb.model.MappingConfigurationEntry;
  13. import br.ufc.mcc.arida.rdb2rdfmb.sqlite.dao.MappingConfigurationDAO;
  14. import com.hp.hpl.jena.ontology.DatatypeProperty;
  15. import com.hp.hpl.jena.ontology.ObjectProperty;
  16. import com.hp.hpl.jena.ontology.OntClass;
  17. import com.hp.hpl.jena.ontology.OntModel;
  18. import com.hp.hpl.jena.ontology.OntModelSpec;
  19. import com.hp.hpl.jena.rdf.model.ModelFactory;
  20. import com.hp.hpl.jena.util.iterator.ExtendedIterator;
  21. import de.fuberlin.wiwiss.d2rq.algebra.Attribute;
  22. import de.fuberlin.wiwiss.d2rq.algebra.RelationName;
  23. import de.fuberlin.wiwiss.d2rq.dbschema.DatabaseSchemaInspector;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.IOException;
  28. import java.net.URL;
  29. import java.sql.SQLException;
  30. import java.util.Collection;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.ResourceBundle;
  34. import java.util.logging.Level;
  35. import java.util.logging.Logger;
  36. import javafx.collections.FXCollections;
  37. import javafx.collections.ObservableList;
  38. import javafx.event.ActionEvent;
  39. import javafx.fxml.FXML;
  40. import javafx.fxml.FXMLLoader;
  41. import javafx.fxml.Initializable;
  42. import javafx.scene.Scene;
  43. import javafx.scene.control.Button;
  44. import javafx.scene.control.TableColumn;
  45. import javafx.scene.control.TableView;
  46. import javafx.scene.control.TreeItem;
  47. import javafx.scene.control.TreeView;
  48. import javafx.scene.control.cell.PropertyValueFactory;
  49. import javafx.scene.layout.AnchorPane;
  50. import javafx.stage.Modality;
  51. import javafx.stage.Stage;
  52. import javafx.stage.StageStyle;
  53. import javax.swing.JOptionPane;
  54. import br.ufc.mcc.arida.rdb2rdfmb.model.Class_;
  55. import br.ufc.mcc.arida.rdb2rdfmb.model.DCA;
  56. import br.ufc.mcc.arida.rdb2rdfmb.model.DataProperty;
  57. import br.ufc.mcc.arida.rdb2rdfmb.model.Fk;
  58. import br.ufc.mcc.arida.rdb2rdfmb.model.OCA;
  59. import br.ufc.mcc.arida.rdb2rdfmb.model.ObjProperty;
  60. import br.ufc.mcc.arida.rdb2rdfmb.model.PCA;
  61. import com.hp.hpl.jena.ontology.MaxCardinalityRestriction;
  62. import com.hp.hpl.jena.ontology.OntProperty;
  63. import com.hp.hpl.jena.ontology.OntResource;
  64. import com.hp.hpl.jena.ontology.Restriction;
  65. import d2rq.server;
  66. import de.fuberlin.wiwiss.d2rq.algebra.Join;
  67. import java.awt.Desktop;
  68. import java.io.PrintStream;
  69. import java.net.URI;
  70. import java.sql.Connection;
  71. import java.sql.Statement;
  72. import java.util.ArrayList;
  73. import java.util.Iterator;
  74. import javafx.event.EventHandler;
  75. import javafx.scene.Node;
  76. import javafx.scene.control.Label;
  77. import javafx.scene.control.ListView;
  78. import javafx.scene.control.RadioButton;
  79. import javafx.scene.control.SelectionMode;
  80. import javafx.scene.control.SplitPane;
  81. import javafx.scene.control.TabPane;
  82. import javafx.scene.control.TextField;
  83. import javafx.scene.control.ToggleGroup;
  84. import javafx.scene.image.Image;
  85. import javafx.scene.image.ImageView;
  86. import javafx.scene.input.KeyCode;
  87. import javafx.scene.input.KeyEvent;
  88. import javafx.scene.input.MouseButton;
  89. import javafx.scene.input.MouseEvent;
  90. import javafx.scene.web.WebView;
  91. import org.apache.commons.io.FileUtils;
  92. /**
  93. *
  94. * @author Luis
  95. */
  96. public class MainController implements Initializable {
  97. @FXML // fx:id="dbTree"
  98. private TreeView<String> dbTree;
  99. @FXML // fx:id="ontoTree"
  100. protected TreeView<String> ontoTree;
  101. @FXML
  102. private TreeView<String> expOntoTree;
  103. @FXML
  104. protected TextField txtAssertion;
  105. StringBuilder views = new StringBuilder("");
  106. List<String> listViews = new ArrayList<>();
  107. final ToggleGroup group = new ToggleGroup();
  108. @FXML
  109. RadioButton rbtRelViews;
  110. @FXML
  111. RadioButton rbtR2rmlViews;
  112. @FXML
  113. Label lblAssertion;
  114. @FXML
  115. TextField txtAssertionExp;
  116. @FXML
  117. Button newMapping;
  118. @FXML
  119. Button edit;
  120. @FXML
  121. Button delete;
  122. @FXML
  123. Button createR2rml;
  124. @FXML
  125. Button genExpOntoTree;
  126. @FXML
  127. Button btnPublishData;
  128. @FXML
  129. TableView<MappingConfigurationEntry> mcTable;
  130. @FXML
  131. TableColumn itemOntoName;
  132. @FXML
  133. TableColumn itemDbName;
  134. @FXML
  135. ListView<CA> assertionsList;
  136. @FXML
  137. TabPane tabPane;
  138. @FXML
  139. WebView r2rmlContent;
  140. @FXML
  141. WebView sqlViews;
  142. public static final Stage secondaryStage = new Stage(StageStyle.UTILITY);
  143. public static final Stage newFilterStage = new Stage(StageStyle.UTILITY);
  144. public static MainController m;
  145. ObservableList<MappingConfigurationEntry> dataMc = FXCollections.observableArrayList();
  146. ObservableList<CA> dataAssertions = FXCollections.observableArrayList();
  147. MappingConfigurationDAO mcDAO = new MappingConfigurationDAO();
  148. protected HashMap<TreeItem, CA> assertions = new HashMap<>();
  149. HashMap<TreeItem, CA> assertionsExp = new HashMap<>();
  150. HashMap<String, Class_> classes = new HashMap<>();
  151. HashMap<TreeItem, Object> dbMap = new HashMap<>();
  152. HashMap<String, List<Attribute>> mapTableCols = new HashMap<>();
  153. HashMap<String, List<Join>> mapTableFks = new HashMap<>();
  154. HashMap<String, List<Join>> mapTableFksInv = new HashMap<>();
  155. HashMap<String, Fk> mapFks = new HashMap<>();
  156. HashMap<String, String> mapPrefixes = new HashMap<>();
  157. String r2rml;
  158. MappingConfiguration mc = null;
  159. @Override
  160. public void initialize(URL url, ResourceBundle rb) {
  161. assert newMapping != null : "fx:id=\"newMapping\" was not injected: check your FXML file 'main.fxml'.";
  162. m = this;
  163. // rbtR2rmlViews.setToggleGroup(group);
  164. // rbtRelViews.setToggleGroup(group);
  165. itemDbName.setCellValueFactory(new PropertyValueFactory<MappingConfigurationEntry, String>("databaseAlias"));
  166. itemOntoName.setCellValueFactory(new PropertyValueFactory<MappingConfigurationEntry, String>("ontologyAlias"));
  167. mcTable.setItems(dataMc);
  168. assertionsList.setItems(dataAssertions);
  169. if (dataAssertions.size() == 0) {
  170. tabPane.getTabs().get(1).setDisable(true);
  171. tabPane.getTabs().get(2).setDisable(true);
  172. tabPane.getTabs().get(3).setDisable(true);
  173. tabPane.getTabs().get(4).setDisable(true);
  174. tabPane.getSelectionModel().select(0);
  175. }
  176. final ObservableList<MappingConfigurationEntry> tableSelection = mcTable.getSelectionModel().getSelectedItems();
  177. tratarEventoMcTable();
  178. tratarEventoOntoTree();
  179. tratarEventoExpOntoTree();
  180. tratarEventoDbTree();
  181. tratarEventoAssertionsList();
  182. try {
  183. List<MappingConfiguration> listMc = mcDAO.findAll();
  184. for (MappingConfiguration mappingConfiguration : listMc) {
  185. dataMc.add(new MappingConfigurationEntry(mappingConfiguration));
  186. }
  187. } catch (SQLException ex) {
  188. Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
  189. }
  190. try {
  191. AnchorPane page = (AnchorPane) FXMLLoader.load(Rdb2RdfMappingBuilder.class.getResource("NewMapping.fxml"));
  192. Scene scene = new Scene(page);
  193. secondaryStage.setScene(scene);
  194. secondaryStage.initModality(Modality.APPLICATION_MODAL);
  195. secondaryStage.initOwner(Rdb2RdfMappingBuilder.pStage);
  196. AnchorPane page2 = (AnchorPane) FXMLLoader.load(Rdb2RdfMappingBuilder.class.getResource("NewFilter.fxml"));
  197. Scene sceneFilter = new Scene(page2);
  198. newFilterStage.setScene(sceneFilter);
  199. newFilterStage.initModality(Modality.APPLICATION_MODAL);
  200. newFilterStage.initOwner(Rdb2RdfMappingBuilder.pStage);
  201. } catch (IOException ex) {
  202. Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
  203. }
  204. System.out.println(this.getClass().getSimpleName() + ".initialize");
  205. }
  206. public void buildOntoTree(MappingConfiguration mc) {
  207. try {
  208. loadOntology(mc);
  209. // Crio o nó pai que será o nome da ontologia
  210. TreeItem<String> ontoRoot = new TreeItem<>(mc.getOntologyAlias());
  211. // Crio nós filhos com os nomes das classes
  212. Collection<Class_> cClasses = classes.values();
  213. for (Class_ class_ : cClasses) {
  214. if (class_.getName() == null) {
  215. continue;
  216. }
  217. Node classIcon = new ImageView(
  218. new Image(getClass().getResourceAsStream("img/ontology/class.gif")));
  219. TreeItem<String> item = new TreeItem<>(class_.toString(), classIcon);
  220. CCA cca = new CCA();
  221. cca.setClass_(class_);
  222. assertions.put(item, cca);
  223. Class_ superClass = class_.getSuperClass();
  224. if (superClass != null) {
  225. /* Cria os data properties da super classe */
  226. for (int i = 0; i < superClass.getdProperties().size(); i++) {
  227. createDpItem(item, superClass.getdProperties().get(i), cca);
  228. }
  229. }
  230. /* Cria os data properties da classe */
  231. for (int i = 0; i < class_.getdProperties().size(); i++) {
  232. createDpItem(item, class_.getdProperties().get(i), cca);
  233. }
  234. if (superClass != null) {
  235. /* Cria os object properties da super classe */
  236. for (int i = 0; i < superClass.getoProperties().size(); i++) {
  237. createOpItem(item, superClass.getoProperties().get(i), cca);
  238. }
  239. }
  240. /* Cria os object properties da classe */
  241. for (int i = 0; i < class_.getoProperties().size(); i++) {
  242. createOpItem(item, class_.getoProperties().get(i), cca);
  243. }
  244. ontoRoot.getChildren().add(item);
  245. }
  246. // Insiro o nó raiz na TreeView
  247. ontoTree.setRoot(ontoRoot);
  248. ontoTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
  249. } catch (FileNotFoundException ex) {
  250. Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
  251. }
  252. }
  253. public void buildDBTree(MappingConfiguration mc) {
  254. /* Crio o nó pai que será o nome do banco */
  255. TreeItem<String> dataBase = new TreeItem<>(mc.getDatabaseAlias());
  256. dataBase.setExpanded(false);
  257. DatabaseSchemaInspector schema = DbConnection.getSchemaInspector(mc.getDatabaseDriver(), mc.getDatabaseUrl(), mc.getDatabaseUser(), mc.getDatabasePassword());
  258. List<RelationName> tables = schema.listTableNames(null);
  259. for (RelationName relationName : tables) {
  260. Node tableIcon = new ImageView(
  261. new Image(getClass().getResourceAsStream("img/database/table.gif")));
  262. // Primeira maiúscula
  263. //String tableName = relationName.tableName().substring(0,1).toUpperCase();
  264. //tableName += relationName.tableName().substring(1);
  265. String tableName = relationName.tableName();
  266. TreeItem<String> treeItem = new TreeItem<>(tableName, tableIcon);
  267. dataBase.getChildren().add(treeItem);
  268. dbMap.put(treeItem, relationName);
  269. List<Attribute> listCols = schema.listColumns(relationName);
  270. for (Attribute attribute : listCols) {
  271. TreeItem<String> colItem = new TreeItem<>(attribute.attributeName());
  272. List<Attribute> listPk = schema.primaryKeyColumns(relationName);
  273. if (isIn(attribute, listPk)) {
  274. Node pkIcon = new ImageView(
  275. new Image(getClass().getResourceAsStream("img/database/pk.gif")));
  276. colItem.setGraphic(pkIcon);
  277. } else {
  278. Node attIcon = new ImageView(
  279. new Image(getClass().getResourceAsStream("img/database/attribute.gif")));
  280. colItem.setGraphic(attIcon);
  281. }
  282. treeItem.getChildren().add(colItem);
  283. dbMap.put(colItem, attribute);
  284. }
  285. mapTableCols.put(relationName.tableName().toLowerCase(), listCols);
  286. List<Join> listFks0 = schema.foreignKeys(relationName, 0);
  287. for (Join fk0 : listFks0) {
  288. String fkName = "fk0_" + fk0.table1() + "2" + fk0.table2();
  289. if (!mapFks.keySet().contains(fkName)) {
  290. mapFks.put(fkName, new Fk(false, fk0));
  291. }
  292. Node fkIcon = new ImageView(
  293. new Image(getClass().getResourceAsStream("img/database/fk.gif")));
  294. TreeItem<String> fk0Item = new TreeItem<>(fkName, fkIcon);
  295. treeItem.getChildren().add(fk0Item);
  296. dbMap.put(fk0Item, fk0);
  297. }
  298. mapTableFks.put(relationName.tableName(), listFks0);
  299. List<Join> listFks1 = schema.foreignKeys(relationName, 1);
  300. for (Join fk1 : listFks1) {
  301. String fkName;
  302. fkName = "fk1_" + fk1.table1() + "2" + fk1.table2();
  303. if (!mapFks.keySet().contains(fkName)) {
  304. mapFks.put(fkName, new Fk(true, fk1));
  305. }
  306. Node fkIcon = new ImageView(
  307. new Image(getClass().getResourceAsStream("img/database/fkInv.gif")));
  308. TreeItem<String> fk1Item = new TreeItem<>(fkName, fkIcon);
  309. treeItem.getChildren().add(fk1Item);
  310. dbMap.put(fk1Item, fk1);
  311. }
  312. mapTableFksInv.put(relationName.tableName(), listFks1);
  313. }
  314. // Insiro o nó raiz na TreeView
  315. dbTree.setRoot(dataBase);
  316. dbTree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  317. }
  318. private void loadOntology(MappingConfiguration mc)
  319. throws FileNotFoundException {
  320. OntModel ontModel = ModelFactory
  321. .createOntologyModel(OntModelSpec.OWL_MEM);
  322. String filePath = mc.getOntologyFilePath();
  323. if (filePath != null && !"".equals(filePath)) {
  324. ontModel.read(new FileInputStream(new File(filePath)),
  325. "http://ufc.br/rdb2rdfmb/", mc.getOntologyLang());
  326. } else {
  327. ontModel.read(mc.getOntologyURL(), mc.getOntologyLang());
  328. }
  329. ExtendedIterator<OntClass> i = ontModel.listClasses();
  330. while (i.hasNext()) {
  331. OntClass ontClass = (OntClass) i.next();
  332. String prefix = ontModel.getNsURIPrefix(ontClass.getNameSpace());
  333. String name = ontClass.getLocalName();
  334. if (classes.get(name + prefix) == null) {
  335. Class_ c = new Class_(prefix, name);
  336. classes.put(prefix + name, c);
  337. mapPrefixes.put(prefix, ontClass.getNameSpace());
  338. OntClass superClass = ontClass.getSuperClass();
  339. if (superClass != null) {
  340. String superName = superClass.getLocalName();
  341. String superPrefix = ontModel.getNsURIPrefix(superClass.getNameSpace());
  342. Class_ superC = classes.get(superPrefix + superName);
  343. if (superC == null) {
  344. superC = new Class_(superPrefix, superName);
  345. classes.put(superPrefix + superName, superC);
  346. mapPrefixes.put(superPrefix, superClass.getNameSpace());
  347. }
  348. c.setSuperClass(superC);
  349. }
  350. }
  351. }
  352. ExtendedIterator<DatatypeProperty> i2 = ontModel
  353. .listDatatypeProperties();
  354. while (i2.hasNext()) {
  355. DatatypeProperty datatypeProperty = (DatatypeProperty) i2.next();
  356. if (datatypeProperty.getDomain() != null && datatypeProperty.getRange() != null) {
  357. ExtendedIterator iDomains = datatypeProperty.listDomain();
  358. while (iDomains.hasNext()) {
  359. OntClass cDomain = (OntClass) iDomains.next();
  360. String dClassPrefix = ontModel.getNsURIPrefix(cDomain.getNameSpace());
  361. String dClassName = cDomain.getLocalName();
  362. Class_ dClass = classes.get(dClassPrefix + dClassName);
  363. String dpName = datatypeProperty.getLocalName();
  364. String dpPrefix = ontModel.getNsURIPrefix(datatypeProperty.getNameSpace());
  365. String rangeName = datatypeProperty.getRange().getLocalName();
  366. DataProperty dp = new DataProperty(dpPrefix, dpName, dClass, rangeName);
  367. if (dClass != null) {
  368. dClass.getdProperties().add(dp);
  369. mapPrefixes.put(dpPrefix, datatypeProperty.getNameSpace());
  370. }
  371. }
  372. }
  373. }
  374. ExtendedIterator<com.hp.hpl.jena.ontology.ObjectProperty> i3 = ontModel.listObjectProperties();
  375. while (i3.hasNext()) {
  376. ObjectProperty objectProperty = (ObjectProperty) i3.next();
  377. if (objectProperty.getDomain() != null && objectProperty.getRange() != null) {
  378. String dClassPrefix = ontModel.getNsURIPrefix(objectProperty.getDomain().getNameSpace());
  379. String dClassName = objectProperty.getDomain().getLocalName();
  380. String rClassPrefix = ontModel.getNsURIPrefix(objectProperty.getRange().getNameSpace());
  381. String rClassName = objectProperty.getRange().getLocalName();
  382. String opName = objectProperty.getLocalName();
  383. String opPrefix = ontModel.getNsURIPrefix(objectProperty.getNameSpace());
  384. Class_ dClass = classes.get(dClassPrefix + dClassName);
  385. Class_ rClass = classes.get(rClassPrefix + rClassName);
  386. ObjProperty op = new ObjProperty(opPrefix, opName, dClass, rClass);
  387. if (dClass != null) {
  388. dClass.getoProperties().add(op);
  389. mapPrefixes.put(opPrefix, objectProperty.getNameSpace());
  390. }
  391. }
  392. }
  393. ExtendedIterator<Restriction> iRest = ontModel.listRestrictions();
  394. while (iRest.hasNext()) {
  395. Restriction r = (Restriction) iRest.next();
  396. if (r.isMaxCardinalityRestriction()) {
  397. MaxCardinalityRestriction mcr = r.asMaxCardinalityRestriction();
  398. System.out.println(mcr.getMaxCardinality());
  399. OntClass ontClass = mcr.getSubClass();
  400. OntProperty p = mcr.getOnProperty();
  401. String prefix = ontModel.getNsURIPrefix(ontClass.getNameSpace());
  402. String name = ontClass.getLocalName();
  403. Class_ c = classes.get(prefix + name);
  404. if (p.isObjectProperty()) {
  405. ObjectProperty objectProperty = p.asObjectProperty();
  406. String opName = objectProperty.getLocalName();
  407. String opPrefix = ontModel.getNsURIPrefix(objectProperty.getNameSpace());
  408. for (ObjProperty objProperty : c.getoProperties()) {
  409. if (objProperty.getPrefix().equals(opPrefix) && objProperty.getName().equals(opName)) {
  410. objProperty.setMaxCardinality(mcr.getMaxCardinality());
  411. }
  412. }
  413. } else {
  414. DatatypeProperty datatypeProperty = p.asDatatypeProperty();
  415. String dpName = datatypeProperty.getLocalName();
  416. String dpPrefix = ontModel.getNsURIPrefix(datatypeProperty.getNameSpace());
  417. for (DataProperty dataProperty : c.getdProperties()) {
  418. if (dataProperty.getPrefix().equals(dpPrefix) && dataProperty.getName().equals(dpName)) {
  419. dataProperty.setMaxCardinality(mcr.getMaxCardinality());
  420. }
  421. }
  422. }
  423. }
  424. }
  425. }
  426. private void updateDeleteButtonState() {
  427. boolean disable = true;
  428. if (delete != null && mcTable != null) {
  429. final boolean nothingSelected = mcTable.getSelectionModel().getSelectedItems().isEmpty();
  430. disable = nothingSelected;
  431. }
  432. if (delete != null) {
  433. delete.setDisable(disable);
  434. }
  435. }
  436. private void updateEditButtonState() {
  437. boolean disable = true;
  438. if (edit != null && mcTable != null) {
  439. final boolean nothingSelected = mcTable.getSelectionModel().getSelectedItems().isEmpty();
  440. disable = nothingSelected;
  441. }
  442. if (edit != null) {
  443. edit.setDisable(disable);
  444. }
  445. }
  446. /**
  447. * Called when the New button is fired.
  448. *
  449. * @param event the action event.
  450. */
  451. public void newMappingFired(ActionEvent event) throws IOException {
  452. NewMappingController.nm.ontoAlias.setText("");
  453. NewMappingController.nm.ontoAlias.setDisable(false);
  454. NewMappingController.nm.dbAlias.setText("");
  455. NewMappingController.nm.dbAlias.setDisable(false);
  456. NewMappingController.nm.filePath.setText("");
  457. NewMappingController.nm.ontoUrl.setText("");
  458. NewMappingController.nm.passwd.setText("");
  459. NewMappingController.nm.url.setText("");
  460. NewMappingController.nm.user.setText("");
  461. NewMappingController.nm.comboDrivers.setValue("");
  462. secondaryStage.setTitle("Config a new Mapping");
  463. secondaryStage.show();
  464. }
  465. /**
  466. * Called when the Add Filter button is fired.
  467. *
  468. * @param event the action event.
  469. */
  470. public void addFilterFired(ActionEvent event) throws IOException {
  471. CCA cca = (CCA) assertions.get(ontoTree.getSelectionModel().getSelectedItem());
  472. NewFilterController.nm.lblTable.setText("Table: " + cca.getRelationName());
  473. DatabaseSchemaInspector schema = DbConnection.getSchemaInspector(mc.getDatabaseDriver(), mc.getDatabaseUrl(), mc.getDatabaseUser(), mc.getDatabasePassword());
  474. RelationName relationName = new RelationName(null, cca.getRelationName());
  475. List<Attribute> listCols = schema.listColumns(relationName);
  476. final ObservableList<String> items = NewFilterController.nm.cbxColumn.getItems();
  477. items.clear();
  478. for (Attribute attribute : listCols) {
  479. items.add(attribute.attributeName() + " [" + schema.columnType(attribute).name() + "]");
  480. }
  481. newFilterStage.setTitle("Add a Selection Filter");
  482. newFilterStage.show();
  483. }
  484. /**
  485. * Called when the Edit button is fired.
  486. *
  487. * @param event the action event.
  488. */
  489. public void editMappingFired(ActionEvent event) throws IOException {
  490. final MappingConfigurationEntry selectedItem = mcTable.getSelectionModel().getSelectedItem();
  491. if (selectedItem == null) {
  492. JOptionPane.showMessageDialog(null, "Select a mapping configuration to be edited", "Info", JOptionPane.INFORMATION_MESSAGE);
  493. return;
  494. }
  495. secondaryStage.setTitle("Edit a Mapping");
  496. secondaryStage.show();
  497. try {
  498. MappingConfiguration mc = mcDAO.findById(selectedItem.getId());
  499. NewMappingController.nm.ontoAlias.setText(mc.getOntologyAlias());
  500. NewMappingController.nm.ontoAlias.setDisable(true);
  501. NewMappingController.nm.dbAlias.setText(mc.getDatabaseAlias());
  502. NewMappingController.nm.dbAlias.setDisable(true);
  503. NewMappingController.nm.filePath.setText(mc.getOntologyFilePath());
  504. NewMappingController.nm.ontoUrl.setText(mc.getOntologyURL());
  505. NewMappingController.nm.comboOntoLangs.setValue(mc.getOntologyLang());
  506. NewMappingController.nm.passwd.setText(mc.getDatabasePassword());
  507. NewMappingController.nm.url.setText(mc.getDatabaseUrl());
  508. NewMappingController.nm.user.setText(mc.getDatabaseUser());
  509. NewMappingController.nm.comboDrivers.setValue(mc.getDatabaseDriver());
  510. } catch (SQLException ex) {
  511. Logger.getLogger(MainController.class
  512. .getName()).log(Level.SEVERE, null, ex);
  513. }
  514. }
  515. /**
  516. * Called when the Delete button is fired.
  517. *
  518. * @param event the action event.
  519. */
  520. public void deleteMappingFired(ActionEvent event) throws IOException {
  521. final MappingConfigurationEntry selectedItem = mcTable.getSelectionModel().getSelectedItem();
  522. if (selectedItem == null) {
  523. JOptionPane.showMessageDialog(null, "Select a mapping configuration to be deleted", "Info", JOptionPane.INFORMATION_MESSAGE);
  524. return;
  525. }
  526. int answer = JOptionPane.showConfirmDialog(null, "The mapping selected will be DELETED. Are you sure?", "Confirmation Question", JOptionPane.OK_CANCEL_OPTION);
  527. if (answer == 0) {
  528. try {
  529. mcDAO.delete(selectedItem.getId());
  530. dataMc.remove(selectedItem);
  531. JOptionPane.showMessageDialog(null, "Mapping Configuration Deleted!", "Success", JOptionPane.INFORMATION_MESSAGE);
  532. } catch (SQLException ex) {
  533. Logger.getLogger(MainController.class
  534. .getName()).log(Level.SEVERE, null, ex);
  535. }
  536. }
  537. }
  538. public static void addRowMcTable(MappingConfigurationEntry mc) {
  539. m.dataMc.add(mc);
  540. }
  541. /**
  542. * Called when the Save button is fired.
  543. *
  544. * @param event the action event.
  545. */
  546. public void saveAssertionFired(ActionEvent event) throws IOException {
  547. final TreeItem selectedItem = ontoTree.getSelectionModel().getSelectedItem();
  548. CA ca = assertions.get(selectedItem);
  549. int pos = dataAssertions.indexOf(ca);
  550. if (pos >= 0) {
  551. dataAssertions.remove(pos);
  552. }
  553. dataAssertions.add(ca);
  554. tabPane.getTabs().get(1).setDisable(false);
  555. tabPane.getSelectionModel().select(1);
  556. assertionsList.getSelectionModel().select(ca);
  557. createR2rml.setDisable(false);
  558. genExpOntoTree.setDisable(false);
  559. }
  560. /**
  561. * Called when the PublishR2RML button is fired.
  562. *
  563. * @param event the action event.
  564. */
  565. public void publishR2rmlFired(ActionEvent event) throws IOException, Exception {
  566. // Mostrar uma mensagem indicando a criação das procedures no banco
  567. }
  568. /**
  569. * Called when the genExpOnto button is fired.
  570. *
  571. * @param event the action event.
  572. */
  573. public void genExpOntoFired(ActionEvent event) throws IOException, Exception {
  574. tabPane.getTabs().get(2).setDisable(false);
  575. tabPane.getSelectionModel().select(2);
  576. // Crio o nó pai que será o nome da ontologia
  577. TreeItem<String> ontoRoot = new TreeItem<>(mc.getOntologyAlias());
  578. for (CA ca : assertionsList.getItems()) {
  579. if (ca instanceof CCA) {
  580. CCA cca = (CCA) ca;
  581. Class_ class_ = cca.getClass_();
  582. Node classIcon = new ImageView(
  583. new Image(getClass().getResourceAsStream("img/ontology/class.gif")));
  584. TreeItem<String> item = new TreeItem<>(class_.toString(), classIcon);
  585. assertionsExp.put(item, cca);
  586. for (DCA dca : cca.getDcaList()) {
  587. if (assertionsList.getItems().contains(dca)) {
  588. Node datatypePIcon = new ImageView(
  589. new Image(getClass().getResourceAsStream("img/ontology/datatypeP.gif")));
  590. TreeItem<String> subItem = new TreeItem<>(dca.getdProperty().toString(), datatypePIcon);
  591. item.getChildren().add(subItem);
  592. assertionsExp.put(subItem, dca);
  593. }
  594. }
  595. for (OCA oca : cca.getOcaList()) {
  596. if (assertionsList.getItems().contains(oca)) {
  597. Node objectPIcon = new ImageView(
  598. new Image(getClass().getResourceAsStream("img/ontology/objectP.gif")));
  599. TreeItem<String> subItem = new TreeItem<>(oca.getoProperty().toString(), objectPIcon);
  600. item.getChildren().add(subItem);
  601. assertionsExp.put(subItem, oca);
  602. }
  603. }
  604. ontoRoot.getChildren().add(item);
  605. }
  606. }
  607. // Insiro o nó raiz na TreeView
  608. expOntoTree.setRoot(ontoRoot);
  609. expOntoTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
  610. }
  611. /**
  612. * Called when the CreateR2RML button is fired.
  613. *
  614. * @param event the action event.
  615. */
  616. public void createSqlViewsFired(ActionEvent event) throws IOException, Exception {
  617. tabPane.getTabs().get(3).setDisable(false);
  618. tabPane.getSelectionModel().select(3);
  619. sqlViews.getEngine()
  620. .loadContent("<pre>" + "INCLUIR AQUI AS TRIGGERS" + "</pre>");
  621. }
  622. /**
  623. * Called when the CreateR2RML button is fired.
  624. *
  625. * @param event the action event.
  626. */
  627. public void createR2rmlViewsFired(ActionEvent event) throws IOException, Exception {
  628. tabPane.getTabs().get(4).setDisable(false);
  629. tabPane.getSelectionModel().select(4);
  630. r2rmlContent.getEngine()
  631. .loadContent("<pre>" + "INCLUIR AQUI AS PROCEDURES" + "</pre>");
  632. }
  633. private void tratarEventoMcTable() {
  634. mcTable.setOnMouseClicked(new EventHandler<MouseEvent>() {
  635. @Override
  636. public void handle(MouseEvent mouseEvent) {
  637. if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
  638. updateDeleteButtonState();
  639. updateEditButtonState();
  640. if (mouseEvent.getClickCount() == 2) {
  641. final MappingConfigurationEntry selectedItem = mcTable.getSelectionModel().getSelectedItem();
  642. if (selectedItem != null) {
  643. if (mc != null && selectedItem.getId() == mc.getId()) {
  644. buildDBTree(mc);
  645. buildOntoTree(mc);
  646. tabPane.getSelectionModel().select(0);
  647. } else {
  648. try {
  649. mc = mcDAO.findById(selectedItem.getId());
  650. dataAssertions.clear();
  651. createR2rml.setDisable(true);
  652. tabPane.getTabs().get(2).setDisable(true);
  653. dbTree.setRoot(null);
  654. ontoTree.setRoot(null);
  655. assertions.clear();
  656. mapTableFks.clear();
  657. mapTableFksInv.clear();
  658. mapFks.clear();
  659. txtAssertion.setText("");
  660. lblAssertion.setText("Correspondence Assertion (CA):");
  661. assertions.clear();
  662. classes.clear();
  663. dbMap.clear();
  664. mapTableCols.clear();
  665. mapPrefixes.clear();
  666. buildDBTree(mc);
  667. buildOntoTree(mc);
  668. tabPane.getTabs().get(1).setDisable(true);
  669. tabPane.getTabs().get(2).setDisable(true);
  670. tabPane.getTabs().get(3).setDisable(true);
  671. tabPane.getTabs().get(4).setDisable(true);
  672. tabPane.getSelectionModel().select(0);
  673. } catch (SQLException ex) {
  674. Logger.getLogger(MainController.class
  675. .getName()).log(Level.SEVERE, null, ex);
  676. }
  677. }
  678. }
  679. }
  680. }
  681. }
  682. });
  683. }
  684. private void tratarEventoOntoTree() {
  685. ontoTree.setOnMouseClicked(new EventHandler<MouseEvent>() {
  686. @Override
  687. public void handle(MouseEvent mouseEvent) {
  688. if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
  689. final TreeItem selectedItem = ontoTree.getSelectionModel().getSelectedItem();
  690. if (selectedItem != null) {
  691. CA ca = assertions.get(selectedItem);
  692. txtAssertion.setText(ca.toString());
  693. if (ca instanceof CCA) {
  694. lblAssertion.setText("Class Correspondence Assertion (CCA):");
  695. } else if (ca instanceof DCA) {
  696. lblAssertion.setText("Datatype property Correspondence Assertion (DCA):");
  697. } else if (ca instanceof OCA) {
  698. lblAssertion.setText("Object property Correspondence Assertion (OCA):");
  699. } else {
  700. lblAssertion.setText("Correspondence Assertion (CA):");
  701. }
  702. } else {
  703. txtAssertion.setText("");
  704. lblAssertion.setText("Correspondence Assertion (CA):");
  705. }
  706. if (mouseEvent.getClickCount() == 2) {
  707. //TODO
  708. }
  709. }
  710. }
  711. });
  712. }
  713. private void tratarEventoExpOntoTree() {
  714. expOntoTree.setOnMouseClicked(new EventHandler<MouseEvent>() {
  715. @Override
  716. public void handle(MouseEvent mouseEvent) {
  717. if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
  718. final TreeItem selectedItem = expOntoTree.getSelectionModel().getSelectedItem();
  719. if (selectedItem != null) {
  720. txtAssertionExp.setText(assertionsExp.get(selectedItem).toString());
  721. }
  722. }
  723. }
  724. });
  725. }
  726. private void tratarEventoDbTree() {
  727. dbTree.setOnMouseClicked(new EventHandler<MouseEvent>() {
  728. @Override
  729. public void handle(MouseEvent mouseEvent) {
  730. if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
  731. ObservableList<TreeItem<String>> dbItens = dbTree.getSelectionModel().getSelectedItems();
  732. if (dbItens != null) {
  733. TreeItem ontoItem = ontoTree.getSelectionModel().getSelectedItem();
  734. CA ca = assertions.get(ontoItem);
  735. int iAtt = 0;
  736. for (Iterator<TreeItem<String>> it = dbItens.iterator(); it.hasNext();) {
  737. TreeItem<String> dbItem = it.next();
  738. Object o = dbMap.get(dbItem);
  739. if (o instanceof RelationName) {
  740. RelationName rn = (RelationName) o;
  741. ca.setRelationName(rn.tableName());
  742. if (ca instanceof PCA) {
  743. PCA pca = (PCA) ca;
  744. pca.setFks(new ArrayList<String>());
  745. }
  746. } else if (o instanceof Attribute) {
  747. Attribute att = (Attribute) o;
  748. String txtGrandParent = dbItem.getParent().getParent().getValue();
  749. if (ca instanceof CCA) {
  750. if (!txtGrandParent.startsWith("fk")) {
  751. CCA cca = (CCA) ca;
  752. if (iAtt == 0) {
  753. cca.getAttributes().clear();
  754. iAtt++;
  755. }
  756. if (!cca.getAttributes().contains(att.attributeName())) {
  757. cca.getAttributes().add(att.attributeName());
  758. }
  759. }
  760. } else if (ca instanceof DCA) {
  761. DCA dca = (DCA) ca;
  762. if (iAtt == 0) {
  763. dca.getAttributes().clear();
  764. iAtt++;
  765. }
  766. if (!dca.getAttributes().contains(att.attributeName())) {
  767. dca.getAttributes().add(att.attributeName());
  768. }
  769. }
  770. if (!txtGrandParent.startsWith("fk")) {
  771. ca.setRelationName(dbItem.getParent().getValue());
  772. }
  773. } else if (o instanceof Join) {
  774. if (ca instanceof PCA) {
  775. PCA pca = (PCA) ca;
  776. if (!pca.getFks().contains(dbItem.getValue())) {
  777. if (!pca.getFks().contains(dbItem.getParent().getParent().getValue())) {
  778. pca.getFks().clear();
  779. if (pca instanceof DCA) {
  780. ((DCA) pca).getAttributes().clear();
  781. }
  782. // Adicionando todas as Fks da hierarquia
  783. int i = 0;
  784. List<String> fksHierarquy = new ArrayList<String>();
  785. TreeItem<String> currItem = dbItem;
  786. while (true) {
  787. if (i % 2 == 1) {
  788. if (!currItem.getParent().getValue().startsWith("fk")) {
  789. pca.setRelationName(currItem.getValue());
  790. break;
  791. }
  792. } else {
  793. fksHierarquy.add(currItem.getValue());
  794. }
  795. i++;
  796. currItem = currItem.getParent();
  797. }
  798. i = fksHierarquy.size() - 1;
  799. while (i >= 0) {
  800. pca.getFks().add(fksHierarquy.get(i));
  801. i--;
  802. }
  803. } else {
  804. pca.getFks().add(dbItem.getValue());
  805. }
  806. }
  807. }
  808. if (mouseEvent.getClickCount() == 2 && dbItem.getChildren().size() == 0) {
  809. Join fk = (Join) o;
  810. RelationName refTable = dbItem.getValue().startsWith("fk0") ? fk.table2() : fk.table1();
  811. Node tableIcon = new ImageView(
  812. new Image(getClass().getResourceAsStream("img/database/table.gif")));
  813. TreeItem<String> tableRefItem = new TreeItem<>(refTable.tableName(), tableIcon);
  814. dbItem.getChildren().add(tableRefItem);
  815. List<Attribute> cols = mapTableCols.get(refTable.tableName().toLowerCase());
  816. for (Attribute column : cols) {
  817. Node attIcon = new ImageView(
  818. new Image(getClass().getResourceAsStream("img/database/attribute.gif")));
  819. TreeItem<String> colItem = new TreeItem<>(column.attributeName(), attIcon);
  820. tableRefItem.getChildren().add(colItem);
  821. dbMap.put(colItem, column);
  822. }
  823. List<Join> fks = mapTableFks.get(refTable.tableName());
  824. for (Join join : fks) {
  825. String tName = join.table2().tableName();
  826. TreeItem<String> currItem = tableRefItem;
  827. boolean include = true;
  828. while (currItem != null) {
  829. if (tName.equals(currItem.getValue())) {
  830. include = false;
  831. break;
  832. }
  833. currItem = currItem.getParent();
  834. }
  835. if (include) {
  836. String fkName = "fk0_" + join.table1() + "2" + join.table2();
  837. Node fkIcon = new ImageView(
  838. new Image(getClass().getResourceAsStream("img/database/fk.gif")));
  839. TreeItem<String> fk0Item = new TreeItem<>(fkName, fkIcon);
  840. tableRefItem.getChildren().add(fk0Item);
  841. dbMap.put(fk0Item, join);
  842. }
  843. }
  844. List<Join> fksInv = mapTableFksInv.get(refTable.tableName());
  845. for (Join joinInv : fksInv) {
  846. String tName = joinInv.table1().tableName();
  847. TreeItem<String> currItem = tableRefItem;
  848. boolean include = true;
  849. while (currItem != null) {
  850. if (tName.equals(currItem.getValue())) {
  851. include = false;
  852. break;
  853. }
  854. currItem = currItem.getParent();
  855. }
  856. if (include) {
  857. String fkName = "fk1_" + joinInv.table1() + "2" + joinInv.table2();
  858. Node fkIcon = new ImageView(
  859. new Image(getClass().getResourceAsStream("img/database/fk.gif")));
  860. TreeItem<String> fk1Item = new TreeItem<>(fkName, fkIcon);
  861. tableRefItem.getChildren().add(fk1Item);
  862. dbMap.put(fk1Item, joinInv);
  863. }
  864. }
  865. }
  866. }
  867. }
  868. if (ca != null) {
  869. txtAssertion.setText(ca.toString());
  870. }
  871. }
  872. }
  873. }
  874. });
  875. }
  876. private void tratarEventoAssertionsList() {
  877. assertionsList.setOnKeyReleased(new EventHandler<KeyEvent>() {
  878. @Override
  879. public void handle(KeyEvent t) {
  880. if (t.getCode() == KeyCode.DELETE) {
  881. dataAssertions.remove(assertionsList.getSelectionModel().getSelectedIndex());
  882. if (dataAssertions.size() == 0) {
  883. tabPane.getTabs().get(1).setDisable(true);
  884. tabPane.getTabs().get(2).setDisable(true);
  885. tabPane.getTabs().get(3).setDisable(true);
  886. tabPane.getTabs().get(4).setDisable(true);
  887. tabPane.getSelectionModel().select(0);
  888. }
  889. }
  890. }
  891. });
  892. }
  893. private void createDpItem(TreeItem<String> item, DataProperty dp, CCA cca) {
  894. Node datatypePIcon = new ImageView(
  895. new Image(getClass().getResourceAsStream("img/ontology/datatypeP.gif")));
  896. TreeItem<String> subItem = new TreeItem<>(dp.toString(), datatypePIcon);
  897. item.getChildren().add(subItem);
  898. DCA dca = new DCA();
  899. dca.setdProperty(dp);
  900. assertions.put(subItem, dca);
  901. cca.getDcaList().add(dca);
  902. }
  903. private void createOpItem(TreeItem<String> item, ObjProperty op, CCA cca) {
  904. Node objectPIcon = new ImageView(
  905. new Image(getClass().getResourceAsStream("img/ontology/objectP.gif")));
  906. TreeItem<String> subItem = new TreeItem<>(op.toString(), objectPIcon);
  907. item.getChildren().add(subItem);
  908. OCA oca = new OCA();
  909. oca.setoProperty(op);
  910. assertions.put(subItem, oca);
  911. cca.getOcaList().add(oca);
  912. }
  913. private boolean isIn(Attribute attribute, List<Attribute> listPk) {
  914. String attName = attribute.attributeName().toLowerCase();
  915. for (Attribute attributePk : listPk) {
  916. String attPkName = attributePk.attributeName().toLowerCase();
  917. if (attName.equals(attPkName)) {
  918. return true;
  919. }
  920. }
  921. return false;
  922. }
  923. }