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