protected void moveFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (FileUtil.notExists(targetFilePath)
        || sanitizeFileName(targetFilePath)
        || isInErrorState(targetFilePath)) {

      return;
    }

    Path parentTargetFilePath = targetFilePath.getParent();

    SyncFile parentSyncFile = SyncFileService.fetchSyncFile(parentTargetFilePath.toString());

    if ((parentSyncFile == null)
        || (!parentSyncFile.isSystem() && (parentSyncFile.getTypePK() == 0))) {

      queueSyncWatchEvent(parentTargetFilePath.toString(), syncWatchEvent);

      return;
    }

    Path sourceFilePath = Paths.get(syncWatchEvent.getPreviousFilePathName());

    SyncFile sourceSyncFile = SyncFileService.fetchSyncFile(sourceFilePath.toString());

    SyncFile targetSyncFile = SyncFileService.fetchSyncFile(targetFilePath.toString());

    if ((sourceSyncFile == null) || (targetSyncFile != null)) {
      if (Files.isDirectory(targetFilePath)) {
        addFolder(syncWatchEvent);
      } else {
        addFile(syncWatchEvent);
      }

      return;
    } else if (isPendingTypePK(sourceSyncFile)) {
      queueSyncWatchEvent(sourceSyncFile.getFilePathName(), syncWatchEvent);

      return;
    }

    String fileType = sourceSyncFile.getType();

    if (fileType.equals(SyncFile.TYPE_FILE)) {
      SyncFileService.moveFileSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, sourceSyncFile);
    } else {
      SyncFileService.moveFolderSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, sourceSyncFile);
    }

    renameFile(syncWatchEvent);
  }
  protected void deleteFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path filePath = Paths.get(syncWatchEvent.getFilePathName());

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

    if ((syncFile == null) || !FileUtil.notExists(Paths.get(syncFile.getFilePathName()))) {

      return;
    } else if ((syncFile.getState() == SyncFile.STATE_ERROR)
        || (syncFile.getState() == SyncFile.STATE_UNSYNCED)) {

      SyncFileService.deleteSyncFile(syncFile, false);

      return;
    } else if (syncFile.getState() == SyncFile.STATE_IN_PROGRESS) {
      Set<Event> events = FileEventManager.getEvents(syncFile.getSyncFileId());

      for (Event event : events) {
        event.cancel();
      }

      if (isPendingTypePK(syncFile)) {
        SyncFileService.deleteSyncFile(syncFile);

        return;
      }
    } else if (isPendingTypePK(syncFile)) {
      queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

      return;
    }

    String type = syncFile.getType();

    if (type.equals(SyncFile.TYPE_FILE)) {
      FileEventUtil.deleteFile(_syncAccountId, syncFile);
    } else {
      FileEventUtil.deleteFolder(_syncAccountId, syncFile);
    }
  }
  protected void addFile(SyncWatchEvent syncWatchEvent) throws Exception {
    final Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (FileUtil.notExists(targetFilePath)
        || sanitizeFileName(targetFilePath)
        || isInErrorState(targetFilePath)) {

      return;
    }

    Path parentTargetFilePath = targetFilePath.getParent();

    final SyncFile parentSyncFile = SyncFileService.fetchSyncFile(parentTargetFilePath.toString());

    if ((parentSyncFile == null)
        || (!parentSyncFile.isSystem() && (parentSyncFile.getTypePK() == 0))) {

      queueSyncWatchEvent(parentTargetFilePath.toString(), syncWatchEvent);

      return;
    }

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

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

      if (!verifySite(syncFile, parentSyncFile)) {
        syncFile = null;
      }
    }

    if (syncFile == null) {
      Runnable runnable =
          new Runnable() {

            @Override
            public void run() {
              try {
                SyncSite syncSite =
                    SyncSiteService.fetchSyncSite(parentSyncFile.getRepositoryId(), _syncAccountId);

                if ((syncSite == null)
                    || !syncSite.isActive()
                    || !FileUtil.checkFilePath(targetFilePath)) {

                  return;
                }

                SyncFileService.addFileSyncFile(
                    targetFilePath,
                    parentSyncFile.getTypePK(),
                    parentSyncFile.getRepositoryId(),
                    _syncAccountId);
              } catch (Exception e) {
                if (SyncFileService.fetchSyncFile(targetFilePath.toString()) == null) {

                  _logger.error(e.getMessage(), e);
                }
              }
            }
          };

      _executorService.execute(runnable);

      return;
    }

    Path sourceFilePath = Paths.get(syncFile.getFilePathName());

    if (targetFilePath.equals(sourceFilePath)) {
      if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      if (FileUtil.isModified(syncFile)) {
        SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
      }
    } else if (FileUtil.exists(sourceFilePath)) {
      try {
        if ((Files.size(targetFilePath) == 0)
            || FileUtil.isModified(syncFile, targetFilePath)
            || isInErrorState(sourceFilePath)) {

          SyncFileService.addFileSyncFile(
              targetFilePath,
              parentSyncFile.getTypePK(),
              parentSyncFile.getRepositoryId(),
              _syncAccountId);
        } else {
          SyncFileService.copySyncFile(
              syncFile,
              targetFilePath,
              parentSyncFile.getTypePK(),
              parentSyncFile.getRepositoryId(),
              _syncAccountId);
        }
      } catch (Exception e) {
        if (SyncFileService.fetchSyncFile(targetFilePath.toString()) == null) {

          _logger.error(e.getMessage(), e);
        }
      }

      return;
    } else if (parentTargetFilePath.equals(sourceFilePath.getParent())) {
      if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.moveFileSyncFile(
          targetFilePath, parentSyncFile.getTypePK(), _syncAccountId, syncFile);

      Path sourceFileNameFilePath = sourceFilePath.getFileName();

      if (!sourceFileNameFilePath.equals(targetFilePath.getFileName())) {
        SyncFileService.updateFileSyncFile(targetFilePath, _syncAccountId, syncFile);
      }
    }

    SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(_syncAccountId);

    if (syncAccount.getState() == SyncAccount.STATE_CONNECTED) {
      SyncWatchEvent relatedSyncWatchEvent =
          SyncWatchEventService.fetchSyncWatchEvent(
              SyncWatchEvent.EVENT_TYPE_DELETE,
              syncWatchEvent.getFilePathName(),
              syncWatchEvent.getTimestamp());

      if (relatedSyncWatchEvent != null) {
        _processedSyncWatchEventIds.add(relatedSyncWatchEvent.getSyncWatchEventId());
      }
    }
  }