Finish Hotfix-2
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
package it.integry.ems.user.service;
|
package it.integry.ems.user.service;
|
||||||
|
|
||||||
import com.annimon.stream.Optional;
|
|
||||||
import com.annimon.stream.Stream;
|
|
||||||
import it.integry.annotations.PostContextConstruct;
|
import it.integry.annotations.PostContextConstruct;
|
||||||
import it.integry.ems.expansion.RunnableThrowable;
|
import it.integry.ems.expansion.RunnableThrowable;
|
||||||
import it.integry.ems.looper.service.LooperService;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.List;
|
import java.util.stream.Collectors;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserCacheService {
|
public class UserCacheService {
|
||||||
@@ -39,6 +36,8 @@ public class UserCacheService {
|
|||||||
|
|
||||||
private final HashMap<String, List<UserDTO>> cachedUsers = new HashMap<>();
|
private final HashMap<String, List<UserDTO>> cachedUsers = new HashMap<>();
|
||||||
|
|
||||||
|
private final ReentrantLock cacheLock = new ReentrantLock();
|
||||||
|
|
||||||
@PostContextConstruct(priority = 10)
|
@PostContextConstruct(priority = 10)
|
||||||
private void init() {
|
private void init() {
|
||||||
if (!UtilityDebug.isDebugExecution())
|
if (!UtilityDebug.isDebugExecution())
|
||||||
@@ -47,60 +46,53 @@ public class UserCacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void internalCheck() {
|
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 {
|
try {
|
||||||
multiDBTransactionManager.addConnection(model.getProfileName());
|
multiDBTransactionManager.addConnection(firstModel);
|
||||||
} catch (Exception ex) {
|
} 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);
|
this.discoverAllUsers(multiDBTransactionManager);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.error("Refresh user cache", 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) {
|
public void cache(String dbName, UserDTO user) {
|
||||||
this.initializeProfile(profileDB);
|
cachedUsers.putIfAbsent(dbName, new ArrayList<>());
|
||||||
|
|
||||||
List<UserDTO> users = cachedUsers.get(profileDB);
|
List<UserDTO> users = cachedUsers.get(dbName);
|
||||||
|
|
||||||
if (users != null) {
|
Optional<UserDTO> existentUser = users.stream()
|
||||||
Optional<UserDTO> existentUser = Stream.of(users).filter(x -> x.getUsername().equalsIgnoreCase(user.getUsername())).findFirst();
|
.filter(x -> x.getUsername().equalsIgnoreCase(user.getUsername()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
if (existentUser.isPresent()) {
|
if (existentUser.isPresent()) {
|
||||||
users.remove(existentUser.get());
|
users.remove(existentUser.get());
|
||||||
}
|
|
||||||
|
|
||||||
users.add(user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
users.add(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> retrieveProfilesOfUserByUsername(String username) {
|
public List<String> retrieveProfilesOfUserByUsername(String username) {
|
||||||
ArrayList<String> profiles = new ArrayList<>();
|
ArrayList<String> profiles = new ArrayList<>();
|
||||||
|
|
||||||
for (Map.Entry<String, List<UserDTO>> users : cachedUsers.entrySet()) {
|
for (Map.Entry<String, List<UserDTO>> users : cachedUsers.entrySet()) {
|
||||||
Optional<UserDTO> optionalUserDTO = Stream.of(users.getValue())
|
Optional<UserDTO> optionalUserDTO = users.getValue().stream()
|
||||||
.withoutNulls()
|
.filter(x -> x != null && x.getUsername().equalsIgnoreCase(username))
|
||||||
.filter(x -> x.getUsername().equalsIgnoreCase(username))
|
|
||||||
.findFirst();
|
.findFirst();
|
||||||
|
|
||||||
if (optionalUserDTO.isPresent()) {
|
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 {
|
public void discoverAllUsers(MultiDBTransactionManager multiDBTransactionManager) throws Exception {
|
||||||
|
cacheLock.lock();
|
||||||
List<RunnableThrowable> calls = new ArrayList<>();
|
List<RunnableThrowable> calls = new ArrayList<>();
|
||||||
|
|
||||||
for (final AdvancedDataSource advancedDataSource : multiDBTransactionManager.getActiveConnections()) {
|
for (final AdvancedDataSource advancedDataSource : multiDBTransactionManager.getActiveConnections()) {
|
||||||
@@ -116,22 +109,20 @@ public class UserCacheService {
|
|||||||
try {
|
try {
|
||||||
Connection conn = advancedDataSource.getConnection();
|
Connection conn = advancedDataSource.getConnection();
|
||||||
|
|
||||||
if (advancedDataSource.isInternalDb()) {
|
String sql =
|
||||||
String sql =
|
"SELECT User_name, " +
|
||||||
"SELECT User_name, " +
|
" e_mail as email, " +
|
||||||
" e_mail as email, " +
|
" Full_name, " +
|
||||||
" Full_name, " +
|
" CAST(CASE WHEN flag_attivo = 'S' THEN 1 ELSE 0 END AS bit) AS attivo, " +
|
||||||
" CAST(CASE WHEN flag_attivo = 'S' THEN 1 ELSE 0 END AS bit) AS attivo, " +
|
" 'internal' AS type " +
|
||||||
" 'internal' AS type " +
|
"FROM " + StbUser.ENTITY + " " +
|
||||||
"FROM " + StbUser.ENTITY + " " +
|
"WHERE ( " + StbUser.ENTITY + ".flag_intra_user = 'S' OR " + StbUser.ENTITY + ".flag_extra_user = 'S') ";
|
||||||
"WHERE ( " + StbUser.ENTITY + ".flag_intra_user = 'S' OR " + StbUser.ENTITY + ".flag_extra_user = 'S') ";
|
|
||||||
|
|
||||||
|
|
||||||
final List<UserDTO> userDTOS = UtilityDB.executeSimpleQueryDTO(conn, sql, UserDTO.class);
|
final List<UserDTO> userDTOS = UtilityDB.executeSimpleQueryDTO(conn, sql, UserDTO.class);
|
||||||
Stream.of(userDTOS)
|
|
||||||
.forEach(x -> cache(advancedDataSource.getDataSource().getProfile(), x));
|
|
||||||
|
|
||||||
}
|
if (userDTOS != null)
|
||||||
|
userDTOS.forEach(x -> cache(advancedDataSource.getDataSource().getDbName(), x));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.error(String.format("Errore durante la retrieve degli utenti su \"%s\". %s", advancedDataSource.getProfileName(), ex.getMessage()), 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);
|
UtilityThread.executeParallel(calls);
|
||||||
}
|
cacheLock.unlock();
|
||||||
|
|
||||||
private void initializeProfile(String profileDB) {
|
|
||||||
cachedUsers.putIfAbsent(profileDB, new ArrayList<>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void invalidateCache() {
|
public void invalidateCache() {
|
||||||
|
cacheLock.lock();
|
||||||
this.cachedUsers.clear();
|
this.cachedUsers.clear();
|
||||||
|
cacheLock.unlock();
|
||||||
|
|
||||||
this.internalCheck();
|
this.internalCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 filecontent_pdf = null WHERE ref_uuid1 IS NOT NULL
|
||||||
UPDATE dtb_doc_xml SET pdf_esito = null WHERE ref_uuid2 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
|
Query per controllare lo stato
|
||||||
|
|||||||
@@ -320,16 +320,14 @@ public class SystemService {
|
|||||||
|
|
||||||
if (profiles.isEmpty()) {
|
if (profiles.isEmpty()) {
|
||||||
// SELEZIONE CICLICA IN TUTTI I DB SPECIFICATI
|
// SELEZIONE CICLICA IN TUTTI I DB SPECIFICATI
|
||||||
profiles = Stream.of(settingsModel.getAvailableConnections())
|
profiles = Stream.of(settingsModel.getAvailableConnections(true))
|
||||||
.filter(x -> x.getInternalDb() && x.getProfileName().equalsIgnoreCase(x.getDbName()))
|
.filter(x -> x.getProfileName().equalsIgnoreCase(x.getDbName()))
|
||||||
.map(AvailableConnectionsModel::getProfileName)
|
.map(AvailableConnectionsModel::getProfileName)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (profiles == null || profiles.isEmpty()) {
|
if (profiles.isEmpty()) {
|
||||||
|
profiles = Stream.of(settingsModel.getAvailableConnections(true))
|
||||||
profiles = Stream.of(settingsModel.getAvailableConnections())
|
|
||||||
.filter(AvailableConnectionsModel::getInternalDb)
|
|
||||||
.distinctBy(AvailableConnectionsModel::getDbName)
|
.distinctBy(AvailableConnectionsModel::getDbName)
|
||||||
.map(AvailableConnectionsModel::getProfileName)
|
.map(AvailableConnectionsModel::getProfileName)
|
||||||
.toList();
|
.toList();
|
||||||
@@ -346,7 +344,7 @@ public class SystemService {
|
|||||||
.setType("web")
|
.setType("web")
|
||||||
.setAttivo("S".equalsIgnoreCase(UtilityHashMap.getValueIfExists(tmpUserData, "flag_attivo")));
|
.setAttivo("S".equalsIgnoreCase(UtilityHashMap.getValueIfExists(tmpUserData, "flag_attivo")));
|
||||||
|
|
||||||
userCacheService.cache(profile, userDTO);
|
userCacheService.cache(settingsModel.getDbNameFromProfileDb(profile), userDTO);
|
||||||
if ((!this.isUtenteAttivo(userData))) {
|
if ((!this.isUtenteAttivo(userData))) {
|
||||||
userData = tmpUserData;
|
userData = tmpUserData;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user