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;
  }
Esempio n. 2
0
  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;
  }
  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;
  }