-
Notifications
You must be signed in to change notification settings - Fork 1
/
SettingService.java
365 lines (309 loc) · 9.94 KB
/
SettingService.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright 2022 youngmonkeys.org
*
* Licensed under the ezyplatform, Version 1.0.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://youngmonkeys.org/licenses/ezyplatform-1.0.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.youngmonkeys.ezyplatform.service;
import com.tvd12.ezyfox.exception.EzyNotImplementedException;
import com.tvd12.ezyfox.function.EzyExceptionFunction;
import com.tvd12.ezyhttp.core.codec.SingletonStringDeserializer;
import org.youngmonkeys.ezyplatform.entity.DataType;
import org.youngmonkeys.ezyplatform.util.Uris;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import static com.tvd12.ezyfox.io.EzyStrings.isNotBlank;
import static org.youngmonkeys.ezyplatform.constant.CommonConstants.*;
@SuppressWarnings("MethodCount")
public interface SettingService {
int DEFAULT_CACHE_PERIOD_IN_SECOND = 5;
void watchLastUpdatedTime(
String settingName,
int periodInSecond,
Runnable onLastUpdatedTimeChange
);
default void watchLastUpdatedTime(
String settingName,
Runnable onLastUpdatedTimeChange
) {
watchLastUpdatedTime(
settingName,
DEFAULT_CACHE_PERIOD_IN_SECOND,
onLastUpdatedTimeChange
);
}
void watchLastUpdatedTimeAndCache(
String settingName,
int periodInSecond
);
default void watchLastUpdatedTimeAndCache(
String settingName
) {
watchLastUpdatedTimeAndCache(
settingName,
DEFAULT_CACHE_PERIOD_IN_SECOND
);
}
void addValueConverter(
String settingName,
EzyExceptionFunction<String, Object> converter
);
void scheduleCacheValue(
String settingName,
int periodInSecond
);
default void scheduleCacheValue(String settingName) {
scheduleCacheValue(
settingName,
DEFAULT_CACHE_PERIOD_IN_SECOND
);
}
default void saveSetting(
String name,
DataType dataType,
Object value
) {
throw new EzyNotImplementedException(
"saveSetting has not implemented yet"
);
}
String getDecryptionValue(String settingName);
default <T> T getCachedValue(String settingName) {
return getCachedValue(settingName, null);
}
<T> T getCachedValue(String settingName, T defaultValue);
Optional<String> getSettingValue(String settingName);
long getMaxUploadFileSize();
Set<String> getAcceptedMediaMimeTypes();
default String encryptValue(String settingValue) {
throw new EzyNotImplementedException(
"encryptValue has not implemented yet"
);
}
String decryptValue(String encryptedSettingValue);
<T> T getObjectValue(String settingName, Class<T> objectType);
default <T> T getObjectValue(
String settingName,
Class<T> objectType,
T defaultValue
) {
T answer = getObjectValue(
settingName,
objectType
);
return answer != null ? answer : defaultValue;
}
default <T> T getObjectValueOrDefault(
String settingName,
Class<T> objectType,
Supplier<T> defaultValueSupplier
) {
T answer = getObjectValue(
settingName,
objectType
);
return answer != null
? answer
: defaultValueSupplier.get();
}
default String getPasswordValue(String settingName) {
return getDecryptionValue(settingName);
}
default boolean getBooleanValue(String settingName) {
return getBooleanValue(settingName, false);
}
default boolean getBooleanValue(
String settingName,
boolean defaultValue
) {
return getSettingValue(settingName)
.map(it -> {
try {
return Boolean.parseBoolean(it);
} catch (Exception e) {
return defaultValue;
}
})
.orElse(defaultValue);
}
default int getIntValue(String settingName) {
return getIntValue(settingName, 0);
}
default int getIntValue(String settingName, int defaultValue) {
return (int) getLongValue(settingName, defaultValue);
}
default long getLongValue(String settingName) {
return getLongValue(settingName, 0L);
}
default long getLongValue(String settingName, long defaultValue) {
return getSettingValue(settingName)
.map(it -> {
try {
return Long.parseLong(it);
} catch (Exception e) {
return defaultValue;
}
})
.orElse(defaultValue);
}
default String getTextValue(String settingName) {
return getTextValue(settingName, null);
}
default String getTextValue(String settingName, String defaultValue) {
return getSettingValue(settingName)
.orElse(defaultValue);
}
default <T> T getArrayValue(String settingName, Class<T> arrayType) {
String textValue = getTextValue(settingName);
if (textValue == null) {
return null;
}
try {
return SingletonStringDeserializer.getInstance()
.deserialize(textValue, arrayType);
} catch (Exception e) {
return null;
}
}
default Set<String> getSetStringValue(String settingName) {
return getSetValue(settingName, String.class);
}
default Set<String> getSetStringValue(
String settingName,
Set<String> defaultValue
) {
Set<String> value = getSetValue(settingName, String.class);
return value.isEmpty() ? defaultValue : value;
}
@SuppressWarnings("unchecked")
default <T> Set<T> getSetValue(String settingName, Class<T> valueType) {
String textValue = getTextValue(settingName);
if (textValue == null) {
return Collections.emptySet();
}
try {
return SingletonStringDeserializer
.getInstance()
.deserialize(textValue, Set.class, valueType);
} catch (Exception e) {
return Collections.emptySet();
}
}
default List<String> getListStringValue(String settingName) {
return getListValue(settingName, String.class);
}
default List<String> getListStringValue(
String settingName,
List<String> defaultValue
) {
List<String> value = getListValue(settingName, String.class);
return value.isEmpty() ? defaultValue : value;
}
@SuppressWarnings("unchecked")
default <T> List<T> getListValue(String settingName, Class<T> valueType) {
String textValue = getTextValue(settingName);
if (textValue == null) {
return Collections.emptyList();
}
try {
return SingletonStringDeserializer
.getInstance()
.deserialize(textValue, List.class, valueType);
} catch (Exception e) {
return Collections.emptyList();
}
}
default String getWebSiteName() {
String siteTitle = getTextValue(
SETTING_NAME_WEB_SITE_TITLE
);
if (isNotBlank(siteTitle)) {
return siteTitle;
}
String webUrl = getWebUrl();
if (isNotBlank(webUrl)) {
return Uris.getSiteName(webUrl);
}
String adminUrl = getAdminUrl();
return Uris.getSiteName(adminUrl, "Website");
}
default String getAdminDateFormat() {
return getTextValue(
SETTING_NAME_ADMIN_DATE_FORMAT,
DEFAULT_DATE_FORMAT
);
}
default String getAdminDateTimeFormat() {
return getTextValue(
SETTING_NAME_ADMIN_DATETIME_FORMAT,
DEFAULT_DATETIME_FORMAT
);
}
default String getWebDateFormat() {
return getTextValue(
SETTING_NAME_WEB_DATE_FORMAT,
DEFAULT_DATE_FORMAT
);
}
default String getWebDateTimeFormat() {
return getTextValue(
SETTING_NAME_WEB_DATETIME_FORMAT,
DEFAULT_DATETIME_FORMAT
);
}
default String getAdminUrl() {
return getTextValue(
SETTING_NAME_ADMIN_URL,
DEFAULT_ADMIN_URL
);
}
default String getWebUrl() {
return getTextValue(SETTING_NAME_WEB_URL, DEFAULT_WEB_URL);
}
default String getWebManagementUrl() {
return getTextValue(
SETTING_NAME_WEB_MANAGEMENT_URL,
DEFAULT_WEB_MANAGEMENT_URL
);
}
default String getWebsocketUrl() {
return getTextValue(SETTING_NAME_WEBSOCKET_URL, DEFAULT_WEBSOCKET_URL);
}
default long getUserTokenExpiredTimeInDay() {
return getLongValue(
SETTING_NAME_USER_TOKEN_EXPIRED_IN_DAY,
DEFAULT_TOKEN_EXPIRED_IN_DAY
);
}
default boolean isAllowSendStatisticsData() {
return getBooleanValue(
SETTING_NAME_ALLOW_SEND_STATISTICS_DATA,
true
);
}
default boolean containSetting(String settingName) {
return getSettingValue(settingName)
.isPresent();
}
default String resolveAdminUri(String uri) {
return Uris.resolveUrl(getAdminUrl(), uri);
}
default String resolveWebUri(String uri) {
return Uris.resolveUrl(getWebUrl(), uri);
}
default String resolveWebManagementUri(String uri) {
return Uris.resolveUrl(getWebManagementUrl(), uri);
}
}