package org.hive2hive.core.processes.files.delete;

import java.io.File;

import org.hive2hive.core.exceptions.AbortModifyException;
import org.hive2hive.core.exceptions.NoPeerConnectionException;
import org.hive2hive.core.exceptions.NoSessionException;
import org.hive2hive.core.model.FolderIndex;
import org.hive2hive.core.model.Index;
import org.hive2hive.core.model.versioned.UserProfile;
import org.hive2hive.core.network.NetworkManager;
import org.hive2hive.core.network.data.DataManager;
import org.hive2hive.core.processes.common.base.BaseModifyUserProfileStep;
import org.hive2hive.core.processes.context.DeleteFileProcessContext;
import org.hive2hive.core.processes.files.GetMetaFileStep;
import org.hive2hive.processframework.exceptions.ProcessExecutionException;

/**
 * Step that deletes a file from the index in the user profile after doing some verification.
 * In case the deleted file is a file (and not a folder), this step initiates the deletion of the meta file
 * and all according chunks.
 * 
 * @author Nico, Seppi
 */
public class DeleteFromUserProfileStep extends BaseModifyUserProfileStep {

	private final DeleteFileProcessContext context;
	private final DataManager dataManager;

	public DeleteFromUserProfileStep(DeleteFileProcessContext context, NetworkManager networkManager)
			throws NoPeerConnectionException, NoSessionException {
		super(networkManager.getSession().getProfileManager());
		this.context = context;
		this.dataManager = networkManager.getDataManager();
	}

	@Override
	public void modifyUserProfile(UserProfile userProfile) throws AbortModifyException {
		File file = context.consumeFile();
		File root = context.consumeRoot();

		Index fileIndex = userProfile.getFileByPath(file, root);

		// validate
		if (fileIndex == null) {
			throw new AbortModifyException("File index not found in user profile");
		} else if (!fileIndex.canWrite()) {
			throw new AbortModifyException("Not allowed to delete this file (read-only permissions)");
		}

		// check preconditions
		if (fileIndex.isFolder()) {
			FolderIndex folder = (FolderIndex) fileIndex;
			if (!folder.getChildren().isEmpty()) {
				throw new AbortModifyException("Cannot delete a directory that is not empty.");
			}
		}

		// remove the node from the tree
		FolderIndex parentIndex = fileIndex.getParent();
		parentIndex.removeChild(fileIndex);

		// store for later
		context.provideIndex(fileIndex);
		if (fileIndex.isFile()) {
			context.provideProtectionKeys(fileIndex.getProtectionKeys());
			context.provideMetaFileEncryptionKeys(fileIndex.getFileKeys());
		}
	}

	@Override
	protected void afterModify() throws ProcessExecutionException {
		// create steps to delete meta and all chunks
		GetMetaFileStep getMeta = new GetMetaFileStep(context, dataManager);
		DeleteChunksProcess deleteChunks = new DeleteChunksProcess(context, dataManager);
		DeleteMetaFileStep deleteMeta = new DeleteMetaFileStep(context, dataManager);

		// insert them in correct order
		getParent().insertNext(getMeta, this);
		getParent().insertNext(deleteChunks, getMeta);
		getParent().insertNext(deleteMeta, deleteChunks);
	}

	@Override
	protected void modifyRollback(UserProfile userProfile) {
		File file = context.consumeFile();
		File root = context.consumeRoot();
		Index index = context.consumeIndex();

		// re-add file to user profile
		FolderIndex parent = (FolderIndex) userProfile.getFileByPath(file.getParentFile(), root);
		parent.addChild(index);
		index.setParent(parent);
	}

}

@@ -2406,16 +2406,140 @@
 Index);%0A
+%09%7D%0A%0A%09@Override%0A%09protected void afterModify() throws ProcessExecutionException %7B%0A%09%09Index fileIndex = context.consumeIndex();%0A
 %09%09if (fi
@@ -2694,92 +2694,10 @@
 ));%0A
-%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09protected void afterModify() throws ProcessExecutionException %7B
 %0A
+%09
 %09%09//
@@ -2740,16 +2740,17 @@
  chunks%0A
+%09
 %09%09GetMet
@@ -2804,32 +2804,33 @@
 , dataManager);%0A
+%09
 %09%09DeleteChunksPr
@@ -2899,16 +2899,17 @@
 ger);%0A%09%09
+%09
 DeleteMe
@@ -2981,16 +2981,17 @@
 er);%0A%0A%09%09
+%09
 // inser
@@ -3014,16 +3014,17 @@
 t order%0A
+%09
 %09%09getPar
@@ -3054,24 +3054,25 @@
 a, this);%0A%09%09
+%09
 getParent().
@@ -3106,16 +3106,17 @@
 tMeta);%0A
+%09
 %09%09getPar
@@ -3159,16 +3159,20 @@
 hunks);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

Fix delete folder