Author: vyemialyanchyk
Date: 2008-10-16 06:41:47 -0400 (Thu, 16 Oct 2008)
New Revision: 10877
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/AllEntitiesInfoCollector.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefEntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefFieldInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefType.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Document.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Foto.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Passport.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Person.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Staff.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Document.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Foto.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Passport.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Person.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Staff.java
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/plugin.xml
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/.classpath
Log:
JBIDE-2077
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF 2008-10-16
10:41:46 UTC (rev 10876)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/META-INF/MANIFEST.MF 2008-10-16
10:41:47 UTC (rev 10877)
@@ -22,4 +22,8 @@
org.eclipse.ltk.ui.refactoring
Eclipse-LazyStart: true
Export-Package: org.hibernate.eclipse.jdt.ui,
- org.hibernate.eclipse.jdt.ui.internal
+ org.hibernate.eclipse.jdt.ui.internal,
+ org.hibernate.eclipse.jdt.ui.internal.jpa.actions,
+ org.hibernate.eclipse.jdt.ui.internal.jpa.collect,
+ org.hibernate.eclipse.jdt.ui.internal.jpa.common,
+ org.hibernate.eclipse.jdt.ui.internal.jpa.process
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/plugin.xml
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/plugin.xml 2008-10-16
10:41:46 UTC (rev 10876)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/plugin.xml 2008-10-16
10:41:47 UTC (rev 10877)
@@ -38,4 +38,69 @@
<super type="org.eclipse.core.resources.textmarker"/>
<persistent value="true"/>
</extension>
+ <extension point="org.eclipse.ui.actionSets">
+ <!-- main menu -->
+ <actionSet label="Java Coding" description="Action set
containing coding related Java actions"
+ visible="true"
id="org.eclipse.jdt.ui.CodingActionSet3">
+ <menu label="&Source" path="edit"
id="org.eclipse.jdt.ui.source.menu">
+ <separator name="commentGroup"/>
+ <separator name="editGroup"/>
+ <separator name="importGroup"/>
+ <separator name="generateGroup"/>
+ <separator name="codeGroup"/>
+ <separator name="externalizeGroup"/>
+ <separator name="convertGroup"/>
+ </menu>
+ <action
+
class="org.hibernate.eclipse.jdt.ui.internal.jpa.actions.JPAMapToolActionPulldownDelegate"
+ id="org.hibernate.eclipse.jdt.ui.run_jpaaction"
+ label="Generate &Hibernate/JPA annotations"
+ menubarPath="org.eclipse.jdt.ui.source.menu/generateGroup"
+ style="push">
+ </action>
+ </actionSet>
+ </extension>
+ <extension point="org.eclipse.ui.popupMenus">
+ <!-- java editor context menu -->
+ <viewerContribution
+ id="org.hibernate.eclipse.jdt.ui.run_jpa.popupMenu"
+ targetID="#CompilationUnitEditorContext">
+ <menu label="&Source" path="edit"
id="org.eclipse.jdt.ui.source.menu">
+ <separator name="commentGroup"/>
+ <separator name="editGroup"/>
+ <separator name="importGroup"/>
+ <separator name="generateGroup"/>
+ <separator name="codeGroup"/>
+ <separator name="externalizeGroup"/>
+ <separator name="convertGroup"/>
+ </menu>
+ <action
+
class="org.hibernate.eclipse.jdt.ui.internal.jpa.actions.JPAMapToolActionDelegate"
+ id="org.hibernate.eclipse.jdt.ui.run_jpaaction2"
+ label="Generate &Hibernate/JPA annotations"
+ menubarPath="org.eclipse.jdt.ui.source.menu/generateGroup"
+ style="push">
+ </action>
+ </viewerContribution>
+ <!-- java element context menu -->
+ <objectContribution adaptable="false"
+ id="org.hibernate.eclipse.jdt.ui.run_jpa.popupMenu.IJavaElementContribution"
+ objectClass="org.eclipse.jdt.core.IJavaElement">
+ <menu label="&Source" path="edit"
id="org.eclipse.jdt.ui.source.menu">
+ <separator name="commentGroup"/>
+ <separator name="editGroup"/>
+ <separator name="importGroup"/>
+ <separator name="generateGroup"/>
+ <separator name="codeGroup"/>
+ <separator name="externalizeGroup"/>
+ <separator name="convertGroup"/>
+ </menu>
+ <action
class="org.hibernate.eclipse.jdt.ui.internal.jpa.actions.JPAMapToolActionDelegate"
+ id="org.hibernate.eclipse.jdt.ui.run_jpaaction3"
+ label="Generate &Hibernate/JPA annotations"
+ menubarPath="org.eclipse.jdt.ui.source.menu/generateGroup">
+ </action>
+ </objectContribution>
+ </extension>
+
</plugin>
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java 2008-10-16
10:41:46 UTC (rev 10876)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -4,11 +4,14 @@
public class JdtUiMessages extends NLS {
private static final String BUNDLE_NAME =
"org.hibernate.eclipse.jdt.ui.internal.JdtUiMessages"; //$NON-NLS-1$
+ public static String AllEntitiesProcessor_header;
+ public static String AllEntitiesProcessor_message;
public static String CriteriaQuickAssistProcessor_copy_to_criteria_editor;
public static String CriteriaQuickAssistProcessor_errormessage;
public static String DebugJavaCompletionProposalComputer_displaystring;
public static String HQLJavaCompletionProposalComputer_errormessage;
public static String HQLQuickAssistProcessor_copy_to_hql_editor;
+ public static String JPAMapToolActionPulldownDelegate_menu;
public static String SaveQueryEditorListener_replacequestion;
public static String SaveQueryEditorListener_replacetitle;
public static String SaveQueryEditorListener_hql_editor;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties 2008-10-16
10:41:46 UTC (rev 10876)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/JdtUiMessages.properties 2008-10-16
10:41:47 UTC (rev 10877)
@@ -8,12 +8,14 @@
# Contributor:
# Red Hat, Inc. - initial API and implementation
##############################################################################/
-
+AllEntitiesProcessor_header=Hibernate: add JPA annotations
+AllEntitiesProcessor_message=Add JPA annotations to the related set of entities. The
following classes will be changed:
CriteriaQuickAssistProcessor_copy_to_criteria_editor=Copy to Criteria Editor
CriteriaQuickAssistProcessor_errormessage= Could not get document contents for
CriteriaQuickAssist
DebugJavaCompletionProposalComputer_displaystring= I wanna show a dialog!
HQLJavaCompletionProposalComputer_errormessage= Error while performing HQL completion in
java
HQLQuickAssistProcessor_copy_to_hql_editor=Copy to HQL Editor
+JPAMapToolActionPulldownDelegate_menu=JPA annotations
SaveQueryEditorListener_replacequestion= Do you want to save this {0} query into
''{1}''?
SaveQueryEditorListener_replacetitle= Query replace
SaveQueryEditorListener_hql_editor=HQL
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionDelegate.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.actions;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.IHandler;
+import org.eclipse.core.commands.IHandlerListener;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.IEditorActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IViewActionDelegate;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPart;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class JPAMapToolActionDelegate implements IObjectActionDelegate,
+ IEditorActionDelegate, IViewActionDelegate, IHandler {
+
+ public JPAMapToolActor actor = new JPAMapToolActor();
+
+ public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+ }
+
+ public void run(IAction action) {
+ actor.updateSelected();
+ //actor.updateOpen();
+ }
+
+ public void selectionChanged(IAction action, ISelection selection) {
+ actor.updateSelectedItems(selection);
+ if (action != null) {
+ action.setEnabled(actor.getSelectedSize() > 0);
+ }
+ }
+
+ public void setActiveEditor(IAction action, IEditorPart targetEditor) {
+ if (action != null) {
+ action.setEnabled(actor.getSelectedSize() > 0);
+ }
+ }
+
+ public void addHandlerListener(IHandlerListener handlerListener) {
+ }
+
+ public void dispose() {
+ }
+
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ actor.updateSelected();
+ //actor.updateOpen();
+ return null;
+ }
+
+ public boolean isEnabled() {
+ return (actor.getSelectedSize() > 0);
+ }
+
+ public boolean isHandled() {
+ return true;
+ }
+
+ public void removeHandlerListener(IHandlerListener handlerListener) {
+ }
+
+ public void init(IViewPart view) {
+ view = null;
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActionPulldownDelegate.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,153 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.actions;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuCreator;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.swt.events.MenuAdapter;
+import org.eclipse.swt.events.MenuEvent;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
+import org.eclipse.ui.IActionDelegate2;
+import org.eclipse.ui.IEditorActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
+import org.hibernate.eclipse.jdt.ui.internal.JdtUiMessages;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class JPAMapToolActionPulldownDelegate implements
+ IWorkbenchWindowPulldownDelegate2, IEditorActionDelegate, IActionDelegate2,
IMenuCreator {
+
+ /**
+ * The menu created by this action
+ */
+ private Menu fMenu;
+ protected boolean fRecreateMenu = false;
+
+ public JPAMapToolActor actor = new JPAMapToolActor();
+
+ public Menu getMenu(Menu parent) {
+ setMenu(new Menu(parent));
+ fillMenu(fMenu);
+ initMenu();
+ return fMenu;
+ }
+
+ public Menu getMenu(Control parent) {
+ setMenu(new Menu(parent));
+ fillMenu(fMenu);
+ initMenu();
+ return fMenu;
+ }
+
+ public void dispose() {
+ setMenu(null);
+ }
+
+ public void init(IWorkbenchWindow window) {
+ }
+
+ public void run(IAction action) {
+ action = null;
+ }
+
+ public void selectionChanged(IAction action, ISelection selection) {
+ actor.updateSelectedItems(selection);
+ if (action != null) {
+ action.setEnabled(actor.getSelectedSize() > 0);
+ }
+ }
+
+ protected void addToMenu(Menu menu, IAction action, int accelerator) {
+ StringBuffer label = new StringBuffer();
+ if (accelerator >= 0 && accelerator < 10) {
+ // add the numerical accelerator
+ label.append('&');
+ label.append(accelerator);
+ label.append(' ');
+ }
+ label.append(action.getText());
+ action.setText(label.toString());
+ ActionContributionItem item = new ActionContributionItem(action);
+ item.fill(menu, -1);
+ }
+
+ protected void fillMenu(Menu menu) {
+ IAction action = new Action(JdtUiMessages.JPAMapToolActionPulldownDelegate_menu) {
+ public void run() {
+ //actor.updateOpen();
+ actor.updateSelected();
+ }
+ };
+ addToMenu(menu, action, -1);
+ }
+
+ private void initMenu() {
+ // Add listener to re-populate the menu each time
+ // it is shown because of dynamic list
+ fMenu.addMenuListener(new MenuAdapter() {
+ public void menuShown(MenuEvent e) {
+ Menu m = (Menu) e.widget;
+ if (fRecreateMenu) {
+ MenuItem[] items = m.getItems();
+ for (int i = 0; i < items.length; i++) {
+ items[i].dispose();
+ }
+ fillMenu(m);
+ fRecreateMenu = false;
+ }
+ m.setEnabled(actor.getSelectedSize() > 0);
+ MenuItem[] items = m.getItems();
+ for (int i = 0; i < items.length; i++) {
+ items[i].setEnabled(actor.getSelectedSize() > 0);
+ }
+ }
+ public void menuHidden(MenuEvent e) {
+ fRecreateMenu = true;
+ }
+ });
+ }
+
+ private void setMenu(Menu menu) {
+ if (fMenu != null) {
+ fMenu.dispose();
+ }
+ fMenu = menu;
+ }
+
+ public void setActiveEditor(IAction action, IEditorPart targetEditor) {
+ if (action != null) {
+ action.setEnabled(actor.getSelectedSize() > 0);
+ }
+ }
+
+ public void init(IAction action) {
+ if (action instanceof Action) {
+ ((Action)action).setMenuCreator(this);
+ }
+ }
+
+ public void runWithEvent(IAction action, Event event) {
+ //actor.updateOpen();
+ actor.updateSelected();
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/actions/JPAMapToolActor.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,219 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.actions;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.internal.filebuffers.SynchronizableDocument;
+import org.eclipse.core.internal.resources.File;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.eclipse.jdt.internal.core.JavaElement;
+import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.TextSelection;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.TreeSelection;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.jdt.ui.Activator;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.collect.AllEntitiesInfoCollector;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.Utils;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.process.AllEntitiesProcessor;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class JPAMapToolActor {
+
+ protected Set<ICompilationUnit> selectionCU = new
HashSet<ICompilationUnit>();
+ protected AllEntitiesInfoCollector collector = new AllEntitiesInfoCollector();
+ protected AllEntitiesProcessor processor = new AllEntitiesProcessor();
+
+
+ protected org.eclipse.jdt.core.dom.CompilationUnit getCompilationUnit(ICompilationUnit
source) {
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(source);
+ org.eclipse.jdt.core.dom.CompilationUnit result =
(org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null);
+ return result;
+ }
+
+ public void clearSelectionCU() {
+ selectionCU.clear();
+ }
+
+ public void addCompilationUnit(ICompilationUnit cu) {
+ if (cu != null) {
+ selectionCU.add(cu);
+ }
+ }
+
+ public void updateSelected() {
+ Iterator<ICompilationUnit> it = selectionCU.iterator();
+ Map<IJavaProject, Set<ICompilationUnit>> mapJP_CUSet =
+ new HashMap<IJavaProject, Set<ICompilationUnit>>();
+ 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(javaProject);
+ while (setIt.hasNext()) {
+ ICompilationUnit icu = setIt.next();
+ collector.collect(icu);
+ }
+ collector.resolveRelations();
+ processor.modify(javaProject, collector.getMapCUs_Info(), true);
+ }
+ }
+
+ public void updateOpen() {
+ IWorkbench workbench = Activator.getDefault().getWorkbench();
+ IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage();
+
+ IEditorPart editor = page.getActiveEditor();
+ if (editor instanceof CompilationUnitEditor) {
+ CompilationUnitEditor cue = (CompilationUnitEditor)editor;
+ ICompilationUnit cu = (ICompilationUnit)cue.getViewPartInput();
+ if (cu != null) {
+ IJavaProject javaProject = cu.getJavaProject();
+ collector.initCollector(javaProject);
+ collector.collect(cu);
+ collector.resolveRelations();
+ processor.modify(javaProject, collector.getMapCUs_Info(), true);
+ }
+ }
+ }
+
+ public void makePersistent(ICompilationUnit cu) throws CoreException {
+ collector.collect(cu);
+ }
+
+ public int getSelectedSize() {
+ return selectionCU.size();
+ }
+
+ public void updateSelectedItems(ISelection selection) {
+ //System.out.println("Blah! " + selection); //$NON-NLS-1$
+ if (selection instanceof TextSelection) {
+ String fullyQualifiedName = ""; //$NON-NLS-1$
+ IDocument fDocument = null;
+ SynchronizableDocument sDocument = null;
+ org.eclipse.jdt.core.dom.CompilationUnit resultCU = null;
+ Class clazz = selection.getClass();
+ Field fd = null;
+ try {
+ fd = clazz.getDeclaredField("fDocument"); //$NON-NLS-1$
+ } catch (NoSuchFieldException e) {
+ // just ignore it!
+ }
+ if (fd != null) {
+ try {
+ fd.setAccessible(true);
+ fDocument = (IDocument)fd.get(selection);
+ if (fDocument instanceof SynchronizableDocument) {
+ sDocument = (SynchronizableDocument)fDocument;
+ }
+ if (sDocument != null) {
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(sDocument.get().toCharArray());
+ parser.setResolveBindings(false);
+ resultCU = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null);
+ }
+ if (resultCU != null && resultCU.types().size() > 0 ) {
+ if (resultCU.getPackage() != null) {
+ fullyQualifiedName = resultCU.getPackage().getName().getFullyQualifiedName() +
"."; //$NON-NLS-1$
+ }
+ else {
+ fullyQualifiedName = ""; //$NON-NLS-1$
+ }
+ fullyQualifiedName += ((TypeDeclaration)(resultCU.types().get(0))).getName();
+ }
+ } catch (IllegalArgumentException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("IllegalArgumentException:
", e); //$NON-NLS-1$
+ } catch (IllegalAccessException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("IllegalAccessException:
", e); //$NON-NLS-1$
+ } catch (SecurityException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("SecurityException: ",
e); //$NON-NLS-1$
+ }
+ }
+ clearSelectionCU();
+ if (fullyQualifiedName.length() > 0) {
+ ICompilationUnit cu = Utils.findCompilationUnit(fullyQualifiedName);
+ addCompilationUnit(cu);
+ }
+ }
+ else if (selection instanceof TreeSelection) {
+ clearSelectionCU();
+ TreeSelection treeSelection = (TreeSelection)selection;
+ Iterator it = treeSelection.iterator();
+ while (it.hasNext()) {
+ Object obj = it.next();
+ if (obj instanceof ICompilationUnit) {
+ ICompilationUnit cu = (ICompilationUnit)obj;
+ addCompilationUnit(cu);
+ }
+ else if (obj instanceof File) {
+ File file = (File)obj;
+ if (file != null && file.getProject() != null) {
+ IJavaProject javaProject = JavaCore.create(file.getProject());
+ ICompilationUnit[] cus = Utils.findCompilationUnits(javaProject,
+ file.getFullPath());
+ if (cus != null) {
+ for (int i = 0; i < cus.length; i++) {
+ addCompilationUnit(cus[i]);
+ }
+ }
+ }
+ }
+ else if (obj instanceof JavaElement) {
+ JavaElement javaElement = (JavaElement)obj;
+ ICompilationUnit cu = javaElement.getCompilationUnit();
+ addCompilationUnit(cu);
+ }
+ else {
+ // ignore
+ //System.out.println("1 Blah! " + selection); //$NON-NLS-1$
+ }
+ }
+ }
+ else {
+ //System.out.println("2 Blah! " + selection); //$NON-NLS-1$
+ selection = null;
+ }
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/AllEntitiesInfoCollector.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/AllEntitiesInfoCollector.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/AllEntitiesInfoCollector.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,357 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.collect;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.JPAConst;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefFieldInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.Utils;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class AllEntitiesInfoCollector {
+
+ protected IJavaProject javaProject;
+ // fullyQualifiedName -> EntityInfo
+ protected Map<String, EntityInfo> mapCUs_Info = new TreeMap<String,
EntityInfo>();
+
+ public void initCollector(IJavaProject javaProject) {
+ this.javaProject = javaProject;
+ mapCUs_Info.clear();
+ }
+
+ protected class ProcessItem {
+ //
+ public String fieldId = null;
+ public String fieldId2 = null;
+ //
+ public RefEntityInfo refEntityInfo = null;
+ public RefEntityInfo refEntityInfo2 = null;
+ }
+
+ protected abstract class IConnector {
+
+ protected ProcessItem pi;
+
+ public void setProcessItem(ProcessItem processItem) {
+ this.pi = processItem;
+ }
+ public abstract boolean updateRelation();
+ }
+
+ protected class EntityProcessor {
+
+ protected IConnector connector;
+
+ public void setConnector(IConnector connector) {
+ this.connector = connector;
+ }
+
+ public void enumEntityPairs() {
+ Iterator<Map.Entry<String, EntityInfo>> it =
mapCUs_Info.entrySet().iterator();
+ ProcessItem pi = new ProcessItem();
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ // entry.getKey() - fully qualified name
+ // entry.getValue() - EntityInfo
+ EntityInfo entryInfo = entry.getValue();
+ assert(entry.getKey().equals(entryInfo.getFullyQualifiedName()));
+ String fullyQualifiedName = entryInfo.getFullyQualifiedName();
+ // get references map:
+ // * field id -> RefEntityInfo
+ Iterator<Map.Entry<String, RefEntityInfo>> referencesIt =
+ entryInfo.getReferences().entrySet().iterator();
+ while (referencesIt.hasNext()) {
+ Map.Entry<String, RefEntityInfo> entry2 = referencesIt.next();
+ // entry2.getKey() - field id
+ // entry2.getValue() - RefEntityInfo
+ pi.fieldId = entry2.getKey();
+ pi.refEntityInfo = entry2.getValue();
+ String fullyQualifiedName2 = pi.refEntityInfo.fullyQualifiedName;
+ EntityInfo entryInfo2 = mapCUs_Info.get(fullyQualifiedName2);
+ assert(fullyQualifiedName2.equals(entryInfo2.getFullyQualifiedName()));
+ if (entryInfo2 != null && pi.refEntityInfo != null) {
+ pi.refEntityInfo2 = null;
+ pi.fieldId2 = null;
+ Set<RefFieldInfo> setRefEntityInfo =
entryInfo2.getRefFieldInfoSet(fullyQualifiedName);
+ if (setRefEntityInfo != null) {
+ if (setRefEntityInfo.size() == 1) {
+ Iterator<RefFieldInfo> itTmp = setRefEntityInfo.iterator();
+ RefFieldInfo rfi = itTmp.next();
+ pi.fieldId2 = rfi.fieldId;
+ pi.refEntityInfo2 = entryInfo2.getFieldIdRefEntityInfo(pi.fieldId2);
+ }
+ else if (setRefEntityInfo.size() > 1) {
+ // this case of complex decision - omit this,
+ // give other entities opportunity to solve this.
+ // in case of no solution - user should define this himself
+ pi.refEntityInfo2 = null;
+ }
+ }
+ if (connector != null) {
+ connector.setProcessItem(pi);
+ connector.updateRelation();
+ }
+ }
+ }
+ }
+ }
+ }
+
+ public void resolveRelations() {
+ Iterator<Map.Entry<String, EntityInfo>> it = null;
+ // generate RefFieldInfoMap (for simple process)
+ it = mapCUs_Info.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ EntityInfo entryInfo = entry.getValue();
+ entryInfo.generateRefFieldInfoMap();
+ }
+ // update relations
+ EntityProcessor ep = new EntityProcessor();
+ // 0)
+ // process the user prompts
+ IConnector promptsConnector = new IConnector() {
+ public boolean updateRelation() {
+ if (pi == null) {
+ return false;
+ }
+ if (pi.refEntityInfo == null || pi.refEntityInfo2 == null) {
+ return false;
+ }
+ boolean hasPrompt = false;
+ // - use it as prompting from the user
+ if ((pi.fieldId != null && pi.fieldId.equals(pi.refEntityInfo2.mappedBy)) ||
+ (pi.fieldId2 != null && pi.fieldId2.equals(pi.refEntityInfo.mappedBy))) {
+ hasPrompt = true;
+ }
+ if (hasPrompt) {
+ if (pi.refEntityInfo.refType == RefType.ONE2ONE) {
+ if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
+ pi.refEntityInfo.refType = RefType.ONE2ONE;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.ONE2ONE;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
+ pi.refEntityInfo.refType = RefType.MANY2ONE;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.ONE2MANY;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ }
+ else if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
+ if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
+ pi.refEntityInfo.refType = RefType.ONE2MANY;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.MANY2ONE;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
+ pi.refEntityInfo.refType = RefType.MANY2MANY;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.MANY2MANY;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ }
+ }
+ return true;
+ }
+ };
+ ep.setConnector(promptsConnector);
+ ep.enumEntityPairs();
+ // prefer other mapping type to ManyToMany, so
+ // 1)
+ // first - try to assign other relations
+ IConnector simpleRelConnector = new IConnector() {
+ public boolean updateRelation() {
+ if (pi == null) {
+ return false;
+ }
+ if (pi.refEntityInfo == null || pi.refEntityInfo2 == null) {
+ return false;
+ }
+ if (pi.refEntityInfo.mappedBy == null && pi.refEntityInfo2.mappedBy == null)
{
+ if (pi.refEntityInfo.refType == RefType.ONE2ONE) {
+ if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
+ pi.refEntityInfo.refType = RefType.ONE2ONE;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.ONE2ONE;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ else if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
+ pi.refEntityInfo.refType = RefType.MANY2ONE;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.ONE2MANY;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ }
+ else if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
+ if (pi.refEntityInfo2.refType == RefType.ONE2ONE) {
+ pi.refEntityInfo.refType = RefType.ONE2MANY;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.MANY2ONE;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ }
+ }
+ return true;
+ }
+ };
+ ep.setConnector(simpleRelConnector);
+ ep.enumEntityPairs();
+ // 2)
+ // second - try to assign - ManyToMany
+ // remember - here prefer other mapping type to ManyToMany
+ IConnector m2mRelConnector = new IConnector() {
+ public boolean updateRelation() {
+ if (pi == null) {
+ return false;
+ }
+ if (pi.refEntityInfo == null || pi.refEntityInfo2 == null) {
+ return false;
+ }
+ if (pi.refEntityInfo.mappedBy == null && pi.refEntityInfo2.mappedBy == null)
{
+ if (pi.refEntityInfo.refType == RefType.ONE2MANY) {
+ if (pi.refEntityInfo2.refType == RefType.ONE2MANY) {
+ if (pi.refEntityInfo2.mappedBy == null) {
+ pi.refEntityInfo.refType = RefType.MANY2MANY;
+ pi.refEntityInfo.mappedBy = pi.fieldId2;
+ pi.refEntityInfo2.refType = RefType.MANY2MANY;
+ pi.refEntityInfo2.mappedBy = pi.fieldId;
+ }
+ }
+ }
+ }
+ return true;
+ }
+ };
+ ep.setConnector(m2mRelConnector);
+ ep.enumEntityPairs();
+ // update import flags
+ it = mapCUs_Info.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ EntityInfo entryInfo = entry.getValue();
+ Iterator<Map.Entry<String, RefEntityInfo>> referencesIt =
+ entryInfo.getReferences().entrySet().iterator();
+ boolean isOne2One = false;
+ boolean isOne2Many = false;
+ boolean isMany2One = false;
+ boolean isMany2Many = false;
+ for ( ; referencesIt.hasNext(); ) {
+ Map.Entry<String, RefEntityInfo> entry2 = referencesIt.next();
+ RefEntityInfo refEntityInfo = entry2.getValue();
+ if (refEntityInfo != null) {
+ if (refEntityInfo.refType == RefType.ONE2ONE &&
!refEntityInfo.resolvedAnnotationName) {
+ isOne2One = true;
+ }
+ else if (refEntityInfo.refType == RefType.ONE2MANY &&
!refEntityInfo.resolvedAnnotationName) {
+ isOne2Many = true;
+ }
+ else if (refEntityInfo.refType == RefType.MANY2ONE &&
!refEntityInfo.resolvedAnnotationName) {
+ isMany2One = true;
+ }
+ else if (refEntityInfo.refType == RefType.MANY2MANY &&
!refEntityInfo.resolvedAnnotationName) {
+ isMany2Many = true;
+ }
+ }
+ }
+ if (isOne2One) {
+ entryInfo.addRequiredImport(JPAConst.IMPORT_ONE2ONE);
+ }
+ if (isOne2Many) {
+ entryInfo.addRequiredImport(JPAConst.IMPORT_ONE2MANY);
+ }
+ if (isMany2One) {
+ entryInfo.addRequiredImport(JPAConst.IMPORT_MANY2ONE);
+ }
+ if (isMany2Many) {
+ entryInfo.addRequiredImport(JPAConst.IMPORT_MANY2MANY);
+ }
+ }
+ // re-generate RefFieldInfoMap (for simple process)
+ it = mapCUs_Info.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ EntityInfo entryInfo = entry.getValue();
+ entryInfo.generateRefFieldInfoMap();
+ }
+ // if the parent has primary id - child should not generate it
+ it = mapCUs_Info.entrySet().iterator();
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ EntityInfo entryInfo = entry.getValue();
+ adjustParentId(entryInfo);
+ }
+ }
+
+ public void adjustParentId(EntityInfo ei) {
+ if (ei == null) {
+ return;
+ }
+ EntityInfo parentEI = mapCUs_Info.get(ei.getFullyQualifiedParentName());
+ adjustParentId(parentEI);
+ ei.adjustPrimaryId(parentEI);
+ }
+
+ public void collect(String fullyQualifiedName) {
+
+ if (fullyQualifiedName == null) {
+ return;
+ }
+ if (mapCUs_Info.containsKey(fullyQualifiedName)) {
+ return;
+ }
+ ICompilationUnit icu = Utils.findCompilationUnit(javaProject, fullyQualifiedName);
+ collect(icu);
+ }
+
+ public void collect(ICompilationUnit icu) {
+
+ if (icu == null) {
+ return;
+ }
+ org.eclipse.jdt.core.dom.CompilationUnit cu = Utils.getCompilationUnit(icu, true);
+ String fullyQualifiedName =
cu.getTypeRoot().findPrimaryType().getFullyQualifiedName();
+ if (mapCUs_Info.containsKey(fullyQualifiedName)) {
+ return;
+ }
+ CollectEntityInfo finder = new CollectEntityInfo();
+ cu.accept(finder);
+ EntityInfo result = finder.getEntityInfo();
+ result.adjustParameters();
+ if (result != null) {
+ mapCUs_Info.put(fullyQualifiedName, result);
+ }
+ Iterator<String> it = result.getDependences();
+ while (it.hasNext()) {
+ String fullyQualifiedNameTmp = it.next();
+ collect(fullyQualifiedNameTmp);
+ }
+ }
+
+ public Map<String, EntityInfo> getMapCUs_Info() {
+ return mapCUs_Info;
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,348 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.collect;
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.Annotation;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.ArrayType;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.eclipse.jdt.core.dom.ITypeBinding;
+import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.eclipse.jdt.core.dom.MarkerAnnotation;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.Modifier;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.ParameterizedType;
+import org.eclipse.jdt.core.dom.PrimitiveType;
+import org.eclipse.jdt.core.dom.QualifiedType;
+import org.eclipse.jdt.core.dom.SimpleType;
+import org.eclipse.jdt.core.dom.Type;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
+import org.eclipse.jdt.core.dom.WildcardType;
+import org.eclipse.jdt.internal.core.BinaryType;
+import org.eclipse.jdt.internal.core.SourceType;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.JPAConst;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class CollectEntityInfo extends ASTVisitor {
+
+ protected EntityInfo entityInfo = new EntityInfo();
+
+ public EntityInfo getEntityInfo() {
+ return entityInfo;
+ }
+
+ public boolean visit(CompilationUnit node) {
+ entityInfo.setFullyQualifiedName(
+ node.getTypeRoot().findPrimaryType().getFullyQualifiedName());
+ if (node.getProblems().length > 0) {
+ entityInfo.setCompilerProblemsFlag(true);
+ }
+ return true;
+ }
+
+ public boolean visit(MarkerAnnotation node) {
+ return processAnnotation(node, null);
+ }
+
+ public boolean visit(NormalAnnotation node) {
+ // try to extract mapping prompts
+ String mappedBy = null;
+ Iterator it = node.values().iterator();
+ while (it.hasNext()) {
+ MemberValuePair mvp = (MemberValuePair)it.next();
+ if ("mappedBy".equals(mvp.getName().toString())) { //$NON-NLS-1$
+ mappedBy = mvp.getValue().toString().replaceAll("\"", "");
//$NON-NLS-1$ //$NON-NLS-2$
+ break;
+ }
+ }
+ return processAnnotation(node, mappedBy);
+ }
+
+ public boolean processAnnotation(Annotation node, String mappedBy) {
+ String fullyQualifiedName = node.getTypeName().getFullyQualifiedName();
+ if (JPAConst.isAnnotationEntity(fullyQualifiedName)) {
+ ITypeBinding tb = node.resolveTypeBinding();
+ CompilationUnit cu = null;
+ ASTNode astNode = node.getParent();
+ if (astNode instanceof TypeDeclaration) {
+ astNode = astNode.getParent();
+ if (astNode instanceof CompilationUnit) {
+ cu = (CompilationUnit)astNode;
+ }
+ }
+ if (cu != null) {
+ entityInfo.setAddEntityFlag(false);
+ if (tb == null) {
+ entityInfo.addRequiredImport(JPAConst.IMPORT_ENTITY);
+ }
+ }
+ }
+ else if (JPAConst.isAnnotationId(fullyQualifiedName)) {
+ ITypeBinding tb = node.resolveTypeBinding();
+ CompilationUnit cu = null;
+ ASTNode astNode = node.getParent();
+ if (astNode instanceof FieldDeclaration ||
+ astNode instanceof MethodDeclaration) {
+ astNode = astNode.getParent();
+ if (astNode instanceof TypeDeclaration) {
+ astNode = astNode.getParent();
+ if (astNode instanceof CompilationUnit) {
+ cu = (CompilationUnit)astNode;
+ }
+ }
+ }
+ if (cu != null) {
+ if (tb == null) {
+ entityInfo.addRequiredImport(JPAConst.IMPORT_ID);
+ }
+ entityInfo.setAddPrimaryIdFlag(false);
+ }
+ }
+ else if (JPAConst.isAnnotationGeneratedValue(fullyQualifiedName)) {
+ ITypeBinding tb = node.resolveTypeBinding();
+ CompilationUnit cu = null;
+ ASTNode astNode = node.getParent();
+ if (astNode instanceof FieldDeclaration ||
+ astNode instanceof MethodDeclaration) {
+ astNode = astNode.getParent();
+ if (astNode instanceof TypeDeclaration) {
+ astNode = astNode.getParent();
+ if (astNode instanceof CompilationUnit) {
+ cu = (CompilationUnit)astNode;
+ }
+ }
+ }
+ if (cu != null) {
+ if (tb == null) {
+ entityInfo.addRequiredImport(JPAConst.IMPORT_GENERATED_VALUE);
+ }
+ entityInfo.setAddGeneratedValueFlag(false);
+ }
+ }
+ else if (JPAConst.isAnnotationOne2One(fullyQualifiedName)) {
+ updateAnnotationRelInfo(node, mappedBy, fullyQualifiedName,
+ RefType.ONE2ONE, JPAConst.ANNOTATION_ONE2ONE, JPAConst.IMPORT_ONE2ONE);
+ }
+ else if (JPAConst.isAnnotationOne2Many(fullyQualifiedName)) {
+ updateAnnotationRelInfo(node, mappedBy, fullyQualifiedName,
+ RefType.ONE2MANY, JPAConst.ANNOTATION_ONE2MANY, JPAConst.IMPORT_ONE2MANY);
+ }
+ else if (JPAConst.isAnnotationMany2One(fullyQualifiedName)) {
+ updateAnnotationRelInfo(node, mappedBy, fullyQualifiedName,
+ RefType.MANY2ONE, JPAConst.ANNOTATION_MANY2ONE, JPAConst.IMPORT_MANY2ONE);
+ }
+ else if (JPAConst.isAnnotationMany2Many(fullyQualifiedName)) {
+ updateAnnotationRelInfo(node, mappedBy, fullyQualifiedName,
+ RefType.MANY2MANY, JPAConst.ANNOTATION_MANY2MANY, JPAConst.IMPORT_MANY2MANY);
+ }
+ return true;
+ }
+
+ public void updateAnnotationRelInfo(Annotation node, String mappedBy, String
fullyQualifiedName,
+ RefType type, String annNameShort, String annNameFull) {
+ ITypeBinding tb = node.resolveTypeBinding();
+ CompilationUnit cu = null;
+ ASTNode astNode = node.getParent();
+ if (astNode instanceof FieldDeclaration) {
+ FieldDeclaration fd = (FieldDeclaration)astNode;
+ Iterator itVarNames = fd.fragments().iterator();
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ entityInfo.updateReference(name, true, type, mappedBy,
+ 0 != annNameShort.compareTo(fullyQualifiedName));
+ }
+ astNode = astNode.getParent();
+ if (astNode instanceof TypeDeclaration) {
+ astNode = astNode.getParent();
+ if (astNode instanceof CompilationUnit) {
+ cu = (CompilationUnit)astNode;
+ }
+ }
+ }
+ if (cu != null) {
+ if (tb == null) {
+ entityInfo.addRequiredImport(annNameFull);
+ }
+ }
+ }
+
+ public boolean visit(ImportDeclaration node) {
+ String name = node.getName().getFullyQualifiedName();
+ for (int i = 0; i < JPAConst.ALL_IMPORTS.size(); i++) {
+ String tmp = JPAConst.ALL_IMPORTS.get(i);
+ if (tmp.compareTo(name) == 0) {
+ entityInfo.addExistingImport(name);
+ break;
+ }
+ }
+ return true;
+ }
+
+ public boolean visit(Modifier node) {
+ if (node.isAbstract()) {
+ entityInfo.setAbstractFlag(true);
+ }
+ return true;
+ }
+
+ public boolean visit(TypeDeclaration node) {
+ entityInfo.setAbstractFlag(entityInfo.isAbstractFlag() || node.isInterface());
+ entityInfo.setInterfaceFlag(node.isInterface());
+ Type superType = node.getSuperclassType();
+ if (superType != null) {
+ ITypeBinding tb = superType.resolveBinding();
+ String entityFullyQualifiedName = ""; //$NON-NLS-1$
+ if (tb.getJavaElement() instanceof SourceType) {
+ SourceType sourceT = (SourceType)tb.getJavaElement();
+ try {
+ entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("error", e);
+ }
+ }
+ entityInfo.addDependency(entityFullyQualifiedName);
+ entityInfo.setFullyQualifiedParentName(entityFullyQualifiedName);
+ }
+ List superInterfaces = node.superInterfaceTypes();
+ Iterator it = superInterfaces.iterator();
+ while (it.hasNext()) {
+ Object obj = it.next();
+ if (obj instanceof SimpleType) {
+ SimpleType st = (SimpleType)obj;
+ String fullyQualifiedName = st.getName().getFullyQualifiedName();
+ if (JPAConst.IMPORT_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
+ entityInfo.setAddSerializableInterfaceFlag(false);
+ }
+ else if (JPAConst.ANNOTATION_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
+ entityInfo.setAddSerializableInterfaceFlag(false);
+ entityInfo.addRequiredImport(JPAConst.IMPORT_SERIALIZABLE);
+ }
+ }
+ }
+ ITypeBinding tb = node.resolveBinding();
+ return true;
+ }
+
+ public boolean visit(MethodDeclaration node) {
+ if (node.getName().getFullyQualifiedName().compareTo(entityInfo.getName()) == 0) {
+ // this is constructor declaration - process it
+ entityInfo.setImplicitConstructorFlag(false);
+ if (node.parameters().size() == 0) {
+ entityInfo.setDefaultConstructorFlag(true);
+ }
+ }
+ return true;
+ }
+
+ public boolean visit(FieldDeclaration node) {
+ if (node.getType().isPrimitiveType()) {
+ PrimitiveType pt = (PrimitiveType)node.getType();
+ ITypeBinding tb = pt.resolveBinding();
+ } else if (node.getType().isSimpleType()) {
+ SimpleType st = (SimpleType)node.getType();
+ ITypeBinding tb = st.resolveBinding();
+ String entityFullyQualifiedName = ""; //$NON-NLS-1$
+ if (tb.getJavaElement() instanceof SourceType) {
+ SourceType sourceT = (SourceType)tb.getJavaElement();
+ try {
+ entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException:
", e); //$NON-NLS-1$
+ }
+ entityInfo.addDependency(entityFullyQualifiedName);
+ Iterator itVarNames = node.fragments().iterator();
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2ONE);
+ }
+ }
+ else if (tb.getJavaElement() instanceof BinaryType) {
+ ITypeBinding tbParent = tb.getTypeDeclaration().getSuperclass();
+ if (tbParent != null &&
"java.lang.Number".equals(tbParent.getBinaryName())) { //$NON-NLS-1$
+ // this is candidate for primary id
+ Iterator itVarNames = node.fragments().iterator();
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ entityInfo.addPrimaryIdCandidate(name);
+ }
+ }
+ }
+ } else if (node.getType().isArrayType()) {
+ ArrayType at = (ArrayType)node.getType();
+ ITypeBinding tb = at.resolveBinding();
+ } else if (node.getType().isParameterizedType()) {
+ ParameterizedType pt = (ParameterizedType)node.getType();
+ Type type = (Type)pt.getType();
+ ITypeBinding tb = type.resolveBinding();
+ ITypeBinding[] interfaces = tb.getTypeDeclaration().getInterfaces();
+ String fullyQualifiedNameTypeName = ""; //$NON-NLS-1$
+ for (int i = 0; i < interfaces.length; i++) {
+ if (interfaces[i].getJavaElement() instanceof BinaryType) {
+ BinaryType binaryT = (BinaryType)interfaces[i].getJavaElement();
+ String tmp = binaryT.getFullyQualifiedName('.');
+ if (0 == "java.util.Collection".compareTo(tmp)) { //$NON-NLS-1$
+ fullyQualifiedNameTypeName = tmp;
+ break;
+ }
+ }
+ }
+ if (fullyQualifiedNameTypeName.length() > 0) {
+ Iterator typeArgsIt = pt.typeArguments().iterator();
+ while (typeArgsIt.hasNext()) {
+ type = (Type)typeArgsIt.next();
+ tb = type.resolveBinding();
+ String entityFullyQualifiedName = ""; //$NON-NLS-1$
+ if (tb.getJavaElement() instanceof SourceType) {
+ SourceType sourceT = (SourceType)tb.getJavaElement();
+ try {
+ entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException:
", e); //$NON-NLS-1$
+ }
+ entityInfo.addDependency(entityFullyQualifiedName);
+ Iterator itVarNames = node.fragments().iterator();
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
+ }
+ }
+ }
+ }
+ } else if (node.getType().isQualifiedType()) {
+ QualifiedType qt = (QualifiedType)node.getType();
+ ITypeBinding tb = qt.resolveBinding();
+ } else if (node.getType().isWildcardType()) {
+ WildcardType wt = (WildcardType)node.getType();
+ ITypeBinding tb = wt.resolveBinding();
+ }
+ return true;
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/EntityInfo.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,380 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class EntityInfo {
+
+ /*
+ * fully qualified entity name
+ */
+ protected String fullyQualifiedName = ""; //$NON-NLS-1$
+ /*
+ * fully qualified parent name
+ */
+ protected String fullyQualifiedParentName = ""; //$NON-NLS-1$
+ /*
+ * true in case of compiler problems
+ */
+ protected boolean compilerProblemsFlag = false;
+ /*
+ * true in case of implicit constructor
+ */
+ protected boolean implicitConstructorFlag = true;
+ /*
+ * true in case of default constructor
+ */
+ protected boolean defaultConstructorFlag = false;
+ /*
+ * if true - "@Entity" annotation should be added
+ */
+ protected boolean addEntityFlag = true;
+ /*
+ * existing imports set
+ */
+ protected Set<String> setExistingImports = new TreeSet<String>();
+ /*
+ * required imports set
+ */
+ protected Set<String> setRequiredImports = new TreeSet<String>();
+ /*
+ * if true - "implements java.io.Serializable" or
+ * "implements Serializable" should be added
+ */
+ protected boolean addSerializableInterfaceFlag = true;
+ /*
+ * if true - this is abstract class declaration
+ */
+ protected boolean isAbstractFlag = false;
+ /*
+ * if true - this is interface declaration
+ */
+ protected boolean isInterfaceFlag = false;
+ /*
+ * paths to entity java files which has a reference from current entity
+ */
+ protected Set<String> dependences = new TreeSet<String>();
+ /*
+ * reference to primary id property
+ */
+ protected String primaryIdName = ""; //$NON-NLS-1$
+ /*
+ * if true - add primary id marker for primaryIdName
+ */
+ protected boolean addPrimaryIdFlag = true;
+ /*
+ * if true - add generated value marker for primaryIdName
+ */
+ protected boolean addGeneratedValueFlag = true;
+ /*
+ * define relations between entities
+ * field id -> RefEntityInfo
+ */
+ protected Map<String, RefEntityInfo> references = new TreeMap<String,
RefEntityInfo>();
+ /*
+ * fully qualified entity name -> Set<RefFieldInfo>
+ * this is generated from references map for easy information get
+ */
+ protected Map<String, Set<RefFieldInfo>> mapRefFieldInfo = null;
+ /*
+ */
+ protected List<String> primaryIdCandidates = new ArrayList<String>();
+
+ public void generateRefFieldInfoMap() {
+ mapRefFieldInfo = new TreeMap<String, Set<RefFieldInfo>>();
+ Iterator<Map.Entry<String, RefEntityInfo>> referencesIt =
+ getReferences().entrySet().iterator();
+ while (referencesIt.hasNext()) {
+ Map.Entry<String, RefEntityInfo> entry = referencesIt.next();
+ RefEntityInfo refEntityInfo = entry.getValue();
+ Set<RefFieldInfo> fieldInfoSet = null;
+ if (mapRefFieldInfo.containsKey(refEntityInfo.fullyQualifiedName)) {
+ fieldInfoSet = mapRefFieldInfo.get(refEntityInfo.fullyQualifiedName);
+ }
+ else {
+ fieldInfoSet = new TreeSet<RefFieldInfo>();
+ }
+ RefFieldInfo fieldInfo = new RefFieldInfo(entry.getKey(), refEntityInfo.refType);
+ fieldInfoSet.add(fieldInfo);
+ mapRefFieldInfo.put(refEntityInfo.fullyQualifiedName, fieldInfoSet);
+ }
+ referencesIt = null;
+ }
+
+ public void adjustPrimaryId(EntityInfo parentEI) {
+ if (parentEI != null) {
+ String parentPrimaryIdName = parentEI.getPrimaryIdName();
+ if (parentPrimaryIdName != null && parentPrimaryIdName.length() > 0) {
+ primaryIdName = ""; //$NON-NLS-1$
+ }
+ }
+ if (isAddPrimaryIdFlag()) {
+ setAddPrimaryIdFlag(primaryIdName.length() > 0);
+ if (isAddPrimaryIdFlag()) {
+ addRequiredImport(JPAConst.IMPORT_ID);
+ }
+ }
+ if (isAddGeneratedValueFlag()) {
+ setAddGeneratedValueFlag(primaryIdName.length() > 0);
+ if (isAddGeneratedValueFlag()) {
+ addRequiredImport(JPAConst.IMPORT_GENERATED_VALUE);
+ }
+ }
+ }
+
+ public RefType getFieldIdRelValue(String fieldId) {
+ if (references == null || !references.containsKey(fieldId)) {
+ return RefType.UNDEF;
+ }
+ return references.get(fieldId).refType;
+ }
+
+ public boolean getFieldIdAnnotatedValue(String fieldId) {
+ if (references == null || !references.containsKey(fieldId)) {
+ return false;
+ }
+ return references.get(fieldId).annotated;
+ }
+
+ public String getFieldIdFQNameValue(String fieldId) {
+ if (references == null || !references.containsKey(fieldId)) {
+ return ""; //$NON-NLS-1$
+ }
+ return references.get(fieldId).fullyQualifiedName;
+ }
+
+ public RefEntityInfo getFieldIdRefEntityInfo(String fieldId) {
+ if (references == null || !references.containsKey(fieldId)) {
+ return null;
+ }
+ return references.get(fieldId);
+ }
+
+ public void adjustParameters() {
+ if (isImplicitConstructorFlag() == true || isDefaultConstructorFlag() == true) {
+ setAddSerializableInterfaceFlag(false);
+ }
+ else {
+ addRequiredImport(JPAConst.IMPORT_SERIALIZABLE);
+ }
+ if (isAddEntityFlag()) {
+ addRequiredImport(JPAConst.IMPORT_ENTITY);
+ }
+ // try to intellectually get primary id
+ primaryIdName = null;
+ String entityName = getName().toLowerCase();
+ Iterator<String> it = primaryIdCandidates.iterator();
+ while (it.hasNext()) {
+ String name = it.next();
+ String check = name.toLowerCase();
+ if ("id".equalsIgnoreCase(check)) { //$NON-NLS-1$
+ primaryIdName = name;
+ break;
+ }
+ else if (check.indexOf("id") != -1 && check.indexOf(entityName) !=
-1) { //$NON-NLS-1$
+ if (primaryIdName == null) {
+ primaryIdName = name;
+ }
+ else if (primaryIdName.length() > name.length()) {
+ primaryIdName = name;
+ }
+ }
+ else if (check.indexOf("id") != -1) { //$NON-NLS-1$
+ if (primaryIdName == null) {
+ primaryIdName = name;
+ }
+ else if (primaryIdName.toLowerCase().indexOf(entityName) == -1 &&
+ primaryIdName.length() > name.length()) {
+ primaryIdName = name;
+ }
+ }
+ }
+ if (primaryIdName == null) {
+ primaryIdName = ""; //$NON-NLS-1$
+ }
+ }
+
+ public String getName() {
+ String[] arr = fullyQualifiedName.split("\\."); //$NON-NLS-1$
+ if (arr.length > 0) {
+ return arr[arr.length - 1];
+ }
+ return ""; //$NON-NLS-1$
+ }
+
+ public String getFullyQualifiedName() {
+ return fullyQualifiedName;
+ }
+
+ public void setFullyQualifiedName(String fullyQualifiedName) {
+ this.fullyQualifiedName = fullyQualifiedName;
+ }
+
+ public String getFullyQualifiedParentName() {
+ return fullyQualifiedParentName;
+ }
+
+ public void setFullyQualifiedParentName(String fullyQualifiedParentName) {
+ this.fullyQualifiedParentName = fullyQualifiedParentName;
+ }
+
+ public boolean isCompilerProblemsFlag() {
+ return compilerProblemsFlag;
+ }
+
+ public void setCompilerProblemsFlag(boolean compilerProblemsFlag) {
+ this.compilerProblemsFlag = compilerProblemsFlag;
+ }
+
+ public boolean isImplicitConstructorFlag() {
+ return implicitConstructorFlag;
+ }
+
+ public void setImplicitConstructorFlag(boolean implicitConstructorFlag) {
+ this.implicitConstructorFlag = implicitConstructorFlag;
+ }
+
+ public boolean isDefaultConstructorFlag() {
+ return defaultConstructorFlag;
+ }
+
+ public void setDefaultConstructorFlag(boolean defaultConstructorFlag) {
+ this.defaultConstructorFlag = defaultConstructorFlag;
+ }
+
+ public boolean isAddEntityFlag() {
+ return addEntityFlag;
+ }
+
+ public void setAddEntityFlag(boolean addEntityFlag) {
+ this.addEntityFlag = addEntityFlag;
+ }
+
+ public boolean isAddSerializableInterfaceFlag() {
+ return addSerializableInterfaceFlag;
+ }
+
+ public void setAddSerializableInterfaceFlag(boolean addSerializableInterfaceFlag) {
+ this.addSerializableInterfaceFlag = addSerializableInterfaceFlag;
+ }
+
+ public boolean isAbstractFlag() {
+ return isAbstractFlag;
+ }
+
+ public void setAbstractFlag(boolean isAbstractFlag) {
+ this.isAbstractFlag = isAbstractFlag;
+ }
+
+ public boolean isInterfaceFlag() {
+ return isInterfaceFlag;
+ }
+
+ public void setInterfaceFlag(boolean isInterfaceFlag) {
+ this.isInterfaceFlag = isInterfaceFlag;
+ }
+
+ public String getPrimaryIdName() {
+ return primaryIdName;
+ }
+
+ public void setPrimaryIdName(String primaryIdName) {
+ this.primaryIdName = primaryIdName;
+ }
+
+ public Map<String, RefEntityInfo> getReferences() {
+ return references;
+ }
+
+ public void addReference(String fieldId, String fullyQualifiedName, RefType refType) {
+ references.put(fieldId, new RefEntityInfo(fullyQualifiedName, refType));
+ }
+
+ public void updateReference(String fieldId, boolean annotated, RefType refType, String
mappedBy,
+ boolean resolvedAnnotationName) {
+ RefEntityInfo rei = references.get(fieldId);
+ if (rei != null) {
+ rei.annotated = annotated;
+ rei.refType = refType;
+ rei.mappedBy = mappedBy;
+ rei.resolvedAnnotationName = resolvedAnnotationName;
+ }
+ }
+
+ public Set<RefFieldInfo> getRefFieldInfoSet(String fullyQualifiedName) {
+ if (mapRefFieldInfo == null) {
+ return null;
+ }
+ return mapRefFieldInfo.get(fullyQualifiedName);
+ }
+
+ public Iterator<String> getDependences() {
+ return dependences.iterator();
+ }
+
+ public void addDependency(String entityFullyQualifiedName) {
+ dependences.add(entityFullyQualifiedName);
+ }
+
+ public void addPrimaryIdCandidate(String name) {
+ if (name != null && name.length() > 0) {
+ primaryIdCandidates.add(name);
+ }
+ }
+
+ public boolean isAddPrimaryIdFlag() {
+ return addPrimaryIdFlag;
+ }
+
+ public void setAddPrimaryIdFlag(boolean addPrimaryIdFlag) {
+ this.addPrimaryIdFlag = addPrimaryIdFlag;
+ }
+
+ public boolean isAddGeneratedValueFlag() {
+ return addGeneratedValueFlag;
+ }
+
+ public void setAddGeneratedValueFlag(boolean addGeneratedValueFlag) {
+ this.addGeneratedValueFlag = addGeneratedValueFlag;
+ }
+
+ public void addExistingImport(String existingImport) {
+ setExistingImports.add(existingImport);
+ }
+
+ public void removeExistingImport(String existingImport) {
+ setExistingImports.remove(existingImport);
+ }
+
+ public void addRequiredImport(String requiredImport) {
+ setRequiredImports.add(requiredImport);
+ }
+
+ public void removeRequiredImport(String requiredImport) {
+ setRequiredImports.remove(requiredImport);
+ }
+
+ public boolean needImport(String checkImport) {
+ return (!setExistingImports.contains(checkImport) &&
setRequiredImports.contains(checkImport));
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/JPAConst.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,109 @@
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+import java.util.Vector;
+
+public class JPAConst {
+
+ static public String IMPORT_SERIALIZABLE = "java.io.Serializable";
//$NON-NLS-1$
+ static public String IMPORT_ENTITY = "javax.persistence.Entity";
//$NON-NLS-1$
+ static public String IMPORT_GENERATED_VALUE =
"javax.persistence.GeneratedValue"; //$NON-NLS-1$
+ static public String IMPORT_ID = "javax.persistence.Id"; //$NON-NLS-1$
+ static public String IMPORT_ONE2ONE = "javax.persistence.OneToOne";
//$NON-NLS-1$
+ static public String IMPORT_ONE2MANY = "javax.persistence.OneToMany";
//$NON-NLS-1$
+ static public String IMPORT_MANY2ONE = "javax.persistence.ManyToOne";
//$NON-NLS-1$
+ static public String IMPORT_MANY2MANY = "javax.persistence.ManyToMany";
//$NON-NLS-1$
+
+ static public String ANNOTATION_SERIALIZABLE = "Serializable"; //$NON-NLS-1$
+ static public String ANNOTATION_ENTITY = "Entity"; //$NON-NLS-1$
+ static public String ANNOTATION_GENERATED_VALUE = "GeneratedValue";
//$NON-NLS-1$
+ static public String ANNOTATION_ID = "Id"; //$NON-NLS-1$
+ static public String ANNOTATION_ONE2ONE = "OneToOne"; //$NON-NLS-1$
+ static public String ANNOTATION_ONE2MANY = "OneToMany"; //$NON-NLS-1$
+ static public String ANNOTATION_MANY2ONE = "ManyToOne"; //$NON-NLS-1$
+ static public String ANNOTATION_MANY2MANY = "ManyToMany"; //$NON-NLS-1$
+
+ static public Vector<String> ALL_IMPORTS = new Vector<String>();
+
+ static {
+ //ALL_IMPORTS.add(IMPORT_SERIALIZABLE);
+ ALL_IMPORTS.add(IMPORT_ENTITY);
+ ALL_IMPORTS.add(IMPORT_GENERATED_VALUE);
+ ALL_IMPORTS.add(IMPORT_ID);
+ ALL_IMPORTS.add(IMPORT_ONE2ONE);
+ ALL_IMPORTS.add(IMPORT_ONE2MANY);
+ ALL_IMPORTS.add(IMPORT_MANY2ONE);
+ ALL_IMPORTS.add(IMPORT_MANY2MANY);
+ }
+
+ static public String getRefType(RefType refType) {
+ if (refType == RefType.ONE2ONE) {
+ return ANNOTATION_ONE2ONE;
+ }
+ else if (refType == RefType.ONE2MANY) {
+ return ANNOTATION_ONE2MANY;
+ }
+ else if (refType == RefType.MANY2ONE) {
+ return ANNOTATION_MANY2ONE;
+ }
+ else if (refType == RefType.MANY2MANY) {
+ return ANNOTATION_MANY2MANY;
+ }
+ return ""; //$NON-NLS-1$
+ }
+
+ static public boolean isAnnotationEntity(String fullyQualifiedName) {
+ if (ANNOTATION_ENTITY.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_ENTITY.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ static public boolean isAnnotationId(String fullyQualifiedName) {
+ if (ANNOTATION_ID.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_ID.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ static public boolean isAnnotationGeneratedValue(String fullyQualifiedName) {
+ if (ANNOTATION_GENERATED_VALUE.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_GENERATED_VALUE.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ static public boolean isAnnotationOne2One(String fullyQualifiedName) {
+ if (ANNOTATION_ONE2ONE.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_ONE2ONE.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ static public boolean isAnnotationOne2Many(String fullyQualifiedName) {
+ if (ANNOTATION_ONE2MANY.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_ONE2MANY.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ static public boolean isAnnotationMany2One(String fullyQualifiedName) {
+ if (ANNOTATION_MANY2ONE.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_MANY2ONE.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ static public boolean isAnnotationMany2Many(String fullyQualifiedName) {
+ if (ANNOTATION_MANY2MANY.compareTo(fullyQualifiedName) == 0 ||
+ IMPORT_MANY2MANY.compareTo(fullyQualifiedName) == 0) {
+ return true;
+ }
+ return false;
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefEntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefEntityInfo.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefEntityInfo.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class RefEntityInfo implements Comparable<RefEntityInfo> {
+ /*
+ * fully qualified entity name
+ */
+ public String fullyQualifiedName;
+ /*
+ * ONE2ONE - one2one reference
+ * ONE2MANY - one2many reference
+ * MANY2ONE - many2one reference
+ * MANY2MANY - many2many reference
+ * so it be possible to resolve @OneToMany & @ManyToMany
+ */
+ public RefType refType;
+ /*
+ * mappedBy attribute for reference
+ */
+ public String mappedBy = null;
+ //
+ public boolean annotated = false;
+ //
+ public boolean resolvedAnnotationName = false;
+ //
+ public boolean hasGetter = false;
+ //
+ public boolean hasSetter = false;
+ //
+ public RefEntityInfo(String fullyQualifiedName, RefType refType) {
+ this.fullyQualifiedName = fullyQualifiedName;
+ this.refType = refType;
+ }
+
+ public int compareTo(RefEntityInfo rei) {
+ return fullyQualifiedName.compareTo(rei.fullyQualifiedName);
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefFieldInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefFieldInfo.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefFieldInfo.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,40 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class RefFieldInfo implements Comparable<RefFieldInfo> {
+ /*
+ * field id
+ */
+ public String fieldId;
+ /*
+ * ONE2ONE - one2one reference
+ * ONE2MANY - one2many reference
+ * MANY2ONE - many2one reference
+ * MANY2MANY - many2many reference
+ * so it be possible to resolve @OneToMany & @ManyToMany
+ */
+ public RefType refType;
+ //
+ public RefFieldInfo(String fieldId, RefType refType) {
+ this.fieldId = fieldId;
+ this.refType = refType;
+ }
+
+ public int compareTo(RefFieldInfo rfi) {
+ return fieldId.compareTo(rfi.fieldId);
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefType.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefType.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/RefType.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,24 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public enum RefType {
+ UNDEF,
+ ONE2ONE,
+ ONE2MANY,
+ MANY2ONE,
+ MANY2MANY,
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.common;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.jdt.core.IClassFile;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragment;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class Utils {
+
+ static public org.eclipse.jdt.core.dom.CompilationUnit getCompilationUnit(
+ ICompilationUnit source, boolean bindings) {
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(source);
+ parser.setResolveBindings(bindings);
+ org.eclipse.jdt.core.dom.CompilationUnit result =
(org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null);
+ return result;
+ }
+
+ static public ICompilationUnit findCompilationUnit(IJavaProject javaProject,
+ String fullyQualifiedName) {
+ IType lwType = null;
+ try {
+ lwType = javaProject.findType(fullyQualifiedName);
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ",
e); //$NON-NLS-1$
+ }
+ ICompilationUnit resCompilationUnit = null;
+ if (lwType != null) {
+ resCompilationUnit = lwType.getCompilationUnit();
+ }
+ return resCompilationUnit;
+ }
+
+ static public ICompilationUnit findCompilationUnit(String fullyQualifiedName) {
+ IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+ IProject[] projects = root.getProjects();
+ ICompilationUnit resCompilationUnit = null;
+ for (int i = 0; i < projects.length; i++) {
+ IJavaProject javaProject = JavaCore.create(projects[i]);
+ IType lwType = null;
+ try {
+ lwType = javaProject.findType(fullyQualifiedName);
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ",
e); //$NON-NLS-1$
+ }
+ if (lwType != null) {
+ resCompilationUnit = lwType.getCompilationUnit();
+ }
+ if (resCompilationUnit != null) {
+ break;
+ }
+ }
+ return resCompilationUnit;
+ }
+
+ static public ICompilationUnit[] findCompilationUnits(IJavaProject javaProject,
+ IPath path) {
+ IJavaElement javaElement = null;
+ try {
+ javaElement = javaProject.findElement(path.makeRelative());
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ",
e); //$NON-NLS-1$
+ }
+ ICompilationUnit[] res = null;
+ if (javaElement instanceof ICompilationUnit) {
+ res = new ICompilationUnit[]{ (ICompilationUnit)javaElement };
+ }
+ else if (javaElement instanceof IPackageFragment) {
+ try {
+ res = ((IPackageFragment)javaElement).getCompilationUnits();
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ",
e); //$NON-NLS-1$
+ }
+ }
+ else if (javaElement instanceof IClassFile) {
+ }
+ return res;
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/AllEntitiesProcessor.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,312 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.process;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.eclipse.core.filebuffers.FileBuffers;
+import org.eclipse.core.filebuffers.ITextFileBuffer;
+import org.eclipse.core.filebuffers.ITextFileBufferManager;
+import org.eclipse.core.filebuffers.LocationKind;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
+import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.CompositeChange;
+import org.eclipse.ltk.core.refactoring.DocumentChange;
+import org.eclipse.ltk.core.refactoring.Refactoring;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
+import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.text.edits.MalformedTreeException;
+import org.eclipse.text.edits.TextEdit;
+import org.eclipse.text.edits.UndoEdit;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.hibernate.eclipse.console.HibernateConsolePlugin;
+import org.hibernate.eclipse.jdt.ui.internal.JdtUiMessages;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.Utils;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class AllEntitiesProcessor {
+
+ protected IJavaProject javaProject;
+
+ protected class ChangeStructure {
+ public String fullyQualifiedName;
+ public IPath path;
+ public IDocument document;
+ public TextEdit textEdit;
+ public ITextFileBuffer textFileBuffer;
+ public Change change;
+ };
+ protected ArrayList<ChangeStructure> changes = new
ArrayList<ChangeStructure>();
+
+ public void modify(IJavaProject project, Map<String, EntityInfo> entities,
+ boolean askConfirmation) {
+ changes.clear();
+ setJavaProject(project);
+ // get the buffer manager
+ ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
+ Iterator<Map.Entry<String, EntityInfo>> it =
entities.entrySet().iterator();
+ String outText = ""; //$NON-NLS-1$
+ String ls = System.getProperties().getProperty("line.separator",
"\n"); //$NON-NLS-1$//$NON-NLS-2$
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ if (entry.getValue().isAbstractFlag()) {
+ continue;
+ }
+ if (entry.getValue().isCompilerProblemsFlag()) {
+ // TODO: save entity name as has compiler problems
+ }
+ outText += entry.getKey() + (it.hasNext() ? ls : ""); //$NON-NLS-1$
+ }
+ boolean performChange = true;
+ int res = 0;
+ if (askConfirmation) {
+ /** /
+ final String outText2 = outText;
+ MessageDialog dialog = new
MessageDialog(JavaPlugin.getActiveWorkbenchShell(),
+ JdtUiMessages.AllEntitiesProcessor_header, null,
+ JdtUiMessages.AllEntitiesProcessor_message,
+ MessageDialog.QUESTION,
+ new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL },
0) {
+ protected Control createCustomArea(Composite parent) {
+ Text messageText = new Text(parent, SWT.WRAP | SWT.V_SCROLL);
+ messageText.setText(outText2);
+ messageText.setEditable(false);
+ GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
+ .grab(true, true)
+
.hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
+ convertHorizontalDLUsToPixels(2 *
IDialogConstants.BUTTON_BAR_HEIGHT)).applyTo(messageText);
+
+ return messageText;
+ }
+ protected boolean isResizable() {
+ return true;
+ }
+ };
+ res = dialog.open();
+ /**/
+ }
+ if (res == 0) {
+ // TODO:
+ // show warning about abstract classes
+ // show warning about compiler problems
+ // ...
+ // modify accepted items
+ it = entities.entrySet().iterator();
+ try {
+ while (it.hasNext()) {
+ Map.Entry<String, EntityInfo> entry = it.next();
+ if (entry.getValue().isAbstractFlag()) {
+ continue;
+ }
+ // this is not only errors, but warnings too
+ //if (entry.getValue().isCompilerProblemsFlag()) {
+ // continue;
+ //}
+ // modify only non-abstract classes
+ collectModification(bufferManager, entry.getKey(), entry.getValue());
+ }
+ } catch (CoreException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
//$NON-NLS-1$
+ }
+ }
+ else {
+ performChange = false;
+ }
+ //
+ if (askConfirmation) {
+ if (!showRefactoringDialog(outText)) {
+ performChange = false;
+ }
+ }
+ if (performChange) {
+ performChange(bufferManager);
+ }
+ performDisconnect(bufferManager);
+ }
+
+ protected void performDisconnect(ITextFileBufferManager bufferManager) {
+ for (int i = 0; i < changes.size(); i++) {
+ ChangeStructure cs = changes.get(i);
+ try {
+ bufferManager.disconnect(cs.path, LocationKind.IFILE, null);
+ } catch (CoreException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
//$NON-NLS-1$
+ }
+ }
+ changes.clear();
+ }
+
+ protected void performChange(ITextFileBufferManager bufferManager) {
+ for (int i = 0; i < changes.size(); i++) {
+ ChangeStructure cs = changes.get(i);
+ try {
+ if (cs.textFileBuffer != null && cs.document != null && cs.textEdit
!= null &&
+ ((cs.change != null && cs.change.isEnabled()) || (cs.change == null))) {
+ cs.document = cs.textFileBuffer.getDocument();
+ UndoEdit undo = cs.textEdit.apply(cs.document);
+ // commit changes to underlying file
+ cs.textFileBuffer.commit(null, true);
+ }
+ } catch (CoreException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e);
//$NON-NLS-1$
+ } catch (MalformedTreeException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("MalformedTreeException:
", e); //$NON-NLS-1$
+ } catch (BadLocationException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("BadLocationException:
", e); //$NON-NLS-1$
+ }
+ }
+ }
+
+ public void collectModification(ITextFileBufferManager bufferManager, String
fullyQualifiedName,
+ EntityInfo entityInfo) throws CoreException {
+
+ ChangeStructure cs = new ChangeStructure();
+ cs.fullyQualifiedName = fullyQualifiedName;
+ ICompilationUnit icu = Utils.findCompilationUnit(javaProject, fullyQualifiedName);
+ org.eclipse.jdt.core.dom.CompilationUnit cu = Utils.getCompilationUnit(icu, false);
+ cs.path = cu.getJavaElement().getPath();
+ try {
+ bufferManager.connect(cs.path, LocationKind.IFILE, null);
+ cs.textFileBuffer = bufferManager.getTextFileBuffer(cs.path, LocationKind.IFILE);
+ // retrieve the buffer
+ cs.document = cs.textFileBuffer.getDocument();
+ AST ast = cu.getAST();
+ ASTRewrite rewriter = ASTRewrite.create(ast);
+ // ... rewrite
+ ProcessEntityInfo processor = new ProcessEntityInfo();
+ processor.setEntityInfo(entityInfo);
+ processor.setASTRewrite(rewriter);
+ cu.accept(processor);
+ //
+ cs.textEdit = rewriter.rewriteAST(cs.document, JavaCore.getOptions());
+ // add change to array of changes
+ changes.add(cs);
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ",
e); //$NON-NLS-1$
+ } catch (MalformedTreeException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("MalformedTreeException:
", e); //$NON-NLS-1$
+ }
+ }
+
+ public boolean showRefactoringDialog(final String text) {
+
+ final String wizard_title = JdtUiMessages.AllEntitiesProcessor_header;
+
+ Refactoring ref = new Refactoring(){
+
+ @Override
+ public RefactoringStatus checkFinalConditions(IProgressMonitor pm){
+ return RefactoringStatus.create(Status.OK_STATUS);
+ }
+
+ @Override
+ public RefactoringStatus checkInitialConditions(IProgressMonitor pm) {
+ return RefactoringStatus.create(Status.OK_STATUS);
+ }
+
+ @Override
+ public Change createChange(IProgressMonitor pm){
+
+ final CompositeChange cc = new CompositeChange("");
+ for (int i = 0; i < changes.size(); i++) {
+ ChangeStructure cs = changes.get(i);
+ String change_name = cs.fullyQualifiedName;
+ DocumentChange change = new DocumentChange(change_name, cs.document);
+ change.setEdit(cs.textEdit);
+ cs.change = change;
+ cc.add(change);
+ }
+ cc.markAsSynthetic();
+ return cc;
+ }
+
+ @Override
+ public String getName() {
+ return JdtUiMessages.SaveQueryEditorListener_composite_change_name;
+ }
+ };
+
+ RefactoringWizard wizard = new RefactoringWizard(ref,
RefactoringWizard.DIALOG_BASED_USER_INTERFACE) {
+
+ @Override
+ protected void addUserInputPages() {
+ UserInputWizardPage page = new UserInputWizardPage(wizard_title) {
+ public void createControl(Composite parent) {
+ initializeDialogUnits(parent);
+ Composite container = new Composite(parent, SWT.NULL);
+ GridLayout layout = new GridLayout();
+ container.setLayout(layout);
+ layout.numColumns = 1;
+ Label label = new Label(container, SWT.NULL);
+ label.setText(JdtUiMessages.AllEntitiesProcessor_message);
+
+ Text messageText = new Text(container, SWT.WRAP | SWT.V_SCROLL);
+ messageText.setText(text);
+ messageText.setEditable(false);
+ GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL)
+ .grab(true, true)
+
.hint(convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
+ convertHorizontalDLUsToPixels(2 *
IDialogConstants.BUTTON_BAR_HEIGHT)).applyTo(messageText);
+ setControl(container);
+ }
+ };
+ addPage(page);
+ }
+
+ };
+
+ wizard.setWindowTitle(wizard_title);
+ wizard.setDefaultPageTitle(wizard_title);
+
+ IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ RefactoringStarter rStarter = new RefactoringStarter();
+ boolean res = rStarter.activate(wizard, win.getShell(), wizard_title,
RefactoringSaveHelper.SAVE_ALL);
+ RefactoringStatus rs = rStarter.getInitialConditionCheckingStatus();
+ return res;
+ }
+
+ public IJavaProject getJavaProject() {
+ return javaProject;
+ }
+
+ protected void setJavaProject(IJavaProject project) {
+ javaProject = project;
+ }
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/process/ProcessEntityInfo.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,269 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.internal.jpa.process;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.Block;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.eclipse.jdt.core.dom.MarkerAnnotation;
+import org.eclipse.jdt.core.dom.MemberValuePair;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.Modifier;
+import org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.eclipse.jdt.core.dom.QualifiedName;
+import org.eclipse.jdt.core.dom.SimpleName;
+import org.eclipse.jdt.core.dom.SimpleType;
+import org.eclipse.jdt.core.dom.StringLiteral;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
+import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.JPAConst;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefEntityInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefFieldInfo;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class ProcessEntityInfo extends ASTVisitor {
+
+ protected EntityInfo entityInfo;
+ protected ASTRewrite rewriter;
+
+ public void setEntityInfo(EntityInfo entityInfo) {
+ this.entityInfo = entityInfo;
+ }
+
+ public void setASTRewrite(ASTRewrite rewriter) {
+ this.rewriter = rewriter;
+ }
+
+ public boolean visit(CompilationUnit node) {
+ for (int i = 0; i < JPAConst.ALL_IMPORTS.size(); i++) {
+ String tmp = JPAConst.ALL_IMPORTS.get(i);
+ if (entityInfo.needImport(tmp)) {
+ addImport(node, tmp);
+ }
+ }
+ return true;
+ }
+
+ public boolean addImport(CompilationUnit node, String importDeclaration) {
+ String[] importDeclarations = importDeclaration.split("\\."); //$NON-NLS-1$
+ if (importDeclarations.length <= 1) {
+ return false;
+ }
+ ImportDeclaration importDecl = rewriter.getAST().newImportDeclaration();
+ SimpleName simpleName0 = rewriter.getAST().newSimpleName(importDeclarations[0]);
+ SimpleName simpleName1 = rewriter.getAST().newSimpleName(importDeclarations[1]);
+ QualifiedName qualifiedName0 = rewriter.getAST().newQualifiedName(simpleName0,
simpleName1);
+ for (int i = 2; i < importDeclarations.length; i++) {
+ SimpleName simpleNameI = rewriter.getAST().newSimpleName(importDeclarations[i]);
+ qualifiedName0 = rewriter.getAST().newQualifiedName(qualifiedName0, simpleNameI);
+ }
+ importDecl.setName(qualifiedName0);
+ ListRewrite lrw = rewriter.getListRewrite(node, CompilationUnit.IMPORTS_PROPERTY);
+ // TODO: insert import declaration in the proper place
+ // prefer alphabetic order and package separation
+ lrw.insertLast(importDecl, null);
+ return true;
+ }
+
+ public boolean visit(TypeDeclaration node) {
+ if (entityInfo.isAddEntityFlag()) {
+ MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
+ matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ENTITY));
+ ListRewrite lrw = rewriter.getListRewrite(node, TypeDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(matd, null);
+ }
+ /** /
+ if (!entityInfo.isImplicitConstructorFlag() &&
!entityInfo.isDefaultConstructorFlag() &&
+ entityInfo.isAddSerializableInterfaceFlag()) {
+ // add serializable interface
+ SimpleName sn = null;
+ //if (!entityInfo.isAddSerializableInterfaceImportFlag()) {
+ sn = rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_SERIALIZABLE);
+ //}
+ //else {
+ // sn = rewriter.getAST().newSimpleName(JPAConst.IMPORT_SERIALIZABLE);
+ //}
+ SimpleType st = rewriter.getAST().newSimpleType(sn);
+ ListRewrite lrw = rewriter.getListRewrite(node,
TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
+ lrw.insertFirst(st, null);
+ // add "private static final long serialVersionUID = 1L;"
+ // ...
+ }
+ /**/
+ if (!entityInfo.isImplicitConstructorFlag() &&
!entityInfo.isDefaultConstructorFlag() &&
+ entityInfo.isAddSerializableInterfaceFlag()) {
+
+ MethodDeclaration md = rewriter.getAST().newMethodDeclaration();
+ md.setConstructor(true);
+ Modifier modifier =
rewriter.getAST().newModifier(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
+ md.modifiers().add(modifier);
+ Block body = rewriter.getAST().newBlock();
+ md.setBody(body);
+ SimpleName sn = rewriter.getAST().newSimpleName(entityInfo.getName());
+ md.setName(sn);
+ ListRewrite lrw = rewriter.getListRewrite(node,
TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
+ List list = lrw.getOriginalList();
+ MethodDeclaration insertBeforeNode = null;
+ Iterator it = list.iterator();
+ while (it.hasNext()) {
+ Object obj = it.next();
+ if (obj instanceof MethodDeclaration) {
+ insertBeforeNode = (MethodDeclaration)obj;
+ break;
+ }
+ }
+ if (insertBeforeNode == null) {
+ lrw.insertLast(md, null);
+ }
+ else {
+ lrw.insertBefore(md, insertBeforeNode, null);
+ }
+ }
+ return true;
+ }
+
+ public boolean visit(FieldDeclaration node) {
+ if (node.getType().isSimpleType()) {
+ if (entityInfo.isAddGeneratedValueFlag()) {
+ String primaryIdName = entityInfo.getPrimaryIdName();
+ Iterator itVarNames = node.fragments().iterator();
+ boolean addGeneratedValueMarker = false;
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ if (primaryIdName.equals(name)) {
+ addGeneratedValueMarker = true;
+ break;
+ }
+ }
+ if (addGeneratedValueMarker) {
+ MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
+ matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_GENERATED_VALUE));
+ ListRewrite lrw = rewriter.getListRewrite(node,
FieldDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(matd, null);
+ }
+ }
+ if (entityInfo.isAddPrimaryIdFlag()) {
+ String primaryIdName = entityInfo.getPrimaryIdName();
+ Iterator itVarNames = node.fragments().iterator();
+ boolean addIdMarker = false;
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ if (primaryIdName.equals(name)) {
+ addIdMarker = true;
+ break;
+ }
+ }
+ if (addIdMarker) {
+ MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
+ matd.setTypeName(rewriter.getAST().newSimpleName(JPAConst.ANNOTATION_ID));
+ ListRewrite lrw = rewriter.getListRewrite(node,
FieldDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(matd, null);
+ }
+ }
+ }
+ if (node.getType().isSimpleType() || node.getType().isParameterizedType()) {
+ Iterator itVarNames = node.fragments().iterator();
+ String fieldId = ""; //$NON-NLS-1$
+ RefType refType = RefType.UNDEF;
+ boolean annotated = false;
+ String fullyQualifiedName2 = ""; //$NON-NLS-1$
+ while (itVarNames.hasNext()) {
+ VariableDeclarationFragment var = (VariableDeclarationFragment)itVarNames.next();
+ String name = var.getName().getIdentifier();
+ fieldId = name;
+ refType = entityInfo.getFieldIdRelValue(fieldId);
+ annotated = entityInfo.getFieldIdAnnotatedValue(fieldId);
+ fullyQualifiedName2 = entityInfo.getFieldIdFQNameValue(fieldId);
+ if (refType != RefType.UNDEF) {
+ break;
+ }
+ }
+ Set<RefFieldInfo> setRFI = entityInfo.getRefFieldInfoSet(fullyQualifiedName2);
+ if (!annotated && setRFI != null) {
+ RefEntityInfo rei = entityInfo.getFieldIdRefEntityInfo(fieldId);
+ // try to process bidirectional relationships:
+ // nRefType == JPAConst.ONE2ONE - OneToOne - the owning side corresponds
+ // to the side that contains the corresponding foreign key
+ // nRefType == JPAConst.MANY2ONE - ManyToOne - owning side is always the
"many" side
+ // nRefType == JPAConst.MANY2MANY - ManyToMany bidirectional relationships
+ // either side may be the owning side
+ if (setRFI.size() > 1 && refType != RefType.MANY2ONE) {
+ if (rei.mappedBy == null || rei.mappedBy == "") { //$NON-NLS-1$
+ addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
+ }
+ else {
+ // give to the user information about selected mapping
+ addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
+ }
+ }
+ else if (refType == RefType.MANY2ONE || rei.mappedBy == null || rei.mappedBy ==
"") { //$NON-NLS-1$
+ addSimpleMarkerAnnotation(node, JPAConst.getRefType(refType));
+ }
+ else {
+ // in case of bidirectional OneToOne - mark both sides with mappedBy -
+ // user should select the right decision
+ addComplexNormalAnnotation(node, JPAConst.getRefType(refType), rei);
+ }
+ }
+ }
+ return true;
+ }
+
+ public boolean addSimpleMarkerAnnotation(FieldDeclaration node, String name) {
+ if (name == null || name.length() == 0) {
+ return false;
+ }
+ MarkerAnnotation matd = rewriter.getAST().newMarkerAnnotation();
+ matd.setTypeName(rewriter.getAST().newSimpleName(name));
+ ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(matd, null);
+ return true;
+ }
+
+ public boolean addComplexNormalAnnotation(FieldDeclaration node, String name,
RefEntityInfo rei) {
+ if (name == null || name.length() == 0) {
+ return false;
+ }
+ NormalAnnotation natd = rewriter.getAST().newNormalAnnotation();
+ MemberValuePair mvp = null;
+ if (rei.mappedBy != null) {
+ mvp = rewriter.getAST().newMemberValuePair();
+ mvp.setName(rewriter.getAST().newSimpleName("mappedBy")); //$NON-NLS-1$
+ StringLiteral sl = rewriter.getAST().newStringLiteral();
+ sl.setLiteralValue(rei.mappedBy);
+ mvp.setValue(sl);
+ }
+ natd.setTypeName(rewriter.getAST().newSimpleName(name));
+ if (mvp != null) {
+ natd.values().add(mvp);
+ }
+ ListRewrite lrw = rewriter.getListRewrite(node, FieldDeclaration.MODIFIERS2_PROPERTY);
+ lrw.insertFirst(natd, null);
+ return true;
+ }
+
+}
Modified: trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/.classpath
===================================================================
--- trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/.classpath 2008-10-16
10:41:46 UTC (rev 10876)
+++ trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/.classpath 2008-10-16
10:41:47 UTC (rev 10877)
@@ -1,11 +1,11 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
- <classpathentry kind="src" path="src"/>
- <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins">
- <accessrules>
- <accessrule kind="accessible" pattern="**/internal/**"/>
- </accessrules>
- </classpathentry>
- <classpathentry kind="output" path="bin"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins">
+ <accessrules>
+ <accessrule kind="accessible" pattern="**/internal/**"/>
+ </accessrules>
+ </classpathentry>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Document.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Document.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Document.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import java.util.Set;
+
+(a)javax.persistence.Entity
+public class Document {
+
+ @javax.persistence.Id
+ protected Long documentId;
+
+ protected Person documentOwner;
+
+ @javax.persistence.OneToMany
+ protected Set<Person> persons;
+
+ protected Byte docType = 0;
+
+ public Long getDocumentId() {
+ return documentId;
+ }
+
+ public void setDocumentId(Long documentId) {
+ this.documentId = documentId;
+ }
+
+ public Person getDocumentOwner() {
+ return documentOwner;
+ }
+
+ public void setDocumentOwner(Person documentOwner) {
+ this.documentOwner = documentOwner;
+ }
+
+ public Set<Person> getPersons() {
+ return persons;
+ }
+
+ public void setPersons(Set<Person> persons) {
+ this.persons = persons;
+ }
+
+ public Byte getDocType() {
+ return docType;
+ }
+
+ protected void setDocType(Byte docType) {
+ this.docType = docType;
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Foto.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Foto.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Foto.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+public class Foto {
+
+ protected Long fid;
+
+ protected Short id;
+
+ protected Person person;
+
+ protected Short width_IDtest;
+
+ protected Short height_testID;
+
+ public Foto() {
+ }
+
+ public Long getFid() {
+ return fid;
+ }
+
+ public void setFid(Long fid) {
+ this.fid = fid;
+ }
+
+ public Short getId() {
+ return id;
+ }
+
+ public void setId(Short id) {
+ this.id = id;
+ }
+
+ public Person getPerson() {
+ return person;
+ }
+
+ public void setPerson(Person person) {
+ this.person = person;
+ }
+
+ public Short getWidth_IDtest() {
+ return width_IDtest;
+ }
+
+ public void setWidth_IDtest(Short width_IDtest) {
+ this.width_IDtest = width_IDtest;
+ }
+
+ public Short getHeight_testID() {
+ return height_testID;
+ }
+
+ public void setHeight_testID(Short height_testID) {
+ this.height_testID = height_testID;
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Passport.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Passport.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Passport.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+public class Passport extends Document {
+
+ protected Long fakeId;
+
+ public Passport() {
+ setDocType((byte)1);
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Person.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Person.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Person.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import java.util.Set;
+
+public class Person {
+
+ protected Long personId;
+
+ @OneToMany(mappedBy="documentOwner")
+ protected Set<Document> documents;
+
+ protected Foto foto;
+
+ protected Set someTestSet;
+
+ public Long getPersonId() {
+ return personId;
+ }
+
+ public void setPersonId(Long personId) {
+ this.personId = personId;
+ }
+
+ public Set<Document> getDocuments() {
+ return documents;
+ }
+
+ public void setDocuments(Set<Document> documents) {
+ this.documents = documents;
+ }
+
+ public Foto getFoto() {
+ return foto;
+ }
+
+ public void setFoto(Foto foto) {
+ this.foto = foto;
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Staff.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Staff.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/project/test/annotated/Staff.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+public class Staff extends Document {
+
+ protected Long code;
+
+ public Staff(long code) {
+ this.code = code;
+ setDocType((byte)2);
+ }
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Document.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Document.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Document.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import java.util.Set;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.ManyToOne;
+
+(a)javax.persistence.Entity
+public class Document {
+
+ @GeneratedValue @javax.persistence.Id
+ protected Long documentId;
+
+ @ManyToOne
+ protected Person documentOwner;
+
+ @javax.persistence.OneToMany
+ protected Set<Person> persons;
+
+ protected Byte docType = 0;
+
+ public Long getDocumentId() {
+ return documentId;
+ }
+
+ public void setDocumentId(Long documentId) {
+ this.documentId = documentId;
+ }
+
+ public Person getDocumentOwner() {
+ return documentOwner;
+ }
+
+ public void setDocumentOwner(Person documentOwner) {
+ this.documentOwner = documentOwner;
+ }
+
+ public Set<Person> getPersons() {
+ return persons;
+ }
+
+ public void setPersons(Set<Person> persons) {
+ this.persons = persons;
+ }
+
+ public Byte getDocType() {
+ return docType;
+ }
+
+ protected void setDocType(Byte docType) {
+ this.docType = docType;
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Foto.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Foto.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Foto.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,76 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+@Entity
+public class Foto {
+
+ protected Long fid;
+
+ @Id @GeneratedValue
+ protected Short id;
+
+ @OneToOne(mappedBy="foto")
+ protected Person person;
+
+ protected Short width_IDtest;
+
+ protected Short height_testID;
+
+ public Foto() {
+ }
+
+ public Long getFid() {
+ return fid;
+ }
+
+ public void setFid(Long fid) {
+ this.fid = fid;
+ }
+
+ public Short getId() {
+ return id;
+ }
+
+ public void setId(Short id) {
+ this.id = id;
+ }
+
+ public Person getPerson() {
+ return person;
+ }
+
+ public void setPerson(Person person) {
+ this.person = person;
+ }
+
+ public Short getWidth_IDtest() {
+ return width_IDtest;
+ }
+
+ public void setWidth_IDtest(Short width_IDtest) {
+ this.width_IDtest = width_IDtest;
+ }
+
+ public Short getHeight_testID() {
+ return height_testID;
+ }
+
+ public void setHeight_testID(Short height_testID) {
+ this.height_testID = height_testID;
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Passport.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Passport.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Passport.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,24 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Passport extends Document {
+
+ protected Long fakeId;
+
+ public Passport() {
+ setDocType((byte)1);
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Person.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Person.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Person.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import javax.persistence.OneToMany;
+
+@Entity
+public class Person {
+
+ @Id @GeneratedValue
+ protected Long personId;
+
+ @OneToMany(mappedBy="documentOwner")
+ protected Set<Document> documents;
+
+ @OneToOne(mappedBy="person")
+ protected Foto foto;
+
+ protected Set someTestSet;
+
+ public Long getPersonId() {
+ return personId;
+ }
+
+ public void setPersonId(Long personId) {
+ this.personId = personId;
+ }
+
+ public Set<Document> getDocuments() {
+ return documents;
+ }
+
+ public void setDocuments(Set<Document> documents) {
+ this.documents = documents;
+ }
+
+ public Foto getFoto() {
+ return foto;
+ }
+
+ public void setFoto(Foto foto) {
+ this.foto = foto;
+ }
+
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Staff.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Staff.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/res/specimen/test/annotated/Staff.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package test.annotated;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Staff extends Document {
+
+ protected Long code;
+
+ protected Staff() {
+ }
+
+ public Staff(long code) {
+ this.code = code;
+ setDocType((byte)2);
+ }
+}
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.jdt.ui.test/src/org/hibernate/eclipse/jdt/ui/test/JPAMapTest.java 2008-10-16
10:41:47 UTC (rev 10877)
@@ -0,0 +1,205 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.jdt.ui.test;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.net.URL;
+import java.util.List;
+
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IPackageFragmentRoot;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.osgi.util.NLS;
+import org.hibernate.eclipse.console.test.ConsoleTestMessages;
+import org.hibernate.eclipse.console.test.mappingproject.TestUtilsCommon;
+import org.hibernate.eclipse.console.utils.ProjectUtils;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.collect.AllEntitiesInfoCollector;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.Utils;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.process.AllEntitiesProcessor;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ *
+ * @author Vitali
+ */
+public class JPAMapTest extends TestCase {
+
+ public static String PROJECT_NAME = "TestProject"; //$NON-NLS-1$
+ public static String RESOURCE_PATH = "res/project/"; //$NON-NLS-1$
+ public static String SPECIMEN_PATH = "res/specimen/"; //$NON-NLS-1$
+ public static String TESTRESOURCE_PATH = "testresources"; //$NON-NLS-1$
+ static {
+ RESOURCE_PATH.replaceAll("//", File.separator); //$NON-NLS-1$
+ SPECIMEN_PATH.replaceAll("//", File.separator); //$NON-NLS-1$
+ }
+
+ protected AllEntitiesInfoCollector collector = new AllEntitiesInfoCollector();
+ protected AllEntitiesProcessor processor = new AllEntitiesProcessor();
+
+ protected IProject project;
+ protected IJavaProject javaProject;
+
+ protected void setUp() throws Exception {
+ try {
+ createTestProject();
+ } catch (JavaModelException e1) {
+ fail(e1.getMessage());
+ } catch (CoreException e1) {
+ fail(e1.getMessage());
+ } catch (IOException e1) {
+ fail(e1.getMessage());
+ }
+ assertNotNull(project);
+ assertNotNull(javaProject);
+ }
+
+ protected void tearDown() throws Exception {
+ try {
+ project.delete(true, true, null);
+ project = null;
+ javaProject = null;
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+ assertNull(project);
+ assertNull(javaProject);
+ }
+
+ public void testTransformer() {
+ javaProject = ProjectUtils.findJavaProject(PROJECT_NAME);
+ assertNotNull(javaProject);
+ try {
+ javaProject.getProject().open(null);
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+ ICompilationUnit icu = Utils.findCompilationUnit(javaProject,
+ "test.annotated.Passport"); //$NON-NLS-1$
+ ICompilationUnit icu2 = Utils.findCompilationUnit(javaProject,
+ "test.annotated.Staff"); //$NON-NLS-1$
+ assertNotNull(icu);
+ assertNotNull(icu2);
+ collector.initCollector(javaProject);
+ collector.collect(icu);
+ collector.collect(icu2);
+ collector.resolveRelations();
+ processor.modify(javaProject, collector.getMapCUs_Info(), false);
+ //
+ checkItem("Document"); //$NON-NLS-1$
+ checkItem("Foto"); //$NON-NLS-1$
+ checkItem("Passport"); //$NON-NLS-1$
+ checkItem("Person"); //$NON-NLS-1$
+ checkItem("Staff"); //$NON-NLS-1$
+ }
+
+ protected void checkItem(String strCheckItem) {
+ ASTNode specimen, generated;
+ specimen = null;
+ try {
+ specimen = getSpecimen(strCheckItem);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ assertNotNull(specimen);
+ generated = getGenerated(strCheckItem);
+ assertNotNull(generated);
+ assertEquals(specimen.toString(), generated.toString());
+ }
+
+ protected ASTNode getGenerated(String strName) {
+ ICompilationUnit icu = Utils.findCompilationUnit(javaProject,
+ "test.annotated." + strName); //$NON-NLS-1$
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(icu);
+ ASTNode astNode = parser.createAST(null);
+ return astNode;
+ }
+
+ protected ASTNode getSpecimen(String strName) throws IOException {
+ File resourceFile = getResourceItem(SPECIMEN_PATH
+ + "test" + File.separator + "annotated" + File.separator +
strName + ".java"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ if (!resourceFile.exists()) {
+ return null;
+ }
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ StringBuffer cbuf = new StringBuffer((int) resourceFile.length());
+ try {
+ String ls = System.getProperties().getProperty("line.separator",
"\n"); //$NON-NLS-1$//$NON-NLS-2$
+ BufferedReader in = new BufferedReader(new FileReader(resourceFile));
+ String str;
+ while ((str = in.readLine()) != null) {
+ cbuf.append(str + ls);
+ }
+ in.close();
+ } catch (IOException e) {
+ }
+
+ parser.setSource(cbuf.toString().toCharArray());
+ ASTNode astNode = parser.createAST(null);
+ return astNode;
+ }
+
+ protected File getResourceItem(String strResPath) throws IOException {
+ IPath resourcePath = new Path(strResPath);
+ File resourceFolder = resourcePath.toFile();
+ URL entry = HibernateJDTuiTestPlugin.getDefault().getBundle().getEntry(
+ strResPath);
+ URL resProject = FileLocator.resolve(entry);
+ String tplPrjLcStr = FileLocator.resolve(resProject).getFile();
+ resourceFolder = new File(tplPrjLcStr);
+ return resourceFolder;
+ }
+
+ protected void createTestProject() throws JavaModelException,
+ CoreException, IOException {
+ TestUtilsCommon commonUtil = new TestUtilsCommon();
+ project = commonUtil.buildNewProject(PROJECT_NAME);
+ javaProject = commonUtil.buildJavaProject(project);
+ File resourceFolder = getResourceItem(RESOURCE_PATH);
+ if (!resourceFolder.exists()) {
+ String out = NLS.bind(
+ ConsoleTestMessages.MappingTestProject_folder_not_found,
+ RESOURCE_PATH);
+ throw new RuntimeException(out);
+ }
+ IPackageFragmentRoot sourceFolder = commonUtil.createSourceFolder(
+ project, javaProject);
+ commonUtil.recursiveCopyFiles(resourceFolder, (IFolder) sourceFolder
+ .getResource());
+ File resourceFolderLib = getResourceItem(TESTRESOURCE_PATH);
+ if (!resourceFolderLib.exists()) {
+ String out = NLS.bind(
+ ConsoleTestMessages.MappingTestProject_folder_not_found,
+ RESOURCE_PATH);
+ throw new RuntimeException(out);
+ }
+ List<IPath> libs = commonUtil.copyLibs2(project, javaProject,
+ resourceFolderLib.getAbsolutePath());
+ commonUtil.generateClassPath(javaProject, libs, sourceFolder);
+ }
+
+}