protected String buildFieldOperand(Operand operand, Query query, Map entityAliasesMaps) {
    String operandElement;
    IModelField datamartField;
    IModelEntity rootEntity;
    String queryName;
    String rootEntityAlias;
    Map targetQueryEntityAliasesMap;

    logger.debug("IN");

    try {

      targetQueryEntityAliasesMap = (Map) entityAliasesMaps.get(query.getId());
      Assert.assertNotNull(
          targetQueryEntityAliasesMap,
          "Entity aliases map for query ["
              + query.getId()
              + "] cannot be null in order to execute method [buildUserProvidedWhereField]");

      datamartField =
          parentStatement.getDataSource().getModelStructure().getField(operand.values[0]);
      Assert.assertNotNull(
          datamartField, "DataMart does not cantain a field named [" + operand.values[0] + "]");
      Couple queryNameAndRoot = datamartField.getQueryName();

      queryName = (String) queryNameAndRoot.getFirst();
      logger.debug("select field query name [" + queryName + "]");

      if (queryNameAndRoot.getSecond() != null) {
        rootEntity = (IModelEntity) queryNameAndRoot.getSecond();
      } else {
        rootEntity = datamartField.getParent().getRoot();
      }
      logger.debug("where field query name [" + queryName + "]");

      logger.debug("where field root entity unique name [" + rootEntity.getUniqueName() + "]");

      if (!targetQueryEntityAliasesMap.containsKey(rootEntity.getUniqueName())) {
        logger.debug("Entity [" + rootEntity.getUniqueName() + "] require a new alias");
        rootEntityAlias =
            getEntityAlias(rootEntity, targetQueryEntityAliasesMap, entityAliasesMaps);
        logger.debug("A new alias has been generated [" + rootEntityAlias + "]");
      }
      rootEntityAlias = (String) targetQueryEntityAliasesMap.get(rootEntity.getUniqueName());
      logger.debug("where field root entity alias [" + rootEntityAlias + "]");

      if (operand instanceof HavingField.Operand) {
        HavingField.Operand havingFieldOperand = (HavingField.Operand) operand;
        IAggregationFunction function = havingFieldOperand.function;
        operandElement = function.apply(parentStatement.getFieldAlias(rootEntityAlias, queryName));
      } else {
        operandElement = parentStatement.getFieldAlias(rootEntityAlias, queryName);
      }
      logger.debug("where element operand value [" + operandElement + "]");
    } finally {
      logger.debug("OUT");
    }

    return operandElement;
  }
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((source == null) ? 0 : source.hashCode());
   result = prime * result + ((target == null) ? 0 : target.hashCode());
   return result;
 }
 @Override
 public boolean equals(Object obj) {
   if (this == obj) return true;
   if (obj == null) return false;
   if (getClass() != obj.getClass()) return false;
   EntitiesPath other = (EntitiesPath) obj;
   if (source == null) {
     if (other.source != null) return false;
   } else if (!source.equals(other.source)) return false;
   if (target == null) {
     if (other.target != null) return false;
   } else if (!target.equals(other.target)) return false;
   return true;
 }
  private IModelField getColumnModelField(Column column, IModelEntity entity) {
    if (column.getSubEntity()
        != null) { // in case it is a subEntity attribute, look for the field inside it

      // In order to recover subentities the new way if DEFAULT_MAX_RECURSION_LEVEL is set to zero
      /*
      	QbeEngineInstance engineInstance = getEngineInstance();
      QbeTemplate template = engineInstance.getTemplate();
      // takes the only datamart's name configured
      String modelName = (String) template.getDatamartNames().get(0);
      IModelStructure md = getDataSource().getModelStructure();
      IModelEntity	subEntity  = md.getEntity(column.getSubEntity()); */

      String entityUName = entity.getUniqueName();
      String subEntityKey =
          entityUName.substring(0, entityUName.lastIndexOf("::"))
              + "::"
              + column.getSubEntity()
              + "("
              + column.getForeignKey()
              + ")";
      IModelEntity subEntity = entity.getSubEntity(subEntityKey);
      if (subEntity == null) {
        throw new SpagoBIEngineServiceException(
            getActionName(),
            "Sub-entity ["
                + column.getSubEntity()
                + "] not found in entity ["
                + entity.getName()
                + "]!");
      }
      entity = subEntity;
    }
    logger.debug(
        "Looking for attribute " + column.getField() + " in entity " + entity.getName() + " ...");
    List<IModelField> fields = entity.getAllFields();
    Iterator<IModelField> it = fields.iterator();
    while (it.hasNext()) {
      IModelField field = it.next();
      if (field.getName().equals(column.getField())) {
        return field;
      }
    }
    return null;
  }
 /**
  * Get all the paths passing from the passed vertex
  *
  * @param vertex
  * @return
  */
 private Set<GraphPath<IModelEntity, Relationship>> getPathsOfAmbigousEntities(
     IModelEntity vertex) {
   logger.debug("IN");
   logger.debug("Getting all the paths passing through the vertex " + vertex.getName());
   Iterator<GraphPath<IModelEntity, Relationship>> iter = getAllGraphPaths().iterator();
   Set<GraphPath<IModelEntity, Relationship>> toReturn =
       new HashSet<GraphPath<IModelEntity, Relationship>>();
   while (iter.hasNext()) {
     GraphPath<IModelEntity, Relationship> path = iter.next();
     if (containsVertex(path, vertex)) {
       toReturn.add(path);
     }
   }
   logger.debug("The path passing through the vertex are " + toReturn.size());
   logger.debug("OUT");
   return toReturn;
 }
  /** Builds the set of paths between all the couple of vertexes */
  private void buildPaths() {

    logger.debug("IN");

    allGraphPaths = new HashSet<GraphPath<IModelEntity, Relationship>>();
    paths = new HashSet<EntitiesPath>();

    // build all the path between entities
    Iterator<IModelEntity> vertexesIter = entities.iterator();

    // First loop for the source of the path
    while (vertexesIter.hasNext()) {
      IModelEntity startVertex = vertexesIter.next();
      logger.debug("Building the list of path starting from " + startVertex.getName());
      // inner loop for the target of the path
      Iterator<IModelEntity> vertexesInnerIter = entities.iterator();
      while (vertexesInnerIter.hasNext()) {
        IModelEntity endVertex = vertexesInnerIter.next();
        if (!startVertex.equals(endVertex)) {
          EntitiesPath entitiesPath = new EntitiesPath(startVertex, endVertex);
          if (!this.paths.contains(entitiesPath)) {
            logger.debug(
                "Building the list of path between the vertexes ["
                    + startVertex.getName()
                    + ","
                    + endVertex.getName()
                    + "]");
            KShortestPaths<IModelEntity, Relationship> kshortestPath =
                new KShortestPaths<IModelEntity, Relationship>(graph, startVertex, maxPathLength);
            List<GraphPath<IModelEntity, Relationship>> graphPaths =
                kshortestPath.getPaths(endVertex);

            // if there is at least one path between the 2 vertex
            if (graphPaths != null) {
              entitiesPath.setPaths(graphPaths);

              // updating the class variables
              this.paths.add(entitiesPath);
              for (int i = 0; i < graphPaths.size(); i++) {
                GraphPath<IModelEntity, Relationship> path = graphPaths.get(i);
                if (!containsPath(this.allGraphPaths, path)) {
                  this.allGraphPaths.add(path);
                }
              }
            }
          }
        }
      }
    }
    logger.debug("OUT");
  }
  protected String buildParentFieldOperand(Operand operand, Query query, Map entityAliasesMaps) {
    String operandElement;

    String[] chunks;
    String parentQueryId;
    String fieldName;
    IModelField datamartField;
    IModelEntity rootEntity;
    String queryName;
    String rootEntityAlias;

    logger.debug("IN");

    try {

      // it comes directly from the client side GUI. It is a composition of the parent query id and
      // filed name,
      // separated by a space
      logger.debug("operand  is equals to [" + operand.values[0] + "]");

      chunks = operand.values[0].split(" ");
      Assert.assertTrue(
          chunks.length >= 2,
          "Operand ["
              + chunks.toString()
              + "] does not contains enougth informations in order to resolve the reference to parent field");

      parentQueryId = chunks[0];
      logger.debug("where right-hand field belonging query [" + parentQueryId + "]");
      fieldName = chunks[1];
      logger.debug("where right-hand field unique name [" + fieldName + "]");

      datamartField = parentStatement.getDataSource().getModelStructure().getField(fieldName);
      Assert.assertNotNull(
          datamartField, "DataMart does not cantain a field named [" + fieldName + "]");

      Couple queryNameAndRoot = datamartField.getQueryName();

      queryName = (String) queryNameAndRoot.getFirst();
      logger.debug("select field query name [" + queryName + "]");

      if (queryNameAndRoot.getSecond() != null) {
        rootEntity = (IModelEntity) queryNameAndRoot.getSecond();
      } else {
        rootEntity = datamartField.getParent().getRoot();
      }
      logger.debug("where right-hand field query name [" + queryName + "]");
      logger.debug(
          "where right-hand field root entity unique name [" + rootEntity.getUniqueName() + "]");

      Map parentEntityAliases = (Map) entityAliasesMaps.get(parentQueryId);
      if (parentEntityAliases != null) {
        if (!parentEntityAliases.containsKey(rootEntity.getUniqueName())) {
          Assert.assertUnreachable(
              "Filter of subquery ["
                  + query.getId()
                  + "] refers to a non "
                  + "existing parent query ["
                  + parentQueryId
                  + "] entity ["
                  + rootEntity.getUniqueName()
                  + "]");
        }
        rootEntityAlias = (String) parentEntityAliases.get(rootEntity.getUniqueName());
      } else {
        rootEntityAlias = "unresoved_alias";
        logger.warn(
            "Impossible to get aliases map for parent query ["
                + parentQueryId
                + "]. Probably the parent query ha not been compiled yet");
        logger.warn(
            "Query ["
                + query.getId()
                + "] refers entities of its parent query ["
                + parentQueryId
                + "] so the generated statement wont be executable until the parent query will be compiled");
      }
      logger.debug("where right-hand field root entity alias [" + rootEntityAlias + "]");

      operandElement = parentStatement.getFieldAlias(rootEntityAlias, queryName);
      logger.debug("where element right-hand field value [" + operandElement + "]");
    } finally {
      logger.debug("OUT");
    }

    return operandElement;
  }