Package org.hive2hive.core.processes.implementations.share

Source Code of org.hive2hive.core.processes.implementations.share.PrepareNotificationsStep

package org.hive2hive.core.processes.implementations.share;

import java.util.HashSet;
import java.util.Set;

import org.hive2hive.core.model.FolderIndex;
import org.hive2hive.core.model.Index;
import org.hive2hive.core.model.PermissionType;
import org.hive2hive.core.model.UserPermission;
import org.hive2hive.core.processes.framework.abstracts.ProcessStep;
import org.hive2hive.core.processes.framework.exceptions.InvalidProcessStateException;
import org.hive2hive.core.processes.implementations.context.ShareProcessContext;
import org.hive2hive.core.processes.implementations.notify.BaseNotificationMessageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Starts the notification process that a file has been shared.
*
* @author Seppi, Nico
*/
public class PrepareNotificationsStep extends ProcessStep {

  private final static Logger logger = LoggerFactory.getLogger(PrepareNotificationsStep.class);

  private final ShareProcessContext context;
  private final String userId;

  public PrepareNotificationsStep(ShareProcessContext context, String userId) {
    this.context = context;
    this.userId = userId; // ownUserId
  }

  @Override
  protected void doExecute() throws InvalidProcessStateException {
    logger.debug(
        "Preparing a notification message to the friend '{}' and all other sharers of the shared folder '{}'.",
        context.getFriendId(), context.getFolder().getName());

    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
    for (UserPermission userPermission : fileNode.getUserPermissions()) {
      sharedNode.addUserPermissions(userPermission);
    }

    // if the friend receives write access, he gets the protection key
    if (context.getPermissionType() == PermissionType.WRITE) {
      logger.debug("Friend '{}' gets WRITE access to the shared folder '{}'.", context.getFriendId(),
          context.getFolder().getName());
      sharedNode.share(context.consumeNewProtectionKeys());
    } else {
      logger.debug("Friend '{}' gets READ access to the shared folder '{}'.",
          context.getFriendId(), context.getFolder().getName());
      sharedNode.share(null);
    }

    // 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

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

Related Classes of org.hive2hive.core.processes.implementations.share.PrepareNotificationsStep

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.