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