// Path: spi/src/main/java/org/jipijapa/management/spi/EntityManagerFactoryAccess.java
// public interface EntityManagerFactoryAccess {
//     /**
//      * returns the entity manager factory that statistics should be obtained for.
//      *
//      * @throws IllegalStateException if scopedPersistenceUnitName is not found
//      *
//      * @param scopedPersistenceUnitName is persistence unit name scoped to the current platform
//      *
//      * @return EntityManagerFactory
//      */
//     EntityManagerFactory entityManagerFactory(String scopedPersistenceUnitName) throws IllegalStateException;
// 
// }
// 
// Path: spi/src/main/java/org/jipijapa/management/spi/Operation.java
// public interface Operation {
// 
//     /**
//      * Invoke operation
//      * @param args will be passed to invoked operation
//      * @return
//      */
//     Object invoke(Object... args);
// }
// 
// Path: spi/src/main/java/org/jipijapa/management/spi/PathAddress.java
// public interface PathAddress {
// 
//     int size();
// 
//     String getValue(String name);
//     String getValue(int index);
// }


import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.jipijapa.management.spi.EntityManagerFactoryAccess;
import org.jipijapa.management.spi.Operation;
import org.jipijapa.management.spi.PathAddress;
        // convert displayed (transformed by QueryNames) query name to original query name to look up query statistics
        if (sessionFactory != null) {
            String[] originalQueryNames = sessionFactory.getStatistics().getQueries();
            if (originalQueryNames != null) {
                for (String originalQueryName : originalQueryNames) {
                    if (QueryName.queryName(originalQueryName).getDisplayName().equals(displayQueryName)) {
                        return sessionFactory.getStatistics().getQueryStatistics(originalQueryName);
                    }
                }
            }
        }
        return null;
    }

    private Operation queryExecutionCount = new Operation() {
        @Override
        public Object invoke(Object... args) {
            org.hibernate.stat.QueryStatistics statistics = getStatistics(getEntityManagerFactory(args), getQueryName(args));
            return Long.valueOf(statistics != null ? statistics.getExecutionCount() : 0);
        }
    };

    private Operation queryExecutionMaximumTime = new Operation() {
        @Override
        public Object invoke(Object... args) {
            org.hibernate.stat.QueryStatistics statistics = getStatistics(getEntityManagerFactory(args), getQueryName(args));
            return Long.valueOf(statistics != null ? statistics.getExecutionMaxTime() : 0);
        }
    };

// Path: spi/src/main/java/org/jipijapa/management/spi/EntityManagerFactoryAccess.java
// public interface EntityManagerFactoryAccess {
//     /**
//      * returns the entity manager factory that statistics should be obtained for.
//      *
//      * @throws IllegalStateException if scopedPersistenceUnitName is not found
//      *
//      * @param scopedPersistenceUnitName is persistence unit name scoped to the current platform
//      *
//      * @return EntityManagerFactory
//      */
//     EntityManagerFactory entityManagerFactory(String scopedPersistenceUnitName) throws IllegalStateException;
// 
// }
// 
// Path: spi/src/main/java/org/jipijapa/management/spi/Operation.java
// public interface Operation {
// 
//     /**
//      * Invoke operation
//      * @param args will be passed to invoked operation
//      * @return
//      */
//     Object invoke(Object... args);
// }
// 
// Path: spi/src/main/java/org/jipijapa/management/spi/PathAddress.java
// public interface PathAddress {
// 
//     int size();
// 
//     String getValue(String name);
//     String getValue(int index);
// }


// Path: hibernate4_3/src/main/java/org/jboss/as/jpa/hibernate4/management/HibernateQueryCacheStatistics.java
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.jipijapa.management.spi.EntityManagerFactoryAccess;
import org.jipijapa.management.spi.Operation;
import org.jipijapa.management.spi.PathAddress;

        // convert displayed (transformed by QueryNames) query name to original query name to look up query statistics
        if (sessionFactory != null) {
            String[] originalQueryNames = sessionFactory.getStatistics().getQueries();
            if (originalQueryNames != null) {
                for (String originalQueryName : originalQueryNames) {
                    if (QueryName.queryName(originalQueryName).getDisplayName().equals(displayQueryName)) {
                        return sessionFactory.getStatistics().getQueryStatistics(originalQueryName);
                    }
                }
            }
        }
        return null;
    }

    private Operation queryExecutionCount = new Operation() {
        @Override
        public Object invoke(Object... args) {
            org.hibernate.stat.QueryStatistics statistics = getStatistics(getEntityManagerFactory(args), getQueryName(args));
            return Long.valueOf(statistics != null ? statistics.getExecutionCount() : 0);
        }
    };

    private Operation queryExecutionMaximumTime = new Operation() {
        @Override
        public Object invoke(Object... args) {
            org.hibernate.stat.QueryStatistics statistics = getStatistics(getEntityManagerFactory(args), getQueryName(args));
            return Long.valueOf(statistics != null ? statistics.getExecutionMaxTime() : 0);
        }
    };

    private Operation queryExecutionRowCount = new Operation() {