/***************************************************************************
* 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/bean/Attic/AddCpMethodEJB.java,v 1.1.2.4 2008/04/22 06:17:38 fab Exp $
- $Author: fab $
- $Revision: 1.1.2.4 $
- $Date: 2008/04/22 06:17:38 $
- $Log: AddCpMethodEJB.java,v $
- Revision 1.1.2.4 2008/04/22 06:17:38 fab
- Aggiornamento indirizzo di posta
-
- Revision 1.1.2.3 2008/04/19 10:28:43 fab
- *** empty log message ***
-
- Revision 1.1.2.2 2008/04/19 10:26:49 fab
- Aggiornamento riferimenti licenza
-
- 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.4 2008/02/25 09:28:10 fab
- *** empty log message ***
-
- Revision 1.1.2.3 2008/02/24 10:21:18 fab
- *** empty log message ***
-
- Revision 1.1.2.2 2008/02/22 15:27:57 fab
- Inserita la funzione che genera il metodo di copia del bean
-
- Revision 1.1.2.1 2008/02/22 13:32:42 fab
- Inserita la funzione che genera il metodo di copia del bean
-
- Revision 1.1.2.6 2008/02/22 07:16:14 fab
- *** empty log message ***
-
- Revision 1.1.2.5 2008/01/07 10:41:38 fab
- *** empty log message ***
-
- Revision 1.1.2.4 2007/11/07 10:35:44 fab
- Fix vari
-
- Revision 1.1.2.3 2007/11/07 10:19:28 fab
- Fix vari
- Gestione errori
-
- Revision 1.1.2.2 2007/11/05 13:31:16 fab
- Modifica delle annotazioni relative ai bean
- @ShowLists
- @Security
-
- Revision 1.1.2.1 2007/11/04 20:56:14 fab
- Review Interfaccia
-
- Revision 1.5.2.17 2007/10/01 15:16:05 fab
- Aggiornata dialog per la creazione del bean
- Riletta l'annotazione @ShowInForm
-
- Revision 1.5.2.16 2007/09/29 20:13:10 fab
- Fix del bug 0122/B
-
- Revision 1.5.2.15 2007/03/27 06:25:53 dev
- Completata gestione dei ruoli sulle azioni del bean
-
- Revision 1.5.2.14 2007/03/26 07:50:59 dev
- Acquisizione dati per liste multiple
-
- Revision 1.5.2.13 2007/03/25 21:33:11 dev
- Modifica interfaccia per supportare le liste multiple e la security dei bean
-
- Revision 1.5.2.12 2007/03/25 14:40:20 dev
- Acquisita la lista principale dell'applicazione
-
- Revision 1.5.2.11 2007/02/13 09:59:12 dev
- Modifica alla equals del bean
-
- Revision 1.5.2.10 2007/02/13 09:57:54 dev
- Modifica alla equals del bean
-
- Revision 1.5.2.9 2007/01/07 14:38:20 dev
- Modifiche Dicembre 2006
-
- Revision 1.5.2.8 2007/01/03 12:51:13 dev
- Fix per i tag CVS
-
- Revision 1.5.2.7 2007/01/03 12:50:24 dev
- Fix per i tag CVS
-
- Revision 1.5.2.6 2007/01/03 11:36:05 dev
- Inseriti disclaymer e tag cvs
-
- Revision 1.5.2.5 2007/01/03 10:39:10 dev
- I Bean sono creati nella CONVERSATION
-
- Revision 1.5.2.4 2006/12/25 11:10:18 dev
- Introdotta gestione degli errori
- Fix per le relazioni N:M che avevano l'ownership invertita
-
- Revision 1.5.2.3 2006/12/24 16:15:57 dev
- Acquisito il nome della relazione per le relazioni N:M
-
- Revision 1.5.2.2 2006/12/17 16:15:35 dev
- Riorganizzata la classe Utils che era divisia in due !?
-
- Revision 1.5.2.1 2006/08/28 08:48:49 dev
- Modifiche principli per il supporto di seam
-
- Revision 1.5 2006/05/21 13:41:18 dev
- Corretta annotazione cvs
-
**/
package boco.ejb3PlugIn.popup.actions.bean;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.Vector;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
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.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import boco.ejb3PlugIn.gui.ErrorMessageDialog;
import boco.ejb3PlugIn.utils.Utils;
public class AddCpMethodEJB implements IObjectActionDelegate
{
private IJavaProject javaProject;
private IPackageFragment packageFragment;
private ICompilationUnit iCompilationUnit;
public AddCpMethodEJB()
{
super();
}
public void setActivePart(IAction action, IWorkbenchPart targetPart)
{
}
public void run(IAction action)
{
try
{
String beanOldSource;
String beanNewSource;
String oldCopyMethodSource;
String newCopyMethodSource = "";
IType bean = iCompilationUnit.getAllTypes()[0];
IMethod methods[] = bean.getMethods();
Vector<IMethod> propertyMethods = getPropertyMethods(methods);
Vector<IMethod> relationshipsMethods = getRelationshipsMethods(methods);
// Vector<IMethod> relationshipsToOneMethods = new Vector<IMethod>();
// for (IMethod method : relationshipsMethods)
// {
// if (method.getSource().contains("ManyToOne"))
// {
// relationshipsToOneMethods.add(method);
// }
// }
Vector<IMethod> relationshipsNToMMethods = new Vector<IMethod>();
for (IMethod method : relationshipsMethods)
{
if (method.getReturnType().startsWith("QList") && method.getSource().contains("ManyToMany") && !method.getSource().contains("mappedBy"))
{
relationshipsNToMMethods.add(method);
}
}
Vector<IMethod> relationshipsOneToOneMethods = new Vector<IMethod>();
for (IMethod method : relationshipsMethods)
{
if (method.getSource().contains("OneToOne") && !method.getSource().contains("mappedBy"))
{
relationshipsOneToOneMethods.add(method);
}
}
oldCopyMethodSource = getCopyMethodSource(methods);
/** Generazione del metodo **/
newCopyMethodSource += "\tpublic " + bean.getElementName() + " copy(";
// for (IMethod method : relationshipsNToMMethods)
// {
// if (!newCopyMethodSource.endsWith("("))
// newCopyMethodSource += ", ";
//
//
// newCopyMethodSource +="\r\n\t";
// newCopyMethodSource += "Vector<" + method.getReturnType().replace("QList<Q", "").replace(";>;", "") + ">";
// newCopyMethodSource += " ";
// newCopyMethodSource += Utils.makeMethod2Field(method.getElementName());
// }
// for (IMethod method : relationshipsToOneMethods)
// {
// if (!newCopyMethodSource.endsWith("("))
// newCopyMethodSource += ", ";
//
//
// //newCopyMethodSource +="\r\n\t";
// newCopyMethodSource += Utils.makeFirstLetterUpperCase(Utils.makeMethod2Field(method.getElementName()));
// newCopyMethodSource += " ";
// newCopyMethodSource += Utils.makeMethod2Field(method.getElementName());
// }
for (IMethod method : relationshipsOneToOneMethods)
{
if (!newCopyMethodSource.endsWith("("))
newCopyMethodSource += ", ";
//newCopyMethodSource +="\r\n\t";
newCopyMethodSource += Utils.makeFirstLetterUpperCase(Utils.makeMethod2Field(method.getElementName()));
newCopyMethodSource += " ";
newCopyMethodSource += Utils.makeMethod2Field(method.getElementName());
}
newCopyMethodSource += ")\r\n";
newCopyMethodSource += "\t{\r\n";
newCopyMethodSource += "\t\t" + bean.getElementName() + " ret = new " + bean.getElementName() + "();\r\n\r\n";
/** Metodi proprietà **/
for (IMethod method : propertyMethods)
{
newCopyMethodSource += "\t\tret." + Utils.switchGet2Set(method.getElementName()) + "(this." + method.getElementName() + "());\r\n";
}
newCopyMethodSource += "\r\n";
/** Metodi relazioni N:N di cui questo bean è owner **/
for (IMethod method : relationshipsNToMMethods)
{
newCopyMethodSource += "\t\t" + method.getElementName() + "().size();\r\n";
newCopyMethodSource += "\t\tret." + Utils.switchGet2Set(method.getElementName()) + "(" + method.getElementName() + "());\r\n";
}
newCopyMethodSource += "\r\n";
// for (IMethod method : relationshipsToOneMethods)
// {
// newCopyMethodSource += "\t\tret." + Utils.switchGet2Set(method.getElementName()) + "(" + Utils.makeMethod2Field(method.getElementName()) + ");\r\n";
// }
// newCopyMethodSource += "\r\n";
/** Metodi relazioni 1:1 di cui questo bean è owner **/
for (IMethod method : relationshipsOneToOneMethods)
{
newCopyMethodSource += "\t\tret." + Utils.switchGet2Set(method.getElementName()) + "(" + Utils.makeMethod2Field(method.getElementName()) + ");\r\n";
}
newCopyMethodSource += "\r\n";
newCopyMethodSource += "\r\n";
newCopyMethodSource += "\t\treturn ret;\r\n";
newCopyMethodSource += "\t}\r\n";
//System.out.println(newCopyMethodSource);
/** Eliminazione dell'eventuale metodo di copia già esistente **/
beanOldSource = iCompilationUnit.getSource();
beanOldSource = beanOldSource.replace(oldCopyMethodSource, "");
/** Nuovo codice del bean **/
beanNewSource = beanOldSource.substring(0, beanOldSource.lastIndexOf("}"));
beanNewSource += "\r\n\r\n";
beanNewSource += newCopyMethodSource;
beanNewSource += "}";
iCompilationUnit.getBuffer().setContents(beanNewSource);
iCompilationUnit.save(null, true);
}
catch (Exception e)
{
ErrorMessageDialog errorMessageDialog = new ErrorMessageDialog(e.getMessage());
errorMessageDialog.pack();
errorMessageDialog.setLocationRelativeTo(null);
errorMessageDialog.setVisible(true);
//e.printStackTrace();
}
}
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))
{
iCompilationUnit = (ICompilationUnit) structSelection.getFirstElement();
javaProject = iCompilationUnit.getJavaProject();
}
}
public static Object getGUIBuilderInstance()
{
return new AddCpMethodEJB(Boolean.FALSE);
}
public AddCpMethodEJB(Boolean initGUI)
{
super();
}
private String getBeanName(String source) throws Exception
{
String line;
LineNumberReader lr = new LineNumberReader(new StringReader(source));
while ((line = lr.readLine()) != null)
{
if (line.contains("public class "))
{
int start = line.indexOf("public class ") + 13;
if (start == -1)
throw new Exception("Errore nella lettura del nome del bean");
int end = line.indexOf(" implements Serializable");
if (end == -1)
throw new Exception("Errore nella lettura del nome del bean");
lr.close();
return line.substring(start, end);
}
}
return "";
}
private Vector<IMethod> getPropertyMethods(IMethod methods[]) throws JavaModelException
{
Vector<IMethod> ret = new Vector<IMethod>();
for (int i = 0; i < methods.length; i++)
{
String returnType = methods[i].getReturnType();
if ((returnType.equals("QString;") ||
returnType.equals("QBoolean;") ||
returnType.equals("QDate;") ||
returnType.equals("QLong;") ||
returnType.equals("QInteger;") ||
returnType.equals("QDouble;") ||
returnType.equals("QDate;") ||
returnType.equals("QFileManagement;") ||
returnType.equals("Qorg.boco.seamUtility.FileManagement;") ||
methods[i].getSource().contains("ManyToOne")
// (methods[i].getSource().contains("OneToOne") && methods[i].getSource().contains("mappedBy")) ||
// (methods[i].getSource().contains("ManyToMany") && !methods[i].getSource().contains("mappedBy"))
) &&
!methods[i].getSource().contains("@Transient") &&
!methods[i].getSource().contains("@EmbeddedId") &&
!methods[i].getSource().contains("@Id") &&
!methods[i].getSource().contains("toString")
)
{
ret.add(methods[i]);
}
}
return ret;
}
private Vector<IMethod> getRelationshipsMethods(IMethod methods[]) throws JavaModelException
{
Vector<IMethod> ret = new Vector<IMethod>();
for (int i = 0; i < methods.length; i++)
{
String returnType = methods[i].getReturnType();
if (!returnType.equals("QString;") &&
!returnType.equals("QBoolean;") &&
!returnType.equals("QDate;") &&
!returnType.equals("QLong;") &&
!returnType.equals("QInteger;") &&
!returnType.equals("QDouble;") && !returnType.equals("QDate;") &&
!returnType.equals("V") && !returnType.equals("Z") &&
!returnType.equals("J") &&
// !(methods[i].getSource().contains("ManyToMany") && !methods[i].getSource().contains("mappedBy")) &&
!methods[i].getSource().contains("@Transient") &&
!methods[i].getSource().contains("toString")
)
{
ret.add(methods[i]);
}
}
return ret;
}
private String getCopyMethodSource(IMethod methods[]) throws JavaModelException
{
String ret = "";
for (int i = 0; i < methods.length; i++)
{
if (methods[i].getSource().contains("copy("))
return methods[i].getSource();
}
return ret;
}
}