package org.wipo.tools.buildmagic.task;

import java.io.File;
import java.util.*;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */

public class CCCheckout extends org.apache.tools.ant.taskdefs.optional.clearcase.CCCheckout {

    protected Vector filesets = new Vector();

    public CCCheckout() {
    }

    /**
     * Adds a set of files (nested fileset attribute).
     */
    public void addFileset(FileSet set) {
        filesets.addElement(set);
    }

    public void execute() throws BuildException {
        if (filesets.size() == 0)
            super.execute();
        else {
            for (int s=0; s<filesets.size(); s++) {
                FileSet fs = (FileSet) filesets.elementAt(s);
                DirectoryScanner ds = fs.getDirectoryScanner(project);
                String[] files = ds.getIncludedFiles();
                for (int f=0; f<files.length; f++) {
                    try {
                        File file = new File(fs.getDir(project), files[f]);
                        // Don't checkout non read-only files
                        if (file.canWrite())
                            continue;
                        setViewPath(file.getAbsolutePath());
                        super.execute();
                    } catch (BuildException be) {
                        log(be.getMessage(), Project.MSG_WARN);
                    }
                }
            }
        }
    }
}