String capitalizedName = StringUtils.capitalize(name);
if (oneToOne && ownerField != null && inverseField != null)
{
StaticJavaStub toReturn = new StaticJavaStub();
JavaStatement getValue = new JavaStatement(ClassUtils.getSimpleName(type) + " " + name
+ " = deletableEntity.get"
+ capitalizedName + "()");
getValue.putImport(type);
toReturn.getChildren().add(getValue);
String fieldToNullify = name.equals(ownerField) ? inverseField : ownerField;
JavaStatement removeRelationship = new JavaStatement(name + ".set"
+ StringUtils.capitalize(fieldToNullify)
+ "(null)");
toReturn.getChildren().add(removeRelationship);
JavaStatement mergeModification = new JavaStatement("this.entityManager.merge(" + name + ")");
toReturn.getChildren().add(mergeModification);
return toReturn;
}
else if (manyToOne && inverseField != null)
{
StaticJavaStub toReturn = new StaticJavaStub();
JavaStatement getValue = new JavaStatement(ClassUtils.getSimpleName(type) + " " + name
+ " = deletableEntity.get"
+ capitalizedName + "()");
getValue.putImport(type);
toReturn.getChildren().add(getValue);
JavaStatement removeInverse = new JavaStatement(name + ".get"
+ StringUtils.capitalize(inverseField)
+ "().remove(deletableEntity)");
toReturn.getChildren().add(removeInverse);
JavaStatement nullOwner = new JavaStatement("deletableEntity.set" + capitalizedName + "(null)");
toReturn.getChildren().add(nullOwner);
JavaStatement mergeModification = new JavaStatement("this.entityManager.merge(" + name + ")");
toReturn.getChildren().add(mergeModification);
return toReturn;
}
else if (oneToMany && ownerField != null)
{
if (type.equals(Collection.class.getName()) || type.equals(Set.class.getName())
|| type.equals(List.class.getName()))
{
String parameterizedType = attributes.get(PARAMETERIZED_TYPE);
String simpleParameterizedType = ClassUtils.getSimpleName(parameterizedType);
StaticJavaStub toReturn = new StaticJavaStub();
JavaStatement iterator = new JavaStatement("Iterator<" + simpleParameterizedType + "> iter"
+ capitalizedName + " = deletableEntity.get"
+ capitalizedName + "().iterator()");
iterator.putImport(Iterator.class.getName());
iterator.putImport(parameterizedType);
toReturn.getChildren().add(iterator);
JavaStatement collectionModifiers = new JavaStatement("for (; iter" + capitalizedName + ".hasNext() ;) ");
String scopedVariable = "nextIn" + capitalizedName;
JavaStatement iterable = new JavaStatement(simpleParameterizedType + " " + scopedVariable
+ " = iter" + capitalizedName + ".next()");
collectionModifiers.getChildren().add(iterable);
JavaStatement nullOwner = new JavaStatement(scopedVariable + ".set"
+ StringUtils.capitalize(ownerField)
+ "(null)");
collectionModifiers.getChildren().add(nullOwner);
JavaStatement removeInverse = new JavaStatement("iter" + capitalizedName + ".remove()");
collectionModifiers.getChildren().add(removeInverse);
JavaStatement mergeModification = new JavaStatement("this.entityManager.merge(" + scopedVariable + ")");
collectionModifiers.getChildren().add(mergeModification);
toReturn.getChildren().add(collectionModifiers);
return toReturn;
}
}
else if (manyToMany && ownerField != null && inverseField != null)
{
if (type.equals(Collection.class.getName()) || type.equals(Set.class.getName())
|| type.equals(List.class.getName()))
{
String parameterizedType = attributes.get(PARAMETERIZED_TYPE);
String simpleParameterizedType = ClassUtils.getSimpleName(parameterizedType);
StaticJavaStub toReturn = new StaticJavaStub();
JavaStatement iterator = new JavaStatement("Iterator<" + simpleParameterizedType + "> iter"
+ capitalizedName + " = deletableEntity.get"
+ capitalizedName + "().iterator()");
iterator.putImport(Iterator.class.getName());
iterator.putImport(parameterizedType);
toReturn.getChildren().add(iterator);
JavaStatement collectionModifiers = new JavaStatement("for (; iter" + capitalizedName + ".hasNext() ;) ");
String scopedVariable = "nextIn" + capitalizedName;
JavaStatement iterable = new JavaStatement(simpleParameterizedType + " " + scopedVariable
+ " = iter" + capitalizedName + ".next()");
collectionModifiers.getChildren().add(iterable);
String collectionToModify = name.equals(ownerField) ? inverseField : ownerField;
JavaStatement removeOtherMember = new JavaStatement(scopedVariable + ".get"
+ StringUtils.capitalize(collectionToModify)
+ "().remove(deletableEntity)");
collectionModifiers.getChildren().add(removeOtherMember);
JavaStatement removeInverse = new JavaStatement("iter" + capitalizedName + ".remove()");
collectionModifiers.getChildren().add(removeInverse);
JavaStatement mergeModification = new JavaStatement("this.entityManager.merge(" + scopedVariable + ")");
collectionModifiers.getChildren().add(mergeModification);
toReturn.getChildren().add(collectionModifiers);
return toReturn;
}