@Test
  public void testDeleteFolderSyncFile() throws Exception {
    List<SyncFile> syncFiles = SyncFileService.findSyncFiles(syncAccount.getSyncAccountId());

    Assert.assertEquals(1, syncFiles.size());

    SyncFile folderSyncFileA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a"), syncAccount.getSyncAccountId());

    SyncFile folderSyncFileAA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a", "a"),
            folderSyncFileA.getTypePK(),
            syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFolderSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "b"),
        folderSyncFileA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFolderSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "a", "a"),
        folderSyncFileAA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFileSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "b.txt"),
        folderSyncFileA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFileSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "c.txt"),
        folderSyncFileA.getTypePK(),
        syncAccount.getSyncAccountId());

    SyncFileTestUtil.addFileSyncFile(
        FileUtil.getFilePathName(filePathName, "a", "a", "a.txt"),
        folderSyncFileAA.getTypePK(),
        syncAccount.getSyncAccountId());

    syncFiles = SyncFileService.findSyncFiles(syncAccount.getSyncAccountId());

    Assert.assertEquals(8, syncFiles.size());

    SyncFileService.deleteSyncFile(folderSyncFileA);

    syncFiles = SyncFileService.findSyncFiles(syncAccount.getSyncAccountId());

    Assert.assertEquals(1, syncFiles.size());
  }
  public static void setFilePathName(long syncAccountId, String targetFilePathName) {

    // Sync account

    SyncAccount syncAccount = fetchSyncAccount(syncAccountId);

    String sourceFilePathName = syncAccount.getFilePathName();

    syncAccount.setFilePathName(targetFilePathName);

    update(syncAccount);

    // Sync files

    List<SyncFile> syncFiles = SyncFileService.findSyncFiles(syncAccountId);

    for (SyncFile syncFile : syncFiles) {
      String syncFileFilePathName = syncFile.getFilePathName();

      syncFileFilePathName = syncFileFilePathName.replace(sourceFilePathName, targetFilePathName);

      syncFile.setFilePathName(syncFileFilePathName);

      SyncFileService.update(syncFile);
    }

    // Sync sites

    List<SyncSite> syncSites = SyncSiteService.findSyncSites(syncAccountId);

    for (SyncSite syncSite : syncSites) {
      String syncSiteFilePathName = syncSite.getFilePathName();

      syncSiteFilePathName = syncSiteFilePathName.replace(sourceFilePathName, targetFilePathName);

      syncSite.setFilePathName(syncSiteFilePathName);

      SyncSiteService.update(syncSite);
    }
  }
Пример #3
0
  public static void fireDeleteEvents(Path filePath) throws IOException {
    long startTime = System.currentTimeMillis();

    Files.walkFileTree(
        filePath,
        new SimpleFileVisitor<Path>() {

          @Override
          public FileVisitResult preVisitDirectory(
              Path filePath, BasicFileAttributes basicFileAttributes) {

            SyncFile syncFile = SyncFileService.fetchSyncFile(filePath.toString());

            if (syncFile == null) {
              syncFile = SyncFileService.fetchSyncFile(FileKeyUtil.getFileKey(filePath));
            }

            if (syncFile != null) {
              syncFile.setLocalSyncTime(System.currentTimeMillis());

              SyncFileService.update(syncFile);
            }

            return FileVisitResult.CONTINUE;
          }

          @Override
          public FileVisitResult visitFile(Path filePath, BasicFileAttributes basicFileAttributes) {

            SyncFile syncFile = SyncFileService.fetchSyncFile(filePath.toString());

            if (syncFile == null) {
              syncFile = SyncFileService.fetchSyncFile(FileKeyUtil.getFileKey(filePath));
            }

            if (syncFile != null) {
              syncFile.setLocalSyncTime(System.currentTimeMillis());

              SyncFileService.update(syncFile);
            }

            return FileVisitResult.CONTINUE;
          }
        });

    List<SyncFile> deletedSyncFiles = SyncFileService.findSyncFiles(filePath.toString(), startTime);

    for (SyncFile deletedSyncFile : deletedSyncFiles) {
      if (deletedSyncFile.getTypePK() == 0) {
        SyncFileService.deleteSyncFile(deletedSyncFile, false);

        continue;
      }

      if (deletedSyncFile.isFolder()) {
        FileEventUtil.deleteFolder(deletedSyncFile.getSyncAccountId(), deletedSyncFile);
      } else {
        FileEventUtil.deleteFile(deletedSyncFile.getSyncAccountId(), deletedSyncFile);
      }
    }
  }