dnet-applications/apps/dnet-exporter-api/src/main/java/eu/dnetlib/openaire/community/CommunityService.java

555 lines
19 KiB
Java
Raw Normal View History

2023-06-28 15:28:12 +02:00
package eu.dnetlib.openaire.community;
2023-06-12 14:30:42 +02:00
2023-07-10 10:42:11 +02:00
import java.time.LocalDateTime;
2023-06-19 15:47:48 +02:00
import java.util.Arrays;
import java.util.LinkedHashSet;
2023-06-12 14:47:27 +02:00
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
2023-06-14 13:53:59 +02:00
import java.util.stream.Collectors;
2023-06-12 14:47:27 +02:00
import javax.transaction.Transactional;
2023-07-10 10:42:11 +02:00
import org.apache.commons.lang3.StringUtils;
2023-06-30 10:46:20 +02:00
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
2023-06-12 14:30:42 +02:00
import org.springframework.beans.factory.annotation.Autowired;
2023-06-12 14:47:27 +02:00
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2023-06-20 09:10:14 +02:00
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
2023-06-12 14:30:42 +02:00
import org.springframework.stereotype.Service;
2023-06-28 15:28:12 +02:00
import eu.dnetlib.openaire.community.model.DbCommunity;
import eu.dnetlib.openaire.community.model.DbDatasource;
import eu.dnetlib.openaire.community.model.DbDatasourcePK;
import eu.dnetlib.openaire.community.model.DbOrganization;
import eu.dnetlib.openaire.community.model.DbProject;
import eu.dnetlib.openaire.community.model.DbProjectPK;
import eu.dnetlib.openaire.community.model.DbSubCommunity;
import eu.dnetlib.openaire.community.model.DbSupportOrg;
2023-07-05 11:41:26 +02:00
import eu.dnetlib.openaire.community.model.DbSupportOrgPK;
2023-06-28 15:28:12 +02:00
import eu.dnetlib.openaire.community.repository.DbCommunityRepository;
import eu.dnetlib.openaire.community.repository.DbDatasourceRepository;
import eu.dnetlib.openaire.community.repository.DbOrganizationRepository;
import eu.dnetlib.openaire.community.repository.DbProjectRepository;
import eu.dnetlib.openaire.community.repository.DbSubCommunityRepository;
import eu.dnetlib.openaire.community.repository.DbSupportOrgRepository;
import eu.dnetlib.openaire.community.utils.CommunityMappingUtils;
2023-06-12 14:47:27 +02:00
import eu.dnetlib.openaire.exporter.exceptions.CommunityException;
import eu.dnetlib.openaire.exporter.exceptions.ResourceNotFoundException;
import eu.dnetlib.openaire.exporter.model.community.CommunityContentprovider;
import eu.dnetlib.openaire.exporter.model.community.CommunityDetails;
import eu.dnetlib.openaire.exporter.model.community.CommunityOrganization;
import eu.dnetlib.openaire.exporter.model.community.CommunityProject;
import eu.dnetlib.openaire.exporter.model.community.CommunitySummary;
import eu.dnetlib.openaire.exporter.model.community.CommunityWritableProperties;
2023-06-27 12:37:23 +02:00
import eu.dnetlib.openaire.exporter.model.community.SubCommunity;
2023-06-12 14:47:27 +02:00
import eu.dnetlib.openaire.exporter.model.community.selectioncriteria.SelectionCriteria;
2023-06-12 14:30:42 +02:00
@Service
2023-06-12 14:47:27 +02:00
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
2023-06-12 14:30:42 +02:00
public class CommunityService {
@Autowired
2023-06-14 13:53:59 +02:00
private DbCommunityRepository dbCommunityRepository;
2023-06-12 14:30:42 +02:00
@Autowired
2023-06-14 13:53:59 +02:00
private DbProjectRepository dbProjectRepository;
2023-06-12 14:30:42 +02:00
@Autowired
2023-06-14 13:53:59 +02:00
private DbDatasourceRepository dbDatasourceRepository;
2023-06-12 14:30:42 +02:00
@Autowired
2023-06-14 13:53:59 +02:00
private DbOrganizationRepository dbOrganizationRepository;
2023-06-12 14:30:42 +02:00
@Autowired
2023-06-14 13:53:59 +02:00
private DbSupportOrgRepository dbSupportOrgRepository;
2023-06-27 12:37:23 +02:00
@Autowired
private DbSubCommunityRepository dbSubCommunityRepository;
2023-06-12 14:47:27 +02:00
2023-06-30 10:46:20 +02:00
private static final Log log = LogFactory.getLog(CommunityService.class);
2023-06-12 14:47:27 +02:00
public List<CommunitySummary> listCommunities() throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return dbCommunityRepository.findAll()
.stream()
.map(CommunityMappingUtils::toCommunitySummary)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-10 10:42:11 +02:00
@Transactional
public CommunityDetails newCommunity(final CommunityDetails details) throws CommunityException {
if (StringUtils.isBlank(details.getId())) {
throw new CommunityException("Empty Id");
} else if (dbCommunityRepository.existsById(details.getId())) {
throw new CommunityException("Community already exists: " + details.getId());
} else {
details.setCreationDate(LocalDateTime.now());
return saveCommunity(details);
}
}
2023-07-04 11:36:44 +02:00
@Transactional
2023-06-16 11:12:24 +02:00
public CommunityDetails saveCommunity(final CommunityDetails details) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-10 10:42:11 +02:00
details.setLastUpdateDate(LocalDateTime.now());
2023-06-30 10:46:20 +02:00
dbCommunityRepository.save(CommunityMappingUtils.toCommunity(details));
return getCommunity(details.getId());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-16 11:12:24 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public CommunityDetails getCommunity(final String id) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
return CommunityMappingUtils.CommunityDetails(c);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-06-27 09:55:39 +02:00
@Transactional
2023-07-04 11:36:44 +02:00
public void setCommunity(final String id, final CommunityWritableProperties details) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
final DbCommunity c = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
CommunityMappingUtils.populateCommunity(c, details);
2023-07-10 10:42:11 +02:00
c.setLastUpdateDate(LocalDateTime.now());
2023-06-30 10:46:20 +02:00
dbCommunityRepository.save(c);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public Page<CommunityProject> getCommunityProjects(final String id, final int page, final int size) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
final DbProject p = CommunityMappingUtils.toDbProject(id, project);
dbProjectRepository.save(p);
return project;
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void addCommunityProjects(final String id, final CommunityProject... projects) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-04 11:36:44 +02:00
final List<DbProject> list = Arrays.stream(projects)
2023-06-30 10:46:20 +02:00
.map(p -> CommunityMappingUtils.toDbProject(id, p))
.collect(Collectors.toList());
2023-06-14 13:53:59 +02:00
2023-06-30 10:46:20 +02:00
dbProjectRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void removeCommunityProjects(final String id, final String... ids) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-04 11:36:44 +02:00
final List<DbProjectPK> list = Arrays.stream(ids)
2023-06-30 10:46:20 +02:00
.map(projectId -> new DbProjectPK(id, projectId))
.collect(Collectors.toList());
dbProjectRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
public List<CommunityContentprovider> getCommunityContentproviders(final String id) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return dbDatasourceRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityContentprovider)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void addCommunityContentProviders(final String id, final CommunityContentprovider... contentproviders)
throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-04 11:36:44 +02:00
final List<DbDatasource> list = Arrays.stream(contentproviders)
2023-06-30 10:46:20 +02:00
.map(cp -> CommunityMappingUtils.toDbDatasource(id, cp))
.collect(Collectors.toList());
2023-06-30 10:46:20 +02:00
dbDatasourceRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void removeCommunityContentProviders(final String id, final String... ids) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-04 11:36:44 +02:00
final List<DbDatasourcePK> list = Arrays.stream(ids)
2023-06-30 10:46:20 +02:00
.map(dsId -> new DbDatasourcePK(id, dsId))
.collect(Collectors.toList());
dbDatasourceRepository.deleteAllById(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
2023-07-05 11:41:26 +02:00
public void removeCommunityOrganizations(final String id, final String... orgNames) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-05 11:41:26 +02:00
final List<DbSupportOrgPK> list = Arrays.stream(orgNames)
.map(name -> new DbSupportOrgPK(id, name))
2023-07-04 11:36:44 +02:00
.collect(Collectors.toList());
2023-07-05 11:41:26 +02:00
dbSupportOrgRepository.deleteAllById(list);
2023-06-30 10:46:20 +02:00
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public List<CommunityOrganization> getCommunityOrganizations(final String id) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return dbSupportOrgRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toCommunityOrganization)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void addCommunityOrganizations(final String id, final CommunityOrganization... orgs) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-04 11:36:44 +02:00
final List<DbSupportOrg> list = Arrays.stream(orgs)
2023-06-30 10:46:20 +02:00
.map(o -> CommunityMappingUtils.toDbSupportOrg(id, o))
.collect(Collectors.toList());
2023-06-16 11:12:24 +02:00
2023-06-30 10:46:20 +02:00
dbSupportOrgRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-16 11:12:24 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void removeSubCommunity(final String id, final String subCommunityId) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
dbSubCommunityRepository.deleteById(subCommunityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-27 12:37:23 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public List<SubCommunity> getSubCommunities(final String id) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return dbSubCommunityRepository.findByCommunity(id)
.stream()
.map(CommunityMappingUtils::toSubCommunity)
.collect(Collectors.toList());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-27 12:37:23 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public void addSubCommunities(final SubCommunity... subs) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
2023-07-04 11:36:44 +02:00
final List<DbSubCommunity> list = Arrays.stream(subs)
2023-06-30 10:46:20 +02:00
.map(CommunityMappingUtils::toDbSubCommunity)
.collect(Collectors.toList());
2023-06-27 12:37:23 +02:00
2023-06-30 10:46:20 +02:00
dbSubCommunityRepository.saveAll(list);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-27 12:37:23 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public CommunityDetails addCommunitySubjects(final String id, final String... subjects) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), false, subjects);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
public CommunityDetails removeCommunitySubjects(final String id, final String... subjects) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return modifyElementToArrayField(id, c -> c.getSubjects(), (c, subs) -> c.setSubjects(subs), true, subjects);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
public CommunityDetails addCommunityFOS(final String id, final String... foss) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), false, foss);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
public CommunityDetails removeCommunityFOS(final String id, final String... foss) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return modifyElementToArrayField(id, c -> c.getFos(), (c, fos) -> c.setFos(fos), true, foss);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
public CommunityDetails addCommunitySDG(final String id, final String... sdgs) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), false, sdgs);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
public CommunityDetails removeCommunitySDG(final String id, final String... sdgs) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return modifyElementToArrayField(id, c -> c.getSdg(), (c, sdg) -> c.setSdg(sdg), true, sdgs);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
2023-06-12 14:47:27 +02:00
public CommunityDetails addCommunityAdvancedConstraint(final String id, final SelectionCriteria advancedCosntraint)
2023-07-04 11:36:44 +02:00
throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(advancedCosntraint);
2023-07-10 10:42:11 +02:00
dbEntry.setLastUpdateDate(LocalDateTime.now());
2023-06-30 10:46:20 +02:00
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
public CommunityDetails removeCommunityAdvancedConstraint(final String id) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setAdvancedConstraints(null);
2023-07-10 10:42:11 +02:00
dbEntry.setLastUpdateDate(LocalDateTime.now());
2023-06-30 10:46:20 +02:00
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
2023-07-05 11:20:53 +02:00
@Transactional
public CommunityDetails addCommunityRemoveConstraint(final String id, final SelectionCriteria removeConstraint) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(removeConstraint);
2023-07-10 10:42:11 +02:00
dbEntry.setLastUpdateDate(LocalDateTime.now());
2023-07-05 11:20:53 +02:00
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public CommunityDetails removeCommunityRemoveConstraint(final String id) throws CommunityException {
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
dbEntry.setRemoveConstraints(null);
2023-07-10 10:42:11 +02:00
dbEntry.setLastUpdateDate(LocalDateTime.now());
2023-07-05 11:20:53 +02:00
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
2023-06-20 09:10:14 +02:00
public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
2023-07-04 11:36:44 +02:00
throws CommunityException {
2023-06-20 09:10:14 +02:00
if (isMain) {
2023-06-26 10:28:29 +02:00
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), null);
2023-06-20 09:10:14 +02:00
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), true, zenodoCommunity);
}
2023-06-12 14:47:27 +02:00
}
2023-06-20 09:10:14 +02:00
public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
2023-07-04 11:36:44 +02:00
throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
if (isMain) {
return updateElementToSimpleField(id, (c, val) -> c.setMainZenodoCommunity(val), zenodoCommunity);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), false, zenodoCommunity);
}
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
2023-06-20 09:10:14 +02:00
}
}
2023-06-26 10:28:29 +02:00
@Transactional
2023-06-20 09:10:14 +02:00
private CommunityDetails updateElementToSimpleField(final String id,
final BiConsumer<DbCommunity, String> setter,
2023-07-04 11:36:44 +02:00
final String value) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
setter.accept(dbEntry, value);
2023-07-10 10:42:11 +02:00
dbEntry.setLastUpdateDate(LocalDateTime.now());
2023-06-30 10:46:20 +02:00
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-12 14:47:27 +02:00
}
2023-06-26 10:28:29 +02:00
@Transactional
private CommunityDetails modifyElementToArrayField(final String id,
final Function<DbCommunity, String[]> getter,
final BiConsumer<DbCommunity, String[]> setter,
2023-06-19 15:47:48 +02:00
final boolean remove,
2023-07-04 11:36:44 +02:00
final String... values) throws CommunityException {
final DbCommunity dbEntry = dbCommunityRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Community not found: " + id));
final Set<String> tmpList = new LinkedHashSet<>();
final String[] oldValues = getter.apply(dbEntry);
if (oldValues != null) {
for (final String s : oldValues) {
tmpList.add(s);
}
}
if (remove) {
2023-06-19 15:47:48 +02:00
tmpList.removeAll(Arrays.asList(values));
} else {
2023-06-19 15:47:48 +02:00
tmpList.addAll(Arrays.asList(values));
}
setter.accept(dbEntry, tmpList.toArray(new String[tmpList.size()]));
2023-07-10 10:42:11 +02:00
dbEntry.setLastUpdateDate(LocalDateTime.now());
dbCommunityRepository.save(dbEntry);
return getCommunity(id);
}
2023-07-04 11:36:44 +02:00
@Transactional
2023-06-30 10:55:48 +02:00
public List<String> getOpenAIRECommunitiesByZenodoId(final String zenodoId) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
return dbCommunityRepository.findByZenodoId(zenodoId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
2023-06-19 15:47:48 +02:00
}
2023-07-04 11:36:44 +02:00
@Transactional
2023-06-30 10:46:20 +02:00
public Map<String, Set<String>> getPropagationOrganizationCommunityMap() throws CommunityException {
try {
return dbOrganizationRepository.findAll()
.stream()
.collect(Collectors.groupingBy(DbOrganization::getOrgId, Collectors.mapping(DbOrganization::getCommunity, Collectors.toSet())));
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
2023-07-04 11:36:44 +02:00
@Transactional
2023-06-30 10:46:20 +02:00
public Set<String> getPropagationOrganizationsForCommunity(final String communityId) throws CommunityException {
try {
return dbOrganizationRepository.findByCommunity(communityId)
.stream()
.map(DbOrganization::getOrgId)
.collect(Collectors.toSet());
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Set<String> addPropagationOrganizationForCommunity(final String communityId, final String... organizationIds) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.save(o);
}
2023-06-30 10:46:20 +02:00
return getPropagationOrganizationsForCommunity(communityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
@Transactional
public Set<String> removePropagationOrganizationForCommunity(final String communityId, final String... organizationIds) throws CommunityException {
2023-06-30 10:46:20 +02:00
try {
for (final String orgId : organizationIds) {
final DbOrganization o = new DbOrganization(communityId.trim(), orgId.trim());
dbOrganizationRepository.delete(o);
}
2023-06-30 10:46:20 +02:00
return getPropagationOrganizationsForCommunity(communityId);
} catch (final Throwable e) {
log.error(e);
throw new CommunityException(e);
}
}
2023-07-12 09:49:08 +02:00
@Transactional
public void deleteCommunity(final String id, final boolean recursive) {
if (recursive) {
dbProjectRepository.deleteByCommunity(id);
dbDatasourceRepository.deleteByCommunity(id);
dbOrganizationRepository.deleteByCommunity(id);
dbSupportOrgRepository.deleteByCommunity(id);
dbSubCommunityRepository.deleteByCommunity(id);
}
dbCommunityRepository.deleteById(id);
}
2023-06-12 14:30:42 +02:00
}