RED-6224: Fixed pr findings
This commit is contained in:
parent
ec0de5b6a2
commit
339833e2d6
@ -8,7 +8,6 @@ import java.util.List;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -16,7 +15,6 @@ import javax.annotation.PostConstruct;
|
|||||||
|
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.apache.commons.lang3.SerializationUtils;
|
import org.apache.commons.lang3.SerializationUtils;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
@ -34,6 +32,7 @@ import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryModel
|
|||||||
import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryRepresentation;
|
import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryRepresentation;
|
||||||
import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryVersion;
|
import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryVersion;
|
||||||
import com.iqser.red.service.redaction.v1.server.redaction.model.TenantDictionary;
|
import com.iqser.red.service.redaction.v1.server.redaction.model.TenantDictionary;
|
||||||
|
import com.iqser.red.service.redaction.v1.server.settings.RedactionServiceSettings;
|
||||||
|
|
||||||
import feign.FeignException;
|
import feign.FeignException;
|
||||||
import io.micrometer.core.annotation.Timed;
|
import io.micrometer.core.annotation.Timed;
|
||||||
@ -49,11 +48,7 @@ public class DictionaryService {
|
|||||||
private static final String DEFAULT_COLOR = "#cccccc";
|
private static final String DEFAULT_COLOR = "#cccccc";
|
||||||
private final DictionaryClient dictionaryClient;
|
private final DictionaryClient dictionaryClient;
|
||||||
|
|
||||||
@Value("${multitenancy.dictionary-cache.maximumSize:100}")
|
private final RedactionServiceSettings settings;
|
||||||
private Long maximumSize;
|
|
||||||
|
|
||||||
@Value("${multitenancy.dictionary-cache.expireAfterAccess:3}")
|
|
||||||
private Integer expireAfterAccess;
|
|
||||||
|
|
||||||
private LoadingCache<String, TenantDictionary> tenantDictionaryCache;
|
private LoadingCache<String, TenantDictionary> tenantDictionaryCache;
|
||||||
|
|
||||||
@ -61,12 +56,15 @@ public class DictionaryService {
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
protected void createCache() {
|
protected void createCache() {
|
||||||
|
|
||||||
tenantDictionaryCache = CacheBuilder.newBuilder().maximumSize(maximumSize).expireAfterAccess(expireAfterAccess, TimeUnit.DAYS).build(new CacheLoader<>() {
|
tenantDictionaryCache = CacheBuilder.newBuilder()
|
||||||
public TenantDictionary load(String key) {
|
.maximumSize(settings.getDictionaryCacheMaximumSize())
|
||||||
|
.expireAfterAccess(settings.getDictionaryCacheExpireAfterAccessDays(), TimeUnit.DAYS)
|
||||||
|
.build(new CacheLoader<>() {
|
||||||
|
public TenantDictionary load(String key) {
|
||||||
|
|
||||||
return new TenantDictionary();
|
return new TenantDictionary();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -76,13 +74,13 @@ public class DictionaryService {
|
|||||||
|
|
||||||
log.info("Updating dictionary data for dossierTemplate {} and dossier {}", dossierTemplateId, dossierId);
|
log.info("Updating dictionary data for dossierTemplate {} and dossier {}", dossierTemplateId, dossierId);
|
||||||
long dossierTemplateDictionaryVersion = dictionaryClient.getVersion(dossierTemplateId);
|
long dossierTemplateDictionaryVersion = dictionaryClient.getVersion(dossierTemplateId);
|
||||||
var dossierTemplateDictionary = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId);
|
var dossierTemplateDictionary = getDossierTemplateDictionary(dossierTemplateId);
|
||||||
if (dossierTemplateDictionary == null || dossierTemplateDictionaryVersion > dossierTemplateDictionary.getDictionaryVersion()) {
|
if (dossierTemplateDictionary == null || dossierTemplateDictionaryVersion > dossierTemplateDictionary.getDictionaryVersion()) {
|
||||||
updateDictionaryEntry(dossierTemplateId, dossierTemplateDictionaryVersion, getVersion(dossierTemplateDictionary), null);
|
updateDictionaryEntry(dossierTemplateId, dossierTemplateDictionaryVersion, getVersion(dossierTemplateDictionary), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
long dossierDictionaryVersion = dictionaryClient.getVersionForDossier(dossierId);
|
long dossierDictionaryVersion = dictionaryClient.getVersionForDossier(dossierId);
|
||||||
var dossierDictionary = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId);
|
var dossierDictionary = getDossierDictionary(dossierId);
|
||||||
if (dossierDictionary == null || dossierDictionaryVersion > dossierDictionary.getDictionaryVersion()) {
|
if (dossierDictionary == null || dossierDictionaryVersion > dossierDictionary.getDictionaryVersion()) {
|
||||||
updateDictionaryEntry(dossierTemplateId, dossierDictionaryVersion, getVersion(dossierDictionary), dossierId);
|
updateDictionaryEntry(dossierTemplateId, dossierDictionaryVersion, getVersion(dossierDictionary), dossierId);
|
||||||
}
|
}
|
||||||
@ -98,7 +96,7 @@ public class DictionaryService {
|
|||||||
DictionaryVersion version = updateDictionary(dossierTemplateId, dossierId);
|
DictionaryVersion version = updateDictionary(dossierTemplateId, dossierId);
|
||||||
|
|
||||||
Set<DictionaryIncrementValue> newValues = new HashSet<>();
|
Set<DictionaryIncrementValue> newValues = new HashSet<>();
|
||||||
List<DictionaryModel> dictionaryModels = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getDictionary();
|
List<DictionaryModel> dictionaryModels = getDossierTemplateDictionary(dossierTemplateId).getDictionary();
|
||||||
dictionaryModels.forEach(dictionaryModel -> {
|
dictionaryModels.forEach(dictionaryModel -> {
|
||||||
dictionaryModel.getEntries().forEach(dictionaryEntry -> {
|
dictionaryModel.getEntries().forEach(dictionaryEntry -> {
|
||||||
if (dictionaryEntry.getVersion() > fromVersion.getDossierTemplateVersion()) {
|
if (dictionaryEntry.getVersion() > fromVersion.getDossierTemplateVersion()) {
|
||||||
@ -117,8 +115,8 @@ public class DictionaryService {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
if (tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().containsKey(dossierId)) {
|
if (dossierDictionaryExists(dossierId)) {
|
||||||
dictionaryModels = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId).getDictionary();
|
dictionaryModels = getDossierDictionary(dossierId).getDictionary();
|
||||||
dictionaryModels.forEach(dictionaryModel -> {
|
dictionaryModels.forEach(dictionaryModel -> {
|
||||||
dictionaryModel.getEntries().forEach(dictionaryEntry -> {
|
dictionaryModel.getEntries().forEach(dictionaryEntry -> {
|
||||||
if (dictionaryEntry.getVersion() > fromVersion.getDossierVersion()) {
|
if (dictionaryEntry.getVersion() > fromVersion.getDossierVersion()) {
|
||||||
@ -156,20 +154,10 @@ public class DictionaryService {
|
|||||||
|
|
||||||
Optional<DictionaryModel> oldModel;
|
Optional<DictionaryModel> oldModel;
|
||||||
if (dossierId == null) {
|
if (dossierId == null) {
|
||||||
DictionaryRepresentation representation = null;
|
var representation = getDossierTemplateDictionary(dossierTemplateId);
|
||||||
try {
|
|
||||||
representation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId);
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
throw new RuntimeException("Failed to load Dictionary cache for tenant: " + TenantContext.getTenantId());
|
|
||||||
}
|
|
||||||
oldModel = representation != null ? representation.getDictionary().stream().filter(f -> f.getType().equals(t.getType())).findAny() : Optional.empty();
|
oldModel = representation != null ? representation.getDictionary().stream().filter(f -> f.getType().equals(t.getType())).findAny() : Optional.empty();
|
||||||
} else {
|
} else {
|
||||||
DictionaryRepresentation representation = null;
|
var representation = getDossierDictionary(dossierId);
|
||||||
try {
|
|
||||||
representation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId);
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
throw new RuntimeException("Failed to load Dictionary cache for tenant: " + TenantContext.getTenantId());
|
|
||||||
}
|
|
||||||
oldModel = representation != null ? representation.getDictionary().stream().filter(f -> f.getType().equals(t.getType())).findAny() : Optional.empty();
|
oldModel = representation != null ? representation.getDictionary().stream().filter(f -> f.getType().equals(t.getType())).findAny() : Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,9 +217,9 @@ public class DictionaryService {
|
|||||||
dictionaryRepresentation.setDictionary(dictionary);
|
dictionaryRepresentation.setDictionary(dictionary);
|
||||||
|
|
||||||
if (dossierId == null) {
|
if (dossierId == null) {
|
||||||
tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().put(dossierTemplateId, dictionaryRepresentation);
|
addDictionaryRepresentationForDossierTemplate(dossierTemplateId, dictionaryRepresentation);
|
||||||
} else {
|
} else {
|
||||||
tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().put(dossierId, dictionaryRepresentation);
|
addDictionaryRepresentationForDossier(dossierId, dictionaryRepresentation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (FeignException e) {
|
} catch (FeignException e) {
|
||||||
@ -273,18 +261,18 @@ public class DictionaryService {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public float[] getColor(String type, String dossierTemplateId) {
|
public float[] getColor(String type, String dossierTemplateId) {
|
||||||
|
|
||||||
DictionaryModel model = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getLocalAccessMap().get(type);
|
DictionaryModel model = getDossierTemplateDictionary(dossierTemplateId).getLocalAccessMap().get(type);
|
||||||
if (model != null) {
|
if (model != null) {
|
||||||
return model.getColor();
|
return model.getColor();
|
||||||
}
|
}
|
||||||
return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getDefaultColor();
|
return getDossierTemplateDictionary(dossierTemplateId).getDefaultColor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public boolean isHint(String type, String dossierTemplateId) {
|
public boolean isHint(String type, String dossierTemplateId) {
|
||||||
|
|
||||||
DictionaryModel model = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getLocalAccessMap().get(type);
|
DictionaryModel model = getDossierTemplateDictionary(dossierTemplateId).getLocalAccessMap().get(type);
|
||||||
if (model != null) {
|
if (model != null) {
|
||||||
return model.isHint();
|
return model.isHint();
|
||||||
}
|
}
|
||||||
@ -298,15 +286,15 @@ public class DictionaryService {
|
|||||||
|
|
||||||
List<DictionaryModel> copy = new ArrayList<>();
|
List<DictionaryModel> copy = new ArrayList<>();
|
||||||
|
|
||||||
var dossierTemplateRepresentation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId);
|
var dossierTemplateRepresentation = getDossierTemplateDictionary(dossierTemplateId);
|
||||||
dossierTemplateRepresentation.getDictionary().forEach(dm -> {
|
dossierTemplateRepresentation.getDictionary().forEach(dm -> {
|
||||||
copy.add(SerializationUtils.clone(dm));
|
copy.add(SerializationUtils.clone(dm));
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO merge dictionaries if they have same names
|
//TODO merge dictionaries if they have same names
|
||||||
long dossierDictionaryVersion = -1;
|
long dossierDictionaryVersion = -1;
|
||||||
if (tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().containsKey(dossierId)) {
|
if (dossierDictionaryExists(dossierId)) {
|
||||||
var dossierRepresentation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId);
|
var dossierRepresentation = getDossierDictionary(dossierId);
|
||||||
dossierRepresentation.getDictionary().forEach(dm -> {
|
dossierRepresentation.getDictionary().forEach(dm -> {
|
||||||
copy.add(SerializationUtils.clone(dm));
|
copy.add(SerializationUtils.clone(dm));
|
||||||
});
|
});
|
||||||
@ -321,7 +309,42 @@ public class DictionaryService {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public float[] getNotRedactedColor(String dossierTemplateId) {
|
public float[] getNotRedactedColor(String dossierTemplateId) {
|
||||||
|
|
||||||
return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getNotRedactedColor();
|
return getDossierTemplateDictionary(dossierTemplateId).getNotRedactedColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private void addDictionaryRepresentationForDossierTemplate(String dossierTemplateId, DictionaryRepresentation dictionaryRepresentation) {
|
||||||
|
|
||||||
|
tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().put(dossierTemplateId, dictionaryRepresentation);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private void addDictionaryRepresentationForDossier(String dossierId, DictionaryRepresentation dictionaryRepresentation) {
|
||||||
|
|
||||||
|
tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().put(dossierId, dictionaryRepresentation);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private DictionaryRepresentation getDossierTemplateDictionary(String dossierTemplateId) {
|
||||||
|
|
||||||
|
return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private DictionaryRepresentation getDossierDictionary(String dossierId) {
|
||||||
|
|
||||||
|
return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
private boolean dossierDictionaryExists(String dossierId) {
|
||||||
|
|
||||||
|
return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().containsKey(dossierId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -24,4 +24,8 @@ public class RedactionServiceSettings {
|
|||||||
|
|
||||||
private boolean priorityMode;
|
private boolean priorityMode;
|
||||||
|
|
||||||
|
private long dictionaryCacheMaximumSize = 100;
|
||||||
|
|
||||||
|
private int dictionaryCacheExpireAfterAccessDays = 3;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user