Skip to content

Commit

Permalink
#1054 fix deletion of old organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Mar 27, 2022
1 parent f54fb15 commit 1385288
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/main/java/alfio/config/DataSourceConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import alfio.manager.i18n.MessageSourceManager;
import alfio.manager.system.AdminJobManager;
import alfio.manager.system.ConfigurationManager;
import alfio.repository.EventDeleterRepository;
import alfio.repository.EventRepository;
import alfio.repository.SubscriptionRepository;
import alfio.repository.TicketCategoryRepository;
import alfio.repository.*;
import alfio.repository.system.AdminJobQueueRepository;
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
Expand Down Expand Up @@ -306,9 +303,10 @@ DemoModeDataManager demoModeDataManager(UserRepository userRepository,
OrganizationRepository organizationRepository,
EventDeleterRepository eventDeleterRepository,
EventRepository eventRepository,
ConfigurationManager configurationManager) {
ConfigurationManager configurationManager,
OrganizationDeleterRepository organizationDeleterRepository) {
return new DemoModeDataManager(userRepository, userOrganizationRepository, organizationRepository,
eventDeleterRepository, eventRepository, configurationManager);
eventDeleterRepository, eventRepository, configurationManager, organizationDeleterRepository);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/alfio/manager/DemoModeDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import alfio.model.user.User;
import alfio.repository.EventDeleterRepository;
import alfio.repository.EventRepository;
import alfio.repository.OrganizationDeleterRepository;
import alfio.repository.user.OrganizationRepository;
import alfio.repository.user.UserRepository;
import alfio.repository.user.join.UserOrganizationRepository;
Expand All @@ -47,6 +48,7 @@ public class DemoModeDataManager {
private final EventDeleterRepository eventDeleterRepository;
private final EventRepository eventRepository;
private final ConfigurationManager configurationManager;
private final OrganizationDeleterRepository organizationDeleterRepository;

public List<Integer> findExpiredUsers(Date date) {
return userRepository.findUsersToDeleteOlderThan(date, User.Type.DEMO);
Expand All @@ -60,8 +62,7 @@ public void deleteAccounts(List<Integer> userIds) {
log.info("found {} events to delete", disabledEventIds.size());
disabledEventIds.forEach(eventDeleterRepository::deleteAllForEvent);
userIds.forEach(userRepository::deleteUserAndReferences);
int deletedOrganizations = organizationRepository.deleteOrganizationsIfEmpty(organizationIds);
log.info("deleted {} empty organizations", deletedOrganizations);
organizationDeleterRepository.deleteEmptyOrganizations(organizationIds);
}
}

Expand Down
86 changes: 86 additions & 0 deletions src/main/java/alfio/repository/OrganizationDeleterRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* This file is part of alf.io.
*
* alf.io 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 3 of the License, or
* (at your option) any later version.
*
* alf.io 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 alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.repository;

import ch.digitalfondue.npjt.Bind;
import ch.digitalfondue.npjt.Query;
import ch.digitalfondue.npjt.QueryRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@QueryRepository
public interface OrganizationDeleterRepository {

Logger LOGGER = LoggerFactory.getLogger(OrganizationDeleterRepository.class);
String SELECT_EMPTY_ORGANIZATIONS = "select distinct(org_id) from j_user_organization where org_id in(:organizationIds)";

@Query("delete from auditing where organization_id_fk in(:organizationIds)" +
" and organization_id_fk not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteAuditingForEmptyOrganizations(@Bind("organizationIds") List<Integer> organizationIds);

@Query("delete from invoice_sequences where organization_id_fk in(:organizationIds)" +
" and organization_id_fk not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteInvoiceSequencesForEmptyOrganizations(@Bind("organizationIds") List<Integer> organizationIds);

@Query("delete from a_group where organization_id_fk in(:organizationIds)" +
" and organization_id_fk not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteGroupsForEmptyOrganizations(@Bind("organizationIds") List<Integer> organizationIds);

@Query("delete from group_member where organization_id_fk in(:organizationIds)" +
" and organization_id_fk not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteGroupMembersForEmptyOrganizations(@Bind("organizationIds") List<Integer> organizationIds);

@Query("delete from configuration_organization where organization_id_fk in(:organizationIds)" +
" and organization_id_fk not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteConfigurationForEmptyOrganizations(@Bind("organizationIds") List<Integer> organizationIds);

@Query("delete from resource_organizer where organization_id_fk in(:organizationIds)" +
" and organization_id_fk not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteResourcesForEmptyOrganizations(@Bind("organizationIds") List<Integer> organizationIds);

@Query("delete from organization where id in(:organizationIds)" +
" and id not in (" + SELECT_EMPTY_ORGANIZATIONS + ")")
int deleteOrganizationsIfEmpty(@Bind("organizationIds") List<Integer> organizationIds);


default void deleteEmptyOrganizations(List<Integer> organizationIds) {
// delete invoice sequences
int deletedSequences = deleteInvoiceSequencesForEmptyOrganizations(organizationIds);
LOGGER.info("deleted {} invoice sequences", deletedSequences);
// delete auditing
int deletedAuditing = deleteAuditingForEmptyOrganizations(organizationIds);
LOGGER.info("deleted {} auditing rows", deletedAuditing);
// delete groups
int deletedGroupMembers = deleteGroupMembersForEmptyOrganizations(organizationIds);
int deletedGroups = deleteGroupsForEmptyOrganizations(organizationIds);
LOGGER.info("deleted {} groups and {} members", deletedGroups, deletedGroupMembers);

// delete configuration
int deletedConfigurations = deleteConfigurationForEmptyOrganizations(organizationIds);
LOGGER.info("deleted {} configurations", deletedConfigurations);

// delete resources
int deletedResources = deleteResourcesForEmptyOrganizations(organizationIds);
LOGGER.info("deleted {} resources", deletedResources);

int deletedOrganizations = deleteOrganizationsIfEmpty(organizationIds);
LOGGER.info("deleted {} empty organizations", deletedOrganizations);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,4 @@ int update(@Bind("id") int id,
"(select * from organization where 'ROLE_ADMIN' in (select role from ba_user inner join authority on ba_user.username = authority.username where ba_user.username = :username) and id = :orgId)")
Optional<Organization> findOrganizationForUser(@Bind("username") String username, @Bind("orgId") int orgId);

@Query("delete from organization where id in(:organizationIds) and id not in (select distinct(org_id) from j_user_organization where org_id in(:organizationIds))")
int deleteOrganizationsIfEmpty(@Bind("organizationIds") List<Integer> organizationIds);
}

0 comments on commit 1385288

Please sign in to comment.