protected void renameFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path sourceFilePath = Paths.get(syncWatchEvent.getPreviousFilePathName());

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

    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (sanitizeFileName(targetFilePath)) {
      return;
    }

    if (syncFile == null) {
      if (Files.isDirectory(targetFilePath)) {
        addFolder(syncWatchEvent);
      } else {
        addFile(syncWatchEvent);
      }

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

      return;
    }

    String fileType = syncFile.getType();

    if (fileType.equals(SyncFile.TYPE_FILE)) {
      SyncFileService.renameFileSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      SyncFileService.renameFolderSyncFile(targetFilePath, _syncAccountId, syncFile);
    }
  }
  public boolean isInProgress() {
    if (SyncWatchEventService.hasSyncWatchEvents(_syncAccountId)
        || SyncFileService.hasSyncFiles(_syncAccountId, SyncFile.UI_EVENT_DOWNLOADING)
        || SyncFileService.hasSyncFiles(_syncAccountId, SyncFile.UI_EVENT_UPLOADING)) {

      return true;
    }

    return false;
  }
  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);
  }
  @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());
  }
Ejemplo n.º 5
0
  public static String getNextFilePathName(String filePathName) {
    Path filePath = Paths.get(filePathName);

    Path parentFilePath = filePath.getParent();

    for (int i = 0; ; i++) {
      StringBuilder sb = new StringBuilder();

      sb.append(FilenameUtils.getBaseName(filePathName));

      if (i > 0) {
        sb.append(" (");
        sb.append(i);
        sb.append(")");
      }

      String extension = FilenameUtils.getExtension(filePathName);

      if (extension.length() > 0) {
        sb.append(".");
        sb.append(extension);
      }

      String tempFilePathName = FileUtil.getFilePathName(parentFilePath.toString(), sb.toString());

      if (SyncFileService.fetchSyncFile(tempFilePathName) == null) {
        Path tempFilePath = Paths.get(tempFilePathName);

        if (!Files.exists(tempFilePath)) {
          return tempFilePathName;
        }
      }
    }
  }
Ejemplo n.º 6
0
  @Override
  public void handleException(Exception e) {
    _logger.error(e.getMessage(), e);

    if (!(e instanceof HttpResponseException)) {
      super.handleException(e);

      return;
    }

    HttpResponseException hre = (HttpResponseException) e;

    int statusCode = hre.getStatusCode();

    if (statusCode != HttpStatus.SC_NOT_FOUND) {
      super.handleException(e);

      return;
    }

    SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(getSyncAccountId());

    if (syncAccount.getState() == SyncAccount.STATE_DISCONNECTED) {
      super.handleException(e);

      return;
    }

    SyncFile syncFile = (SyncFile) getParameterValue("syncFile");

    SyncFileService.deleteSyncFile(syncFile, false);
  }
Ejemplo n.º 7
0
  public static boolean isIgnoredFilePath(Path filePath) {
    String fileName = String.valueOf(filePath.getFileName());

    if (_syncFileIgnoreNames.contains(fileName)
        || MSOfficeFileUtil.isTempCreatedFile(filePath)
        || (PropsValues.SYNC_FILE_IGNORE_HIDDEN && isHidden(filePath))
        || Files.isSymbolicLink(filePath)
        || fileName.endsWith(".lnk")) {

      return true;
    }

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

    if (syncFile == null) {
      return isIgnoredFilePath(filePath.getParent());
    }

    if (!syncFile.isSystem() && (syncFile.getState() == SyncFile.STATE_UNSYNCED)) {

      return true;
    }

    return false;
  }
  private SyncFile _getSyncFile(FullHttpRequest fullHttpRequest) {
    String[] pathArray = StringUtils.split(fullHttpRequest.uri(), "/");

    if (pathArray.length != 4) {
      return null;
    }

    String lanServerUuid = pathArray[0];
    long repositoryId = GetterUtil.getLong(pathArray[1]);
    long typePK = GetterUtil.getLong(pathArray[2]);
    long versionId = GetterUtil.getLong(pathArray[3]);

    if (lanServerUuid.isEmpty() || (repositoryId == 0) || (typePK == 0) || (versionId == 0)) {

      return null;
    }

    List<SyncAccount> syncAccounts = SyncAccountService.findSyncAccounts(lanServerUuid);

    for (SyncAccount syncAccount : syncAccounts) {
      SyncFile syncFile =
          SyncFileService.fetchSyncFile(
              repositoryId, syncAccount.getSyncAccountId(), typePK, versionId);

      if ((syncFile != null) && (syncFile.getState() == SyncFile.STATE_SYNCED)) {

        return syncFile;
      }
    }

    return null;
  }
  protected void modifyFile(SyncWatchEvent syncWatchEvent) throws Exception {
    Path filePath = Paths.get(syncWatchEvent.getFilePathName());

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

    if (syncFile == null) {
      return;
    } else if (isPendingTypePK(syncFile) || (syncFile.getState() == SyncFile.STATE_IN_PROGRESS)) {

      queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

      return;
    } else if (!FileUtil.isModified(syncFile)) {
      return;
    }

    SyncFileService.updateFileSyncFile(filePath, _syncAccountId, syncFile);
  }
Ejemplo n.º 10
0
  @Override
  protected void processRequest() throws Exception {
    SyncFile syncFile = (SyncFile) getParameterValue("syncFile");

    syncFile.setState(SyncFile.STATE_IN_PROGRESS);

    SyncFileService.update(syncFile);

    super.processRequest();
  }
  public SyncWatchEventProcessor(long syncAccountId) {
    _syncAccountId = syncAccountId;

    SyncFileModelListener syncFileModelListener =
        new SyncFileModelListener() {

          @Override
          public void onRemove(SyncFile syncFile) {
            _dependentSyncWatchEventsMaps.remove(syncFile.getFilePathName());

            _pendingTypePKSyncFileIds.remove(syncFile.getTypePK());
          }

          @Override
          public void onUpdate(SyncFile syncFile, Map<String, Object> originalValues) {

            if ((syncFile.getSyncAccountId() != _syncAccountId)
                || (syncFile.getTypePK() == 0)
                || (!originalValues.containsKey("state")
                    && !originalValues.containsKey("typePK"))) {

              return;
            }

            List<SyncWatchEvent> syncWatchEvents =
                _dependentSyncWatchEventsMaps.remove(syncFile.getFilePathName());

            if (syncWatchEvents == null) {
              return;
            }

            if (syncFile.getTypePK() > 0) {
              _pendingTypePKSyncFileIds.remove(syncFile.getSyncFileId());
            }

            for (SyncWatchEvent syncWatchEvent : syncWatchEvents) {
              try {
                if (_logger.isDebugEnabled()) {
                  _logger.debug(
                      "Processing queued event {} {}",
                      syncWatchEvent.getFilePathName(),
                      syncWatchEvent.getEventType());
                }

                processSyncWatchEvent(syncWatchEvent);
              } catch (Exception e) {
                _logger.error(e.getMessage(), e);
              }
            }
          }
        };

    SyncFileService.registerModelListener(syncFileModelListener);
  }
Ejemplo n.º 12
0
  public static SyncFile unsyncFolder(String filePathName) throws Exception {
    SyncFile syncFile = SyncFileService.fetchSyncFile(filePathName);

    if (syncFile == null) {
      return addSyncFile(
          null, null, null, filePathName, null, null, 0, 0, SyncFile.STATE_UNSYNCED, 0, null);
    }

    setStatuses(syncFile, SyncFile.STATE_UNSYNCED, SyncFile.UI_EVENT_NONE);

    return syncFile;
  }
  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);
    }
  }
  @Test
  public void testDoUpdateFolderSyncFile() throws Exception {
    SyncFile folderSyncFileA =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "a"), syncAccount.getSyncAccountId());

    SyncFile folderSyncFileB =
        SyncFileTestUtil.addFolderSyncFile(
            FileUtil.getFilePathName(filePathName, "b"), syncAccount.getSyncAccountId());

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

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

    SyncFileService.updateSyncFile(
        Paths.get(FileUtil.getFilePathName(filePathName, "b", "a")),
        folderSyncFileB.getTypePK(),
        folderSyncFileA);

    SyncFilePersistence syncFilePersistence = SyncFileService.getSyncFilePersistence();

    folderSyncFileAA = syncFilePersistence.queryForId(folderSyncFileAA.getTypePK());

    Assert.assertEquals(
        FileUtil.getFilePathName(filePathName, "b", "a", "a"), folderSyncFileAA.getFilePathName());

    fileSyncFileAA = syncFilePersistence.queryForId(fileSyncFileAA.getTypePK());

    Assert.assertEquals(
        FileUtil.getFilePathName(filePathName, "b", "a", "a.txt"),
        fileSyncFileAA.getFilePathName());
  }
  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);
    }
  }
  protected void doRun() throws Exception {
    SyncWatchEvent lastSyncWatchEvent = SyncWatchEventService.getLastSyncWatchEvent(_syncAccountId);

    if (lastSyncWatchEvent == null) {
      return;
    }

    long delta = System.currentTimeMillis() - lastSyncWatchEvent.getTimestamp();

    if (delta <= 500) {
      SyncEngineUtil.fireSyncEngineStateChanged(
          _syncAccountId, SyncEngineUtil.SYNC_ENGINE_STATE_PROCESSING);

      return;
    }

    if (_logger.isTraceEnabled()) {
      _logger.trace("Processing Sync watch events");
    }

    _pendingTypePKSyncFileIds.clear();

    List<SyncWatchEvent> syncWatchEvents = null;

    if (OSDetector.isApple()) {
      syncWatchEvents = SyncWatchEventService.findBySyncAccountId(_syncAccountId);
    } else {
      syncWatchEvents =
          SyncWatchEventService.findBySyncAccountId(_syncAccountId, "eventType", true);
    }

    for (SyncWatchEvent syncWatchEvent : syncWatchEvents) {
      processSyncWatchEvent(syncWatchEvent);
    }

    for (Map.Entry<String, List<SyncWatchEvent>> entry : _dependentSyncWatchEventsMaps.entrySet()) {

      SyncFile syncFile = SyncFileService.fetchSyncFile(entry.getKey());

      if ((syncFile != null) && (syncFile.getTypePK() > 0)) {
        for (SyncWatchEvent syncWatchEvent : entry.getValue()) {
          processSyncWatchEvent(syncWatchEvent);
        }
      }
    }

    SyncEngineUtil.fireSyncEngineStateChanged(
        _syncAccountId, SyncEngineUtil.SYNC_ENGINE_STATE_PROCESSED);

    _processedSyncWatchEventIds.clear();
  }
  protected String getFileType(String eventType, Path filePath) {
    if (eventType.equals(SyncWatchEvent.EVENT_TYPE_DELETE)) {
      SyncFile syncFile = SyncFileService.fetchSyncFile(filePath.toString());

      if (syncFile != null) {
        return syncFile.getType();
      }
    }

    if (Files.isDirectory(filePath, LinkOption.NOFOLLOW_LINKS)) {
      return SyncFile.TYPE_FOLDER;
    }

    return SyncFile.TYPE_FILE;
  }
  protected long getRepositoryId(Path filePath) {
    while (true) {
      filePath = filePath.getParent();

      if (filePath == null) {
        return 0;
      }

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

      if (syncFile != null) {
        return syncFile.getRepositoryId();
      }
    }
  }
  @Override
  public void processResponse(String response) throws Exception {
    SyncFile remoteSyncFile = JSONUtil.readValue(response, SyncFile.class);

    SyncFile localSyncFile = getLocalSyncFile();

    if (localSyncFile == null) {
      return;
    }

    localSyncFile.setModifiedTime(remoteSyncFile.getModifiedTime());
    localSyncFile.setParentFolderId(remoteSyncFile.getParentFolderId());
    localSyncFile.setSize(remoteSyncFile.getSize());
    localSyncFile.setState(SyncFile.STATE_SYNCED);
    localSyncFile.setUiEvent(SyncFile.UI_EVENT_UPLOADED);
    localSyncFile.setVersion(remoteSyncFile.getVersion());
    localSyncFile.setVersionId(remoteSyncFile.getVersionId());

    SyncFileService.update(localSyncFile);
  }
Ejemplo n.º 20
0
  @Override
  protected void doHandleResponse(HttpResponse httpResponse) throws Exception {

    InputStream inputStream = null;

    try {
      SyncFile syncFile = (SyncFile) getParameterValue("syncFile");

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

      HttpEntity httpEntity = httpResponse.getEntity();

      inputStream = httpEntity.getContent();

      Path tempFilePath = Files.createTempFile(String.valueOf(syncFile.getSyncFileId()), ".tmp");

      if (Files.exists(filePath)) {
        Files.copy(filePath, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
      }

      if ((Boolean) getParameterValue("patch")) {
        IODeltaUtil.patch(tempFilePath, inputStream);
      } else {
        Files.copy(inputStream, tempFilePath, StandardCopyOption.REPLACE_EXISTING);
      }

      syncFile.setFileKey(FileUtil.getFileKey(tempFilePath));
      syncFile.setState(SyncFile.STATE_SYNCED);
      syncFile.setUiEvent((Integer) getParameterValue("uiEvent"));

      SyncFileService.update(syncFile);

      Files.move(
          tempFilePath,
          filePath,
          StandardCopyOption.ATOMIC_MOVE,
          StandardCopyOption.REPLACE_EXISTING);
    } finally {
      StreamUtil.cleanUp(inputStream);
    }
  }
  @Override
  protected void processResponse(String response) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();

    SyncFile remoteSyncFile = objectMapper.readValue(response, new TypeReference<SyncFile>() {});

    SyncFile localSyncFile = (SyncFile) getParameterValue("syncFile");

    processFilePathChange(localSyncFile, remoteSyncFile);

    localSyncFile.setModifiedTime(remoteSyncFile.getModifiedTime());
    localSyncFile.setParentFolderId(remoteSyncFile.getParentFolderId());
    localSyncFile.setSize(remoteSyncFile.getSize());
    localSyncFile.setState(SyncFile.STATE_SYNCED);

    if (getParameterValue("filePath") != null) {
      localSyncFile.setUiEvent(SyncFile.UI_EVENT_UPLOADED);
    }

    localSyncFile.setVersion(remoteSyncFile.getVersion());

    SyncFileService.update(localSyncFile);
  }
  protected boolean isInErrorState(Path filePath) {
    while (true) {
      if (filePath == null) {
        return false;
      }

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

      if (syncFile != null) {
        if (syncFile.isSystem()) {
          break;
        }

        if (syncFile.getState() == SyncFile.STATE_ERROR) {
          return true;
        }
      }

      filePath = filePath.getParent();
    }

    return false;
  }
Ejemplo n.º 23
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);
      }
    }
  }
  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());
      }
    }
  }
  protected void addFolder(SyncWatchEvent syncWatchEvent) throws Exception {
    Path targetFilePath = Paths.get(syncWatchEvent.getFilePathName());

    if (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;
    }

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

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

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

    if (syncFile == null) {
      SyncFileService.addFolderSyncFile(
          targetFilePath,
          parentSyncFile.getTypePK(),
          parentSyncFile.getRepositoryId(),
          _syncAccountId);

      return;
    }

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

    if (targetFilePath.equals(sourceFilePath)) {
      FileKeyUtil.writeFileKey(targetFilePath, String.valueOf(syncFile.getSyncFileId()), true);
    } else if (FileUtil.exists(sourceFilePath)) {
      SyncFileService.addFolderSyncFile(
          targetFilePath,
          parentSyncFile.getTypePK(),
          parentSyncFile.getRepositoryId(),
          _syncAccountId);

      return;
    } else if (parentTargetFilePath.equals(sourceFilePath.getParent())) {
      if (isPendingTypePK(syncFile)) {
        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

      SyncFileService.updateFolderSyncFile(targetFilePath, _syncAccountId, syncFile);
    } else {
      if (isPendingTypePK(syncFile)) {
        queueSyncWatchEvent(syncFile.getFilePathName(), syncWatchEvent);

        return;
      }

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

      Path sourceFileNameFilePath = sourceFilePath.getFileName();

      if (!sourceFileNameFilePath.equals(targetFilePath.getFileName())) {
        SyncFileService.updateFolderSyncFile(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());
      }
    }
  }
  public static SyncAccount addSyncAccount(
      String filePathName,
      String login,
      int maxConnections,
      String name,
      String password,
      int pollInterval,
      SyncSite[] syncSites,
      boolean trustSelfSigned,
      String url)
      throws Exception {

    // Sync account

    SyncAccount syncAccount = new SyncAccount();

    syncAccount.setFilePathName(filePathName);
    syncAccount.setLogin(login);
    syncAccount.setMaxConnections(maxConnections);
    syncAccount.setName(name);
    syncAccount.setPassword(Encryptor.encrypt(password));
    syncAccount.setPollInterval(pollInterval);
    syncAccount.setTrustSelfSigned(trustSelfSigned);
    syncAccount.setUrl(url);

    _syncAccountPersistence.create(syncAccount);

    // Sync file

    Files.createDirectories(Paths.get(filePathName));

    SyncFileService.addSyncFile(
        null,
        null,
        filePathName,
        FileUtil.getFileKey(filePathName),
        filePathName,
        null,
        filePathName,
        0,
        0,
        syncAccount.getSyncAccountId(),
        SyncFile.TYPE_SYSTEM);

    // Sync sites

    if (syncSites != null) {
      for (SyncSite syncSite : syncSites) {
        String syncSiteName = syncSite.getName();

        if (!FileUtil.isValidFileName(syncSiteName)) {
          syncSiteName = String.valueOf(syncSite.getGroupId());
        }

        syncSite.setFilePathName(syncAccount.getFilePathName() + "/" + syncSiteName);

        syncSite.setSyncAccountId(syncAccount.getSyncAccountId());

        SyncSiteService.update(syncSite);
      }
    }

    return syncAccount;
  }