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.cart.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.repository.AmetysObjectResolver;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.plugin.component.PluginAware;
027import org.ametys.runtime.plugins.admin.statistics.Statistics;
028import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
029import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
030import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
031
032/**
033 * Stats about carts
034 */
035public class CartsStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
036{
037    
038    /** The Ametys object resolver */
039    protected AmetysObjectResolver _resolver;
040
041    private String _id;
042    
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    public void setPluginInfo(String pluginName, String featureName, String id)
049    {
050        _id = id;
051    }
052    
053    public Statistics getStatistics()
054    {
055        String xpathQuery = "//element(*, ametys:cart)";
056        long nbCarts = _resolver.query(xpathQuery).getSize();
057
058        return new StatisticsNode(
059            _id,
060            new I18nizableText("plugin.cart", "PLUGINS_CART_STATISTICS_CART_LABEL"),
061            "ametysicon-basket",
062            null,
063            List.of(
064                new StatisticsValue(
065                    "count",
066                    new I18nizableText("plugin.cart", "PLUGINS_CART_STATISTICS_CART_CARTS_LABEL"),
067                    "ametysicon-maths-number-zero-one",
068                    nbCarts
069                )
070            ),
071            true
072        );
073    }
074}