Search in sources :

Example 16 with IService

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) List(java.util.List) ArrayList(java.util.ArrayList) IService(org.jboss.tools.hibernate.runtime.spi.IService)

Example 17 with IService

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;
}
Also used : IService(org.jboss.tools.hibernate.runtime.spi.IService) QueryInputModel(org.hibernate.console.QueryInputModel)

Example 18 with IService

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;
}
Also used : IFile(org.eclipse.core.resources.IFile) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) FileNotFoundException(java.io.FileNotFoundException) IEditorPart(org.eclipse.ui.IEditorPart) IService(org.jboss.tools.hibernate.runtime.spi.IService) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass)

Example 19 with IService

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());
    }
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IExporter(org.jboss.tools.hibernate.runtime.spi.IExporter) IService(org.jboss.tools.hibernate.runtime.spi.IService)

Example 20 with IService

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);
        }
    }
}
Also used : ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) Command(org.hibernate.console.execution.ExecutionContext.Command) ISchemaExport(org.jboss.tools.hibernate.runtime.spi.ISchemaExport) Iterator(java.util.Iterator) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IService(org.jboss.tools.hibernate.runtime.spi.IService)

Aggregations

IService (org.jboss.tools.hibernate.runtime.spi.IService)23 ConsoleConfiguration (org.hibernate.console.ConsoleConfiguration)8 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)8 Properties (java.util.Properties)5 IFile (org.eclipse.core.resources.IFile)5 CoreException (org.eclipse.core.runtime.CoreException)5 IPath (org.eclipse.core.runtime.IPath)5 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)4 Path (org.eclipse.core.runtime.Path)3 PartInitException (org.eclipse.ui.PartInitException)3 IArtifactCollector (org.jboss.tools.hibernate.runtime.spi.IArtifactCollector)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 IResource (org.eclipse.core.resources.IResource)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2