001/*
002 *  Copyright 2018 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.frontoffice.search.requesttime.impl;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021
022import org.ametys.cms.search.advanced.AbstractTreeNode;
023import org.ametys.cms.search.query.Query;
024import org.ametys.web.frontoffice.search.instance.model.FOSearchCriterion;
025import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
026import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
027
028/**
029 * {@link SearchComponent} for transforming a {@link AbstractTreeNode} of {@link FOSearchCriterion} into {@link Query queries} 
030 */
031public class CriterionTreeSearchComponent implements SearchComponent, Serviceable
032{
033    /** The helper for search component */
034    protected SearchComponentHelper _searchComponentHelper;
035    
036    @Override
037    public void service(ServiceManager manager) throws ServiceException
038    {
039        _searchComponentHelper = (SearchComponentHelper) manager.lookup(SearchComponentHelper.ROLE);
040    }
041    
042    @Override
043    public int priority()
044    {
045        return SEARCH_PRIORITY - 8000;
046    }
047
048    @Override
049    public boolean supports(SearchComponentArguments args)
050    {
051        return args.launchSearch() && args.serviceInstance().getCriterionTree().isPresent();
052    }
053
054    @Override
055    public void execute(SearchComponentArguments args) throws Exception
056    {
057        Query criterionTreeQuery = _searchComponentHelper.getCriterionTreeQuery(args, true, true);
058        args.searcher().withQuery(criterionTreeQuery);
059    }
060}