Project

General

Profile

Download (7.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.taxeditor.preference;
10

    
11
import java.util.ArrayList;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.UUID;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.api.application.ICdmRepository;
20
import eu.etaxonomy.cdm.api.service.ITermService;
21
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
22
import eu.etaxonomy.cdm.model.common.TermType;
23
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
24
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
25
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
26
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
27
import eu.etaxonomy.taxeditor.store.CdmStore;
28
import eu.etaxonomy.taxeditor.store.TermStore;
29

    
30
/**
31
 * @author k.luther
32
 * @since 27.04.2018
33
 *
34
 */
35
public class CdmPreferenceCache {
36
    Map<String, CdmPreference> preferenceCache = new HashMap();
37

    
38
    private static CdmPreferenceCache instance;
39

    
40
    private final static Logger logger = Logger.getLogger(CdmPreferenceCache.class);
41

    
42
    public static CdmPreferenceCache instance(){
43
        if(instance == null){
44
            instance = new CdmPreferenceCache();
45
        }
46
        return instance;
47
    }
48

    
49
    public CdmPreference get(String key){
50
        return preferenceCache.get(key);
51
    }
52

    
53
    public void put(CdmPreference pref){
54
        preferenceCache.put(pref.getPredicate(), pref);
55
    }
56

    
57
    public void getAllTaxEditorDBPreferences(){
58
        ICdmRepository controller;
59
        controller = CdmStore.getCurrentApplicationConfiguration();
60
        //ABCD
61
        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.AbcdImportConfig);
62
        loadAndPutPreference(controller, key);
63

    
64
        //ShowIOMenu
65
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowImportExportMenu);
66
        loadAndPutPreference(controller, key);
67

    
68
//        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowMediaView);
69
//        loadAndPutPreference(controller, key);
70

    
71
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowChecklistPerspective);
72
        loadAndPutPreference(controller, key);
73

    
74

    
75
        //Specimen Details
76
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowSpecimen);
77
        loadAndPutPreference(controller, key);
78

    
79
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowTaxonAssociations);
80
        loadAndPutPreference(controller, key);
81

    
82

    
83
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowCollectingAreasInGeneralSection);
84
        loadAndPutPreference(controller, key);
85

    
86

    
87
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowLifeForm);
88
        loadAndPutPreference(controller, key);
89

    
90
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.DeterminationOnlyForFieldUnits);
91
        loadAndPutPreference(controller, key);
92

    
93

    
94
        //Name Details
95
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.NameDetailsView);
96
        loadAndPutPreference(controller, key);
97

    
98
        //Distribution Editor
99
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.DistributionEditorActivated);
100
        loadAndPutPreference(controller, key);
101

    
102
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.AvailableDistributionStatus);
103
        loadAndPutPreference(controller, key);
104

    
105
        if (preferenceCache.get(key) != null){
106
            if (!PreferencesUtil.getBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.AvailableDistributionStatus.getKey())) || !preferenceCache.get(key).isAllowOverride()){
107
                //get terms for the uuids... and add them to the termManager as preferred terms
108
                ITermService termService = CdmStore.getService(ITermService.class);
109
                String[] uuidArray = preferenceCache.get(key).getValue().split(";");
110
                List<UUID> uuidList = new ArrayList();
111
                for (String uuidString:uuidArray){
112
                    try {
113
                        uuidList.add(UUID.fromString(uuidString));
114
                    } catch (Exception e) {
115
                        logger.warn("Preference loading failed", e);
116
                    }
117
                }
118

    
119
                List<DefinedTermBase> definedTermBases = termService.load(uuidList, null);
120
                CdmStore.getTermManager().setPreferredTerms(definedTermBases, TermStore.getTerms(TermType.PresenceAbsenceTerm, null));
121
            }
122
        }
123

    
124
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.DistributionEditorActivated);
125
        loadAndPutPreference(controller, key);
126

    
127
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.DisplayOfAreasInDistributionEditor);
128
        loadAndPutPreference(controller, key);
129

    
130
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.DisplayOfStatus);
131
        loadAndPutPreference(controller, key);
132

    
133
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowRankInDistributionEditor);
134
        loadAndPutPreference(controller, key);
135

    
136
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.AreasSortedByIdInVocabulary);
137
        loadAndPutPreference(controller, key);
138

    
139
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.OwnDescriptionForDistributionEditor);
140
        loadAndPutPreference(controller, key);
141

    
142
        //common names
143

    
144
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.CommonNameAreaVocabularies);
145
        loadAndPutPreference(controller, key);
146

    
147
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.CommonNameReferencesWithMarker);
148
        loadAndPutPreference(controller, key);
149

    
150
        //sources
151
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowIdInSource);
152
        loadAndPutPreference(controller, key);
153

    
154
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowNamespaceInSource);
155
        loadAndPutPreference(controller, key);
156

    
157
        //multiple classifications / taxon navigator
158

    
159
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.DisableMultiClassification);
160
        loadAndPutPreference(controller, key);
161

    
162

    
163
        key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), EditorPreferencePredicate.ShowTaxonNodeWizard);
164
        loadAndPutPreference(controller, key);
165

    
166

    
167
    }
168

    
169
    private void loadAndPutPreference(ICdmRepository controller, PrefKey key) {
170
        if (controller == null){
171
            return ;
172
        }
173
        CdmPreference preference = controller.getPreferenceService().find(key);
174
        if (preference != null){
175
            put(preference);
176
        }
177
    }
178

    
179
}
(1-1/30)