001/*
002 *  Copyright 2023 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.glossary.statistics;
017
018import java.util.List;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.plugins.glossary.DefaultDefinitionFactory;
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.plugins.repository.AmetysObjectIterable;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.plugin.component.AbstractLogEnabled;
030import org.ametys.runtime.plugin.component.PluginAware;
031import org.ametys.runtime.plugins.admin.statistics.Statistics;
032import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
033import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
034import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
035
036/**
037 * Statistics for glossary
038 */
039public class GlossaryStatisticsProvider extends AbstractLogEnabled implements StatisticsProvider, Serviceable, PluginAware
040{
041    private String _id;
042    private AmetysObjectResolver _ametysObjectResolver;
043
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047    }
048    
049    public void setPluginInfo(String pluginName, String featureName, String id)
050    {
051        _id = id;
052    }
053
054    private long _countWords()
055    {
056        try (AmetysObjectIterable<AmetysObject> nodes = _ametysObjectResolver.query("//element(*, " + DefaultDefinitionFactory.DEFINITION_NODE_TYPE + ")"))
057        {
058            return nodes.getSize();
059        }
060    }
061    
062    public Statistics getStatistics()
063    {
064        return new StatisticsNode(
065            _id,
066            new I18nizableText("plugin.glossary", "PLUGINS_GLOSSARY_STATISTICS_LABEL"),
067            "ametysicon-alphabet-a",
068            null,
069            List.of(
070                new StatisticsValue(
071                    "count",
072                    new I18nizableText("plugin.glossary", "PLUGINS_GLOSSARY_STATISTICS_COUNT_LABEL"),
073                    "ametysicon-maths-number-zero-one",
074                    _countWords()
075                )
076            ),
077            true
078        );
079    }
080}