рефакторинг. прописал стандартную проверку существования объекта в бд сервера

This commit is contained in:
2025-03-21 12:56:12 +03:00
parent 637e9c5c0f
commit 2d0d69fd7a
4 changed files with 46 additions and 88 deletions

View File

@@ -88,7 +88,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
public void StartAction() throws Exception {
try {
//--
// db.Patch();
// db.Patch();
//--
machines_db = new MachinesDatabase();
machines_db.Activate();
@@ -143,15 +143,12 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
@Override
protected void afterCloneAction(riDBObject src, riDBObject dst) throws Exception {
if (src instanceof SapforSettings) {
Vector<PassCode> codes = new Vector<>();
for (SapforSettingsCommand command : db.sapforSettingsCommands.Data.values())
if (command.sapforsettings_id == src.id) codes.add(command.passCode);
//--
for (PassCode code : codes) {
SapforSettingsCommand command = new SapforSettingsCommand();
command.sapforsettings_id = dst.id;
command.passCode = code;
db.Insert(command);
SapforSettings sapforSettings = (SapforSettings) src;
for (SapforSettingsCommand src_command : db.getVectorByFK(sapforSettings, SapforSettingsCommand.class)) {
SapforSettingsCommand dst_command = new SapforSettingsCommand();
dst_command.sapforsettings_id = dst.id;
dst_command.passCode = src_command.passCode;
db.Insert(dst_command);
}
}
}
@@ -291,9 +288,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
void PerformAutoSapforTesting() throws Exception {
int sapforId = Integer.parseInt(request.arg);
if (!db.serverSapfors.containsKey(sapforId)) {
throw new RepositoryRefuseException("Версия SAPFOR " + sapforId + " не существует.");
}
checkExistense(sapforId, ServerSapfor.class);
ServerSapfor sapfor = db.serverSapfors.get(sapforId);
TextLog Log = new TextLog();
SapforPackage autoPackage = tryAutoSapforTesting(sapfor, Log);
@@ -310,16 +305,14 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
void DownloadTest() throws Exception {
int test_id = Integer.parseInt(request.arg);
if (db.tests.containsKey(test_id)) {
Test test = db.tests.get(test_id);
File archive = new File(Global.TempDirectory, Utils_.getDateName(String.valueOf(test.id)));
ZipFolderPass zipFolderPass = new ZipFolderPass();
if (!zipFolderPass.Do(test.getServerPath().getAbsolutePath(), archive.getAbsolutePath()))
throw new RepositoryRefuseException("Не удалось заархивировать тест");
response = new ServerExchangeUnit_2021(ServerCode.OK, "", Utils_.fileToBytes(archive));
FileUtils.forceDelete(archive);
} else
throw new RepositoryRefuseException("Теста с именем " + request.arg + " не существует");
checkExistense(test_id, Test.class);
Test test = db.tests.get(test_id);
File archive = new File(Global.TempDirectory, Utils_.getDateName(String.valueOf(test.id)));
ZipFolderPass zipFolderPass = new ZipFolderPass();
if (!zipFolderPass.Do(test.getServerPath().getAbsolutePath(), archive.getAbsolutePath()))
throw new RepositoryRefuseException("Не удалось заархивировать тест");
response = new ServerExchangeUnit_2021(ServerCode.OK, "", Utils_.fileToBytes(archive));
FileUtils.forceDelete(archive);
}
void ReceiveTestsDatabase() throws Exception {
response.object = Utils_.fileToBytes(db.getFile());
@@ -449,8 +442,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
Vector<Integer> ids = (Vector<Integer>) request.object;
Vector<Pair<Integer, Pair<byte[], byte[]>>> res = new Vector<>();
for (int dvmPackage_id : ids) {
if (!db.dvmPackages.containsKey(dvmPackage_id))
throw new RepositoryRefuseException("Не найдено пакета тестирования DVM с ключом " + dvmPackage_id);
checkExistense(dvmPackage_id, DVMPackage.class);
DVMPackage dvmPackage = db.dvmPackages.get(dvmPackage_id);
File workspace = dvmPackage.getLocalWorkspace();
File results_zip = new File(workspace, "results.zip");
@@ -461,8 +453,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
void DownloadSapforPackage() throws Exception {
int sapforPackage_id = (int) request.object;
if (!db.sapforPackages.containsKey(sapforPackage_id))
throw new RepositoryRefuseException("Не найдено пакета тестирования SAPFOR с ключом " + sapforPackage_id);
checkExistense(sapforPackage_id, SapforPackage.class);
SapforPackage sapforPackage = db.sapforPackages.get(sapforPackage_id);
File workspace = sapforPackage.getLocalWorkspace();
File results_zip = Utils.getTempFileName("results");
@@ -544,25 +535,6 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
ex.printStackTrace();
}
}
void GetSapforActualVersion() throws Exception {
File versionFile = new File(Global.TempDirectory, "version.h");
if (versionFile.exists())
FileUtils.forceDelete(versionFile);
//1. Получить версию из репозитория.
Utils.startScript(Global.TempDirectory,
Global.TempDirectory,
"get_version",
"wget --user dvmhuser --password dvmh2013 -P " +
Utils_.DQuotes(Global.TempDirectory.getAbsolutePath()) +
" http://svn.dvm-system.org/svn/dvmhrepo/sapfor/experts/Sapfor_2017/_src/Utils/version.h"
).waitFor();
if (!versionFile.exists())
throw new RepositoryRefuseException("Не удалось загрузить текущую версию SAPFOR из репозитория!");
int current_version = Sapfor.readVersionFromCode(versionFile);
int max_installed_version = db.getInstalledSapforMaxVersion();
if (max_installed_version == current_version)
throw new RepositoryRefuseException("Версия " + max_installed_version + " уже установлена");
}
//---
int getSapforActualVersion() throws Exception {
File versionFile = new File(Global.TempDirectory, "version.h");
@@ -651,10 +623,8 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
String[] packed = request.arg.split("\n");
int groupId = Integer.parseInt(packed[0]);
int packageId = Integer.parseInt(packed[1]);
if (!db.sapforPackages.containsKey(packageId))
throw new RepositoryRefuseException("Пакета с ключом " + packageId + " не существует!");
if (!db.groups.containsKey(groupId))
throw new RepositoryRefuseException("Группы с ключом " + groupId + " не существует!");
checkExistense(packageId, SapforPackage.class);
checkExistense(groupId, Group.class);
//--
SapforPackage sapforPackage = db.sapforPackages.get(packageId);
Group group = db.groups.get(groupId);
@@ -692,7 +662,6 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
//создание тестов.
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
Vector<Test> tests = new Vector<>();
for (String name : versions.keySet()) {
File src = versions.get(name);
File[] files = src.listFiles(pathname -> !pathname.isDirectory());
@@ -725,7 +694,6 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
FileUtils.forceDelete(testProject);
FileUtils.copyDirectory(src, testProject);
//---
tests.add(test);
db.DetectTestMinMaxDim(serverSapfor, group, test);
}
}
@@ -734,9 +702,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
Pair<DVMPackage, byte[]> p = (Pair<DVMPackage, byte[]>) request.object;
DVMPackage src = p.getKey();
byte[] packed_file = p.getValue();
//todo написать стандартное исключение на несуществование объекта с ключом
if (!db.dvmPackages.containsKey(src.id))
throw new RepositoryRefuseException("не существует пакета с ключом " + src.id);
checkExistense(src.id, DVMPackage.class);
DVMPackage dst = db.dvmPackages.get(src.id);
dst.SynchronizeFields(src);
db.Update(dst);
@@ -745,8 +711,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
void DetectTestsMinMaxDim() throws Exception {
Vector<Object> keys = (Vector<Object>) request.object;
for (Object key : keys) {
if (!db.tests.containsKey(key))
throw new RepositoryRefuseException("не существует пакета с ключом " + key);
checkExistense(key, Test.class);
}
ServerSapfor serverSapfor = db.serverSapfors.getLastDoneVersion();
for (Object key : keys) {
@@ -757,8 +722,7 @@ public class TestingServer extends RepositoryServer<TestsDatabase> {
}
void GetDVMPackageCredentials() throws Exception {
int package_id = (int) request.object;
if (!db.dvmPackages.containsKey(package_id))
throw new RepositoryRefuseException("Не найден пакет с ключом " + Utils_.Brackets(package_id));
checkExistense(package_id,DVMPackage.class);
DVMPackageCredentials res = machines_db.dvmPackagesCredentials.getForPackageId(package_id);
if (res == null)
throw new RepositoryRefuseException("Не найдено учетных данных для пакета " + Utils_.Brackets(package_id));