Пример #1
0
  @Override
  protected void doExecute() throws InvalidProcessStateException {
    FolderIndex fileNode = (FolderIndex) context.consumeIndex();

    // create a subtree containing all children
    FolderIndex sharedNode =
        new FolderIndex(fileNode.getParent(), fileNode.getFileKeys(), fileNode.getName());
    for (Index child : fileNode.getChildren()) {
      sharedNode.addChild(child);
      child.setParent(sharedNode);
    }

    // copy all user permissions
    List<UserPermission> userPermissions = fileNode.getUserPermissions();
    UserPermission[] permissionArray = new UserPermission[userPermissions.size() + 1];
    permissionArray = userPermissions.toArray(permissionArray);
    // add the own permission
    permissionArray[permissionArray.length - 1] = new UserPermission(userId, PermissionType.WRITE);

    // if the friend receives write access, he gets the protection key
    if (context.getPermissionType() == PermissionType.WRITE) {
      sharedNode.share(context.consumeNewProtectionKeys(), permissionArray);
    } else {
      sharedNode.share(null, permissionArray);
    }

    // remove the parent and only send the sub-tree
    sharedNode.setParent(null);

    // notify all users of the shared node
    Set<String> friends = new HashSet<String>();
    friends.addAll(fileNode.getCalculatedUserList());
    friends.remove(userId); // skip to notify myself
    logger.debug("Sending a notification message to the friend and all other sharers.");

    BaseNotificationMessageFactory messageFactory =
        new ShareFolderNotificationMessageFactory(sharedNode, context.getUserPermission());
    context.provideMessageFactory(messageFactory);
    context.provideUsersToNotify(friends);
  }