Files
VisualSapfor/src/TestingSystem/Common/Test/Test.java
2023-11-24 12:28:12 +03:00

134 lines
4.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package TestingSystem.Common.Test;
import Common.Constants;
import Common.Current;
import Common.Database.DBObject;
import Common.Database.riDBObject;
import Common.Global;
import Common.UI.UI;
import Common.Utils.Utils;
import ProjectData.Project.db_project_info;
import Repository.RepositoryRefuseException;
import Visual_DVM_2021.Passes.All.UnzipFolderPass;
import Visual_DVM_2021.Passes.All.ZipFolderPass;
import Visual_DVM_2021.Passes.PassException;
import com.sun.org.glassfish.gmbal.Description;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class Test extends riDBObject {
@Description("DEFAULT 1")
public int min_dim = 1; //мин размерность теста.
@Description("DEFAULT 1")
public int max_dim = 1; //макс размерность теста.
@Description("DEFAULT ''")
public String args = ""; //аргументы командной строки. на всякий случай поле зарезервирую. пусть будут.
@Description("DEFAULT -1")
public int group_id = Constants.Nan;
@Override
public void SynchronizeFields(DBObject src) {
super.SynchronizeFields(src);
Test t = (Test) src;
min_dim = t.min_dim;
max_dim = t.max_dim;
args = t.args;
group_id = t.group_id;
}
@Description("DEFAULT ''")
public String files = ""; //файлы теста
//--------------------------------------------->>>
@Description("IGNORE")
public String temp_project_name = "";
@Description("IGNORE")
public byte[] project_archive_bytes = null;
//--------------------------------------------->>>
public Test(Test test) {
this.SynchronizeFields(test);
}
public Test() {
}
@Override
public void select(boolean flag) {
super.select(flag);
if (Current.hasUI())
UI.getMainWindow().ShowCheckedTestsCount();
}
//---
@Override
public boolean isVisible() {
return Current.HasGroup() && (Current.getGroup().id == group_id);
}
//-
public File getArchive() {
return new File(Global.TestsDirectory, id + ".zip");
}
//-
public File getServerPath() {
return new File(Global.TestsDirectory, String.valueOf(id));
}
public File getHomePath() {
return new File(Global.visualiser.getWorkspace(), String.valueOf(id));
}
//--
public File getTempArchive() {
return new File(Global.TempDirectory, temp_project_name + ".zip");
}
public File getTempProject() {
return new File(Global.TempDirectory, temp_project_name);
}
//--
public db_project_info packCode(File dir) throws Exception {
temp_project_name = Utils.getDateName("test");
//-
File tempProject = getTempProject();
File tempArchive = getTempArchive();
//- создать бд.
FileUtils.forceMkdir(tempProject);
FileUtils.copyDirectory(dir, tempProject);
//---
db_project_info project = new db_project_info(tempProject);
project.Open();
project.Close();
project.clearData();
//--
ZipFolderPass zip = new ZipFolderPass();
if (zip.Do(tempProject.getAbsolutePath(), tempArchive.getAbsolutePath())) {
project_archive_bytes = Utils.packFile(tempArchive);
} else throw new PassException("Не удалось создать архив папки с кодом.");
return project;
}
public boolean unpackProjectOnServer() throws Exception {
File tmpArchive = new File(Global.TempDirectory, temp_project_name + ".zip");
File tmpProject = new File(Global.TempDirectory, temp_project_name);
File testProject = new File(Global.TestsDirectory, String.valueOf(id));
File testArchive = new File(Global.TestsDirectory, id + ".zip");
//--
if (tmpArchive.exists())
FileUtils.forceDelete(tmpArchive);
if (tmpProject.exists())
FileUtils.forceDelete(tmpProject);
if (testProject.exists())
FileUtils.forceDelete(testProject);
if (testArchive.exists())
FileUtils.forceDelete(testArchive);
//--
Utils.unpackFile(project_archive_bytes, tmpArchive); // распаковка байтов.
//--
UnzipFolderPass unzipFolderPass = new UnzipFolderPass();
if (!unzipFolderPass.Do(
tmpArchive.getAbsolutePath(),
Global.TempDirectory.getAbsolutePath())) {
return false;
}
//--
FileUtils.moveDirectory(tmpProject, testProject);
//--
ZipFolderPass zip = new ZipFolderPass();
if (!zip.Do(testProject.getAbsolutePath(), testArchive.getAbsolutePath()))
throw new RepositoryRefuseException("Не удалось переписать архив проекта");
return true;
}
public String getFilesForTable() {
return files.replace("\n", ";");
}
}