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.plugins.odforientation;
017
018import java.util.Collections;
019import java.util.Optional;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.search.advanced.AbstractTreeNode;
026import org.ametys.cms.search.advanced.TreeLeaf;
027import org.ametys.web.frontoffice.search.SearchService;
028import org.ametys.web.frontoffice.search.instance.SearchServiceInstance;
029import org.ametys.web.frontoffice.search.instance.SearchServiceInstanceManager;
030import org.ametys.web.frontoffice.search.metamodel.impl.ContentReferencingSearchServiceCriterionDefinition;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.Page.PageType;
033import org.ametys.web.repository.page.ZoneItem;
034import org.ametys.web.repository.page.ZoneItem.ZoneType;
035
036/**
037 * XSLT helper for dataviz in ODF documents.
038 */
039public class OdfXSLTHelper extends org.ametys.plugins.odfweb.xslt.OdfXSLTHelper
040{
041    private static SearchServiceInstanceManager _searchServiceInstanceManager;
042
043    @Override
044    public void service(ServiceManager serviceManager) throws ServiceException
045    {
046        super.service(serviceManager);
047        _searchServiceInstanceManager = (SearchServiceInstanceManager) serviceManager.lookup(SearchServiceInstanceManager.ROLE);
048    }
049    
050    /**
051     * Get the name of the criterion input related to the given reference table in the given search page.
052     * @param searchPageId The id of search page
053     * @param refTableId The id of the reference table
054     * @return The name of the criterion input, null if not found
055     */
056    public static String getReferenceTableCriterionInputName(String searchPageId, String refTableId)
057    {
058        if (StringUtils.isNotEmpty(searchPageId))
059        {
060            Page page = _ametysObjectResolver.resolveById(searchPageId);
061            if (page.getType() == PageType.CONTAINER) 
062            {
063                Optional< ? extends ZoneItem> searchZoneItem = page.getZones().stream()
064                    .flatMap(z -> z.getZoneItems().stream())
065                    .filter(zi -> zi.getType() == ZoneType.SERVICE)
066                    .filter(zi -> zi.getServiceId().equals(SearchService.ROLE))
067                    .findFirst();
068                    
069                if (searchZoneItem.isPresent())
070                {
071                    SearchServiceInstance serviceInstance = _searchServiceInstanceManager.get(searchZoneItem.get().getId());
072                    return serviceInstance.getCriterionTree()
073                            .map(AbstractTreeNode::getFlatLeaves)
074                            .orElseGet(Collections::emptyList)
075                            .stream()
076                            .map(TreeLeaf::getValue)
077                            .filter(c -> c.getCriterionDefinition() instanceof ContentReferencingSearchServiceCriterionDefinition && ((ContentReferencingSearchServiceCriterionDefinition) c.getCriterionDefinition()).getContentTypeId().equals(refTableId))
078                            .findFirst()
079                            .map(c -> c.getName())
080                            .orElseGet(null);
081                }
082            }
083        }
084        
085        // No criteria found related to given reference table
086        return null;
087    }
088}