diff --git a/src/main/java/org/openepics/names/service/DeviceGroupService.java b/src/main/java/org/openepics/names/service/DeviceGroupService.java new file mode 100644 index 0000000000000000000000000000000000000000..d3b22727905e3839ea6b7ea7cf6413f31154f1b3 --- /dev/null +++ b/src/main/java/org/openepics/names/service/DeviceGroupService.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2023 European Spallation Source ERIC. + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.openepics.names.service; + +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.openepics.names.repository.DeviceGroupRepository; +import org.openepics.names.repository.model.DeviceGroup; +import org.openepics.names.rest.beans.Status; +import org.openepics.names.rest.beans.element.StructureElement; +import org.openepics.names.rest.beans.element.StructureElementCommand; +import org.openepics.names.util.HolderSystemDeviceStructure; +import org.openepics.names.util.StructureChoice; +import org.openepics.names.util.StructureElementUtil; +import org.openepics.names.util.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * This class provides structures services for device group. + * + * @author Lars Johansson + */ +@Service +public class DeviceGroupService { + + private static final Logger LOGGER = Logger.getLogger(DeviceGroupService.class.getName()); + + private DeviceGroupRepository deviceGroupRepository; + + @Autowired + public DeviceGroupService( + DeviceGroupRepository deviceGroupRepository) { + this.deviceGroupRepository = deviceGroupRepository; + } + + @Transactional(propagation = Propagation.MANDATORY) + public StructureElement approveStructure(StructureElementCommand structureElement, + Date processed, String processedBy, + HolderSystemDeviceStructure holder) { + // validation outside method + // transaction + // do + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // return + // structure element for approved structure + + String uuid = structureElement.getUuid().toString(); + String processedComment = structureElement.getComment(); + + // find + List<DeviceGroup> deviceGroups = deviceGroupRepository.readDeviceGroups(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); + DeviceGroup deviceGroup = null; + if (ValidateUtil.isSize(deviceGroups, 1)) { + deviceGroup = deviceGroups.get(0); + + // update not latest + deviceGroup.setLatest(Boolean.FALSE); + deviceGroupRepository.updateDeviceGroup(deviceGroup); + } + + // find + deviceGroups = deviceGroupRepository.readDeviceGroups(Status.PENDING, null, uuid, null, null, null, null, null, null); + if (ValidateUtil.isNullOrNotSize(deviceGroups, 1)) { + return null; + } + deviceGroup = deviceGroups.get(0); + + // approve + deviceGroup.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); + deviceGroup.setLatest(Boolean.TRUE); + deviceGroupRepository.updateDeviceGroup(deviceGroup); + + LOGGER.log(Level.FINE, "approveStructure, structureElement: {0}", structureElement); + return StructureElementUtil.getStructureElementProcessed(deviceGroup, holder, StructureChoice.STRUCTURE); + } + +} diff --git a/src/main/java/org/openepics/names/service/DeviceTypeService.java b/src/main/java/org/openepics/names/service/DeviceTypeService.java new file mode 100644 index 0000000000000000000000000000000000000000..7f12429c159af2a9e65a276cc00ec0bb9638899f --- /dev/null +++ b/src/main/java/org/openepics/names/service/DeviceTypeService.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2023 European Spallation Source ERIC. + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.openepics.names.service; + +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.openepics.names.repository.DeviceTypeRepository; +import org.openepics.names.repository.model.DeviceType; +import org.openepics.names.rest.beans.Status; +import org.openepics.names.rest.beans.element.StructureElement; +import org.openepics.names.rest.beans.element.StructureElementCommand; +import org.openepics.names.util.HolderSystemDeviceStructure; +import org.openepics.names.util.StructureChoice; +import org.openepics.names.util.StructureElementUtil; +import org.openepics.names.util.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * This class provides structures services for device type. + * + * @author Lars Johansson + */ +@Service +public class DeviceTypeService { + + private static final Logger LOGGER = Logger.getLogger(DeviceTypeService.class.getName()); + + private DeviceTypeRepository deviceTypeRepository; + + @Autowired + public DeviceTypeService( + DeviceTypeRepository deviceTypeRepository) { + this.deviceTypeRepository = deviceTypeRepository; + } + + @Transactional(propagation = Propagation.MANDATORY) + public StructureElement approveStructure(StructureElementCommand structureElement, + Date processed, String processedBy, + HolderSystemDeviceStructure holder) { + // validation outside method + // transaction + // do + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // return + // structure element for approved structure + + String uuid = structureElement.getUuid().toString(); + String processedComment = structureElement.getComment(); + + // find + List<DeviceType> deviceTypes = deviceTypeRepository.readDeviceTypes(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); + DeviceType deviceType = null; + if (ValidateUtil.isSize(deviceTypes, 1)) { + deviceType = deviceTypes.get(0); + + // update not latest + deviceType.setLatest(Boolean.FALSE); + deviceTypeRepository.updateDeviceType(deviceType); + } + + // find + deviceTypes = deviceTypeRepository.readDeviceTypes(Status.PENDING, null, uuid, null, null, null, null, null, null); + if (ValidateUtil.isNullOrNotSize(deviceTypes, 1)) { + return null; + } + deviceType = deviceTypes.get(0); + + // approve + deviceType.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); + deviceType.setLatest(Boolean.TRUE); + deviceTypeRepository.updateDeviceType(deviceType); + + LOGGER.log(Level.FINE, "approveStructure, structureElement: {0}", structureElement); + return StructureElementUtil.getStructureElementProcessed(deviceType, holder, StructureChoice.STRUCTURE); + } + +} diff --git a/src/main/java/org/openepics/names/service/DisciplineService.java b/src/main/java/org/openepics/names/service/DisciplineService.java new file mode 100644 index 0000000000000000000000000000000000000000..b97c31e7b7796e41db628e7866d7b258ae5bea45 --- /dev/null +++ b/src/main/java/org/openepics/names/service/DisciplineService.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2023 European Spallation Source ERIC. + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.openepics.names.service; + +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.openepics.names.repository.DisciplineRepository; +import org.openepics.names.repository.model.Discipline; +import org.openepics.names.rest.beans.Status; +import org.openepics.names.rest.beans.element.StructureElement; +import org.openepics.names.rest.beans.element.StructureElementCommand; +import org.openepics.names.util.HolderSystemDeviceStructure; +import org.openepics.names.util.StructureChoice; +import org.openepics.names.util.StructureElementUtil; +import org.openepics.names.util.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * This class provides structures services for discipline. + * + * @author Lars Johansson + */ +@Service +public class DisciplineService { + + private static final Logger LOGGER = Logger.getLogger(DisciplineService.class.getName()); + + private DisciplineRepository disciplineRepository; + + @Autowired + public DisciplineService( + DisciplineRepository disciplineRepository) { + this.disciplineRepository = disciplineRepository; + } + + @Transactional(propagation = Propagation.MANDATORY) + public StructureElement approveStructure(StructureElementCommand structureElement, + Date processed, String processedBy, + HolderSystemDeviceStructure holder) { + // validation outside method + // transaction + // do + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // return + // structure element for approved structure + + String uuid = structureElement.getUuid().toString(); + String processedComment = structureElement.getComment(); + + List<Discipline> disciplines = disciplineRepository.readDisciplines(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null); + Discipline discipline = null; + if (ValidateUtil.isSize(disciplines, 1)) { + discipline = disciplines.get(0); + + // update not latest + discipline.setLatest(Boolean.FALSE); + disciplineRepository.updateDiscipline(discipline); + } + + // find + disciplines = disciplineRepository.readDisciplines(Status.PENDING, null, uuid, null, null, null, null, null); + if (ValidateUtil.isNullOrNotSize(disciplines, 1)) { + return null; + } + discipline = disciplines.get(0); + + // approve + discipline.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); + discipline.setLatest(Boolean.TRUE); + disciplineRepository.updateDiscipline(discipline); + + LOGGER.log(Level.FINE, "approveStructure, structureElement: {0}", structureElement); + return StructureElementUtil.getStructureElementProcessed(discipline, holder, StructureChoice.STRUCTURE); + } + +} diff --git a/src/main/java/org/openepics/names/service/NamesService.java b/src/main/java/org/openepics/names/service/NamesService.java index 4c1e1a8b1d0575ab6aac48a6611575b206ac4ea1..2cfaeb3073a306561e07afc987e1adc5a5ba726a 100644 --- a/src/main/java/org/openepics/names/service/NamesService.java +++ b/src/main/java/org/openepics/names/service/NamesService.java @@ -76,16 +76,16 @@ import com.google.common.collect.Lists; @Service public class NamesService { - // HolderIRepositories and HolderSystemDeviceStructure may or may not be used for preparation of what to return - // - // for each method - // document what values come from NameElement and what values come from persistence layer - // somehow provide this information to user - // // note // handling of system structure, device structure // parent system structure uuid (system group, system, subsystem) - ability to find structure // parent device structure uuid (device type) + // holder + // HolderIRepositories and HolderSystemDeviceStructure may or may not be used for preparation of what to return + // latest + // automatically not show names that do not come into play + // = automatically exclude (approved and not latest) + // otherwise refer to history private static final Logger LOGGER = Logger.getLogger(NamesService.class.getName()); diff --git a/src/main/java/org/openepics/names/service/StructuresService.java b/src/main/java/org/openepics/names/service/StructuresService.java index f9e74e810503793e613e157e7ba0f6c98c05243a..a2eb45e327cf0ed22b94853e7bd16f3a1f8431ef 100644 --- a/src/main/java/org/openepics/names/service/StructuresService.java +++ b/src/main/java/org/openepics/names/service/StructuresService.java @@ -48,8 +48,6 @@ import org.openepics.names.repository.model.SystemGroup; import org.openepics.names.rest.beans.FieldStructure; import org.openepics.names.rest.beans.Status; import org.openepics.names.rest.beans.Type; -import org.openepics.names.rest.beans.element.NameElement; -import org.openepics.names.rest.beans.element.NameElementCommand; import org.openepics.names.rest.beans.element.StructureElement; import org.openepics.names.rest.beans.element.StructureElementCommand; import org.openepics.names.rest.beans.response.ResponsePageStructureElements; @@ -62,7 +60,6 @@ import org.openepics.names.util.StructureElementUtil; import org.openepics.names.util.TextUtil; import org.openepics.names.util.Utilities; import org.openepics.names.util.ValidateStructureElementUtil; -import org.openepics.names.util.StructureUtil; import org.openepics.names.util.ValidateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -78,26 +75,28 @@ import com.google.common.collect.Lists; @Service public class StructuresService { - // HolderIRepositories and HolderSystemDeviceStructure may or may not be used for preparation of what to return - - // for each method - // document what values come from StructureElement and what values come from persistence layer - // somehow provide this information to user - - // latest - // automatically not show structures that do not come into play - // = automatically exclude (approved and not latest) - // otherwise refer to history + // note + // holder + // HolderIRepositories and HolderSystemDeviceStructure may or may not be used for preparation of what to return + // latest + // automatically not show structures that do not come into play + // = automatically exclude (approved and not latest) + // otherwise refer to history private static final Logger LOGGER = Logger.getLogger(StructuresService.class.getName()); - private static final String SYSTEM_STRUCTURE_ONLY = "System structure only"; + protected static final String SYSTEM_STRUCTURE_ONLY = "System structure only"; private EssNamingConvention namingConvention; private HolderIRepositories holderIRepositories; private HolderRepositories holderRepositories; - private NamesService namesService; + private SystemGroupService systemGroupService; + private SystemService systemService; + private SubsystemService subsystemService; + private DisciplineService disciplineService; + private DeviceGroupService deviceGroupService; + private DeviceTypeService deviceTypeService; @Autowired public StructuresService( @@ -115,7 +114,12 @@ public class StructuresService { DisciplineRepository disciplineRepository, DeviceGroupRepository deviceGroupRepository, DeviceTypeRepository deviceTypeRepository, - NamesService namesService) { + SystemGroupService systemGroupService, + SystemService systemService, + SubsystemService subsystemService, + DisciplineService disciplineService, + DeviceGroupService deviceGroupService, + DeviceTypeService deviceTypeService) { this.namingConvention = new EssNamingConvention(); this.holderIRepositories = new HolderIRepositories( @@ -134,7 +138,12 @@ public class StructuresService { disciplineRepository, deviceGroupRepository, deviceTypeRepository); - this.namesService = namesService; + this.systemGroupService = systemGroupService; + this.systemService = systemService; + this.subsystemService = subsystemService; + this.disciplineService = disciplineService; + this.deviceGroupService = deviceGroupService; + this.deviceTypeService = deviceTypeService; } @Transactional @@ -1038,13 +1047,14 @@ public class StructuresService { // transaction // do // for each structure element - // find - // update not latest - // find - // approve - update structure to status APPROVED, latest to true - // possibly validate that approved - // automatically create name when system structure is approved - // add + // approve structure + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // add to list // return // structure elements for approved structures @@ -1069,204 +1079,26 @@ public class StructuresService { String processedBy = TextUtil.TEST_WHO; final List<StructureElement> approvedStructureElements = Lists.newArrayList(); for (StructureElementCommand structureElement : structureElements) { - String uuid = structureElement.getUuid().toString(); Type type = structureElement.getType(); - String processedComment = structureElement.getComment(); if (Type.SYSTEMGROUP.equals(type)) { - // find - List<SystemGroup> systemGroups = holderRepositories.getSystemGroupRepository().readSystemGroups(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null); - SystemGroup systemGroup = null; - if (ValidateUtil.isSize(systemGroups, 1)) { - systemGroup = systemGroups.get(0); - - // update not latest - systemGroup.setLatest(Boolean.FALSE); - holderRepositories.getSystemGroupRepository().updateSystemGroup(systemGroup); - } - - // find - systemGroups = holderRepositories.getSystemGroupRepository().readSystemGroups(Status.PENDING, null, uuid, null, null, null, null, null); - if (ValidateUtil.isNullOrNotSize(systemGroups, 1)) { - continue; - } - systemGroup = systemGroups.get(0); - - // approve - systemGroup.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); - systemGroup.setLatest(Boolean.TRUE); - holderRepositories.getSystemGroupRepository().updateSystemGroup(systemGroup); - - // automatically create name when system structure is approved (after create) - // condition on name and structure entry - // system structure should exist (uuid), one entry that is not deleted - // name should not exist (system structure mnemonic) - // create name within current transaction - systemGroups = holderRepositories.getSystemGroupRepository().readSystemGroups(null, Boolean.FALSE, uuid, null, null, null, null, null); - boolean existsName = !StringUtils.isEmpty(systemGroup.getMnemonic()) && namesService.existsName(StructureUtil.getMnemonicPath(systemGroup, holder)); - if (ValidateUtil.isSize(systemGroups, 1) && !existsName) { - NameElementCommand nameElement = new NameElementCommand(null, systemGroup.getUuid(), null, null, SYSTEM_STRUCTURE_ONLY, null); - NameElement createdNameElement = namesService.createName(nameElement, holder, processed, processedBy); - LOGGER.log(Level.FINE, "approveStructures, nameElement created, name: {0}", createdNameElement.getName()); - } - - // add - approvedStructureElements.add(StructureElementUtil.getStructureElementProcessed(systemGroup, holder, StructureChoice.STRUCTURE)); + Utilities.addToCollection(approvedStructureElements, + systemGroupService.approveStructure(structureElement, processed, processedBy, holder)); } else if (Type.SYSTEM.equals(type)) { - // find - List<System> systems = holderRepositories.getSystemRepository().readSystems(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); - System system = null; - if (ValidateUtil.isSize(systems, 1)) { - system = systems.get(0); - - // update not latest - system.setLatest(Boolean.FALSE); - holderRepositories.getSystemRepository().updateSystem(system); - } - - // find - systems = holderRepositories.getSystemRepository().readSystems(Status.PENDING, null, uuid, null, null, null, null, null, null); - if (ValidateUtil.isNullOrNotSize(systems, 1)) { - continue; - } - system = systems.get(0); - - // approve - system.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); - system.setLatest(Boolean.TRUE); - holderRepositories.getSystemRepository().updateSystem(system); - - // automatically create name when system structure is approved (after create) - // condition on name and structure entry - // system structure should exist (uuid), one entry that is not deleted - // name should not exist (system structure mnemonic) - // create name within current transaction - systems = holderRepositories.getSystemRepository().readSystems(null, Boolean.FALSE, uuid, null, null, null, null, null, null); - boolean existsName = !StringUtils.isEmpty(system.getMnemonic()) && namesService.existsName(StructureUtil.getMnemonicPath(system, holder)); - if (ValidateUtil.isSize(systems, 1) && !existsName) { - NameElementCommand nameElement = new NameElementCommand(null, system.getUuid(), null, null, SYSTEM_STRUCTURE_ONLY, null); - NameElement createdNameElement = namesService.createName(nameElement, holder, processed, processedBy); - LOGGER.log(Level.FINE, "approveStructures, nameElement created, name: {0}", createdNameElement.getName()); - } - - // add - approvedStructureElements.add(StructureElementUtil.getStructureElementProcessed(system, holder, StructureChoice.STRUCTURE)); + Utilities.addToCollection(approvedStructureElements, + systemService.approveStructure(structureElement, processed, processedBy, holder)); } else if (Type.SUBSYSTEM.equals(type)) { - // find - List<Subsystem> subsystems = holderRepositories.getSubsystemRepository().readSubsystems(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); - Subsystem subsystem = null; - if (ValidateUtil.isSize(subsystems, 1)) { - subsystem = subsystems.get(0); - - // update not latest - subsystem.setLatest(Boolean.FALSE); - holderRepositories.getSubsystemRepository().updateSubsystem(subsystem); - } - - // find - subsystems = holderRepositories.getSubsystemRepository().readSubsystems(Status.PENDING, null, uuid, null, null, null, null, null, null); - if (ValidateUtil.isNullOrNotSize(subsystems, 1)) { - continue; - } - subsystem = subsystems.get(0); - - // approve - subsystem.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); - subsystem.setLatest(Boolean.TRUE); - holderRepositories.getSubsystemRepository().updateSubsystem(subsystem); - - // automatically create name when system structure is approved (after create) - // condition on name and structure entry - // system structure should exist (uuid), one entry that is not deleted - // name should not exist (system structure mnemonic) - // create name within current transaction - subsystems = holderRepositories.getSubsystemRepository().readSubsystems(null, Boolean.FALSE, uuid, null, null, null, null, null, null); - boolean existsName = !StringUtils.isEmpty(subsystem.getMnemonic()) && namesService.existsName(StructureUtil.getMnemonicPath(subsystem, holder)); - if (ValidateUtil.isSize(subsystems, 1) && !existsName) { - NameElementCommand nameElement = new NameElementCommand(null, subsystem.getUuid(), null, null, SYSTEM_STRUCTURE_ONLY, null); - NameElement createdNameElement = namesService.createName(nameElement, holder, processed, processedBy); - LOGGER.log(Level.FINE, "approveStructures, nameElement created, name: {0}", createdNameElement.getName()); - } - - // add - approvedStructureElements.add(StructureElementUtil.getStructureElementProcessed(subsystem, holder, StructureChoice.STRUCTURE)); + Utilities.addToCollection(approvedStructureElements, + subsystemService.approveStructure(structureElement, processed, processedBy, holder)); } else if (Type.DISCIPLINE.equals(type)) { - List<Discipline> disciplines = holderRepositories.getDisciplineRepository().readDisciplines(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null); - Discipline discipline = null; - if (ValidateUtil.isSize(disciplines, 1)) { - discipline = disciplines.get(0); - - // update not latest - discipline.setLatest(Boolean.FALSE); - holderRepositories.getDisciplineRepository().updateDiscipline(discipline); - } - - // find - disciplines = holderRepositories.getDisciplineRepository().readDisciplines(Status.PENDING, null, uuid, null, null, null, null, null); - if (ValidateUtil.isNullOrNotSize(disciplines, 1)) { - continue; - } - discipline = disciplines.get(0); - - // approve - discipline.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); - discipline.setLatest(Boolean.TRUE); - holderRepositories.getDisciplineRepository().updateDiscipline(discipline); - - // add - approvedStructureElements.add(StructureElementUtil.getStructureElementProcessed(discipline, holder, StructureChoice.STRUCTURE)); + Utilities.addToCollection(approvedStructureElements, + disciplineService.approveStructure(structureElement, processed, processedBy, holder)); } else if (Type.DEVICEGROUP.equals(type)) { - // find - List<DeviceGroup> deviceGroups = holderRepositories.getDeviceGroupRepository().readDeviceGroups(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); - DeviceGroup deviceGroup = null; - if (ValidateUtil.isSize(deviceGroups, 1)) { - deviceGroup = deviceGroups.get(0); - - // update not latest - deviceGroup.setLatest(Boolean.FALSE); - holderRepositories.getDeviceGroupRepository().updateDeviceGroup(deviceGroup); - } - - // find - deviceGroups = holderRepositories.getDeviceGroupRepository().readDeviceGroups(Status.PENDING, null, uuid, null, null, null, null, null, null); - if (ValidateUtil.isNullOrNotSize(deviceGroups, 1)) { - continue; - } - deviceGroup = deviceGroups.get(0); - - // approve - deviceGroup.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); - deviceGroup.setLatest(Boolean.TRUE); - holderRepositories.getDeviceGroupRepository().updateDeviceGroup(deviceGroup); - - // add - approvedStructureElements.add(StructureElementUtil.getStructureElementProcessed(deviceGroup, holder, StructureChoice.STRUCTURE)); + Utilities.addToCollection(approvedStructureElements, + deviceGroupService.approveStructure(structureElement, processed, processedBy, holder)); } else if (Type.DEVICETYPE.equals(structureElement.getType())) { - // find - List<DeviceType> deviceTypes = holderRepositories.getDeviceTypeRepository().readDeviceTypes(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); - DeviceType deviceType = null; - if (ValidateUtil.isSize(deviceTypes, 1)) { - deviceType = deviceTypes.get(0); - - // update not latest - deviceType.setLatest(Boolean.FALSE); - holderRepositories.getDeviceTypeRepository().updateDeviceType(deviceType); - } - - // find - deviceTypes = holderRepositories.getDeviceTypeRepository().readDeviceTypes(Status.PENDING, null, uuid, null, null, null, null, null, null); - if (ValidateUtil.isNullOrNotSize(deviceTypes, 1)) { - continue; - } - deviceType = deviceTypes.get(0); - - // approve - deviceType.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); - deviceType.setLatest(Boolean.TRUE); - holderRepositories.getDeviceTypeRepository().updateDeviceType(deviceType); - - // add - approvedStructureElements.add(StructureElementUtil.getStructureElementProcessed(deviceType, holder, StructureChoice.STRUCTURE)); + Utilities.addToCollection(approvedStructureElements, + deviceTypeService.approveStructure(structureElement, processed, processedBy, holder)); } } diff --git a/src/main/java/org/openepics/names/service/SubsystemService.java b/src/main/java/org/openepics/names/service/SubsystemService.java new file mode 100644 index 0000000000000000000000000000000000000000..e6d73a4079ef419209cc7d9f5b7891d5c1e6b05c --- /dev/null +++ b/src/main/java/org/openepics/names/service/SubsystemService.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2023 European Spallation Source ERIC. + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.openepics.names.service; + +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.lang3.StringUtils; +import org.openepics.names.repository.SubsystemRepository; +import org.openepics.names.repository.model.Subsystem; +import org.openepics.names.rest.beans.Status; +import org.openepics.names.rest.beans.element.NameElement; +import org.openepics.names.rest.beans.element.NameElementCommand; +import org.openepics.names.rest.beans.element.StructureElement; +import org.openepics.names.rest.beans.element.StructureElementCommand; +import org.openepics.names.util.HolderSystemDeviceStructure; +import org.openepics.names.util.StructureChoice; +import org.openepics.names.util.StructureElementUtil; +import org.openepics.names.util.StructureUtil; +import org.openepics.names.util.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * This class provides structures services for subsystem. + * + * @author Lars Johansson + */ +@Service +public class SubsystemService { + + private static final Logger LOGGER = Logger.getLogger(SubsystemService.class.getName()); + + private SubsystemRepository subsystemRepository; + private NamesService namesService; + + @Autowired + public SubsystemService( + SubsystemRepository subsystemRepository, + NamesService namesService) { + this.subsystemRepository = subsystemRepository; + this.namesService = namesService; + } + + @Transactional(propagation = Propagation.MANDATORY) + public StructureElement approveStructure(StructureElementCommand structureElement, + Date processed, String processedBy, + HolderSystemDeviceStructure holder) { + // validation outside method + // transaction + // do + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // return + // structure element for approved structure + + String uuid = structureElement.getUuid().toString(); + String processedComment = structureElement.getComment(); + + // find + List<Subsystem> subsystems = subsystemRepository.readSubsystems(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); + Subsystem subsystem = null; + if (ValidateUtil.isSize(subsystems, 1)) { + subsystem = subsystems.get(0); + + // update not latest + subsystem.setLatest(Boolean.FALSE); + subsystemRepository.updateSubsystem(subsystem); + } + + // find + subsystems = subsystemRepository.readSubsystems(Status.PENDING, null, uuid, null, null, null, null, null, null); + if (ValidateUtil.isNullOrNotSize(subsystems, 1)) { + return null; + } + subsystem = subsystems.get(0); + + // approve + subsystem.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); + subsystem.setLatest(Boolean.TRUE); + subsystemRepository.updateSubsystem(subsystem); + + // automatically create name when system structure is approved (after create) + // condition on name and structure entry + // system structure should exist (uuid), one entry that is not deleted + // name should not exist (system structure mnemonic) + // create name within current transaction + subsystems = subsystemRepository.readSubsystems(null, Boolean.FALSE, uuid, null, null, null, null, null, null); + boolean existsName = !StringUtils.isEmpty(subsystem.getMnemonic()) && namesService.existsName(StructureUtil.getMnemonicPath(subsystem, holder)); + if (ValidateUtil.isSize(subsystems, 1) && !existsName) { + NameElementCommand nameElement = new NameElementCommand(null, subsystem.getUuid(), null, null, StructuresService.SYSTEM_STRUCTURE_ONLY, null); + NameElement createdNameElement = namesService.createName(nameElement, holder, processed, processedBy); + LOGGER.log(Level.FINE, "approveStructures, nameElement created, name: {0}", createdNameElement.getName()); + } + + LOGGER.log(Level.FINE, "approveStructure, structureElement: {0}", structureElement); + return StructureElementUtil.getStructureElementProcessed(subsystem, holder, StructureChoice.STRUCTURE); + } + +} diff --git a/src/main/java/org/openepics/names/service/SystemGroupService.java b/src/main/java/org/openepics/names/service/SystemGroupService.java new file mode 100644 index 0000000000000000000000000000000000000000..cc829e0ee2d862837a9c4921275edab554871a45 --- /dev/null +++ b/src/main/java/org/openepics/names/service/SystemGroupService.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2023 European Spallation Source ERIC. + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.openepics.names.service; + +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.lang3.StringUtils; +import org.openepics.names.repository.SystemGroupRepository; +import org.openepics.names.repository.model.SystemGroup; +import org.openepics.names.rest.beans.Status; +import org.openepics.names.rest.beans.element.NameElement; +import org.openepics.names.rest.beans.element.NameElementCommand; +import org.openepics.names.rest.beans.element.StructureElement; +import org.openepics.names.rest.beans.element.StructureElementCommand; +import org.openepics.names.util.HolderSystemDeviceStructure; +import org.openepics.names.util.StructureChoice; +import org.openepics.names.util.StructureElementUtil; +import org.openepics.names.util.StructureUtil; +import org.openepics.names.util.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * This class provides structures services for system group. + * + * @author Lars Johansson + */ +@Service +public class SystemGroupService { + + private static final Logger LOGGER = Logger.getLogger(SystemGroupService.class.getName()); + + private SystemGroupRepository systemGroupRepository; + private NamesService namesService; + + @Autowired + public SystemGroupService( + SystemGroupRepository systemGroupRepository, + NamesService namesService) { + this.systemGroupRepository = systemGroupRepository; + this.namesService = namesService; + } + + @Transactional(propagation = Propagation.MANDATORY) + public StructureElement approveStructure(StructureElementCommand structureElement, + Date processed, String processedBy, + HolderSystemDeviceStructure holder) { + // validation outside method + // transaction + // do + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // return + // structure element for approved structure + + String uuid = structureElement.getUuid().toString(); + String processedComment = structureElement.getComment(); + + // find + List<SystemGroup> systemGroups = systemGroupRepository.readSystemGroups(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null); + SystemGroup systemGroup = null; + if (ValidateUtil.isSize(systemGroups, 1)) { + systemGroup = systemGroups.get(0); + + // update not latest + systemGroup.setLatest(Boolean.FALSE); + systemGroupRepository.updateSystemGroup(systemGroup); + } + + // find + systemGroups = systemGroupRepository.readSystemGroups(Status.PENDING, null, uuid, null, null, null, null, null); + if (ValidateUtil.isNullOrNotSize(systemGroups, 1)) { + return null; + } + systemGroup = systemGroups.get(0); + + // approve + systemGroup.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); + systemGroup.setLatest(Boolean.TRUE); + systemGroupRepository.updateSystemGroup(systemGroup); + + // automatically create name when system structure is approved (after create) + // condition on name and structure entry + // system structure should exist (uuid), one entry that is not deleted + // name should not exist (system structure mnemonic) + // create name within current transaction + systemGroups = systemGroupRepository.readSystemGroups(null, Boolean.FALSE, uuid, null, null, null, null, null); + boolean existsName = !StringUtils.isEmpty(systemGroup.getMnemonic()) && namesService.existsName(StructureUtil.getMnemonicPath(systemGroup, holder)); + if (ValidateUtil.isSize(systemGroups, 1) && !existsName) { + NameElementCommand nameElement = new NameElementCommand(null, systemGroup.getUuid(), null, null, StructuresService.SYSTEM_STRUCTURE_ONLY, null); + NameElement createdNameElement = namesService.createName(nameElement, holder, processed, processedBy); + LOGGER.log(Level.FINE, "approveStructure, nameElement created, name: {0}", createdNameElement.getName()); + } + + LOGGER.log(Level.FINE, "approveStructure, structureElement: {0}", structureElement); + return StructureElementUtil.getStructureElementProcessed(systemGroup, holder, StructureChoice.STRUCTURE); + } + +} diff --git a/src/main/java/org/openepics/names/service/SystemService.java b/src/main/java/org/openepics/names/service/SystemService.java new file mode 100644 index 0000000000000000000000000000000000000000..870dc869dea3748eb725e66a6bf229d4ec3a739e --- /dev/null +++ b/src/main/java/org/openepics/names/service/SystemService.java @@ -0,0 +1,124 @@ +/* + * Copyright (C) 2023 European Spallation Source ERIC. + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.openepics.names.service; + +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.lang3.StringUtils; +import org.openepics.names.repository.SystemRepository; +import org.openepics.names.repository.model.System; +import org.openepics.names.rest.beans.Status; +import org.openepics.names.rest.beans.element.NameElement; +import org.openepics.names.rest.beans.element.NameElementCommand; +import org.openepics.names.rest.beans.element.StructureElement; +import org.openepics.names.rest.beans.element.StructureElementCommand; +import org.openepics.names.util.HolderSystemDeviceStructure; +import org.openepics.names.util.StructureChoice; +import org.openepics.names.util.StructureElementUtil; +import org.openepics.names.util.StructureUtil; +import org.openepics.names.util.ValidateUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * This class provides structures services for system. + * + * @author Lars Johansson + */ +@Service +public class SystemService { + + private static final Logger LOGGER = Logger.getLogger(SystemService.class.getName()); + + private SystemRepository systemRepository; + private NamesService namesService; + + @Autowired + public SystemService( + SystemRepository systemRepository, + NamesService namesService) { + this.systemRepository = systemRepository; + this.namesService = namesService; + } + + @Transactional(propagation = Propagation.MANDATORY) + public StructureElement approveStructure(StructureElementCommand structureElement, + Date processed, String processedBy, + HolderSystemDeviceStructure holder) { + // validation outside method + // transaction + // do + // find + // update not latest + // find + // approve - update structure to status APPROVED, latest to true + // possibly validate that approved + // automatically create name when system structure is approved + // return + // structure element for approved structure + + String uuid = structureElement.getUuid().toString(); + String processedComment = structureElement.getComment(); + + // find + List<System> systems = systemRepository.readSystems(Status.APPROVED, Boolean.FALSE, uuid, null, null, null, null, null, null); + System system = null; + if (ValidateUtil.isSize(systems, 1)) { + system = systems.get(0); + + // update not latest + system.setLatest(Boolean.FALSE); + systemRepository.updateSystem(system); + } + + // find + systems = systemRepository.readSystems(Status.PENDING, null, uuid, null, null, null, null, null, null); + if (ValidateUtil.isNullOrNotSize(systems, 1)) { + return null; + } + system = systems.get(0); + + // approve + system.setAttributesStatusProcessed(Status.APPROVED, processed, processedBy, processedComment); + system.setLatest(Boolean.TRUE); + systemRepository.updateSystem(system); + + // automatically create name when system structure is approved (after create) + // condition on name and structure entry + // system structure should exist (uuid), one entry that is not deleted + // name should not exist (system structure mnemonic) + // create name within current transaction + systems = systemRepository.readSystems(null, Boolean.FALSE, uuid, null, null, null, null, null, null); + boolean existsName = !StringUtils.isEmpty(system.getMnemonic()) && namesService.existsName(StructureUtil.getMnemonicPath(system, holder)); + if (ValidateUtil.isSize(systems, 1) && !existsName) { + NameElementCommand nameElement = new NameElementCommand(null, system.getUuid(), null, null, StructuresService.SYSTEM_STRUCTURE_ONLY, null); + NameElement createdNameElement = namesService.createName(nameElement, holder, processed, processedBy); + LOGGER.log(Level.FINE, "approveStructure, nameElement created, name: {0}", createdNameElement.getName()); + } + + LOGGER.log(Level.FINE, "approveStructure, structureElement: {0}", structureElement); + return StructureElementUtil.getStructureElementProcessed(system, holder, StructureChoice.STRUCTURE); + } + +} diff --git a/src/main/java/org/openepics/names/util/Utilities.java b/src/main/java/org/openepics/names/util/Utilities.java index c4f6fb25d2bee8237d57e5f1cdef562650b9cbb3..e2b1eb0a1c04a40541772398a81ed3e8383b096d 100644 --- a/src/main/java/org/openepics/names/util/Utilities.java +++ b/src/main/java/org/openepics/names/util/Utilities.java @@ -61,4 +61,16 @@ public class Utilities { : 0; } + /** + * Add value to collection and return if operation was successful or not. + * + * @param <T> type parameter + * @param collection collection + * @param value value + * @return if operation was successful or not + */ + public static <T> boolean addToCollection(Collection<T> collection, T value) { + return collection != null && value != null && collection.add(value); + } + } diff --git a/src/test/java/org/openepics/names/util/UtilitiesTest.java b/src/test/java/org/openepics/names/util/UtilitiesTest.java index 3d85986e5549c4200ef49e3ac0d288f2b40e24e1..9830c533a63330f9bbcee64b9d54e44be86e68cf 100644 --- a/src/test/java/org/openepics/names/util/UtilitiesTest.java +++ b/src/test/java/org/openepics/names/util/UtilitiesTest.java @@ -19,12 +19,15 @@ package org.openepics.names.util; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Collections; import java.util.List; import org.junit.jupiter.api.Test; import org.openepics.names.rest.beans.Type; +import org.openepics.names.rest.beans.element.StructureElement; import com.google.common.collect.Lists; @@ -96,4 +99,37 @@ class UtilitiesTest { assertEquals(6, Utilities.getLength(Type.DEVICETYPE, Type.DEVICEGROUP, Type.DISCIPLINE, Type.SUBSYSTEM, Type.SYSTEM, Type.SYSTEMGROUP)); } + /** + * Test of adding value to collection. + */ + @Test + void addToCollection() { + List<StructureElement> list = null; + StructureElement structureElement = null; + assertFalse(Utilities.addToCollection(list, structureElement)); + + structureElement = StructureElementUtil.getStructureElement( + null, + null, + null, + null, null, null, null, + null, null, null, null, + null, null, null); + assertFalse(Utilities.addToCollection(list, structureElement)); + + list = Lists.newArrayList(); + structureElement = null; + assertFalse(Utilities.addToCollection(list, structureElement)); + + structureElement = StructureElementUtil.getStructureElement( + null, + null, + null, + null, null, null, null, + null, null, null, null, + null, null, null); + assertTrue(Utilities.addToCollection(list, structureElement)); + assertEquals(1, list.size()); + } + }