/
SystemConfig.java
114 lines (91 loc) · 3.26 KB
/
SystemConfig.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
package com.nateyolles.sling.publick.components.admin;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import com.nateyolles.sling.publick.services.SystemSettingsService;
import com.nateyolles.sling.publick.sightly.WCMUse;
/**
* Sightly component to get System Settings such as blog name.
*/
public class SystemConfig extends WCMUse {
/** The Sling Script Helper to get services. */
private SlingScriptHelper scriptHelper;
/** The current resource. */
private Resource resource;
/** The name of the blog. */
private String blogName;
/** The file system temporary directory */
private String temporaryDirectory;
/** Setting for extensionless URLs. */
private boolean extensionlessUrls;
/** The separator between blog name and page title. */
private static final String TITLE_SEPARATOR = " - ";
/** The title property of the resource. */
private static final String TITLE_PROPERTY = "title";
/** Initialize the Sightly component. */
@Override
public void activate() {
scriptHelper = getSlingScriptHelper();
resource = getResource();
SystemSettingsService systemSettingsService = scriptHelper.getService(SystemSettingsService.class);
if (systemSettingsService != null) {
blogName = systemSettingsService.getBlogName();
extensionlessUrls = systemSettingsService.getExtensionlessUrls();
temporaryDirectory = systemSettingsService.getTemporaryDirectory();
}
}
/**
* Get the name of the blog.
*
* @return The name of the blog.
*/
public String getBlogName() {
return blogName;
}
/**
* Get the temporary directory.
*
* @return The temporary directory.
*/
public String getTemporaryDirectory() {
return temporaryDirectory;
}
/**
* Get the setting for extensionless URLs.
*
* @return The setting for extensionless URLs.
*/
public boolean getExtensionlessUrls() {
return extensionlessUrls;
}
/**
* Get the title of the blog.
*
* The title of the blog is the blog post title, a separator (" - "),
* and the name of the blog as configured.
*
* @return The title of the blog.
*/
public String getTitle() {
StringBuilder title = new StringBuilder();
ValueMap properties = resource.adaptTo(ValueMap.class);
String jcrTitle = properties.get("jcr:title", String.class);
String titleProperty = properties.get(TITLE_PROPERTY, String.class);
String resourceName = resource.getName();
if (StringUtils.isNotBlank(jcrTitle)) {
title.append(jcrTitle);
} else if (StringUtils.isNotBlank(titleProperty)) {
title.append(titleProperty);
} else if (StringUtils.isNotBlank(resourceName)) {
title.append(resourceName);
}
if (title.length() > 0 && StringUtils.isNotBlank(blogName)) {
title.append(TITLE_SEPARATOR);
}
if (StringUtils.isNotBlank(blogName)) {
title.append(blogName);
}
return title.toString();
}
}