package _VisualDVM.TestingSystem.Common.TestingPackage;
import Common.CommonConstants;
import Common.Database.Objects.DBObject;
import Common.Database.Objects.riDBObject;
import Common.Utils.Utils_;
import _VisualDVM.TestingSystem.Common.Configuration.Configuration;
import _VisualDVM.TestingSystem.Common.TasksPackageState;
import com.sun.org.glassfish.gmbal.Description;

import java.io.File;
import java.util.Vector;
public abstract class TestingPackage<J> extends riDBObject {
    public String PID = "";
    public int tasksCount = 0; //Общее число задач
    @Description("DEFAULT 0")
    public int doneTasksCount = 0;
    //--
    public int kernels = 1;
    public int needsEmail = 0;
    public String version = ""; //версия тестируемого объекта
    public String drv = ""; //пусть к исполняемому файлы тестируемого объекта
    //--
    public int progress = 0; //прогресс выполнения
    public long StartDate = 0;
    public long ChangeDate = 0;
    @Description("DEFAULT 0")
    public int connectionErrosCount = 0;
    public TasksPackageState state = TasksPackageState.Draft;
    //json где хранятся задачи----------------------------------
    @Description("IGNORE")
    public J package_json = null;
    public TestingPackage(TestingPackage p) {
        SynchronizeFields(p);
    }
    public TestingPackage() {
    }
    public long getMilliseconds() {
        return ((StartDate != 0) && (ChangeDate != 0)) ? ChangeDate - StartDate : 0;
    }
    //--
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        TestingPackage tp = (TestingPackage) src;
        //--
        tasksCount = tp.tasksCount;
        doneTasksCount = tp.doneTasksCount;
        needsEmail = tp.needsEmail;
        version = tp.version;
        drv = tp.drv;
        PID = tp.PID;
        kernels = tp.kernels;
        progress = tp.progress;
        StartDate = tp.StartDate;
        ChangeDate = tp.ChangeDate;
        connectionErrosCount = tp.connectionErrosCount;
        state = tp.state;
        //--
    }
    public File getLocalWorkspace() {
        return new File(getHomeDirectory(), String.valueOf(id));
    }
    public boolean isLoaded() {
        return new File(getLocalWorkspace(), CommonConstants.LOADED).exists();
    }
    public abstract Class getJsonClass();
    public abstract File getHomeDirectory();
    public File getJsonFile() {
        return new File(getLocalWorkspace(), "package_json");
    }
    public File getLoadedFile() {
        return new File(getLocalWorkspace(), CommonConstants.LOADED);
    }
    public void saveJson() throws Exception {
        Utils_.jsonToFile(package_json, getJsonFile());
    }
    public void readJson() throws Exception {
        package_json = (J) Utils_.jsonFromFile(getJsonFile(), getJsonClass());
    }
    public void destructor() {
        package_json = null;
    }
    //----------------------------------------------------------
    public void saveConfigurations(Vector<? extends Configuration> configurations_in) {
    }
    //определить завершен пакет с ошибками или нет.
    public void checkFinishState() throws Exception {
    }
}
