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.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   * Default implementation of {@link PageEditorStatus}.
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()) { // This takes care of optional, not-yet-created areas.
125                     node = NodeUtil.createPath(jcrSession.getRootNode(), area.getPath(), NodeTypes.Area.NAME); //this can be saved via info.magnolia.pages.app.detail.action.CreateAreaAction.execute
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 }