Package com.nexirius.tools.dirsync

Source Code of com.nexirius.tools.dirsync.DefaultDirSyncManager

//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.tools.dirsync;

import com.nexirius.util.XFile;
import com.nexirius.util.StringVector;

import java.net.URL;
import java.io.IOException;
import java.io.InputStream;

public class DefaultDirSyncManager implements DirSyncManager {
    public static final int SURPLUS_IGNORE = 0;
    public static final int SURPLUS_TRASH = 1;
    public static final int SURPLUS_DELETE = 2;

    public int surplus = SURPLUS_IGNORE;

    private boolean interrupted = false;
    protected long createdDirectories;
    protected long deletedDirectories;
    protected long updatedFiles;
    protected long deletedFiles;
    protected long errors;
    protected long totalVolume;
    protected long actualVolume;
    private static final String TRASH_POSTFIX = "_trash_";

    /**
     *
     * @param surplus SURPLUS_IGNORE SURPLUS_TRASH SURPLUS_DELETE
     */
    public void setSurplus(int surplus) {
        this.surplus = surplus;
    }

    public void setTotalVolume(long volume) {
        totalVolume = volume;
        actualVolume = 0;
    }

    public long getActualVolume() {
        return actualVolume;
    }

    public void addActualVolume(long volume) {
        setActualVolume(actualVolume + volume);
    }

    public void setActualVolume(long volume) {
        actualVolume = volume;
        System.out.println("actualVolume = " + actualVolume + "/" + totalVolume);
    }

    public void removeDirectory(String targetRootDirectory, String directoryName) {
        if (surplus == SURPLUS_IGNORE) {
            return ;
        }

        XFile targetDir = new XFile(targetRootDirectory + XFile.separator + directoryName);

        if (surplus == SURPLUS_TRASH) {
            XFile root = new XFile(targetRootDirectory);
            String parent = root.getParent();

            if (parent == null) {
                parent = root.getPath();
            }

            XFile trashDir = new XFile(parent, root.getName() + TRASH_POSTFIX);
            XFile targetTrashDir = new XFile(trashDir + XFile.separator + directoryName);

            targetTrashDir.getParentFile().mkdirs();


            targetDir.renameTo(targetTrashDir);
        } else {
            targetDir.delete();
        }

        System.out.println("remove targetDir = " + directoryName);
        ++deletedDirectories;
    }

    public void createDirectory(XFile targetDir) {
        if (targetDir.isDirectory()) {
            return;
        }
        targetDir.mkdirs();
        System.out.println("create targetDir = " + targetDir);
        ++createdDirectories;
    }

    public void removeFile(String targetRootDirectory, String directoryName, String fileName) {
        if (surplus == SURPLUS_IGNORE) {
            return ;
        }

        XFile targetFile = new XFile(targetRootDirectory + XFile.separator + directoryName, fileName);

        if (surplus == SURPLUS_TRASH) {
            XFile root = new XFile(targetRootDirectory);
            String parent = root.getParent();

            if (parent == null) {
                parent = root.getPath();
            }

            XFile trashDir = new XFile(parent, root.getName() + TRASH_POSTFIX);
            XFile targetTrashDir = new XFile(trashDir + XFile.separator + directoryName);

            targetTrashDir.mkdirs();

            XFile targetTrashFile = new XFile(targetTrashDir.getPath(), fileName);

            targetFile.renameTo(targetTrashFile);
        } else {
            targetFile.delete();
        }

        System.out.println("remove targetFile = " + targetFile);
        ++deletedFiles;
    }

    public void createFile(URL url, XFile targetFile) throws IOException {
        InputStream in = url.openStream();
        createFile(in, targetFile);
    }

    public void createFile(InputStream in, XFile targetFile) throws IOException {
        targetFile.createFrom(in);
        ++updatedFiles;
    }

    public void setError(String message, Exception ex) {
        System.out.println(message);
        ex.printStackTrace();
        ++errors;
    }

    public boolean isInterrupted() {
        return interrupted;
    }

    public void setInterrupted(boolean interrupted) {
        this.interrupted = interrupted;
    }

    public String toString() {
        StringBuffer ret = new StringBuffer();

        ret.append("\ncreatedDirectories = " + createdDirectories);
        ret.append("\ndeletedDirectories = " + deletedDirectories);
        ret.append("\nupdatedFiles = " + updatedFiles);
        ret.append("\ndeletedFiles = " + deletedFiles);
        ret.append("\nerrors = " + errors);

        return ret.toString();
    }

}
TOP

Related Classes of com.nexirius.tools.dirsync.DefaultDirSyncManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.