public void edit(Weapon weapon) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Weapon persistentWeapon = em.find(Weapon.class, weapon.getId());
Item offhandOld = persistentWeapon.getOffhand();
Item offhandNew = weapon.getOffhand();
Item leadhandOld = persistentWeapon.getLeadhand();
Item leadhandNew = weapon.getLeadhand();
Collection<Npc> npcCollectionOld = persistentWeapon.getNpcCollection();
Collection<Npc> npcCollectionNew = weapon.getNpcCollection();
Collection<Weaponset> weaponsetCollectionOld = persistentWeapon.getWeaponsetCollection();
Collection<Weaponset> weaponsetCollectionNew = weapon.getWeaponsetCollection();
List<String> illegalOrphanMessages = null;
for (Npc npcCollectionOldNpc : npcCollectionOld) {
if (!npcCollectionNew.contains(npcCollectionOldNpc)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Npc " + npcCollectionOldNpc + " since its weapons field is not nullable.");
}
}
for (Weaponset weaponsetCollectionOldWeaponset : weaponsetCollectionOld) {
if (!weaponsetCollectionNew.contains(weaponsetCollectionOldWeaponset)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Weaponset " + weaponsetCollectionOldWeaponset + " since its weapons field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (offhandNew != null) {
offhandNew = em.getReference(offhandNew.getClass(), offhandNew.getId());
weapon.setOffhand(offhandNew);
}
if (leadhandNew != null) {
leadhandNew = em.getReference(leadhandNew.getClass(), leadhandNew.getId());
weapon.setLeadhand(leadhandNew);
}
Collection<Npc> attachedNpcCollectionNew = new ArrayList<Npc>();
for (Npc npcCollectionNewNpcToAttach : npcCollectionNew) {
npcCollectionNewNpcToAttach = em.getReference(npcCollectionNewNpcToAttach.getClass(), npcCollectionNewNpcToAttach.getId());
attachedNpcCollectionNew.add(npcCollectionNewNpcToAttach);
}
npcCollectionNew = attachedNpcCollectionNew;
weapon.setNpcCollection(npcCollectionNew);
Collection<Weaponset> attachedWeaponsetCollectionNew = new ArrayList<Weaponset>();
for (Weaponset weaponsetCollectionNewWeaponsetToAttach : weaponsetCollectionNew) {
weaponsetCollectionNewWeaponsetToAttach = em.getReference(weaponsetCollectionNewWeaponsetToAttach.getClass(), weaponsetCollectionNewWeaponsetToAttach.getWeaponsetPK());
attachedWeaponsetCollectionNew.add(weaponsetCollectionNewWeaponsetToAttach);
}
weaponsetCollectionNew = attachedWeaponsetCollectionNew;
weapon.setWeaponsetCollection(weaponsetCollectionNew);
weapon = em.merge(weapon);
if (offhandOld != null && !offhandOld.equals(offhandNew)) {
offhandOld.getWeaponCollection().remove(weapon);
offhandOld = em.merge(offhandOld);
}
if (offhandNew != null && !offhandNew.equals(offhandOld)) {
offhandNew.getWeaponCollection().add(weapon);
offhandNew = em.merge(offhandNew);
}
if (leadhandOld != null && !leadhandOld.equals(leadhandNew)) {
leadhandOld.getWeaponCollection().remove(weapon);
leadhandOld = em.merge(leadhandOld);
}
if (leadhandNew != null && !leadhandNew.equals(leadhandOld)) {
leadhandNew.getWeaponCollection().add(weapon);
leadhandNew = em.merge(leadhandNew);
}
for (Npc npcCollectionNewNpc : npcCollectionNew) {
if (!npcCollectionOld.contains(npcCollectionNewNpc)) {
Weapon oldWeaponsOfNpcCollectionNewNpc = npcCollectionNewNpc.getWeapons();
npcCollectionNewNpc.setWeapons(weapon);
npcCollectionNewNpc = em.merge(npcCollectionNewNpc);
if (oldWeaponsOfNpcCollectionNewNpc != null && !oldWeaponsOfNpcCollectionNewNpc.equals(weapon)) {
oldWeaponsOfNpcCollectionNewNpc.getNpcCollection().remove(npcCollectionNewNpc);
oldWeaponsOfNpcCollectionNewNpc = em.merge(oldWeaponsOfNpcCollectionNewNpc);
}
}
}
for (Weaponset weaponsetCollectionNewWeaponset : weaponsetCollectionNew) {
if (!weaponsetCollectionOld.contains(weaponsetCollectionNewWeaponset)) {
Weapon oldWeaponsOfWeaponsetCollectionNewWeaponset = weaponsetCollectionNewWeaponset.getWeapons();
weaponsetCollectionNewWeaponset.setWeapons(weapon);
weaponsetCollectionNewWeaponset = em.merge(weaponsetCollectionNewWeaponset);
if (oldWeaponsOfWeaponsetCollectionNewWeaponset != null && !oldWeaponsOfWeaponsetCollectionNewWeaponset.equals(weapon)) {
oldWeaponsOfWeaponsetCollectionNewWeaponset.getWeaponsetCollection().remove(weaponsetCollectionNewWeaponset);
oldWeaponsOfWeaponsetCollectionNewWeaponset = em.merge(oldWeaponsOfWeaponsetCollectionNewWeaponset);
}
}
}
em.getTransaction().commit();