public void edit(Item item) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Item persistentItem = em.find(Item.class, item.getId());
Character customizedForOld = persistentItem.getCustomizedFor();
Character customizedForNew = item.getCustomizedFor();
Itembase baseIDOld = persistentItem.getBaseID();
Itembase baseIDNew = item.getBaseID();
Collection<Itemstat> itemstatCollectionOld = persistentItem.getItemstatCollection();
Collection<Itemstat> itemstatCollectionNew = item.getItemstatCollection();
Collection<Storeditem> storeditemCollectionOld = persistentItem.getStoreditemCollection();
Collection<Storeditem> storeditemCollectionNew = item.getStoreditemCollection();
Collection<Weapon> weaponCollectionOld = persistentItem.getWeaponCollection();
Collection<Weapon> weaponCollectionNew = item.getWeaponCollection();
Collection<Weapon> weaponCollection1Old = persistentItem.getWeaponCollection1();
Collection<Weapon> weaponCollection1New = item.getWeaponCollection1();
List<String> illegalOrphanMessages = null;
for (Itemstat itemstatCollectionOldItemstat : itemstatCollectionOld) {
if (!itemstatCollectionNew.contains(itemstatCollectionOldItemstat)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Itemstat " + itemstatCollectionOldItemstat + " since its item field is not nullable.");
}
}
for (Storeditem storeditemCollectionOldStoreditem : storeditemCollectionOld) {
if (!storeditemCollectionNew.contains(storeditemCollectionOldStoreditem)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Storeditem " + storeditemCollectionOldStoreditem + " since its itemID field is not nullable.");
}
}
for (Weapon weaponCollectionOldWeapon : weaponCollectionOld) {
if (!weaponCollectionNew.contains(weaponCollectionOldWeapon)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Weapon " + weaponCollectionOldWeapon + " since its offhand field is not nullable.");
}
}
for (Weapon weaponCollection1OldWeapon : weaponCollection1Old) {
if (!weaponCollection1New.contains(weaponCollection1OldWeapon)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Weapon " + weaponCollection1OldWeapon + " since its leadhand field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
if (customizedForNew != null) {
customizedForNew = em.getReference(customizedForNew.getClass(), customizedForNew.getId());
item.setCustomizedFor(customizedForNew);
}
if (baseIDNew != null) {
baseIDNew = em.getReference(baseIDNew.getClass(), baseIDNew.getId());
item.setBaseID(baseIDNew);
}
Collection<Itemstat> attachedItemstatCollectionNew = new ArrayList<Itemstat>();
for (Itemstat itemstatCollectionNewItemstatToAttach : itemstatCollectionNew) {
itemstatCollectionNewItemstatToAttach = em.getReference(itemstatCollectionNewItemstatToAttach.getClass(), itemstatCollectionNewItemstatToAttach.getItemstatPK());
attachedItemstatCollectionNew.add(itemstatCollectionNewItemstatToAttach);
}
itemstatCollectionNew = attachedItemstatCollectionNew;
item.setItemstatCollection(itemstatCollectionNew);
Collection<Storeditem> attachedStoreditemCollectionNew = new ArrayList<Storeditem>();
for (Storeditem storeditemCollectionNewStoreditemToAttach : storeditemCollectionNew) {
storeditemCollectionNewStoreditemToAttach = em.getReference(storeditemCollectionNewStoreditemToAttach.getClass(), storeditemCollectionNewStoreditemToAttach.getStoreditemPK());
attachedStoreditemCollectionNew.add(storeditemCollectionNewStoreditemToAttach);
}
storeditemCollectionNew = attachedStoreditemCollectionNew;
item.setStoreditemCollection(storeditemCollectionNew);
Collection<Weapon> attachedWeaponCollectionNew = new ArrayList<Weapon>();
for (Weapon weaponCollectionNewWeaponToAttach : weaponCollectionNew) {
weaponCollectionNewWeaponToAttach = em.getReference(weaponCollectionNewWeaponToAttach.getClass(), weaponCollectionNewWeaponToAttach.getId());
attachedWeaponCollectionNew.add(weaponCollectionNewWeaponToAttach);
}
weaponCollectionNew = attachedWeaponCollectionNew;
item.setWeaponCollection(weaponCollectionNew);
Collection<Weapon> attachedWeaponCollection1New = new ArrayList<Weapon>();
for (Weapon weaponCollection1NewWeaponToAttach : weaponCollection1New) {
weaponCollection1NewWeaponToAttach = em.getReference(weaponCollection1NewWeaponToAttach.getClass(), weaponCollection1NewWeaponToAttach.getId());
attachedWeaponCollection1New.add(weaponCollection1NewWeaponToAttach);
}
weaponCollection1New = attachedWeaponCollection1New;
item.setWeaponCollection1(weaponCollection1New);
item = em.merge(item);
if (customizedForOld != null && !customizedForOld.equals(customizedForNew)) {
customizedForOld.getItemCollection().remove(item);
customizedForOld = em.merge(customizedForOld);
}
if (customizedForNew != null && !customizedForNew.equals(customizedForOld)) {
customizedForNew.getItemCollection().add(item);
customizedForNew = em.merge(customizedForNew);
}
if (baseIDOld != null && !baseIDOld.equals(baseIDNew)) {
baseIDOld.getItemCollection().remove(item);
baseIDOld = em.merge(baseIDOld);
}
if (baseIDNew != null && !baseIDNew.equals(baseIDOld)) {
baseIDNew.getItemCollection().add(item);
baseIDNew = em.merge(baseIDNew);
}
for (Itemstat itemstatCollectionNewItemstat : itemstatCollectionNew) {
if (!itemstatCollectionOld.contains(itemstatCollectionNewItemstat)) {
Item oldItemOfItemstatCollectionNewItemstat = itemstatCollectionNewItemstat.getItem();
itemstatCollectionNewItemstat.setItem(item);
itemstatCollectionNewItemstat = em.merge(itemstatCollectionNewItemstat);
if (oldItemOfItemstatCollectionNewItemstat != null && !oldItemOfItemstatCollectionNewItemstat.equals(item)) {
oldItemOfItemstatCollectionNewItemstat.getItemstatCollection().remove(itemstatCollectionNewItemstat);
oldItemOfItemstatCollectionNewItemstat = em.merge(oldItemOfItemstatCollectionNewItemstat);
}
}
}
for (Storeditem storeditemCollectionNewStoreditem : storeditemCollectionNew) {
if (!storeditemCollectionOld.contains(storeditemCollectionNewStoreditem)) {
Item oldItemIDOfStoreditemCollectionNewStoreditem = storeditemCollectionNewStoreditem.getItemID();
storeditemCollectionNewStoreditem.setItemID(item);
storeditemCollectionNewStoreditem = em.merge(storeditemCollectionNewStoreditem);
if (oldItemIDOfStoreditemCollectionNewStoreditem != null && !oldItemIDOfStoreditemCollectionNewStoreditem.equals(item)) {
oldItemIDOfStoreditemCollectionNewStoreditem.getStoreditemCollection().remove(storeditemCollectionNewStoreditem);
oldItemIDOfStoreditemCollectionNewStoreditem = em.merge(oldItemIDOfStoreditemCollectionNewStoreditem);
}
}
}
for (Weapon weaponCollectionNewWeapon : weaponCollectionNew) {
if (!weaponCollectionOld.contains(weaponCollectionNewWeapon)) {
Item oldOffhandOfWeaponCollectionNewWeapon = weaponCollectionNewWeapon.getOffhand();
weaponCollectionNewWeapon.setOffhand(item);
weaponCollectionNewWeapon = em.merge(weaponCollectionNewWeapon);
if (oldOffhandOfWeaponCollectionNewWeapon != null && !oldOffhandOfWeaponCollectionNewWeapon.equals(item)) {
oldOffhandOfWeaponCollectionNewWeapon.getWeaponCollection().remove(weaponCollectionNewWeapon);
oldOffhandOfWeaponCollectionNewWeapon = em.merge(oldOffhandOfWeaponCollectionNewWeapon);
}
}
}
for (Weapon weaponCollection1NewWeapon : weaponCollection1New) {
if (!weaponCollection1Old.contains(weaponCollection1NewWeapon)) {
Item oldLeadhandOfWeaponCollection1NewWeapon = weaponCollection1NewWeapon.getLeadhand();
weaponCollection1NewWeapon.setLeadhand(item);
weaponCollection1NewWeapon = em.merge(weaponCollection1NewWeapon);
if (oldLeadhandOfWeaponCollection1NewWeapon != null && !oldLeadhandOfWeaponCollection1NewWeapon.equals(item)) {
oldLeadhandOfWeaponCollection1NewWeapon.getWeaponCollection1().remove(weaponCollection1NewWeapon);
oldLeadhandOfWeaponCollection1NewWeapon = em.merge(oldLeadhandOfWeaponCollection1NewWeapon);
}
}
}
em.getTransaction().commit();