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.pages.app.detail;
35
36 import info.magnolia.context.WebContext;
37 import info.magnolia.jcr.util.NodeTypes;
38 import info.magnolia.jcr.util.NodeUtil;
39 import info.magnolia.jcr.util.PropertyUtil;
40 import info.magnolia.link.LinkUtil;
41 import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
42 import info.magnolia.ui.contentapp.detail.ContentDetailSubApp;
43 import info.magnolia.ui.datasource.jcr.JcrDatasource;
44 import info.magnolia.ui.ValueContext;
45 import info.magnolia.ui.vaadin.editor.gwt.shared.PlatformType;
46 import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
47 import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
48 import info.magnolia.ui.vaadin.gwt.client.shared.ComponentElement;
49 import info.magnolia.ui.vaadin.gwt.client.shared.PageEditorParameters;
50
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Map;
56 import java.util.Optional;
57
58 import javax.inject.Inject;
59 import javax.inject.Provider;
60 import javax.jcr.Node;
61 import javax.jcr.Session;
62
63 import org.apache.commons.lang3.StringUtils;
64
65 import com.machinezoo.noexception.Exceptions;
66
67 import lombok.Data;
68
69
70
71
72 @Data
73 public class PageEditorStatus {
74
75 private static final String VERSION_PARAMETER = "mgnlVersion";
76 private static final String PREVIEW_PARAMETER = "mgnlPreview";
77 private static final String CHANNEL_PARAMETER = "mgnlChannel";
78 private static final String AVAILABLE_COMPONENTS_DIVIDER = ",";
79 private Locale locale;
80 private String version;
81 private String nodePath;
82 private String viewType;
83 private PlatformType platformType = PlatformType.DESKTOP;
84 private AbstractElement selectedElement;
85 private Map<String, String> i18nKeys;
86
87 private final I18NAuthoringSupport<Node> i18NAuthoringSupport;
88 private final JcrDatasource datasource;
89 private final Provider<WebContext> webContextProvider;
90 private final ValueContext<Node> valueContext;
91
92 @Inject
93 public PageEditorStatus(I18NAuthoringSupport<Node> i18NAuthoringSupport, JcrDatasource datasource, Provider<WebContext> webContextProvider, ValueContext<Node> valueContext) {
94 this.i18NAuthoringSupport = i18NAuthoringSupport;
95 this.datasource = datasource;
96 this.webContextProvider = webContextProvider;
97 this.valueContext = valueContext;
98 }
99
100 public List<String> getAvailableTemplates() {
101 return Optional.ofNullable(selectedElement)
102 .map(element -> {
103 if (element instanceof AreaElement) {
104 return ((AreaElement) element).getAvailableComponents();
105 } else if (element instanceof ComponentElement) {
106 return ((ComponentElement) element).getAvailableTemplates();
107 } else {
108 return null;
109 }
110 })
111 .map(availableComponents -> availableComponents.split(AVAILABLE_COMPONENTS_DIVIDER))
112 .map(Arrays::asList)
113 .orElseGet(ArrayList::new);
114 }
115
116 public void setSelectedElement(AbstractElement selectedElement) {
117 this.selectedElement = selectedElement;
118 Exceptions.wrap().run(() -> {
119 final Session jcrSession = datasource.getJCRSession();
120 Node node = null;
121 if (selectedElement instanceof AreaElement) {
122 AreaElement area = (AreaElement) selectedElement;
123 if (area.isOptional() && !area.isCreated()) {
124 node = NodeUtil.createPath(jcrSession.getRootNode(), area.getPath(), NodeTypes.Area.NAME);
125 }
126 }
127 if (node == null && jcrSession.nodeExists(selectedElement.getPath())) {
128 node = jcrSession.getNode(selectedElement.getPath());
129 }
130 valueContext.set(node);
131 });
132 }
133
134 void updateStatusFromLocation(ContentDetailSubApp.DetailLocation location) {
135 this.nodePath = location.getNodePath();
136 this.version = location.getVersion();
137 this.viewType = location.getViewType();
138 }
139
140 public PageEditorParameters getParameters() {
141 PageEditorParameters parameters = new PageEditorParameters();
142 parameters.setContextPath(webContextProvider.get().getContextPath());
143 parameters.setNodePath(nodePath);
144 parameters.setPlatformType(platformType);
145 parameters.setPreview(isPreview());
146 parameters.setI18nKeys(i18nKeys);
147 parameters.setSelectedElement(selectedElement);
148 parameters.setUrl(createUri());
149 return parameters;
150 }
151
152 public String getPageTitle() {
153 return Exceptions.wrap().get(() -> {
154 final Session session = datasource.getJCRSession();
155 if (session.nodeExists(nodePath)) {
156 Node node = session.getNode(nodePath);
157 if (!NodeUtil.isNodeType(node, NodeTypes.Page.NAME)) {
158 node = NodeUtil.getNearestAncestorOfType(node, NodeTypes.Page.NAME);
159 }
160 return PropertyUtil.getString(node, "title", (node != null ? node.getName() : ""));
161 } else {
162 return null;
163 }
164 });
165 }
166
167 private String createUri() {
168 return Exceptions.wrap().get(() -> {
169 final Session session = datasource.getJCRSession();
170 if (session.nodeExists(nodePath)) {
171 Node node = session.getNode(nodePath);
172 final StringBuffer uri = new StringBuffer(i18NAuthoringSupport.createI18NURI(node, locale));
173 LinkUtil.addParameter(uri, PREVIEW_PARAMETER, Boolean.toString(isPreview()));
174 LinkUtil.addParameter(uri, CHANNEL_PARAMETER, platformType.getId());
175 if (StringUtils.isNotEmpty(version)) {
176 LinkUtil.addParameter(uri, VERSION_PARAMETER, version);
177 }
178 return uri.toString();
179 } else {
180 return null;
181 }
182 });
183 }
184
185 private boolean isPreview() {
186 return "view".equals(viewType) || "version".equals(viewType);
187 }
188
189 void setPlatformType(PlatformType platformType) {
190 this.platformType = platformType;
191 }
192 }