@Test
  @Ignore
  public void testInsertTestData() throws Exception {

    entry1 = new BibtexEntry();
    JabRefPreferences jabRefPreferences = JabRefPreferences.getInstance();
    ExternalFileType fileType = jabRefPreferences.getExternalFileTypeByExt("PDF");
    FileListEntry fileListEntry =
        new FileListEntry("", ImportDataTest.FILE_IN_DATABASE.getAbsolutePath(), fileType);

    FileListTableModel model = new FileListTableModel();
    model.addEntry(0, fileListEntry);

    entry1.setField("file", model.getStringRepresentation());

    database.insertEntry(entry1);

    // #################### SETUP END ##################### //

    UnlinkedFilesCrawler crawler = new UnlinkedFilesCrawler(database);
    CheckableTreeNode treeNode =
        crawler.searchDirectory(ImportDataTest.EXISTING_FOLDER, new EntryFromPDFCreator());

    Assert.assertNotNull(treeNode);

    /** Select all nodes manually. */
    @SuppressWarnings("unchecked")
    Enumeration<CheckableTreeNode> enumeration = treeNode.breadthFirstEnumeration();
    while (enumeration.hasMoreElements()) {
      CheckableTreeNode nextElement = enumeration.nextElement();
      nextElement.setSelected(true);
    }

    List<File> resultList = getFileListFromNode(treeNode);

    Assert.assertFalse(resultList.isEmpty());
    Assert.assertTrue(resultList.contains(ImportDataTest.FILE_NOT_IN_DATABASE));
    Assert.assertFalse(resultList.contains(ImportDataTest.FILE_IN_DATABASE));
  }
示例#2
0
  private void doRenamePDFs(BibtexEntry entry, NamedCompound ce) {
    // Extract the path
    String oldValue = entry.getField(Globals.FILE_FIELD);
    if (oldValue == null) {
      return;
    }
    FileListTableModel flModel = new FileListTableModel();
    flModel.setContent(oldValue);
    if (flModel.getRowCount() == 0) {
      return;
    }
    boolean changed = false;

    for (int i = 0; i < flModel.getRowCount(); i++) {
      String realOldFilename = flModel.getEntry(i).getLink();

      if (cleanUpRenamePDFonlyRelativePaths.isSelected()
          && (new File(realOldFilename).isAbsolute())) {
        continue;
      }

      String newFilename = Util.getLinkedFileName(panel.database(), entry);
      // String oldFilename = bes.getField(GUIGlobals.FILE_FIELD); // would have to be stored for
      // undoing purposes

      // Add extension to newFilename
      newFilename = newFilename + "." + flModel.getEntry(i).getType().getExtension();

      // get new Filename with path
      // Create new Path based on old Path and new filename
      File expandedOldFile =
          FileUtil.expandFilename(
              realOldFilename, panel.metaData().getFileDirectory(Globals.FILE_FIELD));
      if (expandedOldFile.getParent() == null) {
        // something went wrong. Just skip this entry
        continue;
      }
      String newPath =
          expandedOldFile
              .getParent()
              .concat(System.getProperty("file.separator"))
              .concat(newFilename);

      if (new File(newPath).exists()) {
        // we do not overwrite files
        // TODO: we could check here if the newPath file is linked with the current entry. And if
        // not, we could add a link
        continue;
      }

      // do rename
      boolean renameSuccessful = FileUtil.renameFile(expandedOldFile.toString(), newPath);

      if (renameSuccessful) {
        changed = true;

        // Change the path for this entry
        String description = flModel.getEntry(i).getDescription();
        ExternalFileType type = flModel.getEntry(i).getType();
        flModel.removeEntry(i);

        // we cannot use "newPath" to generate a FileListEntry as newPath is absolute, but we want
        // to keep relative paths whenever possible
        File parent = (new File(realOldFilename)).getParentFile();
        String newFileEntryFileName;
        if (parent == null) {
          newFileEntryFileName = newFilename;
        } else {
          newFileEntryFileName =
              parent.toString().concat(System.getProperty("file.separator")).concat(newFilename);
        }
        flModel.addEntry(i, new FileListEntry(description, newFileEntryFileName, type));
      } else {
        unsuccessfulRenames++;
      }
    }

    if (changed) {
      String newValue = flModel.getStringRepresentation();
      assert (!oldValue.equals(newValue));
      entry.setField(Globals.FILE_FIELD, newValue);
      // we put an undo of the field content here
      // the file is not being renamed back, which leads to inconsistencies
      // if we put a null undo object here, the change by "doMakePathsRelative" would overwrite the
      // field value nevertheless.
      ce.addEdit(new UndoableFieldChange(entry, Globals.FILE_FIELD, oldValue, newValue));
    }
  }