/*
 * Created on Mar 18, 2008 (C) Copyright TANDBERG Television Ltd.
 */

package com.tandbergtv.watchpoint.pmm.action;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import com.tandbergtv.watchpoint.pmm.util.ArchiveUtility;

import de.schlichtherle.io.File;

/**
 * Extracts an Archive File to a folder location
 * 
 * @author Vijay Silva
 */
public class ExtractArchiveAction implements ActionHandler {

	/* Serialization Version Id */
	private static final long serialVersionUID = 588772173852715206L;

	protected String archivePath;

	protected String targetFolderPath;

	/**
	 * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)
	 */
	public void execute(ExecutionContext executionContext) throws Exception {
		this.extractArchive(executionContext);
	}

	/**
	 * Extract an Archive File to the appropriate
	 * 
	 * @param context
	 *            The Execution Context
	 * @throws ActionException
	 *             Exception performing the task
	 */
	protected void extractArchive(ExecutionContext context) throws ActionException {
		this.prepareForExtract(context);

		File archiveFile = null;
		try {
			/* Open the Archive and the target folder */
			archiveFile = ArchiveUtility.openArchive(this.archivePath);
			File targetFolder = new File(this.targetFolderPath);

			/* Perform the tasks that must happen before the extraction */
			this.performPreExtractTasks(context, archiveFile, targetFolder);

			if (!archiveFile.copyAllTo(targetFolder)) {
				String message = "Failed to extract contents of archive file: " + archiveFile
						+ " to location: " + targetFolder;
				throw new ActionException(message);
			}

			/* Perform the tasks that must happen after the extraction */
			this.performPostExtractTasks(context, archiveFile, targetFolder);
		} finally {
			if (archiveFile != null) {
				ArchiveUtility.closeArchive(archiveFile);
			}
		}
	}

	/**
	 * Allows any preparation for Extraction Task, including setting paths
	 * 
	 * @param context
	 *            The Execution Context
	 */
	protected void prepareForExtract(ExecutionContext context) throws ActionException {
	}

	/**
	 * Perform any tasks required before extracting the archive
	 * 
	 * @param context
	 *            The Execution Context
	 * @param archiveFile
	 *            The Archive File to extract
	 * @param targetFolder
	 *            The Folder in which to extract the contents to
	 * @throws ActionException
	 *             Exception performing the pre-extract tasks
	 */
	protected void performPreExtractTasks(ExecutionContext context, File archiveFile,
			File targetFolder) throws ActionException {
	}

	/**
	 * Perform any tasks required after extracting the archive
	 * 
	 * @param context
	 *            The Execution Context
	 * @param archiveFile
	 *            The Archive File to extract
	 * @param targetFolder
	 *            The Folder in which to extract the contents to
	 * @throws ActionException
	 *             Exception performing the post-extract tasks
	 */
	protected void performPostExtractTasks(ExecutionContext context, File archiveFile,
			File targetFolder) throws ActionException {
	}
}
