Example usage for org.apache.commons.io FileUtils moveDirectory

List of usage examples for org.apache.commons.io FileUtils moveDirectory

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils moveDirectory.

Prototype

public static void moveDirectory(File srcDir, File destDir) throws IOException 

Source Link

Document

Moves a directory.

Usage

From source file:org.xwiki.store.filesystem.internal.migration.R910100XWIKI14871DataMigration.java

private void storeDeletedAttachment(File directory, long id, Session session)
        throws ParserConfigurationException, SAXException, IOException {
    this.logger.info("Storing attachment metadata [{}] in the database", directory);

    // Find document reference
    File documentDirectory = directory.getParentFile().getParentFile().getParentFile();
    DocumentReference documentReference = getDocumentReference(documentDirectory);

    if (getXWikiContext().getWikiReference().equals(documentReference.getWikiReference())) {
        // Parse ~DELETED_ATTACH_METADATA.xml
        File file = new File(directory, "~DELETED_ATTACH_METADATA.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(file);

        String filename = getElementText(doc, "filename", null);
        String deleter = getElementText(doc, "deleter", null);
        Date deleteDate = new Date(Long.valueOf(getElementText(doc, "datedeleted", null)));

        long docId = new XWikiDocument(documentReference).getId();

        // We need to make sure the deleted attachment is not already in the database with a different id (left
        // there by the attachment porter script for example)
        Query selectQuery = session
                .createQuery("SELECT id FROM DeletedAttachment WHERE docId=? AND filename=? AND date=?");
        selectQuery.setLong(0, docId);/*from w  w w.  j ava 2  s  .co m*/
        selectQuery.setString(1, filename);
        selectQuery.setTimestamp(2, new java.sql.Timestamp(deleteDate.getTime()));
        Long databaseId = (Long) selectQuery.uniqueResult();

        if (databaseId == null) {
            // Try without the milliseconds since most versions of MySQL don't support them
            selectQuery.setTimestamp(2, new java.sql.Timestamp(deleteDate.toInstant().getEpochSecond() * 1000));
            databaseId = (Long) selectQuery.uniqueResult();
        }

        DeletedAttachment dbAttachment;
        if (databaseId != null) {
            // Update the database metadata (probably left there by the attachment porter script)
            dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename,
                    FileSystemStoreUtils.HINT, deleter, deleteDate, null, databaseId);
            session.update(dbAttachment);
        } else {
            // Insert new deleted attachment metadata in the DB
            dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename,
                    FileSystemStoreUtils.HINT, deleter, deleteDate, null);
            databaseId = (Long) session.save(dbAttachment);
        }

        // Refactor file storage to be based on database id instead of date
        File newDirectory = new File(directory.getParentFile(),
                encode(dbAttachment.getFilename() + "-id" + databaseId));
        FileUtils.moveDirectory(directory, newDirectory);
    }
}

From source file:org.zeroturnaround.zip.Zips.java

/**
 * if we are doing something in place, move result file into src.
 *
 * @param result destination zip file//from w  ww .  j av  a2  s . com
 */
private void handleInPlaceActions(File result) throws IOException {
    if (isInPlace()) {
        // we operate in-place
        FileUtils.forceDelete(src);
        if (result.isFile()) {
            FileUtils.moveFile(result, src);
        } else {
            FileUtils.moveDirectory(result, src);
        }
    }
}

From source file:playRepository.SVNRepository.java

public boolean move(String srcProjectOwner, String srcProjectName, String desrProjectOwner,
        String destProjectName) {
    File src = new File(getRepoPrefix() + srcProjectOwner + "/" + srcProjectName);
    File dest = new File(getRepoPrefix() + desrProjectOwner + "/" + destProjectName);
    src.setWritable(true);//from w ww.j  a  v  a2  s.c  o  m

    try {
        if (src.exists()) {
            FileUtils.moveDirectory(src, dest);
        }
        return true;
    } catch (IOException e) {
        play.Logger.error("Move Failed", e);
        return false;
    }
}

From source file:uk.ac.ebi.metabolights.model.queue.SubmissionItem.java

public void moveFileTo(String destinationFolder, Boolean isMoveToErrorFolder) throws IOException {

    File destination = new File(destinationFolder + fileQueued.getName());

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timeStamp = sdf.format(cal.getTime());

    if (isMoveToErrorFolder) {
        destination = new File(destinationFolder + "TS_" + timeStamp + "~" + fileQueued.getName());
    }/*w  ww . j  a  v  a  2 s .com*/

    if (fileQueued.isDirectory()) {
        FileUtils.moveDirectory(fileQueued, destination);
    } else {
        FileUtils.moveFile(fileQueued, destination);
    }

    fileQueued = destination;

}

From source file:uk.ac.ebi.metabolights.webservice.queue.SubmissionItem.java

public void moveFileTo(String destinationFolder, Boolean isMoveToErrorFolder) throws IOException {

    if (!fileQueued.exists()) {
        logger.error("Can't move submitted file {} to {}. File does not exists.");
    }/*  w w w  .jav  a 2 s.  co m*/

    File destination = new File(destinationFolder + fileQueued.getName());

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String timeStamp = sdf.format(cal.getTime());

    if (isMoveToErrorFolder) {
        destination = new File(destinationFolder + "TS_" + timeStamp + "~" + fileQueued.getName());
    }

    if (fileQueued.isDirectory()) {
        FileUtils.moveDirectory(fileQueued, destination);
    } else {
        FileUtils.moveFile(fileQueued, destination);
    }

    fileQueued = destination;

}

From source file:uk.ac.susx.tag.method51.core.organise.Project.java

/**
 * Renames the specified job/*from   ww w.j a v a  2 s.c o  m*/
 * @param currentJID the current uid of the job
 * @param newName the name to give the job
 * @return the new uid of the job
 * @throws IOException when disk bad or job running
 * @throws IllegalArgumentException when currentJID or newName are invalid
 * @throws DecodeException when job config is currently corrupt
 * @throws EncodeException when job can't be reserialized for some reason
 */
public String renameJob(String currentJID, String newName)
        throws IOException, IllegalArgumentException, DecodeException, EncodeException {
    assertValidPath(currentJID);
    assertValidName(newName);
    synchronized (Project.class) {
        assertJobExists(currentJID);
        if (jobIsRunning(currentJID))
            throw new IOException("Can't rename a job while it is running");

        // get the actual job, rename it, then write it back to disk
        Job j = getJob(currentJID);
        j.name.set(newName);
        setJob(currentJID, j);

        // make new id and move directory if necessary
        File jobDir = file(dir, currentJID);

        String newID = cleanName(newName);

        if (!newID.equals(currentJID)) {
            newID = qualifyId(jobDir.getParentFile(), newID);
            FileUtils.moveDirectory(jobDir, file(jobDir.getParentFile(), newID));
        }

        int idx = currentJID.lastIndexOf('/');
        if (idx != -1) {
            newID = currentJID.substring(0, idx + 1) + newID;
        }

        return newID;
    }
}

From source file:uk.ac.susx.tag.method51.core.organise.Project.java

/**
 * Renames this project, changing its UID
 * @param newName the new name for the project
 * @return the new UID of the project/*ww  w . j  a v a  2  s .c  om*/
 * @throws IOException if disk bad
 * @throws DecodeException if the project's config file is corrupt
 * @throws IllegalArgumentException if the name is bad
 */
public String rename(String newName) throws IOException, DecodeException, IllegalArgumentException {
    assertValidName(newName);
    synchronized (Project.class) {
        if (!isEditable())
            throw new IllegalArgumentException("Project is not editable");
        if (jobsAreRunning())
            throw new IOException("Cannot rename project while jobs are running");

        String newId = cleanName(newName);

        if (!newId.equals(getID())) {
            newId = qualifyId(dir.getParentFile(), newId);

            File newDir = file(dir.getParentFile(), newId);

            FileUtils.moveDirectory(dir, newDir);

            this.dir = newDir;
        }

        Config cfg = loadConfig();

        cfg.name.set(newName);

        writeConfig(cfg);

        return newId;
    }
}

From source file:uk.ac.susx.tag.method51.core.organise.Project.java

/**
 * moves a subproject or job//from w  ww .j a va  2 s  . co  m
 * @param id the id of the job or subproject to move
 * @param toSpid the id of the subproject to move it inside, or the empty string to move to the root
 * @return the new id of the subproject or job
 * @throws IOException
 */
public String move(String id, String toSpid) throws IOException {
    assertValidPath(id);
    if (!toSpid.isEmpty())
        assertValidPath(toSpid);
    synchronized (Project.class) {
        if (!hasJob(id) && !hasSubProject(id))
            throw new IllegalArgumentException("No job or subproject with id '" + id + "'");

        if ((hasJob(id) && jobIsRunning(id)) || (hasSubProject(id) && getSubProject(id).jobsAreRunning()))
            throw new IOException("Can't move while jobs are running");

        File dirToMove = file(dir, id);
        File toDir = file(dir, toSpid);
        File toFile = file(toDir, dirToMove.getName());

        if (toFile.exists()) {
            String name = qualifyId(toDir, dirToMove.getName());
            toFile = file(toDir, name);
        }

        if (toSpid.equals("")) {
            FileUtils.moveDirectory(dirToMove, toFile);
            return toFile.getName();

        } else {
            makeSubs(toSpid);

            FileUtils.moveDirectory(dirToMove, toFile);

            return toSpid + "/" + toFile.getName();
        }
    }
}