001/*
002 *  Copyright 2019 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.odfweb.service.search;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020import org.apache.avalon.framework.service.Serviceable;
021import org.apache.cocoon.xml.AttributesImpl;
022import org.apache.cocoon.xml.XMLUtils;
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
028import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
029
030/**
031 * This {@link SearchComponent} sax the skill search field
032 */
033public class SaxSkillSearchComponent extends AbstractSkillSearchComponent implements Serviceable
034{
035    private AmetysObjectResolver _resolver;
036    
037    public void service(ServiceManager smanager) throws ServiceException
038    {
039        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
040    }
041    
042    public int priority()
043    {
044        return SEARCH_PRIORITY + 3000;
045    }
046    
047    @Override
048    public boolean supports(SearchComponentArguments args)
049    {
050        return args.launchSearch() && super.supports(args);
051    }
052    
053    public void execute(SearchComponentArguments args) throws Exception
054    {
055        String skillId = _getSkillId(args.request());
056        if (StringUtils.isNotBlank(skillId))
057        {
058            Content skill = _resolver.resolveById(skillId);
059            
060            AttributesImpl attrs = new AttributesImpl();
061            attrs.addCDATAAttribute("id", skillId);
062            XMLUtils.createElement(args.contentHandler(), "skill", attrs, skill.getTitle());
063        }
064    }
065}