Search in sources :

Example 1 with JptXmlResource

use of org.eclipse.jpt.common.core.resource.xml.JptXmlResource in project jbosstools-hibernate by jbosstools.

the class AddGeneratedClassesJob method runInWorkspace.

@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
        return Status.CANCEL_STATUS;
    }
    final SubMonitor sm = SubMonitor.convert(monitor, UIMessages.SYNC_CLASSES_TASK, 20);
    final JptXmlResource resource = jpaProject.getPersistenceXmlResource();
    if (resource == null) {
        // the resource would only be null if the persistence.xml file had an invalid content type
        return Status.OK_STATUS;
    }
    if (sm.isCanceled()) {
        return Status.CANCEL_STATUS;
    }
    sm.worked(1);
    XmlPersistence persistence = (XmlPersistence) resource.getRootObject();
    XmlPersistenceUnit persistenceUnit;
    if (persistence.getPersistenceUnits().size() > 0) {
        persistenceUnit = persistence.getPersistenceUnits().get(0);
    } else {
        persistenceUnit = PersistenceFactory.eINSTANCE.createXmlPersistenceUnit();
        persistenceUnit.setName(jpaProject.getName());
        persistence.getPersistenceUnits().add(persistenceUnit);
    }
    sm.worked(1);
    IStatus status = addNewClassRefs(sm.newChild(17), jpaProject, persistenceUnit);
    resource.save();
    sm.done();
    return status;
}
Also used : XmlPersistence(org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistence) JptXmlResource(org.eclipse.jpt.common.core.resource.xml.JptXmlResource) IStatus(org.eclipse.core.runtime.IStatus) SubMonitor(org.eclipse.core.runtime.SubMonitor) XmlPersistenceUnit(org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistenceUnit)

Example 2 with JptXmlResource

use of org.eclipse.jpt.common.core.resource.xml.JptXmlResource in project jbosstools-hibernate by jbosstools.

the class JPAProjectConfigurator method configure.

@Override
public void configure(IProject project, Set<IPath> ignoredDirectories, IProgressMonitor monitor) {
    try {
        IFacetedProject facetedProject = ProjectFacetsManager.create(project, true, monitor);
        // $NON-NLS-1$
        IProjectFacet JPA_FACET = ProjectFacetsManager.getProjectFacet("jpt.jpa");
        if (!facetedProject.hasProjectFacet(JPA_FACET)) {
            Set<Action> actions = new LinkedHashSet<>(2, 1);
            IProjectFacetVersion javaFv = JavaFacet.FACET.getVersion(JavaFacetUtil.getCompilerLevel(project));
            if (!facetedProject.hasProjectFacet(JavaFacet.FACET)) {
                actions.add(new IFacetedProject.Action(IFacetedProject.Action.Type.INSTALL, javaFv, null));
            } else if (!facetedProject.hasProjectFacet(javaFv)) {
                actions.add(new IFacetedProject.Action(IFacetedProject.Action.Type.VERSION_CHANGE, javaFv, null));
            }
            // $NON-NLS-1$
            RecursiveFileFinder finder = new RecursiveFileFinder("persistence.xml", ignoredDirectories);
            project.accept(finder);
            PersistenceXmlResourceProvider provider = PersistenceXmlResourceProvider.getXmlResourceProvider(finder.getFile());
            JptXmlResource jpaXmlResource = provider.getXmlResource();
            IProjectFacetVersion version = null;
            if (jpaXmlResource.getVersion() != null) {
                version = JpaProject.FACET.getVersion(jpaXmlResource.getVersion());
            }
            if (version == null) {
                version = JpaProject.FACET.getLatestVersion();
            }
            // use default
            JpaPlatform platform = null;
            // TODO improve platform detection
            LibraryInstallDelegate libraryDelegate = new LibraryInstallDelegate(facetedProject, version);
            // $NON-NLS-1$
            ILibraryProvider libraryProvider = LibraryProviderFramework.getProvider("jpa-no-op-library-provider");
            libraryDelegate.setLibraryProvider(libraryProvider);
            IDataModel dm = DataModelFactory.createDataModel(new JpaFacetInstallDataModelProvider());
            dm.setProperty(IFacetDataModelProperties.FACET_VERSION_STR, version.getVersionString());
            dm.setProperty(JpaFacetDataModelProperties.PLATFORM, platform);
            dm.setProperty(JpaFacetInstallDataModelProperties.DISCOVER_ANNOTATED_CLASSES, true);
            dm.setProperty(JpaFacetInstallDataModelProperties.LIBRARY_PROVIDER_DELEGATE, libraryDelegate);
            actions.add(new IFacetedProject.Action(IFacetedProject.Action.Type.INSTALL, version, dm));
            facetedProject.modify(actions, monitor);
        }
    } catch (Exception ex) {
        HibernateJptUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, HibernateJptUIPlugin.PLUGIN_ID, ex.getMessage(), ex));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IFacetedProject(org.eclipse.wst.common.project.facet.core.IFacetedProject) Action(org.eclipse.wst.common.project.facet.core.IFacetedProject.Action) JptXmlResource(org.eclipse.jpt.common.core.resource.xml.JptXmlResource) RecursiveFileFinder(org.eclipse.ui.wizards.datatransfer.RecursiveFileFinder) IProjectFacetVersion(org.eclipse.wst.common.project.facet.core.IProjectFacetVersion) JpaFacetInstallDataModelProvider(org.eclipse.jpt.jpa.core.internal.facet.JpaFacetInstallDataModelProvider) LibraryInstallDelegate(org.eclipse.jst.common.project.facet.core.libprov.LibraryInstallDelegate) ILibraryProvider(org.eclipse.jst.common.project.facet.core.libprov.ILibraryProvider) IDataModel(org.eclipse.wst.common.frameworks.datamodel.IDataModel) JpaPlatform(org.eclipse.jpt.jpa.core.JpaPlatform) CoreException(org.eclipse.core.runtime.CoreException) IProjectFacet(org.eclipse.wst.common.project.facet.core.IProjectFacet) Action(org.eclipse.wst.common.project.facet.core.IFacetedProject.Action) PersistenceXmlResourceProvider(org.eclipse.jpt.jpa.core.internal.resource.persistence.PersistenceXmlResourceProvider)

Example 3 with JptXmlResource

use of org.eclipse.jpt.common.core.resource.xml.JptXmlResource in project jbosstools-hibernate by jbosstools.

the class JPAPostInstallFasetListener method getPersistenceUnitName.

private String getPersistenceUnitName(IProject project) {
    String result = null;
    PersistenceXmlResourceProvider defaultXmlResourceProvider = PersistenceXmlResourceProvider.getDefaultXmlResourceProvider(project);
    final JptXmlResource resource = defaultXmlResourceProvider.getXmlResource();
    XmlPersistence persistence = (XmlPersistence) resource.getRootObject();
    if (persistence.getPersistenceUnits().size() > 0) {
        XmlPersistenceUnit persistenceUnit = persistence.getPersistenceUnits().get(0);
        result = persistenceUnit.getName();
    }
    return result;
}
Also used : XmlPersistence(org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistence) JptXmlResource(org.eclipse.jpt.common.core.resource.xml.JptXmlResource) PersistenceXmlResourceProvider(org.eclipse.jpt.jpa.core.internal.resource.persistence.PersistenceXmlResourceProvider) XmlPersistenceUnit(org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistenceUnit)

Example 4 with JptXmlResource

use of org.eclipse.jpt.common.core.resource.xml.JptXmlResource in project jbosstools-hibernate by jbosstools.

the class JPAPostInstallFasetListener method exportConnectionProfilePropertiesToPersistenceXml.

private void exportConnectionProfilePropertiesToPersistenceXml(IProject project) {
    PersistenceXmlResourceProvider defaultXmlResourceProvider = PersistenceXmlResourceProvider.getDefaultXmlResourceProvider(project);
    final JptXmlResource resource = defaultXmlResourceProvider.getXmlResource();
    Properties propsToAdd = getConnectionProperties(project);
    if (propsToAdd.isEmpty() || resource == null)
        return;
    XmlPersistence persistence = (XmlPersistence) resource.getRootObject();
    if (persistence.getPersistenceUnits().size() > 0) {
        XmlPersistenceUnit persistenceUnit = persistence.getPersistenceUnits().get(0);
        if (persistenceUnit.getProperties() == null) {
            persistenceUnit.setProperties(PersistenceFactory.eINSTANCE.createXmlProperties());
            for (Entry<Object, Object> entry : propsToAdd.entrySet()) {
                XmlProperty prop = PersistenceFactory.eINSTANCE.createXmlProperty();
                prop.setName((String) entry.getKey());
                prop.setValue((String) entry.getValue());
                persistenceUnit.getProperties().getProperties().add(prop);
            }
        }
    }
    resource.save();
}
Also used : XmlPersistence(org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistence) JptXmlResource(org.eclipse.jpt.common.core.resource.xml.JptXmlResource) XmlProperty(org.eclipse.jpt.jpa.core.resource.persistence.XmlProperty) PersistenceXmlResourceProvider(org.eclipse.jpt.jpa.core.internal.resource.persistence.PersistenceXmlResourceProvider) Properties(java.util.Properties) XmlPersistenceUnit(org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistenceUnit)

Aggregations

JptXmlResource (org.eclipse.jpt.common.core.resource.xml.JptXmlResource)4 PersistenceXmlResourceProvider (org.eclipse.jpt.jpa.core.internal.resource.persistence.PersistenceXmlResourceProvider)3 XmlPersistence (org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistence)3 XmlPersistenceUnit (org.eclipse.jpt.jpa.core.resource.persistence.XmlPersistenceUnit)3 IStatus (org.eclipse.core.runtime.IStatus)2 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 CoreException (org.eclipse.core.runtime.CoreException)1 Status (org.eclipse.core.runtime.Status)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 JpaPlatform (org.eclipse.jpt.jpa.core.JpaPlatform)1 JpaFacetInstallDataModelProvider (org.eclipse.jpt.jpa.core.internal.facet.JpaFacetInstallDataModelProvider)1 XmlProperty (org.eclipse.jpt.jpa.core.resource.persistence.XmlProperty)1 ILibraryProvider (org.eclipse.jst.common.project.facet.core.libprov.ILibraryProvider)1 LibraryInstallDelegate (org.eclipse.jst.common.project.facet.core.libprov.LibraryInstallDelegate)1 RecursiveFileFinder (org.eclipse.ui.wizards.datatransfer.RecursiveFileFinder)1 IDataModel (org.eclipse.wst.common.frameworks.datamodel.IDataModel)1 IFacetedProject (org.eclipse.wst.common.project.facet.core.IFacetedProject)1 Action (org.eclipse.wst.common.project.facet.core.IFacetedProject.Action)1 IProjectFacet (org.eclipse.wst.common.project.facet.core.IProjectFacet)1