View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Default implementation of {@link PageEditorStatus}.
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()) { // This takes care of optional, not-yet-created areas.
124                     node = NodeUtil.createPath(jcrSession.getRootNode(), area.getPath(), NodeTypes.Area.NAME); //this can be saved via info.magnolia.pages.app.detail.action.CreateAreaAction.execute
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 }