Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/wizards/gen/GenerateDynamicEntitiesFromSchemaWizard.java135
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.gen/src/org/eclipse/jpt/jpa/gen/internal/PackageGenerator.java39
2 files changed, 119 insertions, 55 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/wizards/gen/GenerateDynamicEntitiesFromSchemaWizard.java b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/wizards/gen/GenerateDynamicEntitiesFromSchemaWizard.java
index cd7a5f9de5..c2f20f35ef 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/wizards/gen/GenerateDynamicEntitiesFromSchemaWizard.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.ui/src/org/eclipse/jpt/jpa/eclipselink/ui/internal/wizards/gen/GenerateDynamicEntitiesFromSchemaWizard.java
@@ -10,17 +10,37 @@
package org.eclipse.jpt.jpa.eclipselink.ui.internal.wizards.gen;
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResourceRuleFactory;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jpt.common.core.resource.ProjectResourceLocator;
+import org.eclipse.jpt.common.ui.internal.util.SWTUtil;
import org.eclipse.jpt.jpa.core.JpaProject;
+import org.eclipse.jpt.jpa.core.resource.xml.JpaXmlResource;
import org.eclipse.jpt.jpa.db.ConnectionProfile;
import org.eclipse.jpt.jpa.db.Schema;
+import org.eclipse.jpt.jpa.eclipselink.ui.JptJpaEclipseLinkUiPlugin;
import org.eclipse.jpt.jpa.ui.JptJpaUiPlugin;
import org.eclipse.jpt.jpa.ui.internal.wizards.gen.GenerateEntitiesFromSchemaWizard;
import org.eclipse.jpt.jpa.ui.internal.wizards.gen.TableAssociationsWizardPage;
import org.eclipse.jpt.jpa.ui.internal.wizards.gen.TablesSelectorWizardPage;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
public class GenerateDynamicEntitiesFromSchemaWizard extends GenerateEntitiesFromSchemaWizard
implements INewWizard {
@@ -73,10 +93,20 @@ public class GenerateDynamicEntitiesFromSchemaWizard extends GenerateEntitiesFro
WorkspaceJob genEntitiesJob = new GenerateEntitiesJob(this.jpaProject, getCustomizer(), overwriteConfirmer, true);
genEntitiesJob.schedule();
- //TODO need to open file after generation
-// JpaXmlResource jpaXmlResource = this.jpaProject.getMappingFileXmlResource(new Path(getCustomizer().getXmlMappingFile()));
-// OpenXmlMappingFileJob openXmlMappingFileJob = new OpenXmlMappingFileJob(this.jpaProject, jpaXmlResource, getShell());
-// openXmlMappingFileJob.schedule();
+ //open file after generation
+ String xmlMappingFileLocation = getCustomizer().getXmlMappingFile();
+ JpaXmlResource jpaXmlResource = this.jpaProject.getMappingFileXmlResource(new Path(xmlMappingFileLocation));
+ IFile mappingFile;
+ if(jpaXmlResource!=null){
+ mappingFile = jpaXmlResource.getFile();
+ }
+ else{
+ IProject project = jpaProject.getProject();
+ IContainer container = ((ProjectResourceLocator) project.getAdapter(ProjectResourceLocator.class)).getDefaultResourceLocation();
+ mappingFile = container.getFile(new Path(xmlMappingFileLocation.substring(xmlMappingFileLocation.lastIndexOf("/")))); //$NON-NLS-1$
+ }
+ OpenXmlMappingFileJob openXmlMappingFileJob = new OpenXmlMappingFileJob(this.jpaProject, mappingFile);
+ openXmlMappingFileJob.schedule();
}
@Override
@@ -86,55 +116,52 @@ public class GenerateDynamicEntitiesFromSchemaWizard extends GenerateEntitiesFro
this.setWindowTitle(JptJpaEclipseLinkUiEntityGenMessages.GenerateDynamicEntitiesWizard_generateEntities);
}
-// public static class OpenXmlMappingFileJob extends WorkspaceJob {
-// final JpaProject jpaProject;
-// final JpaXmlResource jpaXmlResource;
-// final Shell shell;
-//
-// public OpenXmlMappingFileJob(JpaProject jpaProject, JpaXmlResource jpaXmlResource, Shell shell) {
-// super("Open XML File");
-// this.jpaProject = jpaProject;
-// this.jpaXmlResource = jpaXmlResource;
-// this.shell = shell;
-// IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
-// this.setRule(ruleFactory.modifyRule(jpaProject.getProject()));
-// }
-//
-// @Override
-// public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
-// try {
-// postPerformFinish(this.jpaProject,this.jpaXmlResource, this.shell);
-// } catch (InvocationTargetException e) {
-// throw new CoreException(new Status(IStatus.ERROR, JptJpaEclipseLinkUiPlugin.PLUGIN_ID, "error", e));
-// }
-// return Status.OK_STATUS;
-// }
-//
-// private void postPerformFinish(JpaProject jpaProject, JpaXmlResource jpaXmlResource, Shell shell) throws InvocationTargetException {
-// try {
-// IFile file = jpaXmlResource.getFile();
-// openEditor(file, shell);
-// }
-// catch (Exception cantOpen) {
-// throw new InvocationTargetException(cantOpen);
-// }
-// }
-//
-// private void openEditor(final IFile file, Shell shell) {
-// if (file != null) {
-// shell.getDisplay().asyncExec(new Runnable() {
-// public void run() {
-// try {
-// IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
-// IDE.openEditor(page, file, true);
-// }
-// catch (PartInitException e) {
-// JptJpaUiPlugin.log(e);
-// }
-// }
-// });
-// }
-// }
-// }
+ public static class OpenXmlMappingFileJob extends WorkspaceJob {
+ final JpaProject jpaProject;
+ final IFile mappingFile;
+
+ public OpenXmlMappingFileJob(JpaProject jpaProject, IFile mappingFile) {
+ super("Open XML File");
+ this.jpaProject = jpaProject;
+ this.mappingFile = mappingFile;
+ IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
+ this.setRule(ruleFactory.modifyRule(jpaProject.getProject()));
+ }
+
+ @Override
+ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
+ try {
+ postGeneration(this.jpaProject,this.mappingFile);
+ } catch (InvocationTargetException e) {
+ throw new CoreException(new Status(IStatus.ERROR, JptJpaEclipseLinkUiPlugin.PLUGIN_ID, "error", e));
+ }
+ return Status.OK_STATUS;
+ }
+
+ private void postGeneration(JpaProject jpaProject, IFile mappingFile) throws InvocationTargetException {
+ try {
+ openEditor(mappingFile);
+ }
+ catch (Exception cantOpen) {
+ throw new InvocationTargetException(cantOpen);
+ }
+ }
+
+ private void openEditor(final IFile file) {
+ if (file != null) {
+ SWTUtil.getStandardDisplay().asyncExec(new Runnable() {
+ public void run() {
+ try {
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ IDE.openEditor(page, file, true);
+ }
+ catch (PartInitException e) {
+ JptJpaUiPlugin.log(e);
+ }
+ }
+ });
+ }
+ }
+ }
}
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.gen/src/org/eclipse/jpt/jpa/gen/internal/PackageGenerator.java b/jpa/plugins/org.eclipse.jpt.jpa.gen/src/org/eclipse/jpt/jpa/gen/internal/PackageGenerator.java
index eab0594a7d..832ad03d5f 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.gen/src/org/eclipse/jpt/jpa/gen/internal/PackageGenerator.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.gen/src/org/eclipse/jpt/jpa/gen/internal/PackageGenerator.java
@@ -44,6 +44,7 @@ import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jpt.common.core.resource.ProjectResourceLocator;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.jpa.core.JpaProject;
+import org.eclipse.jpt.jpa.core.context.persistence.MappingFileRef;
import org.eclipse.jpt.jpa.core.context.persistence.Persistence;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpa.core.resource.xml.JpaXmlResource;
@@ -174,7 +175,6 @@ public class PackageGenerator {
resource.save();
}
- //TODO can probably axe this method
protected void generateXmlInternal(IProgressMonitor monitor) throws Exception {
File templDir = prepareTemplatesFolder("templates/xml_entities/"); //$NON-NLS-1$
@@ -189,6 +189,8 @@ public class PackageGenerator {
return;
}
+ updatePersistenceXmlForMappingFile(this.customizer.getXmlMappingFile());
+
sm.worked(2);
}
@@ -221,6 +223,41 @@ public class PackageGenerator {
return templDir;
}
+ private void updatePersistenceXmlForMappingFile(final String mappingFile) {
+ JpaXmlResource resource = this.jpaProject.getPersistenceXmlResource();
+ if (resource == null) {
+ //the resource would only be null if the persistence.xml file had an invalid content type,
+ //do not attempt to update
+ return;
+ }
+
+ Persistence persistence = this.jpaProject.getRootContextNode().getPersistenceXml().getPersistence();
+ if (persistence == null) {
+ // invalid content, do not attempt to update
+ return;
+ }
+
+ PersistenceUnit persistenceUnit;
+ // create a persistence unit if one doesn't already exist
+ if (persistence.getPersistenceUnitsSize() == 0) {
+ persistenceUnit = persistence.addPersistenceUnit();
+ persistenceUnit.setName(PackageGenerator.this.jpaProject.getName());
+ } else {
+ // we only support one persistence unit - take the first one
+ persistenceUnit = persistence.getPersistenceUnit(0);
+ }
+ boolean addSpecifiedMappingFile = true;
+ for (MappingFileRef mappingFileRef : persistenceUnit.getMappingFileRefs()) {
+ if (mappingFileRef.getFileName().equals(mappingFile)){
+ addSpecifiedMappingFile = false;
+ }
+ }
+ if (addSpecifiedMappingFile){
+ persistenceUnit.addSpecifiedMappingFileRef(mappingFile);
+ resource.save();
+ }
+ }
+
/**
* Saves/Creates the .java file corresponding to a database table
* with the given content.

Back to the top