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 package info.magnolia.module.templatingcomponents.components;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.core.AggregationState;
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.core.ItemType;
40 import info.magnolia.cms.i18n.MessagesManager;
41 import info.magnolia.cms.security.Permission;
42 import info.magnolia.module.templating.ParagraphManager;
43 import info.magnolia.module.templating.RenderableDefinition;
44 import info.magnolia.module.templating.TemplateManager;
45 import info.magnolia.module.templatingcomponents.AuthoringUiComponent;
46 import org.apache.commons.lang.StringUtils;
47
48 import javax.jcr.RepositoryException;
49 import java.io.IOException;
50 import java.util.List;
51
52
53
54
55
56
57
58
59
60
61
62 public abstract class AbstractAuthoringUiComponent implements AuthoringUiComponent {
63 private static final String DEFAULT_I18N_BASENAME = "info.magnolia.module.templatingcomponents.messages";
64
65 private final ServerConfiguration server;
66 private final AggregationState aggregationState;
67 private final TemplateManager templateManager;
68 private final ParagraphManager paragraphManagerManager;
69
70 protected AbstractAuthoringUiComponent(final ServerConfiguration server, final AggregationState aggregationState) {
71 this.server = server;
72 this.aggregationState = aggregationState;
73
74 this.templateManager = TemplateManager.getInstance();
75 this.paragraphManagerManager = ParagraphManager.getInstance();
76 }
77
78 protected ServerConfiguration getServer() {
79 return server;
80 }
81
82 protected AggregationState getAggregationState() {
83 return aggregationState;
84 }
85
86 public void render(Appendable out) throws IOException {
87 if (!shouldRender()) {
88 return;
89 }
90 try {
91 doRender(out);
92 } catch (RepositoryException e) {
93 throw new RuntimeException(e);
94 }
95 }
96
97 protected abstract void doRender(Appendable out) throws IOException, RepositoryException;
98
99
100
101
102 public void postRender() {
103 }
104
105
106
107
108
109 protected Content currentContent() {
110 final Content currentContent = aggregationState.getCurrentContent();
111 if (currentContent == null) {
112 throw new IllegalStateException("Could not determine currentContent from AggregationState, currentContent is null");
113 }
114 return currentContent;
115 }
116
117
118
119
120 protected boolean shouldRender() {
121 return (server.isAdmin() && aggregationState.getMainContent().isGranted(Permission.SET));
122 }
123
124
125
126
127
128 protected String getMessage(Content context, String key) {
129 final String i18Basename = getI18BasenameFor(context);
130 return getMessage(i18Basename, key);
131 }
132
133 protected String getI18BasenameFor(Content content) {
134 final String templateName = content.getMetaData().getTemplate();
135 final RenderableDefinition renderable;
136 if (content.isNodeType(ItemType.CONTENT.getSystemName())) {
137 renderable = templateManager.getTemplateDefinition(templateName);
138 } else {
139 renderable = paragraphManagerManager.getParagraphDefinition(templateName);
140 }
141 if (renderable != null && renderable.getI18nBasename() != null) {
142 return renderable.getI18nBasename();
143 } else {
144 return DEFAULT_I18N_BASENAME;
145 }
146 }
147
148 protected String getMessage(String basename, String key) {
149 String s = MessagesManager.getMessages(basename).getWithDefault(key, key);
150 if (key.equals(s)) {
151
152 s = MessagesManager.getMessages(DEFAULT_I18N_BASENAME).getWithDefault(key, key);
153 }
154 return s;
155 }
156
157
158
159
160 protected String asString(List<String> strings) {
161 return StringUtils.join(strings, ',');
162 }
163
164 }