View Javadoc
1   /*
2    * Copyright (C) 2003-2019 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wcm.webui.seo;
18  
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.exoplatform.portal.application.PortalRequestContext;
22  import org.exoplatform.portal.mop.SiteKey;
23  import org.exoplatform.portal.webui.util.Util;
24  import org.exoplatform.services.jcr.util.Text;
25  import org.exoplatform.services.log.ExoLogger;
26  import org.exoplatform.services.log.Log;
27  import org.exoplatform.services.seo.PageMetadataModel;
28  import org.exoplatform.services.seo.SEOService;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  import org.exoplatform.wcm.webui.Utils;
31  import org.exoplatform.wcm.webui.reader.ContentReader;
32  import org.exoplatform.webui.application.WebuiRequestContext;
33  import org.exoplatform.webui.config.annotation.ComponentConfig;
34  import org.exoplatform.webui.config.annotation.EventConfig;
35  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
36  import org.exoplatform.webui.event.Event;
37  import org.exoplatform.webui.event.EventListener;
38  import org.exoplatform.webui.form.UIForm;
39  
40  import javax.jcr.Node;
41  import javax.jcr.PathNotFoundException;
42  import java.util.ArrayList;
43  import java.util.Enumeration;
44  import java.util.List;
45  import java.util.Locale;
46  /**
47   * Created by The eXo Platform SAS
48   * Author : eXoPlatform
49   *          exo@exoplatform.com
50   * Jul 4, 2011
51   */
52  
53  @ComponentConfig(lifecycle = UIFormLifecycle.class,
54  template = "classpath:groovy/webui/seo/UISEOPortletToolbar.gtmpl", events = {
55    @EventConfig(listeners = UISEOToolbarForm.AddSEOActionListener.class)
56  })
57  public class UISEOToolbarForm extends UIForm {
58  
59    private static final Log LOG = ExoLogger.getLogger(UISEOToolbarForm.class.getName());
60  
61    /** The Constant SEO_POPUP_WINDOW. */
62    public static final String SEO_POPUP_WINDOW = "UISEOPopupWindow";
63    private ArrayList<String> paramsArray = null;
64    private String pageReference = null;
65    private PageMetadataModel metaModel = null;
66    private String fullStatus = "Empty";
67    private String lang = null;
68  
69    public UISEOToolbarForm() {
70    }
71  
72    public ArrayList<String> getParamsArray() {
73      return paramsArray;
74    }
75  
76    public String getPageReference() {
77      return pageReference;
78    }
79  
80    public PageMetadataModel getMetaModel() {
81      return metaModel;
82    }
83  
84    public void setMetaModel(PageMetadataModel metaModel) {
85      this.metaModel = metaModel;
86    }
87  
88    public static class AddSEOActionListener extends EventListener<UISEOToolbarForm> {
89      public void execute(Event<UISEOToolbarForm> event) throws Exception {
90        UISEOToolbarForm uiSEOToolbar = event.getSource();
91        PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
92        UISEOForm uiSEOForm = uiSEOToolbar.createUIComponent(UISEOForm.class, null, null);
93        SEOService seoService = WCMCoreUtils.getService(SEOService.class);
94        ArrayList<String> paramsArray = uiSEOToolbar.getParamsArray();
95        if(paramsArray != null) {
96          for(int i = 0; i < paramsArray.size(); i++) {
97            Node contentNode = seoService.getContentNode(paramsArray.get(i));
98            if(contentNode != null) {
99              uiSEOForm.setOnContent(true);
100             uiSEOForm.setContentPath(paramsArray.get(i));
101             uiSEOForm.setContentURI(contentNode.getUUID());
102             break;
103           }
104         }
105         uiSEOToolbar.setMetaModel(seoService.getContentMetadata(paramsArray, uiSEOToolbar.lang));
106       } else {
107         uiSEOForm.setContentPath(uiSEOToolbar.getPageReference());
108         uiSEOForm.setOnContent(false);
109         uiSEOToolbar.setMetaModel(seoService.getPageMetadata(uiSEOToolbar.getPageReference(), uiSEOToolbar.lang));
110       }     
111 
112       uiSEOForm.setParamsArray(paramsArray);
113       if(uiSEOToolbar.getMetaModel() == null) {
114         //If have node seo data for default language, displaying seo data for the first language in the list
115         List<Locale> seoLocales = seoService.getSEOLanguages(portalRequestContext.getPortalOwner(), uiSEOForm.getContentPath(),
116                                                 uiSEOForm.getOnContent());
117         if(seoLocales.size()> 0) {
118           Locale locale = seoLocales.get(0);
119           StringBuffer sb = new StringBuffer();
120           sb.append(locale.getLanguage());
121           String country = locale.getCountry(); 
122           if(StringUtils.isNotEmpty(country)) sb.append("_").append(country);
123           String lang = sb.toString();
124           uiSEOToolbar.setMetaModel(seoService.getMetadata(uiSEOForm.getParamsArray(), uiSEOToolbar.getPageReference(), lang));
125           uiSEOForm.setSelectedLanguage(lang);
126         }
127       } 
128       uiSEOForm.initSEOForm(uiSEOToolbar.getMetaModel());
129 
130       Utils.createPopupWindow(uiSEOToolbar, uiSEOForm, SEO_POPUP_WINDOW, true, 640);
131     }
132   }
133 
134   public void processRender(WebuiRequestContext context) throws Exception {
135     PortalRequestContext pcontext = Util.getPortalRequestContext();
136     StringBuffer sb = new StringBuffer();
137     sb.append(pcontext.getLocale().getLanguage());
138     if(StringUtils.isNotEmpty(pcontext.getLocale().getCountry()))
139       sb.append("_").append(pcontext.getLocale().getCountry());
140     lang = sb.toString();
141     String portalName = pcontext.getPortalOwner();
142     metaModel = null;
143     fullStatus = "Empty";
144     if (!pcontext.useAjax()) {      
145       paramsArray = null;
146       String contentParam;
147       Enumeration params = pcontext.getRequest().getParameterNames();
148       if(params.hasMoreElements()) {
149         paramsArray = new ArrayList<>();
150         while(params.hasMoreElements()) {
151           contentParam = params.nextElement().toString();
152           String contentValue;
153           try {
154             contentValue = Text.unescape(pcontext.getRequestParameter(contentParam));
155           } catch(Exception ex) {
156             contentValue = pcontext.getRequestParameter(contentParam);
157           }
158           contentValue = ContentReader.getXSSCompatibilityContent(contentValue);
159           if(paramsArray != null) {
160             paramsArray.add(Text.escapeIllegalJcrChars(contentValue));
161           }
162         }
163       }
164     }
165     SEOService seoService = WCMCoreUtils.getService(SEOService.class);
166     pageReference = Util.getUIPortal().getSelectedUserNode().getPageRef().format();
167 
168     if(pageReference != null) {
169       SiteKey siteKey = Util.getUIPortal().getSelectedUserNode().getNavigation().getKey();
170       SiteKey portalKey = SiteKey.portal(portalName);
171       if(siteKey != null && siteKey.equals(portalKey)) {
172         metaModel = seoService.getPageMetadata(pageReference, lang);
173         if(paramsArray != null) {
174           PageMetadataModel tmpModel = null;
175           try{
176             tmpModel = seoService.getContentMetadata(paramsArray,lang);
177           }catch(PathNotFoundException ex) {
178             if (LOG.isErrorEnabled()) {
179               LOG.error("Cannot found the content metadata", ex);
180             }
181           }
182           if(tmpModel != null) {
183             metaModel = tmpModel;
184           } else {
185             try {
186               for(int i = 0;i < paramsArray.size();i++) {
187                 Node contentNode = seoService.getContentNode(paramsArray.get(i).toString());
188 
189                 if(contentNode != null ) {
190                   metaModel = null;
191                   break;
192                 }
193               }
194             }catch(PathNotFoundException ex) {
195               metaModel = null;
196             }
197           }
198         }
199       }
200       else fullStatus = "Disabled";      	
201     }  
202     if(metaModel != null)
203       fullStatus = metaModel.getFullStatus();
204     super.processRender(context);
205   }
206 
207   public String getFullStatus() {
208     return this.fullStatus;
209   }
210 }