{
/**
* Acquisisce il nome della relazione
*
*/
RelationshipDialog relationshipNameDialog = new RelationshipDialog(sourceBeanName, "N", targetBeanName, "N",sourceCompilationUnit,targetCompilationUnit);
relationshipNameDialog.pack();
relationshipNameDialog.setLocationRelativeTo(null);
relationshipNameDialog.setVisible(true);
directRelationshipName = relationshipNameDialog.getDirectRelationshipName();
reverseRelationshipName = relationshipNameDialog.getReverseRelationshipName();
if (relationshipNameDialog.isCancelled())
return;
/**
* 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);
sourceCompilationUnit.createImport("javax.persistence.JoinTable", 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)\r\n";
get += "@JoinTable(name=\"" + sourceBeanName +"_" + directRelationshipName + "\")";
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);
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, mappedBy=\"" + Utils.makeFirstLetterLowerCase(directRelationshipName) + "\")\r\n";
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(reverseRelationshipName) + ")\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());