Merge branch 'master' into develop
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good

This commit is contained in:
2025-01-22 17:12:42 +01:00
3 changed files with 49 additions and 58 deletions

View File

@@ -1,7 +1,5 @@
package it.integry.ems.user.service;
import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import it.integry.annotations.PostContextConstruct;
import it.integry.ems.expansion.RunnableThrowable;
import it.integry.ems.looper.service.LooperService;
@@ -20,10 +18,9 @@ import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
@Service
public class UserCacheService {
@@ -39,6 +36,8 @@ public class UserCacheService {
private final HashMap<String, List<UserDTO>> cachedUsers = new HashMap<>();
private final ReentrantLock cacheLock = new ReentrantLock();
@PostContextConstruct(priority = 10)
private void init() {
if (!UtilityDebug.isDebugExecution())
@@ -47,60 +46,53 @@ public class UserCacheService {
}
private void internalCheck() {
MultiDBTransactionManager multiDBTransactionManager = new MultiDBTransactionManager();
Map<String, List<AvailableConnectionsModel>> availableConnectionsModels = settingsModel.getAvailableConnections(true).stream()
.collect(Collectors.groupingBy(AvailableConnectionsModel::getDbName));
List<AvailableConnectionsModel> availableConnectionsModels = Stream.of(settingsModel.getAvailableConnections())
.filter(AvailableConnectionsModel::getInternalDb)
.toList();
try {
for (AvailableConnectionsModel model : availableConnectionsModels) {
try (MultiDBTransactionManager multiDBTransactionManager = new MultiDBTransactionManager()) {
for (Map.Entry<String, List<AvailableConnectionsModel>> profiles : availableConnectionsModels.entrySet()) {
final AvailableConnectionsModel firstModel = profiles.getValue().get(0);
try {
multiDBTransactionManager.addConnection(model.getProfileName());
multiDBTransactionManager.addConnection(firstModel);
} catch (Exception ex) {
logger.info(String.format("Cannot find %s database", model.getDbName()), ex);
logger.info(String.format("Cannot find %s database", firstModel.getDbName()), ex);
}
}
this.discoverAllUsers(multiDBTransactionManager);
} catch (Exception ex) {
logger.error("Refresh user cache", ex);
} finally {
try {
multiDBTransactionManager.closeAll();
} catch (Exception ex) {
logger.error("Refresh user cache", ex);
}
}
}
public void cache(String profileDB, UserDTO user) {
this.initializeProfile(profileDB);
public void cache(String dbName, UserDTO user) {
cachedUsers.putIfAbsent(dbName, new ArrayList<>());
List<UserDTO> users = cachedUsers.get(profileDB);
List<UserDTO> users = cachedUsers.get(dbName);
if (users != null) {
Optional<UserDTO> existentUser = Stream.of(users).filter(x -> x.getUsername().equalsIgnoreCase(user.getUsername())).findFirst();
Optional<UserDTO> existentUser = users.stream()
.filter(x -> x.getUsername().equalsIgnoreCase(user.getUsername()))
.findFirst();
if (existentUser.isPresent()) {
users.remove(existentUser.get());
}
users.add(user);
if (existentUser.isPresent()) {
users.remove(existentUser.get());
}
users.add(user);
}
public List<String> retrieveProfilesOfUserByUsername(String username) {
ArrayList<String> profiles = new ArrayList<>();
for (Map.Entry<String, List<UserDTO>> users : cachedUsers.entrySet()) {
Optional<UserDTO> optionalUserDTO = Stream.of(users.getValue())
.withoutNulls()
.filter(x -> x.getUsername().equalsIgnoreCase(username))
Optional<UserDTO> optionalUserDTO = users.getValue().stream()
.filter(x -> x != null && x.getUsername().equalsIgnoreCase(username))
.findFirst();
if (optionalUserDTO.isPresent()) {
profiles.add(users.getKey());
profiles.add(settingsModel.getProfileDbFromDbName(users.getKey()));
}
}
@@ -109,6 +101,7 @@ public class UserCacheService {
public void discoverAllUsers(MultiDBTransactionManager multiDBTransactionManager) throws Exception {
cacheLock.lock();
List<RunnableThrowable> calls = new ArrayList<>();
for (final AdvancedDataSource advancedDataSource : multiDBTransactionManager.getActiveConnections()) {
@@ -116,22 +109,20 @@ public class UserCacheService {
try {
Connection conn = advancedDataSource.getConnection();
if (advancedDataSource.isInternalDb()) {
String sql =
"SELECT User_name, " +
" e_mail as email, " +
" Full_name, " +
" CAST(CASE WHEN flag_attivo = 'S' THEN 1 ELSE 0 END AS bit) AS attivo, " +
" 'internal' AS type " +
"FROM " + StbUser.ENTITY + " " +
"WHERE ( " + StbUser.ENTITY + ".flag_intra_user = 'S' OR " + StbUser.ENTITY + ".flag_extra_user = 'S') ";
String sql =
"SELECT User_name, " +
" e_mail as email, " +
" Full_name, " +
" CAST(CASE WHEN flag_attivo = 'S' THEN 1 ELSE 0 END AS bit) AS attivo, " +
" 'internal' AS type " +
"FROM " + StbUser.ENTITY + " " +
"WHERE ( " + StbUser.ENTITY + ".flag_intra_user = 'S' OR " + StbUser.ENTITY + ".flag_extra_user = 'S') ";
final List<UserDTO> userDTOS = UtilityDB.executeSimpleQueryDTO(conn, sql, UserDTO.class);
Stream.of(userDTOS)
.forEach(x -> cache(advancedDataSource.getDataSource().getProfile(), x));
final List<UserDTO> userDTOS = UtilityDB.executeSimpleQueryDTO(conn, sql, UserDTO.class);
}
if (userDTOS != null)
userDTOS.forEach(x -> cache(advancedDataSource.getDataSource().getDbName(), x));
} catch (Exception ex) {
logger.error(String.format("Errore durante la retrieve degli utenti su \"%s\". %s", advancedDataSource.getProfileName(), ex.getMessage()), ex);
}
@@ -139,14 +130,14 @@ public class UserCacheService {
}
UtilityThread.executeParallel(calls);
}
private void initializeProfile(String profileDB) {
cachedUsers.putIfAbsent(profileDB, new ArrayList<>());
cacheLock.unlock();
}
public void invalidateCache() {
cacheLock.lock();
this.cachedUsers.clear();
cacheLock.unlock();
this.internalCheck();
}

View File

@@ -74,6 +74,8 @@ public @interface ObjectStorage {
UPDATE dtb_doc_xml SET filecontent_pdf = null WHERE ref_uuid1 IS NOT NULL
UPDATE dtb_doc_xml SET pdf_esito = null WHERE ref_uuid2 IS NOT NULL
UPDATE stb_activity_file SET content = null WHERE ref_uuid IS NOT NULL
Query per controllare lo stato

View File

@@ -320,16 +320,14 @@ public class SystemService {
if (profiles.isEmpty()) {
// SELEZIONE CICLICA IN TUTTI I DB SPECIFICATI
profiles = Stream.of(settingsModel.getAvailableConnections())
.filter(x -> x.getInternalDb() && x.getProfileName().equalsIgnoreCase(x.getDbName()))
profiles = Stream.of(settingsModel.getAvailableConnections(true))
.filter(x -> x.getProfileName().equalsIgnoreCase(x.getDbName()))
.map(AvailableConnectionsModel::getProfileName)
.toList();
}
if (profiles == null || profiles.isEmpty()) {
profiles = Stream.of(settingsModel.getAvailableConnections())
.filter(AvailableConnectionsModel::getInternalDb)
if (profiles.isEmpty()) {
profiles = Stream.of(settingsModel.getAvailableConnections(true))
.distinctBy(AvailableConnectionsModel::getDbName)
.map(AvailableConnectionsModel::getProfileName)
.toList();
@@ -346,7 +344,7 @@ public class SystemService {
.setType("web")
.setAttivo("S".equalsIgnoreCase(UtilityHashMap.getValueIfExists(tmpUserData, "flag_attivo")));
userCacheService.cache(profile, userDTO);
userCacheService.cache(settingsModel.getDbNameFromProfileDb(profile), userDTO);
if ((!this.isUtenteAttivo(userData))) {
userData = tmpUserData;
}