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