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.editor.parameters;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.link.LinkUtil;
38  import info.magnolia.repository.RepositoryConstants;
39  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
40  import info.magnolia.ui.contentapp.detail.DetailLocation;
41  import info.magnolia.ui.contentapp.detail.DetailView;
42  import info.magnolia.ui.vaadin.editor.gwt.shared.PlatformType;
43  import info.magnolia.ui.vaadin.gwt.client.shared.AbstractElement;
44  import info.magnolia.ui.vaadin.gwt.client.shared.PageEditorParameters;
45  import info.magnolia.ui.vaadin.gwt.client.shared.PageElement;
46  
47  import java.util.Locale;
48  import java.util.Map;
49  
50  import javax.inject.Inject;
51  import javax.jcr.Node;
52  import javax.jcr.RepositoryException;
53  
54  import org.apache.commons.lang3.StringUtils;
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  /**
59   * Default implementation of {@link PageEditorStatus}.
60   */
61  public class DefaultPageEditorStatus implements PageEditorStatus {
62  
63      private static final Logger log = LoggerFactory.getLogger(DefaultPageEditorStatus.class);
64  
65      private final I18NAuthoringSupport i18NAuthoringSupport;
66      private Locale locale;
67      private String version;
68      private String nodePath;
69      private DetailView.ViewType viewType;
70      private PlatformType platFormType = PlatformType.DESKTOP;
71      private AbstractElement selectedElement;
72      private Map<String, String> i18nKeys;
73  
74      @Inject
75      public DefaultPageEditorStatus(I18NAuthoringSupport i18NAuthoringSupport) {
76          this.i18NAuthoringSupport = i18NAuthoringSupport;
77      }
78  
79      @Override
80      public void updateStatusFromLocation(DetailLocation location) {
81          this.nodePath = location.getNodePath();
82          this.version = location.getVersion();
83          this.viewType = location.getViewType();
84          this.selectedElement = null;
85      }
86  
87      @Override
88      public boolean isLocationChanged(DetailLocation location) {
89          DetailView.ViewType viewType = location.getViewType();
90          String path = location.getNodePath();
91  
92          if ((path.equals(nodePath) && DetailView.ViewType.VIEW.equals(viewType) == isPreview())
93                  && (location.getVersion() == null ? version == null : location.getVersion().equals(version))) {
94              return false;
95          }
96          return true;
97      }
98  
99      @Override
100     public String getNodePath() {
101         return nodePath;
102     }
103 
104     @Override
105     public PlatformType getPlatformType() {
106         return platFormType;
107     }
108 
109     @Override
110     public Locale getLocale() {
111         return locale;
112     }
113 
114     @Override
115     public String getVersion() {
116         return version;
117     }
118 
119     @Override
120     public boolean isPreview() {
121         return DetailView.ViewType.VIEW.equals(viewType);
122     }
123 
124     @Override
125     public AbstractElement getSelectedElement() {
126         return selectedElement;
127     }
128 
129     @Override
130     public void setNodePath(String nodePath) {
131         this.nodePath = nodePath;
132     }
133 
134     @Override
135     public void setPlatformType(PlatformType platform) {
136         this.platFormType = platform;
137     }
138 
139     @Override
140     public void setLocale(Locale locale) {
141         this.locale = locale;
142     }
143 
144     @Override
145     public void setSelectedElement(AbstractElement element) {
146         this.selectedElement = element;
147     }
148 
149     @Override
150     public void setI18nKeys(Map<String, String> i18nKeys) {
151         this.i18nKeys = i18nKeys;
152     }
153 
154     @Override
155     public PageEditorParameters getParameters() {
156         PageEditorParameters parameters = new PageEditorParameters();
157         parameters.setContextPath(MgnlContext.getContextPath());
158         parameters.setNodePath(nodePath);
159         parameters.setPlatformType(platFormType);
160         parameters.setPreview(isPreview());
161         parameters.setI18nKeys(i18nKeys);
162 
163         // Ideally we would pass the PageElement as well, but that would need more changes on the client-side, as 'null'
164         // currently resolves to the PageElement.
165         AbstractElement element = selectedElement instanceof PageElement ? null : selectedElement;
166         parameters.setSelectedElement(element);
167 
168         String uri = createUri(nodePath, isPreview(), version, platFormType, locale);
169         parameters.setUrl(uri);
170         return parameters;
171     }
172 
173     protected String createUri(String nodePath, boolean isPreview, String version, PlatformType platformType, Locale locale) {
174         String uri = "";
175         try {
176             Node node = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE).getNode(nodePath);
177             uri = i18NAuthoringSupport.createI18NURI(node, locale);
178             StringBuffer sb = new StringBuffer(uri);
179 
180 
181             LinkUtil.addParameter(sb, PREVIEW_PARAMETER, Boolean.toString(isPreview));
182 
183             LinkUtil.addParameter(sb, CHANNEL_PARAMETER, platformType.getId());
184 
185             if (StringUtils.isNotEmpty(version)) {
186                 LinkUtil.addParameter(sb, VERSION_PARAMETER, version);
187             }
188             uri = sb.toString();
189 
190         } catch (RepositoryException e) {
191             log.error("Could not get page node from location object.", e);
192         }
193         return uri;
194     }
195 }