/**
   * validates the action. checks to verify inputs are available to execute
   *
   * <p>- verify query is available - verify connection is available, via jndi, connection string,
   * or prepared component - verify output is specified
   */
  @Override
  public boolean validateAction() {
    boolean result = true;

    IActionDefinition actionDefinition = getActionDefinition();
    String actionName = getActionName();

    if (actionDefinition instanceof AbstractRelationalDbAction) {
      AbstractRelationalDbAction relationalDbAction = (AbstractRelationalDbAction) actionDefinition;
      IActionInput query = relationalDbAction.getQuery();
      IActionInput dbUrl = relationalDbAction.getDbUrl();
      IActionInput jndi = relationalDbAction.getJndi();

      IActionInput sharedConnection = relationalDbAction.getSharedConnection();
      if (query == ActionInputConstant.NULL_INPUT) {

        error(
            Messages.getInstance()
                .getErrorString(
                    "SQLBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", actionName)); // $NON-NLS-1$
        result = false;
      }

      if ((jndi == ActionInputConstant.NULL_INPUT)
          && (dbUrl == ActionInputConstant.NULL_INPUT)
          && (sharedConnection == ActionInputConstant.NULL_INPUT)) {
        error(
            Messages.getInstance()
                .getErrorString(
                    "SQLBaseComponent.ERROR_0002_CONNECTION_NOT_SPECIFIED",
                    actionName)); //$NON-NLS-1$
        result = false;
      }
    } else if (actionDefinition instanceof SqlConnectionAction) {
      SqlConnectionAction sqlConnectionAction = (SqlConnectionAction) actionDefinition;
      IActionInput dbUrl = sqlConnectionAction.getDbUrl();
      IActionInput jndi = sqlConnectionAction.getJndi();
      if ((jndi == ActionInputConstant.NULL_INPUT) && (dbUrl == ActionInputConstant.NULL_INPUT)) {
        error(
            Messages.getInstance()
                .getErrorString(
                    "SQLBaseComponent.ERROR_0002_CONNECTION_NOT_SPECIFIED",
                    actionName)); //$NON-NLS-1$
        result = false;
      }
    } else {
      error(
          Messages.getInstance()
              .getErrorString(
                  "ComponentBase.ERROR_0001_UNKNOWN_ACTION_TYPE",
                  actionDefinition.getElement().asXML())); // $NON-NLS-1$
      result = false;
    }
    return result;
  }
  @SuppressWarnings("rawtypes")
  public static void createOutputFileName(RepositoryFile file, IOutputHandler outputHandler) {
    IPentahoSession userSession = PentahoSessionHolder.getSession();
    ActionSequenceJCRHelper actionHelper = new ActionSequenceJCRHelper(userSession);
    IActionSequence actionSequence =
        actionHelper.getActionSequence(
            file.getPath(), PentahoSystem.loggingLevel, RepositoryFilePermission.READ);

    String fileName = "content"; // $NON-NLS-1$
    if (actionSequence != null) {
      String title = actionSequence.getTitle();
      if ((title != null) && (title.length() > 0)) {
        fileName = title;
      } else {
        String sequenceName = actionSequence.getSequenceName();

        if ((sequenceName != null) && (sequenceName.length() > 0)) {
          fileName = sequenceName;
        } else {
          List actionDefinitionsList = actionSequence.getActionDefinitionsAndSequences();
          int i = 0;
          boolean done = false;

          while ((actionDefinitionsList.size() > i) && (!done)) {
            IActionDefinition actionDefinition = (IActionDefinition) actionDefinitionsList.get(i);
            String componentName = actionDefinition.getComponentName();
            if ((componentName != null) && (componentName.length() > 0)) {
              fileName = componentName;
              done = true;
            } else {
              ++i;
            }
          }
        }
      }
    }
    IMimeTypeListener mimeTypeListener = outputHandler.getMimeTypeListener();
    if (mimeTypeListener != null) {
      mimeTypeListener.setName(fileName);
    }
  }
  /**
   * determines state of component, and executes accordingly.
   *
   * <p>various inputs that impact the state include:
   *
   * <p>live - returns a live result set vs. an in memory copy transform - transform a result set
   * based on additional inputs prepared_component - if available, use existing connection from
   * prepared component max_rows - sets the number of rows that should be returned in result sets
   *
   * <p>The specified output also impacts the state of the execution. If prepared_component is
   * defined as an output, setup the query but delay execution.
   */
  @Override
  protected boolean executeAction() {
    IActionDefinition actionDefinition = getActionDefinition();
    try {

      if (actionDefinition instanceof AbstractRelationalDbAction) {
        AbstractRelationalDbAction relationalDbAction =
            (AbstractRelationalDbAction) actionDefinition;
        // Added by Arijit Chatterjee
        IActionInput queryTimeoutInput = relationalDbAction.getQueryTimeout();
        IActionInput maxRowsInput = relationalDbAction.getMaxRows();
        IActionInput readOnlyInput = relationalDbAction.getReadOnly();

        String baseQuery = getQuery();
        if (baseQuery == null) {
          error(
              Messages.getInstance()
                  .getErrorString(
                      "SQLBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED",
                      actionDefinition.getDescription())); // $NON-NLS-1$
          return false;
        }

        IPreparedComponent sharedConnection =
            (IPreparedComponent) relationalDbAction.getSharedConnection().getValue();

        if (readOnlyInput != ActionInputConstant.NULL_INPUT) {
          this.setReadOnly(readOnlyInput.getBooleanValue());
        }

        if (sharedConnection != null) {
          connectionOwner = false;
          IPentahoConnection conn = sharedConnection.shareConnection();
          if (conn == null) {
            error(
                Messages.getInstance()
                    .getErrorString(
                        "IPreparedComponent.ERROR_0002_CONNECTION_NOT_AVAILABLE",
                        getActionName())); //$NON-NLS-1$
            return false;
          } else if (conn.getDatasourceType() == IPentahoConnection.SQL_DATASOURCE) {
            connection = conn;
          } else {
            error(
                Messages.getInstance()
                    .getErrorString(
                        "IPreparedComponent.ERROR_0001_INVALID_CONNECTION_TYPE",
                        getActionName())); //$NON-NLS-1$
            return false;
          }
        } else {
          dispose();
          connection = getDatasourceConnection();
        }

        if (connection == null) {
          return false;
        }

        // Check if this is a prepared query that will be executed later. If so cache the
        // query and set this component as the output. This query will be run later from a
        // subreport.
        if (relationalDbAction.getOutputPreparedStatement() != null) {
          prepareQuery(baseQuery);
          IActionOutput actionOutput = relationalDbAction.getOutputPreparedStatement();
          if (actionOutput != null) {
            actionOutput.setValue(this);
          }
          return true;
        }

        // TODO not sure if this should be allowed without connection ownership?
        // int maxRows = relationalDbAction.getMaxRows().getIntValue(-1);
        if (maxRowsInput != ActionInputConstant.NULL_INPUT) {
          this.setMaxRows(maxRowsInput.getIntValue());
        }

        // Added by Arijit Chatterjee.Sets the value of timeout. Default is -1, if parameter not
        // found.
        if (queryTimeoutInput != ActionInputConstant.NULL_INPUT) {
          this.setQueryTimeout(queryTimeoutInput.getIntValue());
        }

        if (relationalDbAction.getPerformTransform().getBooleanValue(false)) {
          runQuery(baseQuery, false); // The side effect of
          // transform rSet here

          rSet =
              PentahoDataTransmuter.crossTab(
                  rSet,
                  relationalDbAction.getTransformPivotColumn().getIntValue(-1) - 1,
                  relationalDbAction.getTransformMeasuresColumn().getIntValue(-1) - 1,
                  relationalDbAction.getTransformSortColumn().getIntValue(0) - 1,
                  (Format) relationalDbAction.getTransformPivotDataFormat().getValue(),
                  (Format) relationalDbAction.getTransformSortDataFormat().getValue(),
                  relationalDbAction.getTransformOrderOutputColumns().getBooleanValue(false));

          IActionOutput actionOutput = relationalDbAction.getOutputResultSet();
          if (actionOutput != null) {
            actionOutput.setValue(rSet);
          }
          return true;
        } else {
          return runQuery(baseQuery, relationalDbAction.getLive().getBooleanValue(false));
        }
      } else if (actionDefinition instanceof SqlConnectionAction) {
        SqlConnectionAction sqlConnectionAction = (SqlConnectionAction) actionDefinition;
        dispose();
        connection = getDatasourceConnection();
        if (connection == null) {
          return false;
        } else {
          IActionOutput actionOutput = sqlConnectionAction.getOutputConnection();
          if (actionOutput != null) {
            actionOutput.setValue(this);
            return true;
          } else {
            return false;
          }
        }
      }
    } catch (Exception e) {
      error(
          Messages.getInstance()
              .getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()),
          e); //$NON-NLS-1$
    }

    return false;
  }