/**
   * This method will check that replacing the current DE with a mapping to existing DE will not
   * conflict with any other DE.
   *
   * @param oldDe currentMapping
   * @param newDe new Mapping
   * @return null if there is no conflict. The conflicting DE if there is a conflict.
   */
  public static DataElement checkConflict(DataElement oldDe, DataElement newDe) {

    ObjectClass oc = oldDe.getDataElementConcept().getObjectClass();
    if (oc.getPublicId() != null) {
      // Verify conflicts
      if (!newDe.getDataElementConcept().getObjectClass().getPublicId().equals(oc.getPublicId())
          || !newDe.getDataElementConcept().getObjectClass().getVersion().equals(oc.getVersion())) {
        // Oc was already mapped by an existing DE. This DE conflicts with the previous mapping.
        // Now we need to find out what DE set the oc_id previously
        // return newDe;
        List<DataElement> des =
            ElementsLists.getInstance().getElements(DomainObjectFactory.newDataElement());

        for (DataElement de : des) {
          if (de.getDataElementConcept().getObjectClass()
              != oldDe.getDataElementConcept().getObjectClass()) continue;

          if (StringUtil.isEmpty(de.getPublicId()) || de.getVersion() == null) continue;

          String ocId = de.getDataElementConcept().getObjectClass().getPublicId();
          if (oldDe != de
              && ocId != null
              && !ocId.equals(newDe.getDataElementConcept().getObjectClass().getPublicId())) {
            return de;
          }
        }
        // we shouldn't be here
        return null;
      }
    }
    return null;
  }