@Override
  public String buildSQL() {
    final StringBuilder sql = new StringBuilder();
    sql.append(SELECT.getSQLSelectType());
    sql.append(" ");

    if (distinct) {
      sql.append(DISTINCT.getSQLSelectType());
      sql.append(" ");
    }

    boolean readFieldsOfJoins = false;
    if (select.getFields().isEmpty()) {
      readFieldsOfJoins = true;

      Class<?> superTypeClass = entityClass.getSuperclass();
      if (!(Object.class.equals(superTypeClass))) {
        addJoin(JoinBuilder.newJoin(entityClass, fromAlias));
      }
    }

    sql.append(select.buildSQL());

    if (readFieldsOfJoins) {
      for (Join j : joins) {
        if (!(sql.toString().endsWith(" "))) {
          sql.append(", ");
        }

        Select selectJoin = j.builSelect();
        sql.append(selectJoin.buildSQL());
      }
    }

    sql.append(" ");
    sql.append(FROM.getSQLSelectType());
    sql.append(" ");
    sql.append(getTableName(entityClass));

    if (fromAlias != null && !(fromAlias.isEmpty())) {
      sql.append(" " + AS.getSQLSelectType() + " " + fromAlias);
    }

    if (!joins.isEmpty()) {
      for (Join j : joins) {
        sql.append(" ");
        sql.append(j.buildSQL());
      }
    }

    if (where != null) {
      sql.append(" ");
      sql.append(WHERE.getSQLSelectType());
      sql.append(" ");
      sql.append(where.buildSQL());
    }

    if (groupBy != null) {
      sql.append(" ");
      sql.append(groupBy.buildSQL());
    }

    if (orderBy != null) {
      sql.append(" ");
      sql.append(orderBy.buildSQL());
    }

    if (offset > DEFAULT_LIMIT_AND_OFFSET) {
      sql.append(" ");
      sql.append(OFFSET.getSQLSelectType());
      sql.append(" ");
      sql.append(offset);
    }

    if (limit > DEFAULT_LIMIT_AND_OFFSET) {
      sql.append(" ");
      sql.append(LIMIT.getSQLSelectType());
      sql.append(" ");
      sql.append(limit);
    }

    if (union != null) {
      sql.append(" ");
      sql.append(unionType.getUnionType());
      sql.append(" ");
      sql.append(union.buildSQL());
    }

    return sql.toString();
  }
Exemplo n.º 2
0
  /**
   * Converts a single user interaction to a Selenium command.
   *
   * @return the Selenium command name invoked.
   */
  private String handleUserInteraction(
      SeleniumHolder seleniumHolder, UserInteraction userInteraction) {

    IActionElement element = userInteraction.getElement();
    ActionType actionType = userInteraction.getActionType();
    boolean withinFrame = false;
    if (element instanceof PageElement
        && seleniumHolder.isPageElementWithinFrame((PageElement) element)) {
      // check if parent frame was found:
      if (TestPartStatus.FAIL == seleniumHolder.getParentFrame((PageElement) element).getStatus()) {
        ErrorHandler.logAndShowErrorDialogAndThrow(
            "Cannot interact with element "
                + element
                + ":\n"
                + "Parent frame "
                + seleniumHolder.getParentFrame((PageElement) element)
                + " not found.");
      }
      withinFrame = true;
      getToRightFrame(seleniumHolder, seleniumHolder.getParentFrame((PageElement) element));
    }
    // Getting selenium commands, locators and values:
    String commandName = SeleniumUtils.getCommandName(actionType);

    String locator = null;
    String inputValue = null;

    if (element instanceof Option) {
      Select selectbox = ((Option) element).getParent();
      locator = "xpath=" + seleniumHolder.getFullContextWithAllElements(selectbox);
      inputValue = SeleniumUtils.getOptionLocator((Option) element);
      if (SELECT.equals(actionType) && selectbox.getIdentifier(MULTISELECT).getProbability() > 0) {
        commandName = "addSelection"; // appropriate for multi-selection
      }
    } else {
      // all other elements
      if (element instanceof PageElement) {
        locator = "xpath=" + seleniumHolder.getFullContextWithAllElements((PageElement) element);
      } else if (element instanceof WebBrowser) {
        locator = userInteraction.getValue();
      } else {
        throw new ExporterException("Unsupported action element type");
      }
      inputValue = SeleniumUtils.getValue(userInteraction);
    }

    try {
      // invoke user interaction by reflection using command name from SeleniumUtil (legacy since
      // Selenese exporter was written first):

      if (SeleniumUtils.hasSeleniumInputColumn(userInteraction)) {
        // two parameters
        seleniumHolder.getSelenium().execute(commandName, locator, inputValue);
      } else {
        // one parameter only
        seleniumHolder.getSelenium().execute(commandName, locator);
      }
    } catch (Throwable e) {
      throw new UserInteractionException(e);
    }

    if (withinFrame && commandName.equals(SeleniumUtils.FIREEVENT)) {
      upToParentFrame(seleniumHolder);
    }
    return commandName;
  }