All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.paypal.butterfly.utilities.operations.file.MoveDirectory Maven / Gradle / Ivy

There is a newer version: 3.2.7
Show newest version
package com.paypal.butterfly.utilities.operations.file;

import com.paypal.butterfly.extensions.api.TOExecutionResult;
import com.paypal.butterfly.extensions.api.TransformationContext;
import com.paypal.butterfly.extensions.api.exception.TransformationOperationException;
import com.paypal.butterfly.utilities.operations.AbstractToOperation;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * Moves a directory and its content from one location to another.
 * The directory to be moved is specified from relative
 * or absolute location. The name of the destination
 * directory will not be the same as the original one, but the one
 * specified via {@link #setToRelative(String)}
 * or {@link #setToAbsolute(String)}. If the destination directory
 * already exists, an error is returned.
 *
 * @see CopyFile
 * @see CopyDirectory
 * @see MoveFile
 *
 * @author facarvalho
 */
public class MoveDirectory extends AbstractToOperation {

    private static final String DESCRIPTION = "Move a directory from %s to %s";

    /**
     * Moves a directory and its content from one location to another.
     * The directory to be moved is specified from relative
     * or absolute location. The name of the destination
     * directory will not be the same as the original one, but the one
     * specified via {@link #setToRelative(String)}
     * or {@link #setToAbsolute(String)}. If the destination directory
     * already exists, an error is returned.
     */
    public MoveDirectory() {
        super(DESCRIPTION);
    }

    @Override
    protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) {
        // TODO Validation must be done here!!! In case none has been set!
        File filesFrom = getAbsoluteFile(transformedAppFolder, transformationContext);
        File fileTo = getFileTo(transformedAppFolder, transformationContext);
        TOExecutionResult result = null;

        try {
            FileUtils.moveDirectory(filesFrom, fileTo);
            String details = String.format("Directory '%s' has been moved to '%s'", getRelativePath(transformedAppFolder, filesFrom), getRelativePath(transformedAppFolder, fileTo));
            result = TOExecutionResult.success(this, details);
        } catch (IOException e) {
            result = TOExecutionResult.error(this, new TransformationOperationException("Directory could not be moved", e));
        }

        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy