package org.hive2hive.core.process.delete;

import java.io.File;

import org.hive2hive.core.file.FileManager;
import org.hive2hive.core.model.FileTreeNode;
import org.hive2hive.core.model.UserCredentials;
import org.hive2hive.core.network.NetworkManager;
import org.hive2hive.core.process.Process;
import org.hive2hive.core.process.common.File2MetaFileStep;
import org.hive2hive.core.process.common.get.GetUserProfileStep;

/**
 * 1. get user profile and find the {@link FileTreeNode} in there. Check if write-access to this file
 * 2. get the meta file / folder
 * 3. delete all chunks of all versions from the DHT
 * 4. delete the meta file / folder
 * 5. remove tree node from user profile and update it
 * 6. notify other clients
 * 
 * @author Nico
 * 
 */
// TODO verify if the file is a folder. If yes, either delete recursively or deny deletion
public class DeleteFileProcess extends Process {

	private final DeleteFileProcessContext context;

	public DeleteFileProcess(File file, FileManager fileManager, NetworkManager networkManager,
			UserCredentials credentials) {
		super(networkManager);
		context = new DeleteFileProcessContext(this, fileManager, file.isDirectory(), credentials);

		File2MetaFileStep file2MetaStep = new File2MetaFileStep(file, fileManager, context, context,
				new DeleteChunkStep());
		GetUserProfileStep getUserProfileStep = new GetUserProfileStep(credentials, file2MetaStep, context);
		setNextStep(getUserProfileStep);
	}

	public DeleteFileProcess(FileTreeNode file, FileManager fileManager, NetworkManager networkManager,
			UserCredentials credentials) {
		super(networkManager);
		context = new DeleteFileProcessContext(this, fileManager, file.isFolder(), credentials);

		File2MetaFileStep file2MetaStep = new File2MetaFileStep(file, fileManager, context, context, null /* TODO */);
		GetUserProfileStep getUserProfileStep = new GetUserProfileStep(credentials, file2MetaStep, context);
		setNextStep(getUserProfileStep);
	}

	@Override
	public DeleteFileProcessContext getContext() {
		return context;
	}
}

@@ -1062,32 +1062,64 @@
 als credentials)
+ throws IllegalArgumentException
  %7B%0A%09%09super(netwo
@@ -1122,32 +1122,49 @@
 etworkManager);%0A
+%09%09verify(file);%0A%0A
 %09%09context = new 
@@ -1546,24 +1546,28 @@
 reeNode file
+Node
 , FileManage
@@ -1648,34 +1648,200 @@
 ls) 
-%7B%0A%09%09super(networkManager);
+throws IllegalArgumentException %7B%0A%09%09super(networkManager);%0A%09%09File file = fileManager.getFile(fileNode);%0A%09%09if (file.exists()) %7B%0A%09%09%09// verfiy file only if it exists on disk%0A%09%09%09verify(file);%0A%09%09%7D%0A
 %0A%09%09c
@@ -1901,16 +1901,20 @@
 er, file
+Node
 .isFolde
@@ -2188,16 +2188,301 @@
 p);%0A%09%7D%0A%0A
+%09private void verify(File file) throws IllegalArgumentException %7B%0A%09%09if (file == null) %7B%0A%09%09%09throw new IllegalArgumentException(%22File may not be null%22);%0A%09%09%7D%0A%0A%09%09if (file.isDirectory() && file.listFiles().length %3E 0) %7B%0A%09%09%09throw new IllegalArgumentException(%22Folder is not empty%22);%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Overri

Verify file deletion if folder