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