use of org.jboss.tools.hibernate.runtime.spi.IService in project jbosstools-hibernate by jbosstools.
the class OpenMappingUtils method searchInEjb3MappingFiles.
/**
* Trying to find hibernate console config ejb3 mapping file,
* which is corresponding to provided element.
*
* @param consoleConfig
* @param element
* @return
*/
@SuppressWarnings("unchecked")
public static IFile searchInEjb3MappingFiles(ConsoleConfiguration consoleConfig, Object element) {
IFile file = null;
if (consoleConfig == null) {
return file;
}
final ConsoleConfiguration cc2 = consoleConfig;
List<String> documentPaths = (List<String>) consoleConfig.execute(new ExecutionContext.Command() {
public Object execute() {
String persistenceUnitName = cc2.getPreferences().getPersistenceUnitName();
EntityResolver entityResolver = cc2.getConfiguration().getEntityResolver();
IService service = cc2.getHibernateExtension().getHibernateService();
return service.getJPAMappingFilePaths(persistenceUnitName, entityResolver);
}
});
if (documentPaths == null) {
return file;
}
IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfig);
ArrayList<IPath> pathsSrc = new ArrayList<IPath>();
ArrayList<IPath> pathsOut = new ArrayList<IPath>();
ArrayList<IPath> pathsFull = new ArrayList<IPath>();
for (int i = 0; i < projs.length; i++) {
IJavaProject proj = projs[i];
IPath projPathFull = proj.getResource().getLocation();
IPath projPath = proj.getPath();
IPath projPathOut = null;
try {
projPathOut = proj.getOutputLocation();
projPathOut = projPathOut.makeRelativeTo(projPath);
} catch (JavaModelException e) {
// just ignore
}
IPackageFragmentRoot[] pfrs = new IPackageFragmentRoot[0];
try {
pfrs = proj.getAllPackageFragmentRoots();
} catch (JavaModelException e) {
// just ignore
}
for (int j = 0; j < pfrs.length; j++) {
// TODO: think about possibility to open resources from jar files
if (pfrs[j].isArchive() || pfrs[j].isExternal()) {
continue;
}
final IPath pathSrc = pfrs[j].getPath();
final IPath pathOut = projPathOut;
final IPath pathFull = projPathFull;
pathsSrc.add(pathSrc);
pathsOut.add(pathOut);
pathsFull.add(pathFull);
}
}
int scanSize = Math.min(pathsSrc.size(), pathsOut.size());
scanSize = Math.min(pathsFull.size(), scanSize);
for (int i = 0; i < scanSize && file == null; i++) {
final IPath pathSrc = pathsSrc.get(i);
final IPath pathOut = pathsOut.get(i);
final IPath pathFull = pathsFull.get(i);
Iterator<String> it = documentPaths.iterator();
while (it.hasNext() && file == null) {
String docPath = it.next();
IPath path2DocFull = Path.fromOSString(docPath);
IPath resPath = path2DocFull.makeRelativeTo(pathFull);
if (pathOut != null) {
resPath = resPath.makeRelativeTo(pathOut);
}
resPath = pathSrc.append(resPath);
file = ResourcesPlugin.getWorkspace().getRoot().getFile(resPath);
if (file == null || !file.exists()) {
file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(resPath);
}
if (file != null && file.exists()) {
if (elementInFile(consoleConfig, file, element)) {
break;
}
}
file = null;
}
}
return file;
}
use of org.jboss.tools.hibernate.runtime.spi.IService in project jbosstools-hibernate by jbosstools.
the class AbstractQueryEditor method getQueryInputModel.
public QueryInputModel getQueryInputModel() {
if (queryInputModel == null) {
IService service = getConsoleConfiguration().getHibernateExtension().getHibernateService();
queryInputModel = new QueryInputModel(service);
}
return queryInputModel;
}
use of org.jboss.tools.hibernate.runtime.spi.IService in project jbosstools-hibernate by jbosstools.
the class OpenMappingAction method run.
/**
* @param consoleConfig
* @param selection
* @param selectionParent
* @throws JavaModelException
* @throws PartInitException
* @throws PresistanceClassNotFoundException
* @throws FileNotFoundException
*/
public static IEditorPart run(ConsoleConfiguration consoleConfig, Object selection, Object selectionParent) throws PartInitException, JavaModelException, FileNotFoundException {
IEditorPart editorPart = null;
IFile file = null;
if (selection instanceof IProperty) {
IProperty p = (IProperty) selection;
if (p.getPersistentClass() != null) {
// use PersistentClass to open editor
file = OpenMappingUtils.searchFileToOpen(consoleConfig, p.getPersistentClass());
}
} else {
if (selectionParent != null) {
file = OpenMappingUtils.searchFileToOpen(consoleConfig, selectionParent);
} else {
file = OpenMappingUtils.searchFileToOpen(consoleConfig, selection);
}
}
if (file != null) {
editorPart = OpenMappingUtils.openFileInEditor(file);
IService service = consoleConfig.getHibernateExtension().getHibernateService();
boolean updateRes = updateEditorSelection(editorPart, selection, service);
if (!updateRes && selectionParent != null) {
// if it is not possible to select object, try to select it's parent
updateRes = updateEditorSelection(editorPart, selectionParent, service);
}
}
if (editorPart == null) {
// try to find hibernate-annotations
IPersistentClass rootClass = null;
if (selection instanceof IPersistentClass) {
rootClass = (IPersistentClass) selection;
} else if (selection instanceof IProperty) {
IProperty p = (IProperty) selection;
if (p.getPersistentClass() != null) {
rootClass = p.getPersistentClass();
}
}
if (rootClass != null) {
if (OpenMappingUtils.hasConfigXMLMappingClassAnnotation(consoleConfig, rootClass)) {
String fullyQualifiedName = rootClass.getClassName();
editorPart = OpenSourceAction.run(consoleConfig, selection, fullyQualifiedName);
}
}
}
if (editorPart == null) {
final String title = HibernateConsoleMessages.OpenMappingAction_open_mapping_file;
final String msg = NLS.bind(HibernateConsoleMessages.OpenMappingAction_mapping_for_not_found, selection);
MessageDialog.openError(null, title, msg);
throw new FileNotFoundException(msg);
}
return editorPart;
}
use of org.jboss.tools.hibernate.runtime.spi.IService in project jbosstools-hibernate by jbosstools.
the class NewConfigurationWizard method openContentStream.
/**
* We will initialize file contents with a sample text.
* @throws UnsupportedEncodingException
*/
private InputStream openContentStream(Properties props) {
StringWriter stringWriter = new StringWriter();
IService service = ServiceLookup.findService(connectionInfoPage.getHibernateVersion());
// $NON-NLS-1$
IExporter hce = service.createExporter("org.hibernate.tool.hbm2x.HibernateConfigurationExporter");
hce.setCustomProperties(props);
hce.setOutput(stringWriter);
hce.start();
try {
// $NON-NLS-1$
return new ByteArrayInputStream(stringWriter.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException uec) {
HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.NewConfigurationWizard_problems_converting_to_utf8, uec);
return new ByteArrayInputStream(stringWriter.toString().getBytes());
}
}
use of org.jboss.tools.hibernate.runtime.spi.IService in project jbosstools-hibernate by jbosstools.
the class SchemaExportAction method doRun.
public void doRun() {
for (Iterator<?> i = getSelectedNonResources().iterator(); i.hasNext(); ) {
Object node = i.next();
if (!(node instanceof ConsoleConfiguration)) {
continue;
}
final ConsoleConfiguration config = (ConsoleConfiguration) node;
try {
config.execute(new Command() {
public Object execute() {
final IConfiguration cfg = config.getConfiguration();
if (cfg == null) {
return null;
}
String out = NLS.bind(HibernateConsoleMessages.SchemaExportAction_sure_run_schemaexport, config.getName());
boolean res = openWarningYesNoDlg(viewer.getControl().getShell(), HibernateConsoleMessages.SchemaExportAction_run_schemaexport, out);
if (!res) {
return null;
}
IService service = config.getHibernateExtension().getHibernateService();
ISchemaExport export = service.newSchemaExport(cfg);
export.create();
if (!export.getExceptions().isEmpty()) {
Iterator<Throwable> iterator = export.getExceptions().iterator();
int cnt = 1;
while (iterator.hasNext()) {
Throwable element = iterator.next();
String outStr = NLS.bind(HibernateConsoleMessages.SchemaExportAction_errornum_while_performing_schemaexport, cnt++);
HibernateConsolePlugin.getDefault().logErrorMessage(outStr, element);
}
HibernateConsolePlugin.getDefault().showError(viewer.getControl().getShell(), NLS.bind(HibernateConsoleMessages.SchemaExportAction_error_while_performing_schemaexport, cnt - 1), (Throwable) null);
}
return null;
}
});
// todo: should we do it here or should
viewer.refresh(node);
// the view just react to config being
// build ?
} catch (Exception he) {
HibernateConsolePlugin.getDefault().showError(viewer.getControl().getShell(), HibernateConsoleMessages.SchemaExportAction_exception_running_schemaexport, he);
}
}
}
Aggregations