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