ArrayList individualVOs = (ArrayList)mergeEntityDetails.get("individualVOs");
ArrayList addressVOs = (ArrayList)mergeEntityDetails.get("addressVOs");
ArrayList methodOfContactVOs = (ArrayList)mergeEntityDetails.get("methodOfContactVOs");
// get the EntityVO for the matching id we picked. To serve as a starting point.
EntityVO survivor = null;
int entityId = Integer.parseInt(idSelect);
for (int i = 0; (survivor == null) && (i < entityVOs.size()); i++) {
EntityVO current = (EntityVO)entityVOs.get(i);
if (current.getContactID() == entityId) {
survivor = current;
}
}
// Now we have our survivor, lets update the fields.
IndividualVO primaryContact = null;
int primaryContactId = 0;
for (int i = 0; (primaryContact == null) && (i < individualVOs.size()); i++) {
IndividualVO current = (IndividualVO)individualVOs.get(i);
if (current.getContactID() == Integer.parseInt(primaryContactSelect)) {
primaryContact = current;
primaryContactId = current.getContactID();
primaryContact.setEntityID(entityId);
}
}
// Time to start using the EJB layer
ContactFacade remoteContactFacade = null;
ContactFacadeHome contactFacadeHome = (ContactFacadeHome)CVUtility.getHomeObject("com.centraview.contact.contactfacade.ContactFacadeHome", "ContactFacade");
try {
remoteContactFacade = contactFacadeHome.create();
remoteContactFacade.setDataSource(dataSource);
} catch (Exception e) {
logger.error("[Exception] EntityMerge.Execute Handler ", e);
throw new ServletException(e);
}
ArrayList addressCheckList = new ArrayList();
for (int i = 0; i < addressCheckBox.length; i++) {
int addressId = Integer.parseInt(addressCheckBox[i]);
addressCheckList.add(addressId+"");
remoteContactFacade.changeAddressRelate(addressId, entityId, contactType);
}
// delete un-used addresses
// Of course only delete the ones that weren't selected
// thats what all the following convoluted code does
for (int i = 0; i < addressVOs.size(); i++) {
int currentId = ((AddressVO)addressVOs.get(i)).getAddressID();
if (!addressCheckList.contains(currentId+"")) {
try {
remoteContactFacade.deleteAddress(currentId, entityId, individualId);
} catch (RemoteException re) {
logger.error("[Exception] EntityMerge.Execute Handler ", re);
} catch (AuthorizationFailedException afe) {
logger.error("[Exception] EntityMerge.Execute Handler ", afe);
}
}
}
ArrayList methodOfContactCheckList = new ArrayList();
for (int i = 0; i < methodOfContactCheckBox.length; i++) {
int mocId = Integer.parseInt(methodOfContactCheckBox[i]);
methodOfContactCheckList.add(mocId+"");
remoteContactFacade.changeMOCRelate(mocId, entityId, contactType);
}
// delete un-used mocs.
// Of course only delete the ones that weren't selected
// thats what all the following convoluted code does
for (int i = 0; i < methodOfContactVOs.size(); i++) {
int currentId = ((MethodOfContactVO)methodOfContactVOs.get(i)).getMocID();
if (!methodOfContactCheckList.contains(currentId+"")) {
try {
remoteContactFacade.deleteMOC(currentId, entityId, individualId);
} catch (RemoteException re) {
logger.error("[Exception] EntityMerge.Execute Handler ", re);
} catch (AuthorizationFailedException afe) {
logger.error("[Exception] EntityMerge.Execute Handler ", afe);
}
}
}
MergedEntityConfirmationBean mergedEntityBean = (MergedEntityConfirmationBean)session.getAttribute("mergedEntity");
int listID = mergedEntityBean.getListID();
// populate the surviving VO
survivor.setName(mergedEntityBean.getEntityName());
survivor.setExternalID(mergedEntityBean.getId2());
survivor.setAccManager(mergedEntityBean.getAccountManagerID());
survivor.setAccTeam(mergedEntityBean.getAccountTeamID());
survivor.setSource(mergedEntityBean.getSourceID());
survivor.setSourceName(mergedEntityBean.getSourceName());
survivor.setIndividualID(primaryContactId);
survivor.setIndividualVO(primaryContact);
survivor.setModifiedBy(individualId);
survivor.setList(listID);
survivor.clearMOC(); // Don't make millions of MOCs
// Move all Individuals to the surviving record.
// I can either update the IndividualVOs and call update
// Or do a DB query. For now I am going to do the former, but we
// maybe should update to do the latter, to save precious Compute
// resources. But for now this is cleaner.
for (int i = 0; i < individualVOs.size(); i++) {
IndividualVO current = (IndividualVO)individualVOs.get(i);
current.setEntityID(entityId);
current.setList(listID);
current.setIsPrimaryContact("NO");
try {
remoteContactFacade.updateIndividual(current, individualId);
} catch (RemoteException re) {
logger.error("[Exception] EntityMerge.Execute Handler ", re);