2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.taxeditor
.preference
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashMap
;
13 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
21 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmPreference
;
22 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmPreference
.PrefKey
;
23 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
24 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferenceResolver
;
25 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferenceSubject
;
26 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
27 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
28 import eu
.etaxonomy
.cdm
.persistence
.dto
.TermDto
;
29 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
36 public class CdmPreferenceCache
{
37 Map
<String
,List
<CdmPreference
>> preferenceCache
= new HashMap();
39 private static CdmPreferenceCache instance
;
41 private final static Logger logger
= Logger
.getLogger(CdmPreferenceCache
.class);
45 public static CdmPreferenceCache
instance(){
47 instance
= new CdmPreferenceCache();
52 public CdmPreference
get(String predicate
){
53 if (preferenceCache
.get(predicate
) != null){
54 List
<CdmPreference
> preferences
= preferenceCache
.get(predicate
);
55 if (preferenceCache
.get(predicate
).size() > 1){
56 CdmPreference pref
= PreferenceResolver
.resolve(preferenceCache
.get(predicate
), CdmPreference
.NewKey(PreferenceSubject
.NewTaxEditorInstance(), PreferencePredicate
.getByKey(predicate
)));
59 return preferenceCache
.get(predicate
).get(0);
64 public List
<CdmPreference
> getAllForPredicate(String predicate
){
65 if (preferenceCache
.get(predicate
) != null){
66 return preferenceCache
.get(predicate
);
72 //looking for the equal prefkey
73 public CdmPreference
get(PrefKey key
){
74 if (preferenceCache
.containsKey(key
.getPredicate())){
75 for (CdmPreference pref
: preferenceCache
.get(key
.getPredicate())){
76 if (pref
.getSubjectString().equals(key
.getSubject())){
85 public void put(CdmPreference pref
){
86 if (preferenceCache
.containsKey(pref
.getPredicate())){
87 List
<CdmPreference
> cachedPreferences
= preferenceCache
.get(pref
.getPredicate());
88 CdmPreference prefToRemove
= null;
89 for (CdmPreference cachedPref
:cachedPreferences
){
90 if (pref
.getKey().equals(cachedPref
.getKey())){
91 prefToRemove
= cachedPref
;
94 cachedPreferences
.remove(prefToRemove
);
95 cachedPreferences
.add(pref
);
97 List
<CdmPreference
> prefs
= new ArrayList();
99 preferenceCache
.put(pref
.getPredicate(), prefs
);
105 public boolean remove(CdmPreference pref
){
106 if (preferenceCache
.containsKey(pref
.getPredicate())){
107 List
<CdmPreference
> cachedPreferences
= preferenceCache
.get(pref
.getPredicate());
108 CdmPreference prefToRemove
= null;
109 for (CdmPreference cachedPref
:cachedPreferences
){
110 if (pref
.getKey().equals(cachedPref
.getKey())){
111 prefToRemove
= cachedPref
;
114 cachedPreferences
.remove(prefToRemove
);
120 public void getAllTaxEditorDBPreferences(){
121 ICdmRepository controller
;
122 controller
= CdmStore
.getCurrentApplicationConfiguration();
123 List
<CdmPreference
> allPreferences
= controller
.getPreferenceService().list();
124 preferenceCache
.clear();
126 for (CdmPreference pref
: allPreferences
){
130 PrefKey key
= CdmPreference
.NewKey(PreferenceSubject
.NewTaxEditorInstance(), PreferencePredicate
.AvailableDistributionStatus
);
132 if (get(key
) != null){
133 if (!PreferencesUtil
.getBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.AvailableDistributionStatus
.getKey())) || !get(key
).isAllowOverride()){
134 //get terms for the uuids... and add them to the termManager as preferred terms
135 ITermService termService
= CdmStore
.getService(ITermService
.class);
136 List
<UUID
> uuidList
= new ArrayList();
137 if (get(key
).getValue() != null){
138 String
[] uuidArray
=findBestMatching(key
).getValue().split(";");
139 for (String uuidString
:uuidArray
){
141 uuidList
.add(UUID
.fromString(uuidString
));
142 } catch (Exception e
) {
143 logger
.warn("Preference loading failed", e
);
148 List
<DefinedTermBase
> definedTermBases
= termService
.load(uuidList
, null);
149 List
<TermDto
> dtos
= new ArrayList
<>();
150 for (DefinedTermBase term
: definedTermBases
){
151 dtos
.add(TermDto
.fromTerm(term
));
153 CdmStore
.getTermManager().setPreferredTermsByType(dtos
, TermType
.PresenceAbsenceTerm
);
156 key
= CdmPreference
.NewKey(PreferenceSubject
.NewTaxEditorInstance(), PreferencePredicate
.AvailableRanks
);
158 if (get(key
) != null){
159 if (!PreferencesUtil
.getBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.AvailableRanks
.getKey())) || !get(key
).isAllowOverride()){
160 //get terms for the uuids... and add them to the termManager as preferred terms
161 ITermService termService
= CdmStore
.getService(ITermService
.class);
162 List
<UUID
> uuidList
= new ArrayList();
163 if (get(key
).getValue() != null){
164 String
[] uuidArray
=findBestMatching(key
).getValue().split(";");
165 for (String uuidString
:uuidArray
){
167 uuidList
.add(UUID
.fromString(uuidString
));
168 } catch (Exception e
) {
169 logger
.warn("Preference loading failed", e
);
174 List
<DefinedTermBase
> definedTermBases
= termService
.load(uuidList
, null);
175 List
<TermDto
> dtos
= new ArrayList();
176 for (DefinedTermBase term
: definedTermBases
){
177 dtos
.add(TermDto
.fromTerm(term
));
179 CdmStore
.getTermManager().setPreferredTermsByType(dtos
, TermType
.Rank
);
183 key
= CdmPreference
.NewKey(PreferenceSubject
.NewTaxEditorInstance(), PreferencePredicate
.AvailableNomenclaturalStatus
);
185 if (get(key
) != null){
186 if (!PreferencesUtil
.getBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.AvailableNomenclaturalStatus
.getKey())) || !get(key
).isAllowOverride()){
187 //get terms for the uuids... and add them to the termManager as preferred terms
188 ITermService termService
= CdmStore
.getService(ITermService
.class);
189 List
<UUID
> uuidList
= new ArrayList();
190 if (get(key
).getValue() != null){
191 String
[] uuidArray
=findBestMatching(key
).getValue().split(";");
192 for (String uuidString
:uuidArray
){
194 uuidList
.add(UUID
.fromString(uuidString
));
195 } catch (Exception e
) {
196 logger
.warn("Preference loading failed", e
);
201 List
<DefinedTermBase
> definedTermBases
= termService
.load(uuidList
, null);
202 List
<TermDto
> dtos
= new ArrayList();
203 for (DefinedTermBase term
: definedTermBases
){
204 dtos
.add(TermDto
.fromTerm(term
));
206 CdmStore
.getTermManager().setPreferredTermsByType(dtos
, TermType
.NomenclaturalStatusType
);
213 public CdmPreference
findBestMatching(PrefKey key
) {
214 List
<CdmPreference
> allPrefs
= new ArrayList();
215 for (String predicate
: preferenceCache
.keySet()){
216 allPrefs
.addAll(preferenceCache
.get(predicate
));
218 return PreferenceResolver
.resolve(allPrefs
, key
);