/**
   * This method returns the business with the specified business key. If it does not exist in the
   * list, then null is returned.
   *
   * @param oEntities The list of businesses to search.
   * @param sBusinessKey The business key to look for.
   * @return The item from the list that matches the business key.
   */
  protected CMBusinessEntity findSpecificBusiness(
      List<CMBusinessEntity> oaEntities, String sBusinessKey) {
    CMBusinessEntity oEntity = null;

    if ((oaEntities != null) && (oaEntities.size() > 0)) {
      for (CMBusinessEntity oTempEntity : oaEntities) {
        if ((oTempEntity.getBusinessKey() != null)
            && (oTempEntity.getBusinessKey().equals(sBusinessKey))) {
          oEntity = oTempEntity;
          break; // We found it - get out of the loop...
        }
      }
    }

    return oEntity;
  }
  /**
   * This method returns the service with the specified business key and service key. If it does not
   * exist in the list, then null is returned.
   *
   * @param oEntities The list of businesses to search.
   * @param sBusinessKey The business key for the business entity.
   * @param sServiceKey The service key to look for.
   * @return The item from the list that matches the business key.
   */
  private CMBusinessService findSpecificService(
      List<CMBusinessEntity> oaEntities, String sBusinessKey, String sServiceKey) {
    CMBusinessService oService = null;

    CMBusinessEntity oEntity = findSpecificBusiness(oaEntities, sBusinessKey);

    if ((oEntity != null)
        && (oEntity.getBusinessServices() != null)
        && (oEntity.getBusinessServices().getBusinessService() != null)
        && (oEntity.getBusinessServices().getBusinessService().size() > 0)) {
      for (CMBusinessService oTempService : oEntity.getBusinessServices().getBusinessService()) {
        if ((oTempService.getServiceKey() != null)
            && (oTempService.getServiceKey().equals(sServiceKey))) {
          oService = oTempService;
          break; // We found it - get out of the loop...
        }
      }
    }

    return oService;
  }
  /**
   * This method is used to populate the Business Service with the retrieved uniform service
   * information. If there is a single service name, the service passed in will be populated with
   * that name. If there are multiple aliases for a service name, a copy of the service will be
   * created and it will be populated with the alias and then added to the Business Entity
   * structure.
   *
   * @param oUDDIService The information on the service as retrieved from the UDDI registry.
   * @param oEntities The complete list of business entities against which the desired entity is
   *     found. If multiple uniform service names are detected a new service will be added to this
   *     entity.
   * @param oService The business service which is updated with the uniform service name. If
   *     multiples are detected a copy of this service will be made to form a new one.
   */
  public void populateUniformServiceNameAndReplicateService(
      BusinessService oUDDIService, CMBusinessEntities oEntities, CMBusinessService oService) {
    if ((oUDDIService.getCategoryBag() != null)
        && (oUDDIService.getCategoryBag().getKeyedReference() != null)
        && (oUDDIService.getCategoryBag().getKeyedReference().size() > 0)) {

      // Uniform Service Name
      // ---------------------
      List<String> oServiceNames =
          findAndGetValueFromKeyedReference(
              oUDDIService.getCategoryBag().getKeyedReference(), UNIFORM_SERVICE_NAME_KEY);

      if (oServiceNames != null && oServiceNames.size() > 0) {

        // The existance of more than one service name will create multiple services for the entity
        int serviceCount = 0;
        for (String sValue : oServiceNames) {
          if ((sValue != null) && (sValue.length() > 0)) {
            if (serviceCount == 0) {
              oService.setUniformServiceName(sValue);
            } else {
              CMBusinessService oServiceCopy = oService.createCopy();
              oServiceCopy.setUniformServiceName(sValue);
              CMBusinessEntity oEntity =
                  findSpecificBusiness(
                      oEntities.getBusinessEntity(), oUDDIService.getBusinessKey());
              oEntity.getBusinessServices().getBusinessService().add(oServiceCopy);
            }
            serviceCount++;
          }
        }
      } else {
        log.debug(
            "A Normal Service value is NOT detected for UDDI Service "
                + oUDDIService.getBusinessKey()
                + " - "
                + oUDDIService.getServiceKey());
      }
    } // if ((oUDDIService.getCategoryBag() != null) &&  ...
  }
  /**
   * This method retrieves the service information from the UDDI server for each of the business
   * entities.
   *
   * @param oEntities The business entities for which services should be retrieved. The information
   *     is placed in the appropriate location in this object.
   * @throws UDDIAccessorException
   */
  private void retrieveDetailedServiceInfoFromUDDI(CMBusinessEntities oEntities)
      throws UDDIAccessorException {
    if ((oEntities == null)
        || (oEntities.getBusinessEntity() == null)
        || (oEntities.getBusinessEntity().size() <= 0)) {
      return; // we are done  there is nothing to retrieve.
    }

    ServiceDetail oResult = null;

    try {
      GetServiceDetail oSearchParams = new GetServiceDetail();

      // Load up the list of service keys to retrieve the details of...
      // --------------------------------------------------------
      for (CMBusinessEntity oEntity : oEntities.getBusinessEntity()) {
        if ((oEntity.getBusinessServices() != null)
            && (oEntity.getBusinessServices().getBusinessService() != null)
            && (oEntity.getBusinessServices().getBusinessService().size() > 0)) {
          for (CMBusinessService oService : oEntity.getBusinessServices().getBusinessService()) {
            if ((oService.getServiceKey() != null) && (oService.getServiceKey().length() > 0)) {
              oSearchParams.getServiceKey().add(oService.getServiceKey());
            }
          } // for (CMBusinessService oService : oEntity.getBusinessServices().getBusinessService())
        } // if ((oEntity.getBusinessServices() != null) && ...
      } // for (CMBusinessEntity oEntity : oEntities.getBusinessEntity())

      UDDIInquiryPortType oPort = getUDDIInquiryWebService();
      oResult = oPort.getServiceDetail(oSearchParams);
    } catch (Exception e) {
      String sErrorMessage =
          "Failed to call UDDI web service get_serviceDetail method.  Error: " + e.getMessage();
      log.error(sErrorMessage, e);
      throw new UDDIAccessorException(sErrorMessage, e);
    }

    // Now let's process the results...
    // ---------------------------------
    if ((oResult != null)
        && (oResult.getBusinessService() != null)
        && (oResult.getBusinessService().size() > 0)) {
      // Now put the returned information back into our structure.
      // -----------------------------------------------------------
      for (BusinessService oUDDIService : oResult.getBusinessService()) {
        if ((oUDDIService.getServiceKey() != null)
            && (oUDDIService.getServiceKey().length() > 0)
            && (oUDDIService.getBusinessKey() != null)
            && (oUDDIService.getBusinessKey().length() > 0)) {
          CMBusinessService oService =
              findSpecificService(
                  oEntities.getBusinessEntity(),
                  oUDDIService.getBusinessKey(),
                  oUDDIService.getServiceKey());

          if (oService != null) {
            // Binding Service Name
            // ----------------------
            if ((oUDDIService.getName() != null) && (oUDDIService.getName().size() > 0)) {
              CMBindingNames oNames = new CMBindingNames();
              oService.setNames(oNames);

              for (Name oUDDIName : oUDDIService.getName()) {
                if ((oUDDIName.getValue() != null) && (oUDDIName.getValue().length() > 0)) {
                  oService.getNames().getName().add(oUDDIName.getValue());
                }
              }
            } // if ((oUDDIService.getName() != null) &&

            // Binding Descriptions
            // ---------------------
            if ((oUDDIService.getDescription() != null)
                && (oUDDIService.getDescription().size() > 0)) {
              CMBindingDescriptions oDescripts = new CMBindingDescriptions();
              oService.setDescriptions(oDescripts);

              for (Description oUDDIDescript : oUDDIService.getDescription()) {
                if ((oUDDIDescript.getValue() != null) && (oUDDIDescript.getValue().length() > 0)) {
                  oService.getDescriptions().getDescription().add(oUDDIDescript.getValue());
                }
              }
            } // if ((oUDDIService.getDescription() != null) && ...

            // Binding Template Information
            // -----------------------------
            if ((oUDDIService.getBindingTemplates() != null)
                && (oUDDIService.getBindingTemplates().getBindingTemplate() != null)
                && (oUDDIService.getBindingTemplates().getBindingTemplate().size() > 0)) {
              CMBindingTemplates oTemplates = new CMBindingTemplates();
              for (BindingTemplate oUDDITemplate :
                  oUDDIService.getBindingTemplates().getBindingTemplate()) {
                CMBindingTemplate oTemplate = new CMBindingTemplate();
                boolean bHaveData = false;

                // Endpoint URL
                // --------------
                if ((oUDDITemplate.getAccessPoint() != null)
                    && (oUDDITemplate.getAccessPoint().getValue() != null)
                    && (oUDDITemplate.getAccessPoint().getValue().length() > 0)) {
                  oTemplate.setEndpointURL(oUDDITemplate.getAccessPoint().getValue());
                  bHaveData = true;
                }

                // Service Version
                // ---------------
                if ((oUDDITemplate.getCategoryBag() != null)
                    && (oUDDITemplate.getCategoryBag().getKeyedReference() != null)
                    && (oUDDITemplate.getCategoryBag().getKeyedReference().size() > 0)) {

                  List<String> oServiceVersions =
                      findAndGetValueFromKeyedReference(
                          oUDDITemplate.getCategoryBag().getKeyedReference(), SERVICE_VERSION_KEY);
                  if (oServiceVersions != null && oServiceVersions.size() == 1) {
                    for (String sValue : oServiceVersions) {
                      if ((sValue != null) && (sValue.length() > 0)) {
                        oTemplate.setServiceVersion(sValue);
                        bHaveData = true;
                      }
                    }
                  } else {
                    log.debug(
                        "A single Service Version is NOT detected for UDDI Service "
                            + oUDDIService.getBusinessKey()
                            + " - "
                            + oUDDIService.getServiceKey());
                  }
                }
                if (bHaveData) {
                  oTemplates.getBindingTemplate().add(oTemplate);
                }
              }

              if ((oTemplates.getBindingTemplate() != null)
                  && (oTemplates.getBindingTemplate().size() > 0)) {
                oService.setBindingTemplates(oTemplates);
              }
            }

            // Uniform Service Name
            //
            // Multiple Uniform Service Names will result in the
            // creation of oService copies so this should be the last
            // population to be performed.
            // ----------------------------------------
            populateUniformServiceNameAndReplicateService(oUDDIService, oEntities, oService);
          } // if (oService != null)
        } // if ((oUDDIService.getServiceKey() != null) &&  ...
      } // for (BusinessService oUDDIService : oResult.getBusinessService())
    } // if ((oResult != null) &&
  }
  /**
   * This method loops through the business entities and fills in any pertinent detailed information
   * by calling the UDDI server get_businessDetail function. Note that this information was not
   * available in the find_business. In order to get it we have to do separate call.
   *
   * @param oEntities The businesses to retrieve the detail and the object where the details will be
   *     placed.
   */
  private void retrieveDetailedBusinessInfoFromUDDI(CMBusinessEntities oEntities)
      throws UDDIAccessorException {
    if ((oEntities == null)
        || (oEntities.getBusinessEntity() == null)
        || (oEntities.getBusinessEntity().size() <= 0)) {
      return; // we are done  there is nothing to retrieve.
    }

    BusinessDetail oResult = null;

    try {
      GetBusinessDetail oSearchParams = new GetBusinessDetail();

      // Load up the list of keys to retrieve the details of...
      // --------------------------------------------------------
      for (CMBusinessEntity oEntity : oEntities.getBusinessEntity()) {
        if ((oEntity.getBusinessKey() != null) && (oEntity.getBusinessKey().length() > 0)) {
          oSearchParams.getBusinessKey().add(oEntity.getBusinessKey());
        }
      } // for (CMBusinessEntity oEntity : oEntities.getBusinessEntity())

      UDDIInquiryPortType oPort = getUDDIInquiryWebService();
      oResult = oPort.getBusinessDetail(oSearchParams);
    } catch (Exception e) {
      String sErrorMessage =
          "Failed to call UDDI web service get_businessDetail method.  Error: " + e.getMessage();
      log.error(sErrorMessage, e);
      throw new UDDIAccessorException(sErrorMessage, e);
    }

    // Now let's process the results...
    // ---------------------------------
    if ((oResult != null)
        && (oResult.getBusinessEntity() != null)
        && (oResult.getBusinessEntity().size() > 0)) {
      // Now put the returned information back into our structure.
      // -----------------------------------------------------------
      for (BusinessEntity oUDDIEntity : oResult.getBusinessEntity()) {
        if ((oUDDIEntity.getBusinessKey() != null) && (oUDDIEntity.getBusinessKey().length() > 0)) {
          CMBusinessEntity oEntity =
              findSpecificBusiness(oEntities.getBusinessEntity(), oUDDIEntity.getBusinessKey());

          if (oEntity != null) {
            // Home community ID
            // ------------------
            if ((oUDDIEntity.getIdentifierBag() != null)
                && (oUDDIEntity.getIdentifierBag().getKeyedReference() != null)
                && (oUDDIEntity.getIdentifierBag().getKeyedReference().size() > 0)) {
              List<String> oValues =
                  findAndGetValueFromKeyedReference(
                      oUDDIEntity.getIdentifierBag().getKeyedReference(), HOME_COMMUNITY_ID_KEY);
              if (oValues != null && oValues.size() == 1) {
                for (String sValue : oValues) {
                  if ((sValue != null) && (sValue.length() > 0)) {
                    if (sValue.startsWith("urn:oid:")) {
                      sValue = sValue.substring("urn:oid:".length());
                    }
                    oEntity.setHomeCommunityId(sValue);
                  }
                }
              } else {
                log.debug(
                    "A single Home Community value is NOT detected for UDDI Entity "
                        + oUDDIEntity.getBusinessKey());
              }
            } // if ((oUDDIEntity.getIdentifierBag() != null) && ...

            if ((oUDDIEntity.getCategoryBag() != null)
                && (oUDDIEntity.getCategoryBag().getKeyedReference() != null)
                && (oUDDIEntity.getCategoryBag().getKeyedReference().size() > 0)) {
              // Public Key
              // -----------
              List<String> oPublicKeys =
                  findAndGetValueFromKeyedReference(
                      oUDDIEntity.getCategoryBag().getKeyedReference(), PUBLIC_KEY_ID_KEY);
              if (oPublicKeys != null && oPublicKeys.size() == 1) {
                for (String sValue : oPublicKeys) {
                  if ((sValue != null) && (sValue.length() > 0)) {
                    oEntity.setPublicKey(sValue);
                  }
                }
              } else {
                log.debug(
                    "A single Public Key value is NOT detected for UDDI Entity "
                        + oUDDIEntity.getBusinessKey());
              }
              // State names
              // ------------
              List<String> oStateNames =
                  findAndGetValueFromKeyedReference(
                      oUDDIEntity.getCategoryBag().getKeyedReference(), STATE_NAME_ID_KEY);
              if (oStateNames != null && oStateNames.size() > 0) {
                CMStates oStates = new CMStates();
                List<String> oStatesList = oStates.getState();
                for (String sValue : oStateNames) {
                  if ((sValue != null) && (sValue.length() > 0)) {
                    oStatesList.add(sValue);
                  }
                }
                oEntity.setStates(oStates);
              } else {
                log.debug(
                    "No State name is detected for UDDI Entity " + oUDDIEntity.getBusinessKey());
              }
            }
          } // if (oEntity != nulll)
        } // if ((oUDDIEntity.getBusinessKey() != null) && (oUDDIEntity.getBusinessKey().length() >
          // 0))
      } // for (BusinessEntity oUDDIEntity : oResult.getBusinessEntity())
    } // if ((oResult != null) &&
  }
  /**
   * This method craetes a business entity by extracting the information from a business info
   * object.
   *
   * @param oBusInfo The business information that contains the information.
   * @return The translated information.
   */
  private CMBusinessEntity businessEntityFromBusinesInfo(BusinessInfo oBusInfo) {
    CMBusinessEntity oEntity = new CMBusinessEntity();
    boolean bHaveData = false;

    if (oBusInfo != null) {
      // Business Key
      // -------------
      if ((oBusInfo.getBusinessKey() != null) && (oBusInfo.getBusinessKey().length() > 0)) {
        oEntity.setBusinessKey(oBusInfo.getBusinessKey());
        bHaveData = true;
      }

      // Names
      // ------
      if ((oBusInfo.getName() != null) && (oBusInfo.getName().size() > 0)) {
        CMBusinessNames oNames = new CMBusinessNames();
        oEntity.setNames(oNames);
        for (Name oUDDIName : oBusInfo.getName()) {
          if ((oUDDIName.getValue() != null) && (oUDDIName.getValue().length() > 0)) {
            oNames.getBusinessName().add(oUDDIName.getValue());
          }
        }
        bHaveData = true;
      } // if ((oBusInfo.getName() != null) && ...

      // Description
      // ------------
      if ((oBusInfo.getDescription() != null) && (oBusInfo.getDescription().size() > 0)) {
        CMBusinessDescriptions oDescripts = new CMBusinessDescriptions();
        oEntity.setDescriptions(oDescripts);
        for (Description oUDDIDescript : oBusInfo.getDescription()) {
          if ((oUDDIDescript.getValue() != null) && (oUDDIDescript.getValue().length() > 0)) {
            oDescripts.getBusinessDescription().add(oUDDIDescript.getValue());
          }
        }
        bHaveData = true;
      }

      // Set up for the services.  - This pass will only put in the service key.
      // We will have to do another retrieval to get the rest of the service information.
      // ------------------------------------------------------------------------
      if ((oBusInfo.getServiceInfos() != null)
          && (oBusInfo.getServiceInfos().getServiceInfo() != null)
          && (oBusInfo.getServiceInfos().getServiceInfo().size() > 0)) {
        CMBusinessServices oServices = new CMBusinessServices();
        for (ServiceInfo oUDDIService : oBusInfo.getServiceInfos().getServiceInfo()) {
          boolean bHaveServiceData = false;
          CMBusinessService oService = new CMBusinessService();

          // Service Key
          // ------------
          if ((oUDDIService.getServiceKey() != null)
              && (oUDDIService.getServiceKey().length() > 0)) {
            oService.setServiceKey(oUDDIService.getServiceKey());
            bHaveServiceData = true;
          }

          oService.setInternalWebService(false); // If it is in UDDI - it is not internal

          // Service Name - We will pick this up on the detail.
          // ---------------------------------------------------

          if (bHaveServiceData) {
            oServices.getBusinessService().add(oService);
            bHaveData = true;
          }
        } // for (ServiceInfo oUDDIService : oBusInfo.getServiceInfos().getServiceInfo())

        if (oServices.getBusinessService().size() > 0) {
          oEntity.setBusinessServices(oServices);
        }
      }
    }

    if (bHaveData) {
      return oEntity;
    } else {
      return null;
    }
  }