001/* 002 * Copyright 2019 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.languages.Language; 027import org.ametys.cms.languages.LanguagesManager; 028import org.ametys.runtime.i18n.I18nizableText; 029import org.ametys.web.repository.page.Page; 030import org.ametys.web.repository.page.SitemapElement; 031import org.ametys.web.repository.sitemap.Sitemap; 032 033/** 034 * Abstract client side element for pages or sitemap. 035 * 036 */ 037public abstract class AbstractSitemapElementClientSideElement extends AbstractPageClientSideElement 038{ 039 private LanguagesManager _languageManager; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 super.service(smanager); 045 _languageManager = (LanguagesManager) smanager.lookup(LanguagesManager.ROLE); 046 } 047 048 /** 049 * Get the default sitemap element's parameters 050 * @param sitemapElement The page or sitemap 051 * @return The default parameters 052 */ 053 protected Map<String, Object> getSitemapElementDefaultParameters (SitemapElement sitemapElement) 054 { 055 if (sitemapElement instanceof Page) 056 { 057 return getPageDefaultParameters((Page) sitemapElement); 058 } 059 else 060 { 061 return getSitemapDefaultParameters((Sitemap) sitemapElement); 062 } 063 } 064 065 /** 066 * Get the default sitemap's parameters 067 * @param sitemap The sitemap 068 * @return The default parameters 069 */ 070 protected Map<String, Object> getSitemapDefaultParameters (Sitemap sitemap) 071 { 072 Map<String, Object> sitemapParams = new HashMap<>(); 073 sitemapParams.put("id", sitemap.getId()); 074 sitemapParams.put("title", getSitemapTitle(sitemap)); 075 sitemapParams.put("name", sitemap.getSitemapName()); 076 077 return sitemapParams; 078 } 079 080 /** 081 * Get the sitemap's title 082 * @param sitemap the sitemap 083 * @return the sitemap's title 084 */ 085 protected I18nizableText getSitemapTitle(Sitemap sitemap) 086 { 087 Language language = _languageManager.getLanguage(sitemap.getName()); 088 return language != null ? language.getLabel() : new I18nizableText(sitemap.getName()); 089 } 090 091 /** 092 * Get the description when user has no right on sitemap element 093 * @param sitemapElement The page or sitemap 094 * @return the description 095 */ 096 protected I18nizableText getNoRightSitemapElementDescription(SitemapElement sitemapElement) 097 { 098 if (sitemapElement instanceof Page) 099 { 100 return getNoRightPageDescription((Page) sitemapElement); 101 } 102 else 103 { 104 return getNoRightSitemapDescription((Sitemap) sitemapElement); 105 } 106 } 107 108 /** 109 * Get the description when user has no right on sitemap 110 * @param sitemap The sitemap 111 * @return the description 112 */ 113 protected I18nizableText getNoRightSitemapDescription(Sitemap sitemap) 114 { 115 List<String> i18nParameters = new ArrayList<>(); 116 i18nParameters.add(sitemap.getSitemapName()); 117 118 I18nizableText ed = (I18nizableText) this._script.getParameters().get("noright-sitemap-description"); 119 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 120 } 121 122 /** 123 * Get i18n description for all right sitemap element 124 * @param sitemapElement The page or sitemap 125 * @return The {@link I18nizableText} description 126 */ 127 protected I18nizableText getAllRightSitemapElementDescription(SitemapElement sitemapElement) 128 { 129 if (sitemapElement instanceof Page) 130 { 131 return getAllRightPageDescription((Page) sitemapElement); 132 } 133 else 134 { 135 return getAllRightSitemapDescription((Sitemap) sitemapElement); 136 } 137 } 138 /** 139 * Get i18n description for all right sitemap 140 * @param sitemap The sitemap 141 * @return The {@link I18nizableText} description 142 */ 143 protected I18nizableText getAllRightSitemapDescription(Sitemap sitemap) 144 { 145 List<String> i18nParameters = new ArrayList<>(); 146 i18nParameters.add(sitemap.getSitemapName()); 147 148 I18nizableText ed = (I18nizableText) this._script.getParameters().get("allright-sitemap-description"); 149 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 150 } 151}