1 | /** |
---|
2 | * Provides a service class with methods to interact with AppConfig. |
---|
3 | * AppConfig stores various application configuration (settings) in the database. |
---|
4 | */ |
---|
5 | class AppConfigService { |
---|
6 | |
---|
7 | boolean transactional = false |
---|
8 | |
---|
9 | /** |
---|
10 | * Set the value of an appConfig. |
---|
11 | * @param name The name of the appConfig. |
---|
12 | * @param value Set to this value if specified else defaults to "true". |
---|
13 | * @returns True if success otherwise false. |
---|
14 | */ |
---|
15 | public Boolean set(String name, String value = "true") { |
---|
16 | def appConfig = AppConfig.findByName(name) |
---|
17 | appConfig ? (appConfig.value = value) : (appConfig = new AppConfig(name: name, value: value)) |
---|
18 | appConfig.save() ? true : false |
---|
19 | } |
---|
20 | |
---|
21 | /** |
---|
22 | * Check if an appConfig exists. |
---|
23 | * @param name The name of the appConfig. |
---|
24 | * @returns True if success otherwise false. |
---|
25 | */ |
---|
26 | public Boolean exists(String name) { |
---|
27 | AppConfig.findByName(name) ? true : false |
---|
28 | } |
---|
29 | |
---|
30 | /** |
---|
31 | * Get the value of an appConfig. |
---|
32 | * @param name The name of the appConfig. |
---|
33 | * @returns The value of the appConfig else false. |
---|
34 | */ |
---|
35 | def getValue(String name) { |
---|
36 | def appConfig = AppConfig.findByName(name) |
---|
37 | appConfig ? appConfig.value : false |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Delete an appConfig. |
---|
42 | * @param name The name of the appConfig. |
---|
43 | * @returns True if success otherwise false. |
---|
44 | */ |
---|
45 | public Boolean delete(String name) { |
---|
46 | try { |
---|
47 | AppConfig.findByName(name).delete(flush:true) |
---|
48 | return true |
---|
49 | } |
---|
50 | catch(e) { |
---|
51 | log.debug("Could not delete, appConfig may not exist.") |
---|
52 | return false |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | } // end of class |
---|