001/*
002 *  Copyright 2024 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.cms.search.ui.model;
017
018import java.util.Arrays;
019import java.util.Collection;
020import java.util.List;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.component.Component;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.cms.contenttype.ContentType;
030import org.ametys.cms.search.content.ContentSearchHelper;
031import org.ametys.cms.search.model.CriterionDefinition;
032import org.ametys.cms.search.model.SearchModelCriterionDefinitionHelper;
033import org.ametys.cms.search.ui.model.impl.DefaultSearchModelCriterionViewItem;
034import org.ametys.runtime.model.ElementDefinition;
035import org.ametys.runtime.model.Model;
036import org.ametys.runtime.plugin.component.AbstractLogEnabled;
037
038/**
039 * Helper for  referencing {@link CriterionDefinition}
040 */
041public class SearchModelCriterionViewItemHelper extends AbstractLogEnabled implements Component, Serviceable
042{
043    /** The component role. */
044    public static final String ROLE = SearchModelCriterionViewItemHelper.class.getName();
045    
046    /** The content search helper */
047    protected ContentSearchHelper _contentSearchHelper;
048    
049    /** The search model criterion definition helper */
050    protected SearchModelCriterionDefinitionHelper _criterionDefinitionHelper;
051    
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        _contentSearchHelper = (ContentSearchHelper) manager.lookup(ContentSearchHelper.ROLE);
055        _criterionDefinitionHelper = (SearchModelCriterionDefinitionHelper) manager.lookup(SearchModelCriterionDefinitionHelper.ROLE);
056    }
057    
058    /**
059     * Retrieves a {@link SearchModelCriterionViewItem} referencing the element at the given path
060     * @param searchModel The {@link SearchUIModel} defining this criterion
061     * @param referencePath the path of the element to reference
062     * @return the {@link SearchModelCriterionViewItem} 
063     */
064    public SearchModelCriterionViewItem createReferencingCriterionViewItem(Model searchModel, String referencePath)
065    {
066        return createReferencingCriterionViewItem(searchModel, referencePath, List.of());
067    }
068    
069    /**
070     * Retrieves a {@link SearchModelCriterionViewItem} referencing the element at the given path
071     * @param searchModel The {@link SearchUIModel} defining this criterion
072     * @param referencePath the path of the element to reference
073     * @param contentTypes the content types containing the reference
074     * @return the {@link SearchModelCriterionViewItem} 
075     */
076    public SearchModelCriterionViewItem createReferencingCriterionViewItem(Model searchModel, String referencePath, ContentType... contentTypes)
077    {
078        return createReferencingCriterionViewItem(searchModel, referencePath, Arrays.asList(contentTypes));
079    }
080    
081    /**
082     * Retrieves a {@link SearchModelCriterionViewItem} referencing the element at the given path
083     * @param searchModel The {@link SearchUIModel} defining this criterion
084     * @param referencePath the path of the element to reference
085     * @param contentTypes the content types containing the reference
086     * @return the {@link SearchModelCriterionViewItem} 
087     */
088    public SearchModelCriterionViewItem createReferencingCriterionViewItem(Model searchModel, String referencePath, Collection<ContentType> contentTypes)
089    {
090        Set<String> contentTypeIds = contentTypes.stream().map(ContentType::getId).collect(Collectors.toSet());
091        return createReferencingCriterionViewItem(searchModel, referencePath, contentTypeIds);
092    }
093    
094    /**
095     * Retrieves a {@link SearchModelCriterionViewItem} referencing the element at the given path
096     * @param searchModel The {@link SearchUIModel} defining this criterion
097     * @param referencePath the path of the element to reference
098     * @param contentTypeIds the identifiers of the content types containing the reference
099     * @return the {@link SearchModelCriterionViewItem} 
100     */
101    public SearchModelCriterionViewItem createReferencingCriterionViewItem(Model searchModel, String referencePath, Set<String> contentTypeIds)
102    {
103        ElementDefinition reference = _contentSearchHelper.getReferenceFromFieldPath(referencePath, contentTypeIds);
104        return createReferencingCriterionViewItem(searchModel, reference, referencePath);
105    }
106    
107    /**
108     * Retrieves a {@link SearchModelCriterionViewItem} referencing the given definition
109     * @param searchModel The {@link SearchUIModel} defining this criterion
110     * @param reference the definition of the element to reference
111     * @param referencePath the path of the reference from the model containing the reference
112     * @return the {@link SearchModelCriterionViewItem}.Can be <code>null</code> if the reference can not be used as a criterion
113     */
114    public SearchModelCriterionViewItem createReferencingCriterionViewItem(Model searchModel, ElementDefinition reference, String referencePath)
115    {
116        CriterionDefinition criterion = _criterionDefinitionHelper.createReferencingCriterionDefinition(reference, referencePath);
117        if (criterion != null)
118        {
119            criterion.setModel(searchModel);
120
121            SearchModelCriterionViewItem<ElementDefinition> criterionViewItem = new DefaultSearchModelCriterionViewItem();
122            criterionViewItem.setDefinition(criterion);
123
124            return criterionViewItem;
125        }
126        else
127        {
128            return null;
129        }
130    }
131}