Package boco.ejb3PlugIn.popup.actions.relationships

Source Code of boco.ejb3PlugIn.popup.actions.relationships.RelationshipN2NOwner

/***************************************************************************
* Copyright (c) 2004 - 2008  Fabrizio Boco fabboco@users.sourceforge.net  *
*                                                                         *
*                                                                         *
*   This is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Library General Public           *
*   License (version 2.1) as published by the Free Software Foundation    *
*                                                                         *
*   This library  is distributed in the hope that it will be useful,      *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this library; see the file COPYING.LIB. If not,    *
*   write to the Free Software Foundation, Inc., 59 Temple Place,         *
*   Suite 330, Boston, MA  02111-1307, USA                                *
*                                                                         *
***************************************************************************/

/**
- $Header: /usr/local/cvslocalrepository/EJB3PlugIn/src/boco/ejb3PlugIn/popup/actions/relationships/Attic/RelationshipN2NOwner.java,v 1.1.2.5 2008/06/02 20:04:31 fab Exp $
- $Author: fab $
- $Revision: 1.1.2.5 $
- $Date: 2008/06/02 20:04:31 $
- $Log: RelationshipN2NOwner.java,v $
- Revision 1.1.2.5  2008/06/02 20:04:31  fab
- Eliminate System.out
-
- Revision 1.1.2.4  2008/04/22 06:17:37  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.1.2.3  2008/04/19 10:26:48  fab
- Aggiornamento riferimenti licenza
-
- Revision 1.1.2.2  2008/03/29 13:30:00  fab
- Internazionalizzazione
-
- Revision 1.1.2.1  2008/03/28 18:44:26  fab
- Refactoring delle azioni
- Internazionalizzazione di tutte le voci di menu
-
- Revision 1.1.2.1  2008/03/22 12:00:00  fab
- Fix 0643/B
- Rimoninate alcune classi
-
- Revision 1.7.2.13  2007/10/06 08:13:28  fab
- Gestione della JoinTable nelle relazioni N:M
-
- Revision 1.7.2.12  2007/04/14 11:49:39  dev
- Cambiante le relazioni da EAGER a LAZY
-
- Revision 1.7.2.11  2007/04/13 19:42:49  dev
- Riunita creazione della relazione e delle annotazioni per il generatore
-
- Revision 1.7.2.10  2007/04/13 19:37:06  dev
- Riunita creazione della relazione e delle annotazioni per il generatore
-
- Revision 1.7.2.9  2007/04/13 17:24:17  dev
- Riunita generazione delle annotazioni EJB e Generatore per le relazioni. Funziona solo le 1:N
-
- Revision 1.7.2.8  2006/12/25 11:10:18  dev
- Introdotta gestione degli errori
- Fix per le relazioni N:M che avevano l'ownership invertita
-
- Revision 1.7.2.7  2006/12/24 16:15:57  dev
- Acquisito il nome della relazione per le relazioni N:M
-
- Revision 1.7.2.6  2006/12/17 16:15:35  dev
- Riorganizzata la classe Utils che era divisia in due !?
-
- Revision 1.7.2.5  2006/11/30 12:39:51  dev
- Eliminata generazione della funzione chek per PopUp
-
- Revision 1.7.2.4  2006/11/26 11:42:10  dev
- Fix per la gestione delle relazioni required di tipo PopUp
-
- Revision 1.7.2.3  2006/09/01 12:44:13  dev
- Fix sul mappedBy per supportare la generazione di più relazioni fra i medesimi bean
-
- Revision 1.7.2.2  2006/08/31 16:55:58  dev
- Fix per supportare la generazione di più relazioni fra i medesimi bean
-
- Revision 1.7.2.1  2006/08/28 08:48:49  dev
- Modifiche principli per il supporto di seam
-
- Revision 1.7  2006/06/01 16:08:41  dev
- Fix nella creazione dei nomi con lettere maiscole es. tipoIrregolarita
-
- Revision 1.6  2006/05/21 13:41:18  dev
- Corretta annotazione cvs
-

**/
package boco.ejb3PlugIn.popup.actions.relationships;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import boco.ejb3PlugIn.gui.ErrorMessageDialog;
import boco.ejb3PlugIn.gui.RelationshipDialog;
import boco.ejb3PlugIn.gui.SelectionBeanDialog;
import boco.ejb3PlugIn.utils.Utils;

public class RelationshipN2NOwner implements IObjectActionDelegate
{

  private ICompilationUnit  sourceCompilationUnit;
  private ICompilationUnit  targetCompilationUnit;

  private String          sourceBeanName;
  private String          targetBeanName;

  private String          directRelationshipName;
  private String          reverseRelationshipName;
 

  public RelationshipN2NOwner()
  {
    super();
  }

  public void setActivePart(IAction action, IWorkbenchPart targetPart)
  {
  }

  public void run(IAction action)
  {
    Shell shell = new Shell();

    if (!sourceCompilationUnit.getParent().getElementName().endsWith("entity"))
    {
      MessageDialog.openInformation(shell, "EJB_PlugIn Plug-in", "Una relazione può essere creata solo in un bean");
      return;
    }

    try
    {
      sourceBeanName = sourceCompilationUnit.getElementName();
      sourceBeanName = sourceBeanName.substring(0, sourceBeanName.length() - 5);
     
      IPackageFragment entityPackage = (IPackageFragment) sourceCompilationUnit.getParent();
      String[] beanNames = Utils.getBeanNames(entityPackage);

      SelectionBeanDialog targetBeanDialog = new SelectionBeanDialog(null, beanNames);
      targetBeanDialog.pack();
      targetBeanDialog.setLocationRelativeTo(null);
      targetBeanDialog.setVisible(true);
      targetBeanName = targetBeanDialog.getSelectedBeanName();

      targetCompilationUnit = Utils.getCompilationUnit(entityPackage, targetBeanName);
     
      if ((targetBeanName != null) && (!targetBeanName.equals("")))
      {
        /**
         * Acquisisce le informazioni sulla relazione
         *
         */
        RelationshipDialog relationshipNameDialog = new RelationshipDialog(sourceBeanName, "N", targetBeanName, "N",sourceCompilationUnit,targetCompilationUnit);
        relationshipNameDialog.pack();
        relationshipNameDialog.setLocationRelativeTo(null);
        relationshipNameDialog.setVisible(true);
       
        if (relationshipNameDialog.isCancelled())
          return;
       
        directRelationshipName = relationshipNameDialog.getDirectRelationshipName();
        reverseRelationshipName = relationshipNameDialog.getReverseRelationshipName();
       
        /**
         * Generazione del bean di partenza
         */       
        sourceCompilationUnit.createImport("java.util.List", null, null);
        sourceCompilationUnit.createImport("java.util.Vector", null, null);
        sourceCompilationUnit.createImport("javax.persistence.ManyToMany", null, null);
        sourceCompilationUnit.createImport("javax.persistence.FetchType", null, null);
        sourceCompilationUnit.createImport("javax.persistence.CascadeType", null, null);
       
        IType type = sourceCompilationUnit.getTypes()[0];

        type.createField("private List<" + targetBeanName + "> " + Utils.makeFirstLetterLowerCase(directRelationshipName) + ";", null, true, null);

        String get = "";
        get += "@ManyToMany(targetEntity=" + targetBeanName + ".class, cascade = {CascadeType.REFRESH}, fetch=FetchType.LAZY, mappedBy=\"" + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + "\")\r\n";
        get += "public List<" + targetBeanName + "> " + Utils.makeGet(directRelationshipName+ "()\r\n";
        get += "{\r\n";
        get += "\treturn " + Utils.makeFirstLetterLowerCase(directRelationshipName+ ";\r\n";
        get += "}\r\n";
        type.createMethod(get, null, true, null);

        String set = "";
        set += "public void " + Utils.makeSet(directRelationshipName+ "(List<" + targetBeanName + "> " + Utils.makeFirstLetterLowerCase(directRelationshipName+ ")\r\n";
        set += "{\r\n";
        set += "\tthis." + Utils.makeFirstLetterLowerCase(directRelationshipName+ " = " + Utils.makeFirstLetterLowerCase(directRelationshipName+ ";\r\n";
        set += "}\r\n";
        type.createMethod(set, null, true, null);

        String add = "";
        add += "public void " + Utils.makeAdd(directRelationshipName+ "(" + targetBeanName + " " + Utils.makeFirstLetterLowerCase(directRelationshipName+ ")\r\n";
        add += "{\r\n";
        add += "\tif (this." + Utils.makeFirstLetterLowerCase(directRelationshipName+ " == null)\r\n";
        add += "\t\tthis." + Utils.makeFirstLetterLowerCase(directRelationshipName+ " = new Vector();\r\n\r\n";
        add += "\tthis." + Utils.makeFirstLetterLowerCase(directRelationshipName+ ".add(" + Utils.makeFirstLetterLowerCase(directRelationshipName+ ");\r\n";
        add += "}\r\n";
        type.createMethod(add, null, true, null);

        String remove = "";
        remove += "public void " + Utils.makeRemove(directRelationshipName+ "(" + targetBeanName + " " + Utils.makeFirstLetterLowerCase(directRelationshipName+ ")\r\n";
        remove += "{\r\n";
        remove += "\tif (this." + Utils.makeFirstLetterLowerCase(directRelationshipName+ " == null)\r\n";
        remove += "\t\treturn;\r\n\r\n";
        remove += "\tthis." + Utils.makeFirstLetterLowerCase(directRelationshipName+ ".remove(" + Utils.makeFirstLetterLowerCase(directRelationshipName+ ");\r\n";
        remove += "}\r\n";
        type.createMethod(remove, null, true, null);

        String removeAll = "";
        removeAll += "public void " + Utils.makeRemoveAll(directRelationshipName+ "()\r\n";
        removeAll += "{\r\n";
        removeAll += "\tthis." + Utils.makeFirstLetterLowerCase(directRelationshipName+ " = new Vector<"+targetBeanName+">();\r\n";
        removeAll += "}\r\n";
        type.createMethod(removeAll, null, true, null);
               
        sourceCompilationUnit.save(null, true);
       
        // Inserimento delle annotazioni relative al generatore
       
        IMethod method=null;
        IJavaElement types[] = type.getChildren();
       
        for (IJavaElement element : types)
        {
          if (element instanceof IMethod && element.getElementName().contains(Utils.makeGet(directRelationshipName)))
            method = (IMethod)element;
        }
       
        Utils.addSeamGeneratorAnnotations(method, sourceCompilationUnit, relationshipNameDialog.getSourceRelationshipOption(), relationshipNameDialog.getSourceAttributeSelectionOption(), relationshipNameDialog.getTargetAttributeSelectionOption());
       
       
        /**
         * Generazione del bean Target
         */
       
        targetCompilationUnit.createImport("java.util.List", null, null);
        targetCompilationUnit.createImport("java.util.Vector", null, null);
        targetCompilationUnit.createImport("javax.persistence.ManyToMany", null, null);
        targetCompilationUnit.createImport("javax.persistence.FetchType", null, null);
        targetCompilationUnit.createImport("javax.persistence.CascadeType", null, null);
        sourceCompilationUnit.createImport("javax.persistence.JoinTable", null, null);

        type = targetCompilationUnit.getTypes()[0];

        type.createField("private List<" + sourceBeanName + "> " + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ";", null, true, null);

        get = "";
        get += "@ManyToMany(targetEntity=" + sourceBeanName + ".class, cascade = {CascadeType.REFRESH}, fetch=FetchType.LAZY)\r\n";
        get += "@JoinTable(name=\"" + targetBeanName +"_" + reverseRelationshipName + "\")";
        get += "public List<" + sourceBeanName + "> " + Utils.makeGet(reverseRelationshipName) + "()\r\n";
        get += "{\r\n";
        get += "\treturn " + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ";\r\n";
        get += "}\r\n";
        type.createMethod(get, null, true, null);

        set = "";
        set += "public void " + Utils.makeSet(reverseRelationshipName) + "(List<" + sourceBeanName + "> " + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ")\r\n";
        set += "{\r\n";
        set += "\tthis." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + " = " + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ";\r\n";
        set += "}\r\n";
        type.createMethod(set, null, true, null);

        add = "";
        add += "public void " + Utils.makeAdd(reverseRelationshipName) + "(" + sourceBeanName + " " + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ")\r\n";
        add += "{\r\n";
        add += "\tif (this." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + " == null)\r\n";
        add += "\t\tthis." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + " = new Vector();\r\n\r\n";
        add += "\tthis." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ".add(" + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ");\r\n";
        add += "}\r\n";
        type.createMethod(add, null, true, null);

        remove = "";
        remove += "public void " + Utils.makeRemove(reverseRelationshipName) + "(" + sourceBeanName + " " + Utils.makeFirstLetterLowerCase(sourceBeanName) + ")\r\n";
        remove += "{\r\n";
        remove += "\tif (this." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + " == null)\r\n";
        remove += "\t\treturn;\r\n\r\n";
        remove += "\tthis." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ".remove(" + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + ");\r\n";
        remove += "}\r\n";
        type.createMethod(remove, null, true, null);

        removeAll = "";
        removeAll += "public void " + Utils.makeRemoveAll(reverseRelationshipName) + "()\r\n";
        removeAll += "{\r\n";
        removeAll += "\tthis." + Utils.makeFirstLetterLowerCase(reverseRelationshipName) + " = new Vector<"+sourceBeanName+">();\r\n";
        removeAll += "}\r\n";
        type.createMethod(removeAll, null, true, null);
     
        targetCompilationUnit.save(null, true);
       
        // Inserimento delle annotazioni relative al generatore
       
        types = type.getChildren();
       
        for (IJavaElement element : types)
        {
          if (element instanceof IMethod && element.getElementName().contains(Utils.makeGet(reverseRelationshipName)))
            method = (IMethod)element;
        }
       
        Utils.addSeamGeneratorAnnotations(method, targetCompilationUnit, relationshipNameDialog.getTargetRelationshipOption(), relationshipNameDialog.getTargetAttributeSelectionOption(), relationshipNameDialog.getSourceAttributeSelectionOption());       
      }

    }
    catch (JavaModelException e)
    {
      ErrorMessageDialog errorMessageDialog = new ErrorMessageDialog(e.getMessage());
      errorMessageDialog.pack();
      errorMessageDialog.setLocationRelativeTo(null);
      errorMessageDialog.setVisible(true);
    }

  }

  public void selectionChanged(IAction action, ISelection selection)
  {
    StructuredSelection structSelection = (StructuredSelection) selection;

    // System.out.println("Class " + structSelection.getFirstElement().getClass());

    if (structSelection.getFirstElement().getClass().equals(org.eclipse.jdt.internal.core.CompilationUnit.class))
    {

      sourceCompilationUnit = (ICompilationUnit) structSelection.getFirstElement();
    }
  }
}
TOP

Related Classes of boco.ejb3PlugIn.popup.actions.relationships.RelationshipN2NOwner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.