forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingsWrapper.java
276 lines (223 loc) · 8.02 KB
/
SettingsWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.harvard.iq.dataverse;
import edu.harvard.iq.dataverse.branding.BrandingUtil;
import edu.harvard.iq.dataverse.settings.Setting;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean.Key;
import edu.harvard.iq.dataverse.util.MailUtil;
import edu.harvard.iq.dataverse.util.StringUtil;
import edu.harvard.iq.dataverse.util.config.SystemConfig;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.mail.internet.InternetAddress;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author gdurand
*/
@ViewScoped
@Named
public class SettingsWrapper implements java.io.Serializable {
@EJB
SettingsServiceBean settingService;
@EJB
DataverseServiceBean dataverseService;
@EJB
SystemConfig systemConfig;
private Map<String, String> settingsMap;
// Related to a specific setting for guide urls
private String guidesBaseUrl = null;
public String get(String settingKey) {
if (settingsMap == null) {
initSettingsMap();
}
return settingsMap.get(settingKey);
}
/**
* Return value from map, initiating settings map if needed
* @param settingKey
* @param defaultValue
* @return
*/
public String get(String settingKey, String defaultValue) {
if (settingsMap == null) {
initSettingsMap();
}
if (!settingsMap.containsKey(settingKey)){
return defaultValue;
}
return settingsMap.get(settingKey);
}
/**
* Pass the map key as a "Key" object instead of a string
*
* @param key
* @return
*/
public String getValueForKey(Key key){
if (key == null){
return null;
}
return get(key.toString());
}
/**
* Pass the map key as a "Key" object instead of a string
* Allow a default value if null is encountered
*
* @param key
* @param defaultValue
* @return
*/
public String getValueForKey(Key key, String defaultValue){
if (key == null){
return null;
}
return get(key.toString(), defaultValue);
}
public boolean isTrueForKey(Key key, boolean safeDefaultIfKeyNotFound) {
return isTrueForKey(key.toString(), safeDefaultIfKeyNotFound);
}
public boolean isTrueForKey(String settingKey, boolean safeDefaultIfKeyNotFound) {
if (settingsMap == null) {
initSettingsMap();
}
String val = get(settingKey);;
return ( val==null ) ? safeDefaultIfKeyNotFound : StringUtil.isTrue(val);
}
private void initSettingsMap() {
// initialize settings map
settingsMap = new HashMap<>();
for (Setting setting : settingService.listAll()) {
settingsMap.put(setting.getName(), setting.getContent());
}
}
public String getGuidesBaseUrl() {
if (true)
if (guidesBaseUrl == null) {
String saneDefault = "https://guides.dataverse.org";
guidesBaseUrl = getValueForKey(SettingsServiceBean.Key.GuidesBaseUrl);
if (guidesBaseUrl == null) {
guidesBaseUrl = saneDefault + "/en";
} else {
guidesBaseUrl = guidesBaseUrl + "/en";
}
// TODO:
// hard-coded "en"; will need to be configuratble once
// we have support for other languages.
// TODO:
// remove a duplicate of this method from SystemConfig
}
return guidesBaseUrl;
}
public String getGuidesVersion() {
return systemConfig.getGuidesVersion();
}
public boolean isPublicInstall(){
return systemConfig.isPublicInstall();
}
public boolean isRsyncUpload() {
return systemConfig.isRsyncUpload();
}
public boolean isRsyncDownload() {
return systemConfig.isRsyncDownload();
}
public boolean isRsyncOnly() {
return systemConfig.isRsyncOnly();
}
public boolean isHTTPUpload(){
return systemConfig.isHTTPUpload();
}
public boolean isDataFilePIDSequentialDependent(){
return systemConfig.isDataFilePIDSequentialDependent();
}
public String getSupportTeamName() {
String systemEmail = getValueForKey(SettingsServiceBean.Key.SystemEmail);
InternetAddress systemAddress = MailUtil.parseSystemAddress(systemEmail);
return BrandingUtil.getSupportTeamName(systemAddress, dataverseService.findRootDataverse().getName());
}
public String getSupportTeamEmail() {
String systemEmail = getValueForKey(SettingsServiceBean.Key.SystemEmail);
InternetAddress systemAddress = MailUtil.parseSystemAddress(systemEmail);
return BrandingUtil.getSupportTeamEmailAddress(systemAddress) != null ? BrandingUtil.getSupportTeamEmailAddress(systemAddress) : BrandingUtil.getSupportTeamName(systemAddress, dataverseService.findRootDataverse().getName());
}
public Integer getUploadMethodsCount() {
return systemConfig.getUploadMethodCount();
}
public boolean isRootDataverseThemeDisabled() {
return isTrueForKey(Key.DisableRootDataverseTheme, false);
}
public String getDropBoxKey() {
String configuredDropBoxKey = System.getProperty("dataverse.dropbox.key");
if (configuredDropBoxKey != null) {
return configuredDropBoxKey;
}
return "";
}
public Boolean isHasDropBoxKey() {
return !getDropBoxKey().isEmpty();
}
// Language Locales Configuration:
// Map from locale to display name eg en -> English
private Map<String, String> configuredLocales;
public boolean isLocalesConfigured() {
if (configuredLocales == null) {
initLocaleSettings();
}
return configuredLocales.size() > 1;
}
public Map<String, String> getConfiguredLocales() {
if (configuredLocales == null) {
initLocaleSettings();
}
return configuredLocales;
}
private void initLocaleSettings() {
configuredLocales = new LinkedHashMap<>();
try {
JSONArray entries = new JSONArray(getValueForKey(SettingsServiceBean.Key.Languages, "[]"));
for (Object obj : entries) {
JSONObject entry = (JSONObject) obj;
String locale = entry.getString("locale");
String title = entry.getString("title");
configuredLocales.put(locale, title);
}
} catch (JSONException e) {
//e.printStackTrace();
// do we want to know? - probably not
}
}
public boolean isDoiInstallation() {
String protocol = getValueForKey(SettingsServiceBean.Key.Protocol);
if ("doi".equals(protocol)) {
return true;
} else {
return false;
}
}
public boolean isDataCiteInstallation() {
String protocol = getValueForKey(SettingsServiceBean.Key.DoiProvider);
if ("DataCite".equals(protocol)) {
return true;
} else {
return false;
}
}
public boolean isMakeDataCountDisplayEnabled() {
boolean safeDefaultIfKeyNotFound = (getValueForKey(SettingsServiceBean.Key.MDCLogPath)!=null); //Backward compatible
return isTrueForKey(SettingsServiceBean.Key.DisplayMDCMetrics, safeDefaultIfKeyNotFound);
}
public boolean displayChronologicalDateFacets() {
//Defaults to true
return isTrueForKey(SettingsServiceBean.Key.ChronologicalDateFacets, true);
}
}