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;
031import org.ametys.plugins.repository.AmetysObjectIterable;
032
033/**
034 * This class represents a count component of the extraction module
035 */
036public class CountExtractionComponent extends AbstractGroupExtractionComponent
037{
038    
039    @Override
040    protected void processContents(AmetysObjectIterable<Content> contents, ContentHandler contentHandler, ExtractionExecutionContext context) throws Exception
041    {
042        XMLUtils.startElement(contentHandler, _tagName);
043        
044        GroupSearchContent rootGroup = organizeContentsInGroups(contents, context.getDefaultLocale());
045        saxGroup(contentHandler, rootGroup, 0, context, Collections.EMPTY_LIST);
046        
047        XMLUtils.endElement(contentHandler, _tagName);
048    }
049
050    @Override
051    protected void saxContents(ContentHandler contentHandler, ExtractionExecutionContext context, Collection< ? extends ResultField> resultFields, List<Content> contents) throws Exception
052    {
053        XMLUtils.data(contentHandler, String.valueOf(contents.size()));
054    }
055
056    @Override
057    public void addSubComponent(ExtractionComponent subComponent)
058    {
059        throw new UnsupportedOperationException(getLogsPrefix() + "a count component can't have sub components");
060    }
061    
062    @Override
063    public Map<String, Object> getComponentDetailsForTree()
064    {
065        Map<String, Object> details = super.getComponentDetailsForTree();
066        details.put("tag", ExtractionConstants.COUNT_COMPONENT_TAG);
067        details.put("iconCls", "ametysicon-abacus");
068        return details;
069    }
070
071    @Override
072    protected String getDefaultTagName()
073    {
074        return "count";
075    }
076    
077    @Override
078    protected String getLogsPrefix()
079    {
080        return "Count component '" + _tagName + "': ";
081    }
082}