Search in sources :

Example 76 with IConfiguration

use of org.jboss.tools.hibernate.runtime.spi.IConfiguration in project jbosstools-hibernate by jbosstools.

the class TypeVisitor method createConfigurations.

/**
 * @return different configuration for different projects
 */
public Map<IJavaProject, IConfiguration> createConfigurations(int processDepth) {
    Map<IJavaProject, IConfiguration> configs = new HashMap<IJavaProject, IConfiguration>();
    if (selectionCU.size() == 0) {
        return configs;
    }
    AllEntitiesInfoCollector collector = new AllEntitiesInfoCollector();
    Iterator<ICompilationUnit> it = selectionCU.iterator();
    Map<IJavaProject, Set<ICompilationUnit>> mapJP_CUSet = new HashMap<IJavaProject, Set<ICompilationUnit>>();
    // separate by parent project
    while (it.hasNext()) {
        ICompilationUnit cu = it.next();
        Set<ICompilationUnit> set = mapJP_CUSet.get(cu.getJavaProject());
        if (set == null) {
            set = new HashSet<ICompilationUnit>();
            mapJP_CUSet.put(cu.getJavaProject(), set);
        }
        set.add(cu);
    }
    Iterator<Map.Entry<IJavaProject, Set<ICompilationUnit>>> mapIt = mapJP_CUSet.entrySet().iterator();
    while (mapIt.hasNext()) {
        Map.Entry<IJavaProject, Set<ICompilationUnit>> entry = mapIt.next();
        IJavaProject javaProject = entry.getKey();
        Iterator<ICompilationUnit> setIt = entry.getValue().iterator();
        collector.initCollector();
        while (setIt.hasNext()) {
            ICompilationUnit icu = setIt.next();
            collector.collect(icu, processDepth);
        }
        collector.resolveRelations();
        // I don't check here if any non abstract class selected
        IConfiguration config = createConfiguration(javaProject, collector.getMapCUs_Info());
        if (config != null) {
            configs.put(javaProject, config);
        }
    }
    return configs;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Entry(java.util.Map.Entry) IJavaProject(org.eclipse.jdt.core.IJavaProject) AllEntitiesInfoCollector(org.hibernate.eclipse.jdt.ui.internal.jpa.collect.AllEntitiesInfoCollector) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with IConfiguration

use of org.jboss.tools.hibernate.runtime.spi.IConfiguration in project jbosstools-hibernate by jbosstools.

the class NewHibernateMappingFileWizard method getPlaces2Gen.

protected Map<IJavaProject, IPath> getPlaces2Gen() {
    updateCompilationUnits();
    Map<IJavaProject, IConfiguration> configs = createConfigurations();
    Map<IJavaProject, IPath> places2Gen = new HashMap<IJavaProject, IPath>();
    for (Entry<IJavaProject, IConfiguration> entry : configs.entrySet()) {
        IConfiguration config = entry.getValue();
        IPath place2Gen = previewPage.getRootPlace2Gen().append(entry.getKey().getElementName());
        places2Gen.put(entry.getKey(), place2Gen);
        File folder2Gen = new File(place2Gen.toOSString());
        // cleanup folder before gen info
        FileUtils.delete(folder2Gen);
        if (!folder2Gen.exists()) {
            folder2Gen.mkdirs();
        }
        HibernateMappingExporterWrapper hce = new HibernateMappingExporterWrapper(entry.getKey(), config, folder2Gen);
        try {
            hce.start();
        } catch (Exception e) {
            HibernateConsolePlugin.getDefault().log(e);
        }
    }
    return places2Gen;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IFile(org.eclipse.core.resources.IFile) File(java.io.File) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException)

Example 78 with IConfiguration

use of org.jboss.tools.hibernate.runtime.spi.IConfiguration in project jbosstools-hibernate by jbosstools.

the class CodeGenerationLaunchDelegate method buildConfiguration.

private IConfiguration buildConfiguration(final ExporterAttributes attributes, ConsoleConfiguration cc, IWorkspaceRoot root) {
    final boolean reveng = attributes.isReverseEngineer();
    final String reverseEngineeringStrategy = attributes.getRevengStrategy();
    final boolean preferBasicCompositeids = attributes.isPreferBasicCompositeIds();
    final IResource revengres = PathHelper.findMember(root, attributes.getRevengSettings());
    if (reveng) {
        IConfiguration configuration = null;
        if (cc.hasConfiguration()) {
            configuration = cc.getConfiguration();
        } else {
            configuration = cc.buildWith(null, false);
        }
        // final JDBCMetaDataConfiguration cfg = new JDBCMetaDataConfiguration();
        final IService service = cc.getHibernateExtension().getHibernateService();
        final IConfiguration cfg = service.newJDBCMetaDataConfiguration();
        Properties properties = configuration.getProperties();
        cfg.setProperties(properties);
        cc.buildWith(cfg, false);
        cfg.setPreferBasicCompositeIds(preferBasicCompositeids);
        cc.execute(new // need to execute in the consoleconfiguration to let it handle classpath stuff!
        Command() {

            public Object execute() {
                // todo: factor this setup of revengstrategy to core
                IReverseEngineeringStrategy res = service.newDefaultReverseEngineeringStrategy();
                IOverrideRepository repository = null;
                if (revengres != null) {
                    File file = PathHelper.getLocation(revengres).toFile();
                    repository = service.newOverrideRepository();
                    repository.addFile(file);
                }
                if (repository != null) {
                    res = repository.getReverseEngineeringStrategy(res);
                }
                if (reverseEngineeringStrategy != null && reverseEngineeringStrategy.trim().length() > 0) {
                    res = service.newReverseEngineeringStrategy(reverseEngineeringStrategy, res);
                }
                IReverseEngineeringSettings qqsettings = service.newReverseEngineeringSettings(res).setDefaultPackageName(attributes.getPackageName()).setDetectManyToMany(attributes.detectManyToMany()).setDetectOneToOne(attributes.detectOneToOne()).setDetectOptimisticLock(attributes.detectOptimisticLock());
                res.setSettings(qqsettings);
                cfg.setReverseEngineeringStrategy(res);
                cfg.readFromJDBC();
                cfg.buildMappings();
                return null;
            }
        });
        return cfg;
    } else {
        cc.build();
        cc.buildMappings();
        return cc.getConfiguration();
    }
}
Also used : IReverseEngineeringSettings(org.jboss.tools.hibernate.runtime.spi.IReverseEngineeringSettings) IReverseEngineeringStrategy(org.jboss.tools.hibernate.runtime.spi.IReverseEngineeringStrategy) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) Properties(java.util.Properties) IOverrideRepository(org.jboss.tools.hibernate.runtime.spi.IOverrideRepository) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IService(org.jboss.tools.hibernate.runtime.spi.IService)

Example 79 with IConfiguration

use of org.jboss.tools.hibernate.runtime.spi.IConfiguration in project jbosstools-hibernate by jbosstools.

the class ConsoleConfiguration method buildWith.

/**
 * @return
 */
public IConfiguration buildWith(final IConfiguration cfg, final boolean includeMappings) {
    reinitClassLoader();
    executionContext = new DefaultExecutionContext(getName(), classLoader);
    IConfiguration result = (IConfiguration) execute(new Command() {

        public Object execute() {
            ConfigurationFactory csf = new ConfigurationFactory(prefs, fakeDrivers);
            return csf.createConfiguration(cfg, includeMappings);
        }
    });
    return result;
}
Also used : DefaultExecutionContext(org.hibernate.console.execution.DefaultExecutionContext) Command(org.hibernate.console.execution.ExecutionContext.Command) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration)

Example 80 with IConfiguration

use of org.jboss.tools.hibernate.runtime.spi.IConfiguration in project jbosstools-hibernate by jbosstools.

the class ConfigurationFactory method loadConfigurationXML.

@SuppressWarnings("unchecked")
private IConfiguration loadConfigurationXML(IConfiguration localCfg, boolean includeMappings, EntityResolver entityResolver) {
    File configXMLFile = prefs.getConfigXMLFile();
    if (!includeMappings) {
        org.dom4j.Document doc;
        XMLHelper xmlHelper = new XMLHelper();
        InputStream stream = null;
        // $NON-NLS-1$
        String resourceName = "<unknown>";
        if (configXMLFile != null) {
            resourceName = configXMLFile.toString();
            try {
                stream = new FileInputStream(configXMLFile);
            } catch (FileNotFoundException e1) {
                throw new HibernateConsoleRuntimeException(ConsoleMessages.ConsoleConfiguration_could_not_access + configXMLFile, e1);
            }
        } else {
            // $NON-NLS-1$
            resourceName = "/hibernate.cfg.xml";
            if (checkHibernateResoureExistence(resourceName)) {
                // simulate hibernate's
                stream = getResourceAsStream(resourceName);
            // default look up
            } else {
                return localCfg;
            }
        }
        try {
            List<SAXParseException> errors = new ArrayList<SAXParseException>();
            doc = xmlHelper.createSAXReader(resourceName, errors, entityResolver).read(new InputSource(stream));
            if (errors.size() != 0) {
                throw new MappingException(ConsoleMessages.ConsoleConfiguration_invalid_configuration, errors.get(0));
            }
            List<Node> list = doc.getRootElement().element("session-factory").elements(// $NON-NLS-1$ //$NON-NLS-2$
            "mapping");
            for (Node element : list) {
                element.getParent().remove(element);
            }
            DOMWriter dw = new DOMWriter();
            Document document = dw.write(doc);
            return localCfg.configure(document);
        } catch (DocumentException e) {
            throw new HibernateException(ConsoleMessages.ConsoleConfiguration_could_not_parse_configuration + resourceName, e);
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException ioe) {
            // log.warn( "could not close input stream for: " + resourceName, ioe );
            }
        }
    } else {
        if (configXMLFile != null) {
            return localCfg.configure(configXMLFile);
        } else {
            IConfiguration resultCfg = localCfg;
            if (checkHibernateResoureExistence("/hibernate.cfg.xml")) {
                // $NON-NLS-1$
                resultCfg = localCfg.configure();
            }
            return resultCfg;
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMWriter(org.dom4j.io.DOMWriter) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(org.dom4j.Node) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) MappingException(org.jboss.tools.hibernate.exception.MappingException) SAXParseException(org.xml.sax.SAXParseException) DocumentException(org.dom4j.DocumentException) XMLHelper(org.hibernate.util.xpl.XMLHelper) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) File(java.io.File)

Aggregations

IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)137 Test (org.junit.Test)72 Configuration (org.hibernate.cfg.Configuration)65 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)29 JDBCMetaDataConfiguration (org.hibernate.cfg.JDBCMetaDataConfiguration)25 File (java.io.File)19 ConsoleConfiguration (org.hibernate.console.ConsoleConfiguration)17 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)16 Properties (java.util.Properties)13 CoreException (org.eclipse.core.runtime.CoreException)10 IReverseEngineeringStrategy (org.jboss.tools.hibernate.runtime.spi.IReverseEngineeringStrategy)10 IJDBCReader (org.jboss.tools.hibernate.runtime.spi.IJDBCReader)9 FileWriter (java.io.FileWriter)8 IOException (java.io.IOException)8 JDBCReader (org.hibernate.cfg.reveng.JDBCReader)8 SimpleValue (org.hibernate.mapping.SimpleValue)8 IProperty (org.jboss.tools.hibernate.runtime.spi.IProperty)8 JavaModelException (org.eclipse.jdt.core.JavaModelException)7 PartInitException (org.eclipse.ui.PartInitException)7 Ejb3Configuration (org.hibernate.ejb.Ejb3Configuration)7