/**
   * 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;
  }
  public static AdminComponent checkDuplicate(DataElement currentDe, DataElement newDataElement) {
    ElementsLists elements = ElementsLists.getInstance();

    // 1. Verify that this DE will not implicitely map the OC to one
    // that is already used in the model.
    List<ObjectClass> ocs = elements.getElements(DomainObjectFactory.newObjectClass());
    if (ocs != null) {
      for (ObjectClass oc : ocs) {
        if (oc.getPublicId() != null) {
          if (currentDe.getDataElementConcept().getObjectClass() != oc)
            if (oc.getPublicId()
                .equals(newDataElement.getDataElementConcept().getObjectClass().getPublicId()))
              return oc;
        }
      }
    }

    // 2. Check that we don't already have this exact DE public ID
    List<DataElement> des = elements.getElements(DomainObjectFactory.newDataElement());
    for (DataElement de : des) {
      if (de != currentDe) {
        if (!StringUtil.isEmpty(de.getPublicId())
            && de.getPublicId().equals(newDataElement.getPublicId())) return de;
      }
    }
    //    if(des != null && ocs != null) {
    //      for(ObjectClass oc : ocs)
    ////        Map<String, DataElement> deList = new HashMap<String, DataElement>();
    //        for(DataElement de : des)
    //          if(newDataElement.getDataElementConcept().getObjectClass() ==
    // de.getDataElementConcept().getObjectClass())
    //            if(newDataElement.getPublicId() == de.getPublicId())
    //              return false;
    //    }

    return null;
  }