001/* 002 * Copyright 2020 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.HashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.Optional; 022 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 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.Callable; 030import org.ametys.web.parameters.view.ViewParametersManager; 031import org.ametys.web.parameters.view.ViewParametersModel; 032import org.ametys.web.repository.page.ModifiableSitemapElement; 033import org.ametys.web.repository.page.ModifiableZoneItem; 034import org.ametys.web.repository.page.ZoneItem.ZoneType; 035import org.ametys.web.repository.site.Site; 036import org.ametys.web.skin.Skin; 037import org.ametys.web.skin.SkinTemplate; 038import org.ametys.web.skin.SkinTemplateZone; 039import org.ametys.web.skin.SkinsManager; 040 041/** 042 * This client side handles a controller enabled if the active page has at least one view parameter. 043 */ 044public class ConfigureViewParametersClientSideElement extends AbstractSitemapElementClientSideElement 045{ 046 /** The skins manager */ 047 protected SkinsManager _skinsManager; 048 049 /** The view parameters manager */ 050 protected ViewParametersManager _viewParametersManager; 051 052 @Override 053 public void service(ServiceManager smanager) throws ServiceException 054 { 055 super.service(smanager); 056 _skinsManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE); 057 _viewParametersManager = (ViewParametersManager) smanager.lookup(ViewParametersManager.ROLE); 058 } 059 060 @Override 061 public void configure(Configuration configuration) throws ConfigurationException 062 { 063 super.configure(configuration); 064 } 065 066 @Override 067 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 068 { 069 // TODO Auto-generated method stub 070 return super.getScripts(ignoreRights, contextParameters); 071 } 072 073 /** 074 * Get the status of the page for the view parameters 075 * @param pageId the page id 076 * @param zoneName the zone name 077 * @param zoneItemId the id of the zone item 078 * @return the status for the page view parameters 079 */ 080 @Callable 081 public Map<String, Object> getStatus (String pageId, String zoneName, String zoneItemId) 082 { 083 Map<String, Object> results = new HashMap<>(); 084 085 ModifiableSitemapElement page = _resolver.resolveById(pageId); 086 Site site = page.getSite(); 087 Skin skin = _skinsManager.getSkin(site.getSkinId()); 088 089 String templateName = page.getTemplate(); 090 if (StringUtils.isBlank(templateName) || skin.getTemplate(templateName) == null) 091 { 092 results.put("no-parameters", true); 093 return results; 094 } 095 096 SkinTemplate template = skin.getTemplate(templateName); 097 Optional<ViewParametersModel> templateViewParametersModel = _viewParametersManager.getTemplateViewParametersModel(template); 098 if (templateViewParametersModel.isPresent() && templateViewParametersModel.get().isNotEmpty()) 099 { 100 // we have parameters 101 results.put("allright-view-parameters", true); 102 return results; 103 } 104 105 if (StringUtils.isNotBlank(zoneName)) 106 { 107 SkinTemplateZone zone = template.getZone(zoneName); 108 if (zone == null) 109 { 110 results.put("no-parameters", true); 111 return results; 112 } 113 114 Optional<ViewParametersModel> zoneViewParametersModel = _viewParametersManager.getZoneViewParametersModel(zone); 115 if (zoneViewParametersModel.isPresent() && zoneViewParametersModel.get().isNotEmpty()) 116 { 117 // we have parameters 118 results.put("allright-view-parameters", true); 119 return results; 120 } 121 } 122 123 if (StringUtils.isNotBlank(zoneItemId)) 124 { 125 ModifiableZoneItem zoneItem = _resolver.resolveById(zoneItemId); 126 SkinTemplateZone zone = template.getZone(zoneItem.getZone().getName()); 127 Optional<ViewParametersModel> zoneItemViewParameters = zone.getZoneItemViewParameters(); 128 if (zoneItemViewParameters.isPresent() && zoneItemViewParameters.get().isNotEmpty()) 129 { 130 // we have parameters 131 results.put("allright-view-parameters", true); 132 return results; 133 } 134 135 if (zoneItem.getType() == ZoneType.SERVICE) 136 { 137 Map<String, ViewParametersModel> serviceViewParametersModels = _viewParametersManager.getServiceViewParametersModels(skin.getId(), zoneItem.getServiceId()); 138 for (ViewParametersModel viewParameters : serviceViewParametersModels.values()) 139 { 140 if (viewParameters.isNotEmpty()) 141 { 142 // we have parameters 143 results.put("allright-view-parameters", true); 144 return results; 145 } 146 } 147 } 148 else if (zoneItem.getType() == ZoneType.CONTENT) 149 { 150 Map<String, ViewParametersModel> contentViewParametersModels = _viewParametersManager.getContentViewParametersModels(skin.getId(), zoneItem.getContent()); 151 for (ViewParametersModel viewParameters : contentViewParametersModels.values()) 152 { 153 if (viewParameters.isNotEmpty()) 154 { 155 // we have parameters 156 results.put("allright-view-parameters", true); 157 return results; 158 } 159 } 160 } 161 } 162 163 results.put("no-parameters", true); 164 return results; 165 } 166 167}