/**
  * Returns <code>true</code> if the entity name needs to be filtered.
  *
  * <p>Implementation filteres out inherited hibernate mappings, since the select query for the
  * base class will cover any inherited classes as well.
  *
  * <p>Note, that this method is called after it has been verified that the class has Compass
  * mappings (either directly, or indirectly by an interface or a super class).
  *
  * @param entityname The name of the entity
  * @param classMetadata The Hibernate class meta data.
  * @param device The Jpa Gps device
  * @return <code>true</code> if the entity should be filtered out, <code>false</code> if not.
  */
 protected boolean shouldFilter(
     String entityname, ClassMetadata classMetadata, Map allClassMetaData, JpaGpsDevice device) {
   Class<?> clazz = classMetadata.getMappedClass();
   // if it is inherited, do not add it to the classes to index, since the "from [entity]"
   // query for the base class will return results for this class as well
   if (classMetadata.isInherited()) {
     String superClassEntityName = ((AbstractEntityPersister) classMetadata).getMappedSuperclass();
     ClassMetadata superClassMetadata = (ClassMetadata) allClassMetaData.get(superClassEntityName);
     Class superClass = superClassMetadata.getMappedClass();
     // only filter out classes that their super class has compass mappings
     if (superClass != null
         && ((CompassGpsInterfaceDevice) device.getGps())
             .hasMappingForEntityForIndex(superClass)) {
       if (log.isDebugEnabled()) {
         log.debug(
             "Entity ["
                 + entityname
                 + "] is inherited and super class ["
                 + superClass
                 + "] has compass mapping, filtering it out");
       }
       return true;
     }
   }
   return false;
 }
  public EntityInformation[] locate(EntityManagerFactory entityManagerFactory, JpaGpsDevice device)
      throws JpaGpsDeviceException {

    CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps();

    HibernateEntityManagerFactory hibernateEntityManagerFactory =
        (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = hibernateEntityManagerFactory.getSessionFactory();

    ArrayList<EntityInformation> entitiesList = new ArrayList<EntityInformation>();

    Map allClassMetaData = sessionFactory.getAllClassMetadata();
    for (Object o : allClassMetaData.keySet()) {
      String entityname = (String) o;
      if (!gps.hasMappingForEntityForIndex((entityname))) {
        if (log.isDebugEnabled()) {
          log.debug("Entity [" + entityname + "] does not have compass mapping, filtering it out");
        }
        continue;
      }

      ClassMetadata classMetadata = (ClassMetadata) allClassMetaData.get(entityname);
      if (shouldFilter(entityname, classMetadata, allClassMetaData, device)) {
        continue;
      }
      Class<?> clazz = classMetadata.getMappedClass();
      ResourceMapping resourceMapping = gps.getMappingForEntityForIndex(entityname);
      EntityInformation entityInformation =
          new EntityInformation(
              clazz,
              entityname,
              new HibernateJpaQueryProvider(clazz, entityname),
              resourceMapping.getSubIndexHash().getSubIndexes());
      entitiesList.add(entityInformation);
      if (log.isDebugEnabled()) {
        log.debug("Entity [" + entityname + "] will be indexed");
      }
    }
    return entitiesList.toArray(new EntityInformation[entitiesList.size()]);
  }