@Nullable
  @Override
  public OrderEntry findIdeModuleOrderEntry(@NotNull DependencyData data) {
    Module ownerIdeModule = findIdeModule(data.getOwnerModule());
    if (ownerIdeModule == null) return null;

    LibraryDependencyData libraryDependencyData = null;
    ModuleDependencyData moduleDependencyData = null;
    if (data instanceof LibraryDependencyData) {
      libraryDependencyData = (LibraryDependencyData) data;
    } else if (data instanceof ModuleDependencyData) {
      moduleDependencyData = (ModuleDependencyData) data;
    } else {
      return null;
    }

    for (OrderEntry entry : getOrderEntries(ownerIdeModule)) {
      if (entry instanceof LibraryOrderEntry && libraryDependencyData != null) {
        if (((LibraryOrderEntry) entry).isModuleLevel()
            && libraryDependencyData.getLevel() != LibraryLevel.MODULE) continue;
        if (StringUtil.isEmpty(((LibraryOrderEntry) entry).getLibraryName())) {
          final Set<String> paths =
              ContainerUtil.map2Set(
                  libraryDependencyData.getTarget().getPaths(LibraryPathType.BINARY),
                  new Function<String, String>() {
                    @Override
                    public String fun(String path) {
                      return PathUtil.getLocalPath(path);
                    }
                  });
          final Set<String> entryPaths =
              ContainerUtil.map2Set(
                  entry.getUrls(OrderRootType.CLASSES),
                  new Function<String, String>() {
                    @Override
                    public String fun(String s) {
                      return PathUtil.getLocalPath(VfsUtilCore.urlToPath(s));
                    }
                  });
          if (entryPaths.equals(paths) && ((LibraryOrderEntry) entry).getScope() == data.getScope())
            return entry;
          continue;
        }
      }

      String entryName =
          libraryDependencyData != null
              ? libraryDependencyData.getInternalName()
              : moduleDependencyData.getInternalName();
      if (entryName.equals(entry.getPresentableName())
          && (!(entry instanceof ExportableOrderEntry)
              || ((ExportableOrderEntry) entry).getScope() == data.getScope())) {
        return entry;
      }
    }
    return null;
  }
 private void syncExistingAndRemoveObsolete(
     @NotNull IdeModifiableModelsProvider modelsProvider,
     @NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport,
     @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport,
     @NotNull Set<LibraryDependencyData> toImport,
     @NotNull Map<OrderEntry, OrderAware> orderEntryDataMap,
     @NotNull ModifiableRootModel moduleRootModel,
     boolean hasUnresolvedLibraries) {
   for (OrderEntry entry : moduleRootModel.getOrderEntries()) {
     if (entry instanceof ModuleLibraryOrderEntryImpl) {
       ModuleLibraryOrderEntryImpl moduleLibraryOrderEntry = (ModuleLibraryOrderEntryImpl) entry;
       Library library = moduleLibraryOrderEntry.getLibrary();
       if (library == null) {
         LOG.warn(
             "Skipping module-level library entry because it doesn't have backing Library object. Entry: "
                 + entry);
         continue;
       }
       final VirtualFile[] libraryFiles = library.getFiles(OrderRootType.CLASSES);
       final Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet(libraryFiles.length);
       for (VirtualFile file : libraryFiles) {
         moduleLibraryKey.add(
             ExternalSystemApiUtil.getLocalFileSystemPath(file)
                 + moduleLibraryOrderEntry.getScope().name());
       }
       LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey);
       if (existing == null || !StringUtil.equals(existing.getInternalName(), library.getName())) {
         moduleRootModel.removeOrderEntry(entry);
       } else {
         orderEntryDataMap.put(entry, existing);
         syncExistingLibraryDependency(
             modelsProvider,
             existing,
             library,
             moduleRootModel,
             moduleLibraryOrderEntry.getOwnerModule());
         toImport.remove(existing);
       }
     } else if (entry instanceof LibraryOrderEntry) {
       LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) entry;
       String libraryName = libraryOrderEntry.getLibraryName();
       LibraryDependencyData existing =
           projectLibrariesToImport.remove(libraryName + libraryOrderEntry.getScope().name());
       if (existing != null) {
         toImport.remove(existing);
         orderEntryDataMap.put(entry, existing);
       } else if (!hasUnresolvedLibraries) {
         // There is a possible case that a project has been successfully imported from external
         // model and after
         // that network/repo goes down. We don't want to drop existing binary mappings then.
         moduleRootModel.removeOrderEntry(entry);
       }
     }
   }
 }
  private LibraryOrderEntry syncExistingLibraryDependency(
      @NotNull IdeModifiableModelsProvider modelsProvider,
      @NotNull final LibraryDependencyData libraryDependencyData,
      @NotNull final Library library,
      @NotNull final ModifiableRootModel moduleRootModel,
      @NotNull final Module module) {
    final Library.ModifiableModel libraryModel = modelsProvider.getModifiableLibraryModel(library);
    final String libraryName = libraryDependencyData.getInternalName();
    Map<OrderRootType, Collection<File>> files =
        myLibraryManager.prepareLibraryFiles(libraryDependencyData.getTarget());
    myLibraryManager.registerPaths(files, libraryModel, libraryName);
    LibraryOrderEntry orderEntry =
        findLibraryOrderEntry(moduleRootModel, library, libraryDependencyData.getScope());

    assert orderEntry != null;
    setLibraryScope(orderEntry, library, module, libraryDependencyData);
    return orderEntry;
  }