/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.application;

import net.aavalla.utils.jobpool.JobCategory;
import compid.data.DAOFactory;
import compid.data.UpdateHandler;
import compid.report.DatasetCollection;
import net.aavalla.utils.jobpool.ParameterizedJob;
import net.aavalla.utils.jobpool.parameters.FieldAnnotations;
import net.aavalla.utils.jobpool.parameters.ParameterCollection;
import compid.report.DatasetCollectionNotEmptyValidator;

/**
 *
 * @author Lari Natri
 */
public class DatasetDeleter extends ParameterizedJob {

    public final class DatasetDeleterParams extends ParameterCollection {

        @FieldAnnotations(
            name="Datasets",
            validator=DatasetCollectionNotEmptyValidator.class,
            description="Datasets to delete")
        private DatasetCollection dataSets;

        DatasetDeleterParams() {
            this.setDescription("Deletes datasets. Note that this is a slow" +
                    " operation.");
        }

    }

    public DatasetDeleter() {
        super();
        this.addCategory(JobCategory.DB_WRITE);
        this.addConflictingCategory(JobCategory.DB_READ);
        this.addConflictingCategory(JobCategory.DB_WRITE);
        this.setName("Dataset deleter");
    }


    @Override
    public ParameterCollection getNonfinalEditableParameters() {
        return new DatasetDeleterParams();
    }

    @Override
    public Object work(ParameterCollection parameters) {
        
        DatasetDeleterParams p = (DatasetDeleterParams)parameters;

        UpdateHandler uh = DAOFactory.getInstance().getUpdateHandler();

        int delCount = uh.deleteDatasets(p.dataSets.getCollection());
        if (delCount>0) {
            CompidApp.getApplication().getDatasetsHandler().refresh();
        }
        return "Deleted "+delCount+" datasets succesfully";

    }

}
