001/*
002 *  Copyright 2017 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.extraction.component;
017
018import java.util.Collection;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.cocoon.xml.XMLUtils;
024import org.xml.sax.ContentHandler;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.search.cocoon.GroupSearchContent;
028import org.ametys.cms.search.model.ResultField;
029import org.ametys.plugins.extraction.ExtractionConstants;
030import org.ametys.plugins.extraction.execution.ExtractionExecutionContext;
031
032/**
033 * This class represents a count component of the extraction module
034 */
035public class CountExtractionComponent extends AbstractGroupExtractionComponent implements TwoStepsExecutingExtractionComponent
036{
037    @Override
038    protected void processContents(Iterable<Content> contents, ContentHandler contentHandler, ExtractionExecutionContext context) throws Exception
039    {
040        XMLUtils.startElement(contentHandler, _tagName);
041        
042        GroupSearchContent rootGroup = organizeContentsInGroups(contents, context.getDefaultLocale());
043        saxGroup(contentHandler, rootGroup, 0, context, Collections.EMPTY_LIST);
044        
045        XMLUtils.endElement(contentHandler, _tagName);
046    }
047    
048    @Override
049    public Iterable<Content> computeFirstLevelResults(ExtractionExecutionContext context) throws Exception
050    {
051        return getContents(context);
052    }
053    
054    @Override
055    public void executeFor(ContentHandler contentHandler, Iterable<Content> firstLevelResults, ExtractionExecutionContext context) throws Exception
056    {
057        processContents(firstLevelResults, contentHandler, context);
058    }
059
060    @Override
061    protected void saxContents(ContentHandler contentHandler, ExtractionExecutionContext context, Collection< ? extends ResultField> resultFields, List<Content> contents) throws Exception
062    {
063        XMLUtils.data(contentHandler, String.valueOf(contents.size()));
064    }
065
066    @Override
067    public void addSubComponent(ExtractionComponent subComponent)
068    {
069        throw new UnsupportedOperationException(getLogsPrefix() + "a count component can't have sub components");
070    }
071    
072    @Override
073    public Map<String, Object> getComponentDetailsForTree()
074    {
075        Map<String, Object> details = super.getComponentDetailsForTree();
076        details.put("tag", ExtractionConstants.COUNT_COMPONENT_TAG);
077        details.put("iconCls", "ametysicon-abacus");
078        return details;
079    }
080
081    @Override
082    protected String getDefaultTagName()
083    {
084        return "count";
085    }
086    
087    @Override
088    protected String getLogsPrefix()
089    {
090        return "Count component '" + _tagName + "': ";
091    }
092}