/**
   * Method to get the Party information for all participants and get the logical Id for claim
   * import.
   *
   * @param claimImportCompositeDTO
   * @param lstParticipants
   */
  private void retrieveParty(
      ClaimImportCompositeDTO claimImportCompositeDTO, List<DataTransferObject> lstParticipants) {
    for (DataTransferObject participant : lstParticipants) {
      Long partyId = null;
      if (participant instanceof CALClaimParticipationDTO) {
        partyId = ((CALClaimParticipationDTO) participant).getPartyId();
      } else if (participant instanceof CaseParticipationDTO) {
        partyId = ((CaseParticipationDTO) participant).getPartyId();
      } else if (participant instanceof CALClaimParticipationContactDTO) {
        partyId = ((CALClaimParticipationContactDTO) participant).getPartyId();
      } else if (participant instanceof ClaimPolicyParticipationDTO) {
        partyId = ((ClaimPolicyParticipationDTO) participant).getPartyId();
      } else if (participant instanceof CALClaimParticipationProvDTO) {
        partyId = ((CALClaimParticipationProvDTO) participant).getPartyId();
      }

      // If participant is unknown then party id will be null
      if (partyId == null) {
        continue;
      }

      Long partyIndex = null;
      if (partyList.contains(partyId)) {
        partyIndex = new Long(partyList.indexOf(partyId) + 1);
        participant.setObjectId(partyIndex.toString());
      } else {
        PartyDTO partyDTO = getClientPartyService().retrievePartyDetails(partyId);
        if (partyDTO != null) {
          partyList.add(partyId);
          partyDTO.setPreferences(null);
          partyDTO.setPartyPreference(null);
          partyIndex = new Long(partyList.indexOf(partyId) + 1);
          partyDTO.setRecordId(partyIndex);
          participant.setObjectId(partyIndex.toString());
          partyDTO.setAuthorizations(null);
          if (partyDTO instanceof PersonDTO) {
            claimImportCompositeDTO.getPersons().add((ClientPersonDTO) partyDTO);
          } else {
            claimImportCompositeDTO.getOrganizations().add((ClientOrganizationDTO) partyDTO);
          }
        }
      }

      if (participant instanceof CALClaimParticipationDTO) {
        ((CALClaimParticipationDTO) participant).setPartyId(null);
      } else if (participant instanceof CaseParticipationDTO) {
        ((CaseParticipationDTO) participant).setPartyId(null);
      } else if (participant instanceof CALClaimParticipationContactDTO) {
        ((CALClaimParticipationContactDTO) participant).setPartyId(null);
      } else if (participant instanceof ClaimPolicyParticipationDTO) {
        ((ClaimPolicyParticipationDTO) participant).setPartyId(null);
      } else if (participant instanceof CALClaimParticipationProvDTO) {
        ((CALClaimParticipationProvDTO) participant).setPartyId(null);
      }
    }
  }
  /**
   * Method to get the Party Information for Participants and return the Logical Id for Claim
   * Import.
   *
   * @param claimImportCompositeDTO
   * @param partyId
   * @return Logical Id for Claim Import
   */
  private Long retrieveParty(ClaimImportCompositeDTO claimImportCompositeDTO, Long partyId) {
    Long partyIndex = null;
    if (partyList.contains(partyId)) {
      return new Long(partyList.indexOf(partyId) + 1);
    } else {
      PartyDTO partyDTO = getClientPartyService().retrievePartyDetails(partyId);
      if (partyDTO != null) {
        partyList.add(partyId);
        partyDTO.setPreferences(null);
        partyDTO.setPartyPreference(null);
        partyIndex = new Long(partyList.indexOf(partyId) + 1);
        partyDTO.setRecordId(partyIndex);

        if (partyDTO instanceof PersonDTO) {
          claimImportCompositeDTO.getPersons().add((ClientPersonDTO) partyDTO);
        } else {
          claimImportCompositeDTO.getOrganizations().add((ClientOrganizationDTO) partyDTO);
        }
      }
      return partyIndex;
    }
  }
  /**
   * @param request ClaimDetailRequestCriteria
   * @param returnObject ClaimImportCompositeDTO
   */
  public void processClaimDetailsForImport(
      ClaimDetailRequestCriteria request, ClaimImportCompositeDTO returnObject) {
    // to import Reserve, Payment and Recovery from Claim Details
    // response using Claim Import
    // we need to set external identifier for Unit and Participant and
    // set the same for Reserve, Payment and Recovery

    // First create a List of Unit/Participant Record Id and use the
    // List Index as the external identifier.
    List<String> lstUnitParticipantRecordId = new ArrayList<String>();

    Map<Long, String> claimUnitExternalSourceId = new HashMap<Long, String>();
    if (returnObject.getUnits() != null) {
      for (ClaimUnitDTO claimUnit : returnObject.getUnits()) {
        String unitRecordId = claimUnit.getRecordId().toString();
        lstUnitParticipantRecordId.add(unitRecordId);

        if (claimUnit.getExternalSourceId() == null) {
          claimUnit.setExternalSourceId(
              "" + (lstUnitParticipantRecordId.indexOf(unitRecordId) + 1));
        }

        claimUnitExternalSourceId.put(claimUnit.getRecordId(), claimUnit.getExternalSourceId());

        if (claimUnit instanceof ClaimUnitVehicleDTO) {
          ClaimUnitVehicleDTO claimUnitVehicle = (ClaimUnitVehicleDTO) claimUnit;

          retrieveParticipantRecordId(
              claimUnitVehicle.getRecordId(),
              claimUnitVehicle.getDrivers(),
              lstUnitParticipantRecordId);
          retrieveParticipantRecordId(
              claimUnitVehicle.getRecordId(),
              claimUnitVehicle.getPassengers(),
              lstUnitParticipantRecordId);
        }

        retrieveParticipantRecordId(
            claimUnit.getRecordId(),
            claimUnit.getFinancialInterestList(),
            lstUnitParticipantRecordId);
        retrieveParticipantRecordId(
            claimUnit.getRecordId(), claimUnit.getInjuredPersonList(), lstUnitParticipantRecordId);
        retrieveParticipantRecordId(
            claimUnit.getRecordId(), claimUnit.getOtherPartiesList(), lstUnitParticipantRecordId);
        retrieveParticipantRecordId(
            claimUnit.getRecordId(), claimUnit.getOwners(), lstUnitParticipantRecordId);
      }
    }

    // Set the Identifier for Reserve to associate to Unit and
    // Participant
    // This is require for Claim Import if Units, Participants and
    // Reserves are imported at the same time
    if (request.isReturnClaimReservesWithCoverages()
        && returnObject.getClaimReservesWithCoverages() != null) {
      for (CALClaimReserveDTO claimReserveDTO : returnObject.getClaimReservesWithCoverages()) {

        claimReserveDTO.setExternalUnitIdentifier(
            claimUnitExternalSourceId.get(claimReserveDTO.getCalClaimUnitIdDerived()));

        if (claimReserveDTO.getExternalUnitIdentifier() == null) {
          claimReserveDTO.setExternalUnitIdentifier(
              ""
                  + (lstUnitParticipantRecordId.indexOf(
                          claimReserveDTO.getCalClaimUnitIdDerived().toString())
                      + 1));
        }

        claimReserveDTO.setExternalParticipantIdentifier(
            ""
                + (lstUnitParticipantRecordId.indexOf(
                        claimReserveDTO.getCalClaimUnitIdDerived().toString()
                            + "_"
                            + claimReserveDTO.getCalClaimPtcpId().toString())
                    + 1));
        claimReserveDTO.setExternalClaimIdentifier(null);

        claimReserveDTO.setClaimNumber(null);
        claimReserveDTO.setCalClaimIdDerived(null);
        claimReserveDTO.setCalClaimPtcpId(null);
        claimReserveDTO.setCalClaimUnitIdDerived(null);

        if (claimReserveDTO.getUser() == null) {
          claimReserveDTO.setUser(getLoginName(claimReserveDTO.getUserIdCreated()));
        }

        if (claimReserveDTO.getAllAgreementData() != null
            && !claimReserveDTO.getAllAgreementData().isEmpty()) {
          claimReserveDTO.getAllAgreementData().clear();
        }
      }
    }

    // Set the Identifier for Payment to associate to Unit and
    // Participant
    // This is require for Claim Import if Units, Participants, Reserves
    // and Payments are imported at the same time
    if (request.isReturnClaimPayment() && returnObject.getClaimPayments() != null) {
      for (ClaimPayment claimPayment : returnObject.getClaimPayments()) {
        if (claimPayment.getClaimPayables() != null) {
          for (ClaimPayable claimPayable : claimPayment.getClaimPayables()) {
            if (claimPayable.getUnitId() != null) {

              claimPayable.setExternalUnitIdentifier(
                  claimUnitExternalSourceId.get(claimPayable.getUnitId()));

              if (claimPayable.getExternalUnitIdentifier() == null) {
                claimPayable.setExternalUnitIdentifier(
                    ""
                        + (lstUnitParticipantRecordId.indexOf(claimPayable.getUnitId().toString())
                            + 1));
              }
              if (claimPayable.getParticipantId() != null) {
                claimPayable.setExternalParticipantIdentifier(
                    ""
                        + (lstUnitParticipantRecordId.indexOf(
                                claimPayable.getUnitId().toString()
                                    + "_"
                                    + claimPayable.getParticipantId().toString())
                            + 1));
              }
            }
            claimPayable.setExternalClaimIdentifier(null);
            claimPayable.setClaimId(null);
            claimPayable.setClaimNumber(null);
            claimPayable.setUnitId(null);
            claimPayable.setParticipantId(null);
          }
        }
      }
    }

    // Set the Identifier for Recovery to associate to Unit and
    // Participant
    // This is require for Claim Import if Units, Participants, Reserves
    // and Recoveries are imported at the same time
    if (request.isReturnClaimRecovery() && returnObject.getRecoveries() != null) {
      for (ClaimRecovery claimRecovery : returnObject.getRecoveries()) {
        if (claimRecovery.getClaimRecoveryPayables() != null) {
          for (ClaimRecoveryPayable claimRecoveryPayable :
              claimRecovery.getClaimRecoveryPayables()) {

            claimRecoveryPayable.setExternalUnitIdentifier(
                ""
                    + (lstUnitParticipantRecordId.indexOf(
                            claimRecoveryPayable.getUnitId().toString())
                        + 1));
            claimRecoveryPayable.setExternalParticipantIdentifier(
                ""
                    + (lstUnitParticipantRecordId.indexOf(
                            claimRecoveryPayable.getUnitId().toString()
                                + "_"
                                + claimRecoveryPayable.getParticipantId().toString())
                        + 1));
            claimRecoveryPayable.setExternalClaimIdentifier(null);

            claimRecoveryPayable.setClaimId(null);
            claimRecoveryPayable.setClaimNumber(null);
            claimRecoveryPayable.setUnitId(null);
            claimRecoveryPayable.setParticipantId(null);
          }
        }
      }
    }

    partyList = new ArrayList<Long>();

    // get party id for all participants returned from Claim Details and
    // add it to a Collection.
    returnObject.setPersons(new ArrayList<ClientPersonDTO>());
    returnObject.setOrganizations(new ArrayList<ClientOrganizationDTO>());
    List<DataTransferObject> lstParticipants = new ArrayList<DataTransferObject>();
    if (returnObject.getClaimDTO().getClmParticipants() != null
        && !returnObject.getClaimDTO().getClmParticipants().isEmpty()) {

      lstParticipants.addAll(returnObject.getClaimDTO().getClmParticipants());
      retrieveParticipantEntities(returnObject.getClaimDTO().getClmParticipants(), lstParticipants);
    }

    // Iterate thru' Claim Policy and add all participants to the List
    // to set Logical Id for Parties
    if (request.isReturnClaimPolicy() && returnObject.getClaimDTO().getPolicy() != null) {
      ClaimPolicyDTO claimPolicy = returnObject.getClaimDTO().getPolicy();
      if (claimPolicy.getClmPolicyAgencyParticipants() != null) {
        lstParticipants.addAll(claimPolicy.getClmPolicyAgencyParticipants());
      }
      if (claimPolicy.getClmPolicyDriverParticipants() != null) {
        lstParticipants.addAll(claimPolicy.getClmPolicyDriverParticipants());
      }
      if (claimPolicy.getClmPolicyFinancialInterestParticipants() != null) {
        lstParticipants.addAll(claimPolicy.getClmPolicyFinancialInterestParticipants());
      }
      if (claimPolicy.getClmPolicyInsuredParticipants() != null) {
        lstParticipants.addAll(claimPolicy.getClmPolicyInsuredParticipants());
      }

      if (claimPolicy.getUnits() != null) {
        for (ClaimPolicyUnitDTO claimPolicyUnit : claimPolicy.getUnits()) {
          if (claimPolicyUnit.getPartyId() != null) {
            claimPolicyUnit.setObjectId(
                retrieveParty(returnObject, claimPolicyUnit.getPartyId()).toString());
            claimPolicyUnit.setPartyId(null);
          }
          if (claimPolicyUnit.getParticipations() != null) {
            lstParticipants.addAll(claimPolicyUnit.getParticipations());
          }
        }
      }
    }

    // Iterate thru' Claim Services and add all participants to the List
    // to set Logical Id for Parties
    if (returnObject.getClaimServices() != null && !returnObject.getClaimServices().isEmpty()) {
      lstParticipants.addAll(returnObject.getClaimServices());
      retrieveParticipantEntities(returnObject.getClaimServices(), lstParticipants);
    }

    // Iterate thru' Units and add all participants to the List to set
    // Logical Id for Parties
    if (returnObject.getUnits() != null) {
      for (ClaimUnitDTO claimUnit : returnObject.getUnits()) {
        if (claimUnit instanceof ClaimUnitVehicleDTO) {
          ClaimUnitVehicleDTO claimUnitVehicle = (ClaimUnitVehicleDTO) claimUnit;
          if (claimUnitVehicle.getDrivers() != null && !claimUnitVehicle.getDrivers().isEmpty()) {
            lstParticipants.addAll(claimUnitVehicle.getDrivers());
            retrieveParticipantEntities(claimUnitVehicle.getDrivers(), lstParticipants);
          }
          if (claimUnitVehicle.getPassengers() != null
              && !claimUnitVehicle.getPassengers().isEmpty()) {
            lstParticipants.addAll(claimUnitVehicle.getPassengers());
            retrieveParticipantEntities(claimUnitVehicle.getPassengers(), lstParticipants);
          }
        }

        if (claimUnit.getClaimDamageDTO() != null
            && claimUnit.getClaimDamageDTO().getDamageServicesDTO() != null) {
          lstParticipants.addAll(claimUnit.getClaimDamageDTO().getDamageServicesDTO());
          retrieveParticipantEntities(
              claimUnit.getClaimDamageDTO().getDamageServicesDTO(), lstParticipants);
        }
        if (claimUnit.getClaimSalvageDTO() != null
            && claimUnit.getClaimSalvageDTO().getSalvageSalvorsDTO() != null) {
          lstParticipants.addAll(claimUnit.getClaimSalvageDTO().getSalvageSalvorsDTO());
          retrieveParticipantEntities(
              claimUnit.getClaimSalvageDTO().getSalvageSalvorsDTO(), lstParticipants);
        }
        if (claimUnit.getFinancialInterestList() != null
            && !claimUnit.getFinancialInterestList().isEmpty()) {
          lstParticipants.addAll(claimUnit.getFinancialInterestList());
          retrieveParticipantEntities(claimUnit.getFinancialInterestList(), lstParticipants);
        }
        if (claimUnit.getInjuredPersonList() != null
            && !claimUnit.getInjuredPersonList().isEmpty()) {
          lstParticipants.addAll(claimUnit.getInjuredPersonList());
          retrieveParticipantEntities(claimUnit.getInjuredPersonList(), lstParticipants);
        }
        if (claimUnit.getOtherCarriersList() != null
            && !claimUnit.getOtherCarriersList().isEmpty()) {
          lstParticipants.addAll(claimUnit.getOtherCarriersList());
          retrieveParticipantEntities(claimUnit.getOtherCarriersList(), lstParticipants);
        }
        if (claimUnit.getOtherPartiesList() != null && !claimUnit.getOtherPartiesList().isEmpty()) {
          lstParticipants.addAll(claimUnit.getOtherPartiesList());
          retrieveParticipantEntities(claimUnit.getOtherPartiesList(), lstParticipants);
        }
        if (claimUnit.getOwners() != null && !claimUnit.getOwners().isEmpty()) {
          lstParticipants.addAll(claimUnit.getOwners());
          retrieveParticipantEntities(claimUnit.getOwners(), lstParticipants);
        }
      }
    }

    // Add Authorities to the Claim Participant Collection
    if (returnObject.getAuthorities() != null) {
      lstParticipants.addAll(returnObject.getAuthorities());
      retrieveParticipantEntities(returnObject.getAuthorities(), lstParticipants);
      for (ClaimParticipationAuthorityDTO authorityDTO : returnObject.getAuthorities()) {
        retrieveParticipantEntities(authorityDTO.getCitations(), lstParticipants);
      }
    }
    // Add Witnesses to the Claim Participant Collection
    if (returnObject.getWitnesses() != null) {
      lstParticipants.addAll(returnObject.getWitnesses());
      retrieveParticipantEntities(returnObject.getWitnesses(), lstParticipants);
    }

    // Iterate through case level participants to update party Id
    if (returnObject.getClaimCases() != null) {
      for (CaseDTO caseDTO : returnObject.getClaimCases()) {
        if (caseDTO.getParticipationCompositeDTO() != null
            && caseDTO.getParticipationCompositeDTO().getCaseParticipations() != null
            && !caseDTO.getParticipationCompositeDTO().getCaseParticipations().isEmpty()) {
          lstParticipants.addAll(caseDTO.getParticipationCompositeDTO().getCaseParticipations());
        }
      }
    }

    // For each party in Payments retrieve Party Information and set
    // logical Id so that
    // the response can be used for Claim Import
    if (request.isReturnClaimPayment() && returnObject.getClaimPayments() != null) {
      for (ClaimPayment claimPayment : returnObject.getClaimPayments()) {
        if (claimPayment.getClaimPayables() != null) {
          for (ClaimPayable claimPayable : claimPayment.getClaimPayables()) {
            if (claimPayable.getPartyId() != null) {
              claimPayable.setPartyId(retrieveParty(returnObject, claimPayable.getPartyId()));
            }
          }
        }
        if (claimPayment.getClaimPaymentDisbursements() != null) {
          for (ClaimPaymentDisbursement claimPaymentDisbursement :
              claimPayment.getClaimPaymentDisbursements()) {
            if (claimPaymentDisbursement.getPartyId() != null) {
              claimPaymentDisbursement.setPartyId(
                  retrieveParty(returnObject, claimPaymentDisbursement.getPartyId()));
            }
            if (claimPaymentDisbursement.getInCareOfPayeeId() != null) {
              claimPaymentDisbursement.setInCareOfPayeeId(
                  retrieveParty(returnObject, claimPaymentDisbursement.getInCareOfPayeeId()));
            }
            if (claimPaymentDisbursement.getAdditionalPayeeId() != null) {
              claimPaymentDisbursement.setAdditionalPayeeId(
                  retrieveParty(returnObject, claimPaymentDisbursement.getAdditionalPayeeId()));
            }
            if (claimPaymentDisbursement.getPartyBankAccountIdPayee() != null) {
              // claimPaymentDisbursement.setPartyBankAccountIdPayee(retrieveParty(returnObject,
              // claimPaymentDisbursement.getPartyBankAccountIdPayee()));
            }
            if (claimPaymentDisbursement.getPartyIdPayee() != null) {
              claimPaymentDisbursement.setPartyIdPayee(
                  retrieveParty(returnObject, claimPaymentDisbursement.getPartyIdPayee()));
            }
            if (claimPaymentDisbursement.getClaimDisbursementAdditionalPayees() != null) {
              for (ClaimDisbursementAdditionalPayee clmDisbursementPayees :
                  claimPaymentDisbursement.getClaimDisbursementAdditionalPayees()) {
                if (clmDisbursementPayees.getPartyIdPayee() != null) {
                  clmDisbursementPayees.setPartyIdPayee(
                      retrieveParty(returnObject, clmDisbursementPayees.getPartyIdPayee()));
                }
              }
            }
          }
        }
      }
    }

    // For each party in Recoveries retrieve Party Information and set
    // logical Id so that
    // the response can be used for Claim Import
    if (request.isReturnClaimRecovery() && returnObject.getRecoveries() != null) {
      for (ClaimRecovery claimRecovery : returnObject.getRecoveries()) {
        if (claimRecovery.getClaimRemittance() != null
            && claimRecovery.getClaimRemittance().getRemitterPartyNumber() != null) {
          claimRecovery
              .getClaimRemittance()
              .setRemitterPartyNumber(
                  retrieveParty(
                      returnObject, claimRecovery.getClaimRemittance().getRemitterPartyNumber()));
        }
      }
    }

    // For each participant in the Claim retrieve Party Information and
    // set logical Id so that
    // the response can be used for Claim Import
    retrieveParty(returnObject, lstParticipants);
  }