public void edit(Attribute attribute) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Attribute persistentAttribute = em.find(Attribute.class, attribute.getId());
Profession professionOld = persistentAttribute.getProfession();
Profession professionNew = attribute.getProfession();
Collection<Skill> skillCollectionOld = persistentAttribute.getSkillCollection();
Collection<Skill> skillCollectionNew = attribute.getSkillCollection();
Collection<Attributepoint> attributepointCollectionOld = persistentAttribute.getAttributepointCollection();
Collection<Attributepoint> attributepointCollectionNew = attribute.getAttributepointCollection();
List<String> illegalOrphanMessages = null;
for (Skill skillCollectionOldSkill : skillCollectionOld) {
if (!skillCollectionNew.contains(skillCollectionOldSkill)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Skill " + skillCollectionOldSkill + " since its attribute field is not nullable.");
}
}
for (Attributepoint attributepointCollectionOldAttributepoint : attributepointCollectionOld) {
if (!attributepointCollectionNew.contains(attributepointCollectionOldAttributepoint)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Attributepoint " + attributepointCollectionOldAttributepoint + " since its attribute field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (professionNew != null) {
professionNew = em.getReference(professionNew.getClass(), professionNew.getId());
attribute.setProfession(professionNew);
}
Collection<Skill> attachedSkillCollectionNew = new ArrayList<Skill>();
for (Skill skillCollectionNewSkillToAttach : skillCollectionNew) {
skillCollectionNewSkillToAttach = em.getReference(skillCollectionNewSkillToAttach.getClass(), skillCollectionNewSkillToAttach.getId());
attachedSkillCollectionNew.add(skillCollectionNewSkillToAttach);
}
skillCollectionNew = attachedSkillCollectionNew;
attribute.setSkillCollection(skillCollectionNew);
Collection<Attributepoint> attachedAttributepointCollectionNew = new ArrayList<Attributepoint>();
for (Attributepoint attributepointCollectionNewAttributepointToAttach : attributepointCollectionNew) {
attributepointCollectionNewAttributepointToAttach = em.getReference(attributepointCollectionNewAttributepointToAttach.getClass(), attributepointCollectionNewAttributepointToAttach.getAttributepointPK());
attachedAttributepointCollectionNew.add(attributepointCollectionNewAttributepointToAttach);
}
attributepointCollectionNew = attachedAttributepointCollectionNew;
attribute.setAttributepointCollection(attributepointCollectionNew);
attribute = em.merge(attribute);
if (professionOld != null && !professionOld.equals(professionNew)) {
professionOld.getAttributeCollection().remove(attribute);
professionOld = em.merge(professionOld);
}
if (professionNew != null && !professionNew.equals(professionOld)) {
professionNew.getAttributeCollection().add(attribute);
professionNew = em.merge(professionNew);
}
for (Skill skillCollectionNewSkill : skillCollectionNew) {
if (!skillCollectionOld.contains(skillCollectionNewSkill)) {
Attribute oldAttributeOfSkillCollectionNewSkill = skillCollectionNewSkill.getAttribute();
skillCollectionNewSkill.setAttribute(attribute);
skillCollectionNewSkill = em.merge(skillCollectionNewSkill);
if (oldAttributeOfSkillCollectionNewSkill != null && !oldAttributeOfSkillCollectionNewSkill.equals(attribute)) {
oldAttributeOfSkillCollectionNewSkill.getSkillCollection().remove(skillCollectionNewSkill);
oldAttributeOfSkillCollectionNewSkill = em.merge(oldAttributeOfSkillCollectionNewSkill);
}
}
}
for (Attributepoint attributepointCollectionNewAttributepoint : attributepointCollectionNew) {
if (!attributepointCollectionOld.contains(attributepointCollectionNewAttributepoint)) {
Attribute oldAttributeOfAttributepointCollectionNewAttributepoint = attributepointCollectionNewAttributepoint.getAttribute();
attributepointCollectionNewAttributepoint.setAttribute(attribute);
attributepointCollectionNewAttributepoint = em.merge(attributepointCollectionNewAttributepoint);
if (oldAttributeOfAttributepointCollectionNewAttributepoint != null && !oldAttributeOfAttributepointCollectionNewAttributepoint.equals(attribute)) {
oldAttributeOfAttributepointCollectionNewAttributepoint.getAttributepointCollection().remove(attributepointCollectionNewAttributepoint);
oldAttributeOfAttributepointCollectionNewAttributepoint = em.merge(oldAttributeOfAttributepointCollectionNewAttributepoint);
}
}
}
em.getTransaction().commit();