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

com.paypal.butterfly.utilities.operations.file.MoveFile 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.codehaus.plexus.util.FileUtils;

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

/**
 * Moves a file. The relative or absolute file is the
 * "from" file, while the "to" location is specified via {@link #setToRelative(String)}
 * or {@link #setToAbsolute(String)}
 * 
* Notes: *
    *
  1. If destinationDirectory does not exist, it (and any parent directories) will be created.
  2. *
  3. If a file source in destinationDirectory exists, it will be overwritten.
  4. *
  5. If you want to move a set of specific files from one location to another, then use a multiple transformation operation (see {@code TransformationTemplate.addMultiple()}) with {@link MoveFile}.
  6. *
  7. If you want to move a directory and its content from one location to another, then use {@link MoveDirectory} instead.
  8. *
  9. If source file is actually a directory, an operation results in error.
  10. *
* * @see CopyFile * @see CopyDirectory * @see MoveDirectory * * @author facarvalho */ public class MoveFile extends AbstractToOperation { // TODO document in javadoc that by default it overwrites, in case the file // already exists, or, make it configurable to overwrite or not private static final String DESCRIPTION = "Move file %s to %s"; /** * Moves a file. The relative or absolute file is the * "from" file, while the "to" location is specified via {@link #setToRelative(String)} * or {@link #setToAbsolute(String)} */ public MoveFile() { super(DESCRIPTION); } @Override protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { File fileFrom = getAbsoluteFile(transformedAppFolder, transformationContext); File fileTo = getFileTo(transformedAppFolder, transformationContext); TOExecutionResult result; // TODO // Check if it is really a file and if it exists! try { if (fileFrom.isDirectory()) { IOException ex = new IOException(getRelativePath(transformedAppFolder, fileFrom) + " (Is a directory)"); result = TOExecutionResult.error(this, new TransformationOperationException("File could not be moved", ex)); } else { String details = String.format("File '%s' has been moved to '%s'", getRelativePath(), getRelativePath(transformedAppFolder, fileTo)); FileUtils.copyFileToDirectory(fileFrom, fileTo); FileUtils.fileDelete(fileFrom.getAbsolutePath()); result = TOExecutionResult.success(this, details); } } catch (IOException e) { result = TOExecutionResult.error(this, new TransformationOperationException("File could not be moved", e)); } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy