001/* 002 * Copyright 2025 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.sitemap; 017 018import java.util.ArrayList; 019import java.util.Comparator; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.commons.lang3.StringUtils; 028 029import org.ametys.core.ui.ClientSideElementHelper; 030import org.ametys.core.ui.StaticClientSideElement; 031import org.ametys.core.util.JSONUtils; 032import org.ametys.web.repository.site.Site; 033import org.ametys.web.repository.site.SiteManager; 034 035/** 036 * Client side element for sitemap tree tool that supports indicators 037 * 038 */ 039public class SitemapTreeToolClientSideElement extends StaticClientSideElement 040{ 041 /** Extension point for indicators */ 042 protected SitemapDecoratorsProviderExtensionPoint _decoratorsProviderEP; 043 /** JSON utils */ 044 protected JSONUtils _jsonUtils; 045 /** Site manager */ 046 protected SiteManager _siteManager; 047 048 @Override 049 public void service(ServiceManager smanager) throws ServiceException 050 { 051 super.service(smanager); 052 _decoratorsProviderEP = (SitemapDecoratorsProviderExtensionPoint) smanager.lookup(SitemapDecoratorsProviderExtensionPoint.ROLE); 053 _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE); 054 _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE); 055 } 056 057 @Override 058 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 059 { 060 List<Script> scripts = super.getScripts(ignoreRights, contextParameters); 061 062 if (scripts.size() > 0) 063 { 064 Script script = ClientSideElementHelper.cloneScript(scripts.get(0)); 065 066 scripts = new ArrayList<>(); 067 String siteName = (String) contextParameters.get("siteName"); 068 Site site = _siteManager.getSite(siteName); 069 String skinId = site != null ? site.getSkinId() : StringUtils.EMPTY; 070 List<Map<String, Object>> indicators = new ArrayList<>(); 071 for (String providerId : _decoratorsProviderEP.getExtensionsIds()) 072 { 073 SitemapDecoratorsProvider decoratorsProvider = _decoratorsProviderEP.getExtension(providerId); 074 Set<SitemapTreeIndicator> decorators = decoratorsProvider.getDecorators(skinId); 075 for (SitemapTreeIndicator decorator : decorators) 076 { 077 indicators.add(_indicator2json(decorator)); 078 } 079 } 080 indicators.sort(new DecoratorOrderComparator()); 081 script.getParameters().put("indicators", _jsonUtils.convertObjectToJson(indicators)); 082 083 scripts = new ArrayList<>(); 084 scripts.add(script); 085 } 086 087 return scripts; 088 } 089 090 private Map<String, Object> _indicator2json(SitemapTreeIndicator indicator) 091 { 092 Map<String, Object> indicatorInfo = new HashMap<>(); 093 indicatorInfo.put("id", indicator.getId()); 094 indicatorInfo.put("label", indicator.getLabel()); 095 indicatorInfo.put("iconGlyph", indicator.getIconGlyph()); 096 indicatorInfo.put("icon", indicator.getIcon()); 097 indicatorInfo.put("decorator", indicator.getIconDecorator()); 098 indicatorInfo.put("priority", indicator.getPriority()); 099 return indicatorInfo; 100 } 101 102 class DecoratorOrderComparator implements Comparator<Map<String, Object>> 103 { 104 public int compare(Map<String, Object> o1, Map<String, Object> o2) 105 { 106 return (int) o1.get("priority") - (int) o2.get("priority"); 107 } 108 } 109}