/*
* (c) Copyright 2010-2011 AgileBirds
*
* This file is part of OpenFlexo.
*
* OpenFlexo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenFlexo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openflexo.foundation.viewpoint.action;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;
import java.util.logging.Logger;
import org.openflexo.foundation.FlexoEditor;
import org.openflexo.foundation.FlexoModelObject;
import org.openflexo.foundation.IOFlexoException;
import org.openflexo.foundation.action.FlexoAction;
import org.openflexo.foundation.action.FlexoActionType;
import org.openflexo.foundation.ontology.FlexoOntology;
import org.openflexo.foundation.ontology.ImportedOWLOntology;
import org.openflexo.foundation.ontology.owl.OWLOntology;
import org.openflexo.foundation.ontology.owl.OWLOntology.OntologyNotFoundException;
import org.openflexo.foundation.rm.SaveResourceException;
import org.openflexo.foundation.viewpoint.ViewPoint;
import org.openflexo.foundation.viewpoint.ViewPointFolder;
import org.openflexo.foundation.viewpoint.ViewPointLibrary;
import org.openflexo.foundation.viewpoint.ViewPointLibraryObject;
import org.openflexo.foundation.viewpoint.ViewPointObject;
import org.openflexo.toolbox.FileUtils;
import org.openflexo.toolbox.JavaUtils;
import org.openflexo.toolbox.StringUtils;
import org.openflexo.toolbox.ToolBox;
public class CreateViewPoint extends FlexoAction<CreateViewPoint, ViewPointLibraryObject, ViewPointObject> {
private static final Logger logger = Logger.getLogger(CreateViewPoint.class.getPackage().getName());
public static FlexoActionType<CreateViewPoint, ViewPointLibraryObject, ViewPointObject> actionType = new FlexoActionType<CreateViewPoint, ViewPointLibraryObject, ViewPointObject>(
"create_view_point", FlexoActionType.newMenu, FlexoActionType.defaultGroup, FlexoActionType.ADD_ACTION_TYPE) {
/**
* Factory method
*/
@Override
public CreateViewPoint makeNewAction(ViewPointLibraryObject focusedObject, Vector<ViewPointObject> globalSelection,
FlexoEditor editor) {
return new CreateViewPoint(focusedObject, globalSelection, editor);
}
@Override
public boolean isVisibleForSelection(ViewPointLibraryObject object, Vector<ViewPointObject> globalSelection) {
return object != null;
}
@Override
public boolean isEnabledForSelection(ViewPointLibraryObject object, Vector<ViewPointObject> globalSelection) {
return object != null;
}
};
static {
FlexoModelObject.addActionForClass(CreateViewPoint.actionType, ViewPointLibrary.class);
FlexoModelObject.addActionForClass(CreateViewPoint.actionType, ViewPointFolder.class);
}
public static enum OntologicalScopeChoices {
IMPORT_EXISTING_ONTOLOGY, CREATES_NEW_ONTOLOGY
}
public OntologicalScopeChoices ontologicalScopeChoice = OntologicalScopeChoices.IMPORT_EXISTING_ONTOLOGY;
private String _newCalcName;
private String _newCalcURI;
private String _newCalcDescription;
private File _ontologyFile;
private ViewPointFolder _calcFolder;
private ViewPoint _newViewPoint;
public Vector<FlexoOntology> importedOntologies = new Vector<FlexoOntology>();
// private boolean createsOntology = false;
CreateViewPoint(ViewPointLibraryObject focusedObject, Vector<ViewPointObject> globalSelection, FlexoEditor editor) {
super(actionType, focusedObject, globalSelection, editor);
}
@Override
protected void doAction(Object context) throws IOFlexoException {
logger.info("Create new calc");
ViewPointLibrary viewPointLibrary = getFocusedObject().getViewPointLibrary();
File newCalcDir = getCalcDir();
newCalcDir.mkdirs();
if (ontologicalScopeChoice == OntologicalScopeChoices.CREATES_NEW_ONTOLOGY) {
buildOntology();
} else if (ontologicalScopeChoice == OntologicalScopeChoices.IMPORT_EXISTING_ONTOLOGY) {
try {
FileUtils.copyFileToDir(getOntologyFile(), newCalcDir);
} catch (IOException e) {
throw new IOFlexoException(e);
}
_ontologyFile = new File(newCalcDir, _ontologyFile.getName());
}
// Instanciate new Calc
_newViewPoint = ViewPoint.newViewPoint(getBaseName(), getNewCalcURI(), getOntologyFile(), newCalcDir, viewPointLibrary,
getCalcFolder());
_newViewPoint.setDescription(getNewCalcDescription());
// And register it to the library
viewPointLibrary.registerViewPoint(_newViewPoint);
}
private ImportedOWLOntology buildOntology() {
_ontologyFile = new File(getCalcDir(), getBaseName() + ".owl");
ImportedOWLOntology newOntology = ImportedOWLOntology.createNewImportedOntology(getNewCalcURI(), _ontologyFile, getCalcFolder()
.getOntologyLibrary());
for (FlexoOntology importedOntology : importedOntologies) {
try {
if (importedOntology instanceof OWLOntology) {
newOntology.importOntology(importedOntology);
} else {
logger.warning("Could not import anything else than an OWL ontology");
}
} catch (OntologyNotFoundException e) {
e.printStackTrace();
}
}
try {
newOntology.save();
} catch (SaveResourceException e) {
e.printStackTrace();
}
return newOntology;
}
public String getNewCalcName() {
return _newCalcName;
}
public void setNewCalcName(String newCalcName) {
this._newCalcName = newCalcName;
}
public String getNewCalcURI() {
if (StringUtils.isEmpty(_newCalcURI) && getOntologyFile() != null) {
return OWLOntology.findOntologyURI(getOntologyFile());
}
return _newCalcURI;
}
public void setNewCalcURI(String newCalcURI) {
this._newCalcURI = newCalcURI;
}
public String getNewCalcDescription() {
return _newCalcDescription;
}
public void setNewCalcDescription(String newCalcDescription) {
this._newCalcDescription = newCalcDescription;
}
public File getOntologyFile() {
return _ontologyFile;
}
public void setOntologyFile(File ontologyFile) {
this._ontologyFile = ontologyFile;
if (ontologyFile != null) {
String ontologyURI = OWLOntology.findOntologyURI(getOntologyFile());
String ontologyName = ToolBox.getJavaClassName(OWLOntology.findOntologyName(getOntologyFile()));
if (ontologyName == null && ontologyFile != null && ontologyFile.getName().length() > 4) {
ontologyName = ontologyFile.getName().substring(0, ontologyFile.getName().length() - 4);
}
if (StringUtils.isNotEmpty(ontologyURI)) {
_newCalcURI = ontologyURI;
}
setNewCalcName(ontologyName);
}
}
public ViewPointFolder getCalcFolder() {
if (_calcFolder == null) {
if (getFocusedObject() instanceof ViewPointFolder) {
return _calcFolder = (ViewPointFolder) getFocusedObject();
} else if (getFocusedObject() instanceof ViewPointLibrary) {
return _calcFolder = ((ViewPointLibrary) getFocusedObject()).getRootFolder();
}
return null;
}
return _calcFolder;
}
public void setCalcFolder(ViewPointFolder viewPointFolder) {
_calcFolder = viewPointFolder;
}
public boolean isNewCalcNameValid() {
if (StringUtils.isEmpty(getNewCalcName())) {
errorMessage = "please_supply_valid_view_point_name";
return false;
}
return true;
}
public boolean isNewCalcURIValid() {
if (StringUtils.isEmpty(getNewCalcURI())) {
errorMessage = "please_supply_valid_uri";
return false;
}
try {
new URL(getNewCalcURI());
} catch (MalformedURLException e) {
errorMessage = "malformed_uri";
return false;
}
if (!getNewCalcURI().endsWith(".owl")) {
errorMessage = "malformed_uri";
return false;
}
return true;
}
public String errorMessage;
public boolean isValid() {
if (!isNewCalcNameValid()) {
return false;
}
if (!isNewCalcURIValid()) {
return false;
}
if (ontologicalScopeChoice == OntologicalScopeChoices.IMPORT_EXISTING_ONTOLOGY) {
return getOntologyFile() != null;
}
return true;
}
public ViewPoint getNewViewPoint() {
return _newViewPoint;
}
private String getBaseName() {
return JavaUtils.getClassName(getNewCalcName());
}
private File getCalcDir() {
String baseName = getBaseName();
return new File(getCalcFolder().getExpectedPath(), baseName + ".viewpoint");
}
/*public void createOntology() {
createsOntology = true;
_ontologyFile = new File(getCalcDir(), getBaseName() + ".owl");
}*/
public void addToImportedOntologies(FlexoOntology ontology) {
System.out.println("import ontology " + ontology);
importedOntologies.add(ontology);
}
public void removeFromImportedOntologies(FlexoOntology ontology) {
System.out.println("remove ontology " + ontology);
importedOntologies.remove(ontology);
}
}