package it.unibz.krdb.obda.protege4.gui.action;

/*
 * #%L
 * ontop-protege4
 * %%
 * Copyright (C) 2009 - 2013 KRDB Research Centre. Free University of Bozen Bolzano.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import it.unibz.krdb.obda.exception.DuplicateMappingException;
import it.unibz.krdb.obda.model.OBDAMappingAxiom;
import it.unibz.krdb.obda.model.OBDAModel;
import it.unibz.krdb.obda.model.impl.OBDAModelImpl;
import it.unibz.krdb.obda.protege4.core.OBDAModelManager;
import it.unibz.krdb.obda.r2rml.R2RMLReader;

import java.awt.event.ActionEvent;
import java.io.File;
import java.net.URI;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.protege.editor.core.ui.action.ProtegeAction;
import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.model.OWLWorkspace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class R2RMLImportAction extends ProtegeAction {

	private static final long serialVersionUID = -1211395039869926309L;

	private OWLEditorKit editorKit = null;
	private OBDAModel obdaModel = null;

	private Logger log = LoggerFactory.getLogger(R2RMLImportAction.class);

	@Override
	public void initialise() throws Exception {
		editorKit = (OWLEditorKit) getEditorKit();
		obdaModel = ((OBDAModelManager) editorKit.get(OBDAModelImpl.class
				.getName())).getActiveOBDAModel();
	}

	@Override
	public void dispose() throws Exception {
		// Does nothing!
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		final OWLWorkspace workspace = editorKit.getWorkspace();

		String message = "The imported mappings will be appended to the existing data source. Continue?";
		int response = JOptionPane.showConfirmDialog(workspace, message,
				"Confirmation", JOptionPane.YES_NO_OPTION);

		if (response == JOptionPane.YES_OPTION) {

			final JFileChooser fc = new JFileChooser();
			fc.showOpenDialog(workspace);
			File file = null;
			try {

				file = (fc.getSelectedFile());
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (file != null) {
				R2RMLReader reader = new R2RMLReader(file);

				URI sourceID = obdaModel.getSources().get(0).getSourceID();

				try {
					for (OBDAMappingAxiom mapping : reader.readMappings()) {
						if (mapping.getTargetQuery().toString().contains("BNODE")){
							JOptionPane.showMessageDialog(workspace, "The mapping "+mapping.getId()+" contains BNode. -ontoPro- does not support it yet.");
						} else{
							obdaModel.addMapping(sourceID, mapping);
						}
					}
				} catch (DuplicateMappingException dm) {
					JOptionPane.showMessageDialog(workspace, "Duplicate mapping id found. Please correct the Resource node name: "+dm.getLocalizedMessage());
					throw new RuntimeException("Duplicate mapping found: "+dm.getMessage());
				}
		}

	}
	}
}

@@ -2619,24 +2619,170 @@
 der(file);%0A%0A
+%09%09%09%09if (obdaModel.getSources().isEmpty()) %7B%0A%09%09%09%09%09JOptionPane.showMessageDialog(workspace, %22The data source has not been set.%22);%0A%09%09%09%09%7D%0A%09%09%09%09else %7B%0A%09
 %09%09%09%09URI sour
@@ -2838,22 +2838,24 @@
 );%0A%0A%09%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09%09for
@@ -2910,24 +2910,25 @@
 ()) %7B%0A%09%09%09%09%09%09
+%09
 if (mapping.
@@ -2973,18 +2973,20 @@
 BNODE%22))
+ 
 %7B%0A
+%09
 %09%09%09%09%09%09%09J
@@ -3039,17 +3039,19 @@
 apping %22
-+
+ + 
 mapping.
@@ -3057,17 +3057,19 @@
 .getId()
-+
+ + 
 %22 contai
@@ -3112,24 +3112,25 @@
  it yet.%22);%0A
+%09
 %09%09%09%09%09%09%7D else
@@ -3129,18 +3129,20 @@
 %09%09%7D else
+ 
 %7B%0A
+%09
 %09%09%09%09%09%09%09o
@@ -3181,16 +3181,17 @@
 pping);%0A
+%09
 %09%09%09%09%09%09%7D%0A
@@ -3191,26 +3191,28 @@
 %09%09%09%09%09%7D%0A%09%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%7D catch 
@@ -3240,24 +3240,25 @@
 ption dm) %7B%0A
+%09
 %09%09%09%09%09JOption
@@ -3360,17 +3360,19 @@
  name: %22
-+
+ + 
 dm.getLo
@@ -3395,16 +3395,17 @@
 );%0A%09%09%09%09%09
+%09
 throw ne
@@ -3450,17 +3450,19 @@
 found: %22
-+
+ + 
 dm.getMe
@@ -3471,22 +3471,30 @@
 age());%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A
+%09
 %09%09%7D%0A%0A%09%7D%0A

Add error message for R2rml