public void edit(Skill skill) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Skill persistentSkill = em.find(Skill.class, skill.getId());
Attribute attributeOld = persistentSkill.getAttribute();
Attribute attributeNew = skill.getAttribute();
Collection<Character> characterCollectionOld = persistentSkill.getCharacterCollection();
Collection<Character> characterCollectionNew = skill.getCharacterCollection();
Collection<EquippedSkill> equippedSkillCollectionOld = persistentSkill.getEquippedSkillCollection();
Collection<EquippedSkill> equippedSkillCollectionNew = skill.getEquippedSkillCollection();
List<String> illegalOrphanMessages = null;
for (EquippedSkill equippedSkillCollectionOldEquippedSkill : equippedSkillCollectionOld) {
if (!equippedSkillCollectionNew.contains(equippedSkillCollectionOldEquippedSkill)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain EquippedSkill " + equippedSkillCollectionOldEquippedSkill + " since its skill field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (attributeNew != null) {
attributeNew = em.getReference(attributeNew.getClass(), attributeNew.getId());
skill.setAttribute(attributeNew);
}
Collection<Character> attachedCharacterCollectionNew = new ArrayList<Character>();
for (Character characterCollectionNewCharacterToAttach : characterCollectionNew) {
characterCollectionNewCharacterToAttach = em.getReference(characterCollectionNewCharacterToAttach.getClass(), characterCollectionNewCharacterToAttach.getId());
attachedCharacterCollectionNew.add(characterCollectionNewCharacterToAttach);
}
characterCollectionNew = attachedCharacterCollectionNew;
skill.setCharacterCollection(characterCollectionNew);
Collection<EquippedSkill> attachedEquippedSkillCollectionNew = new ArrayList<EquippedSkill>();
for (EquippedSkill equippedSkillCollectionNewEquippedSkillToAttach : equippedSkillCollectionNew) {
equippedSkillCollectionNewEquippedSkillToAttach = em.getReference(equippedSkillCollectionNewEquippedSkillToAttach.getClass(), equippedSkillCollectionNewEquippedSkillToAttach.getEquippedSkillPK());
attachedEquippedSkillCollectionNew.add(equippedSkillCollectionNewEquippedSkillToAttach);
}
equippedSkillCollectionNew = attachedEquippedSkillCollectionNew;
skill.setEquippedSkillCollection(equippedSkillCollectionNew);
skill = em.merge(skill);
if (attributeOld != null && !attributeOld.equals(attributeNew)) {
attributeOld.getSkillCollection().remove(skill);
attributeOld = em.merge(attributeOld);
}
if (attributeNew != null && !attributeNew.equals(attributeOld)) {
attributeNew.getSkillCollection().add(skill);
attributeNew = em.merge(attributeNew);
}
for (Character characterCollectionOldCharacter : characterCollectionOld) {
if (!characterCollectionNew.contains(characterCollectionOldCharacter)) {
characterCollectionOldCharacter.getSkillCollection().remove(skill);
characterCollectionOldCharacter = em.merge(characterCollectionOldCharacter);
}
}
for (Character characterCollectionNewCharacter : characterCollectionNew) {
if (!characterCollectionOld.contains(characterCollectionNewCharacter)) {
characterCollectionNewCharacter.getSkillCollection().add(skill);
characterCollectionNewCharacter = em.merge(characterCollectionNewCharacter);
}
}
for (EquippedSkill equippedSkillCollectionNewEquippedSkill : equippedSkillCollectionNew) {
if (!equippedSkillCollectionOld.contains(equippedSkillCollectionNewEquippedSkill)) {
Skill oldSkillOfEquippedSkillCollectionNewEquippedSkill = equippedSkillCollectionNewEquippedSkill.getSkill();
equippedSkillCollectionNewEquippedSkill.setSkill(skill);
equippedSkillCollectionNewEquippedSkill = em.merge(equippedSkillCollectionNewEquippedSkill);
if (oldSkillOfEquippedSkillCollectionNewEquippedSkill != null && !oldSkillOfEquippedSkillCollectionNewEquippedSkill.equals(skill)) {
oldSkillOfEquippedSkillCollectionNewEquippedSkill.getEquippedSkillCollection().remove(equippedSkillCollectionNewEquippedSkill);
oldSkillOfEquippedSkillCollectionNewEquippedSkill = em.merge(oldSkillOfEquippedSkillCollectionNewEquippedSkill);
}
}
}
em.getTransaction().commit();