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