/**
  * Test columns as this dataset defintion contains custom definitions. An exception must be thrown
  * due to cannot change employee column type to label, as it's an anaylzed string in the EL index
  * mapping.
  */
 @Test(expected = RuntimeException.class)
 public void testColumnsBadDefined() throws Exception {
   dataSetManager.lookupDataSet(
       DataSetFactory.newDataSetLookupBuilder()
           .dataset(EL_DATASET_BAD_COLUMNS_UUID)
           .sort(ExpenseReportsData.COLUMN_ID, SortOrder.ASCENDING)
           .buildLookup());
 }
 @Test(expected = RuntimeException.class)
 public void testSortingWithNonDefinedColumn() throws Exception {
   DataSet result =
       dataSetManager.lookupDataSet(
           DataSetFactory.newDataSetLookupBuilder()
               .dataset(EL_DATASET_CUSTOM_COLUMNS2_UUID)
               .sort(ExpenseReportsData.COLUMN_DEPARTMENT, SortOrder.DESCENDING)
               .buildLookup());
 }
 /**
  * **********************************************************************************************************************************************************************************************
  * LOOKUP TESTING.
  * **********************************************************************************************************************************************************************************************
  */
 @Test(expected = RuntimeException.class)
 public void testSortingWithNonExstingColumn() throws Exception {
   DataSet result =
       dataSetManager.lookupDataSet(
           DataSetFactory.newDataSetLookupBuilder()
               .dataset(EL_DATASET_CUSTOM_COLUMNS_UUID)
               .sort("mycolumn", SortOrder.DESCENDING)
               .buildLookup());
 }
  /** Test using column defined in def (allColumns flag is set to false) */
  @Test
  public void testGivenColumns() throws Exception {
    DataSet result =
        dataSetManager.lookupDataSet(
            DataSetFactory.newDataSetLookupBuilder()
                .dataset(EL_DATASET_CUSTOM_COLUMNS2_UUID)
                .sort(ExpenseReportsData.COLUMN_ID, SortOrder.ASCENDING)
                .buildLookup());

    // Columns size assertion.
    Assert.assertNotNull(result.getColumns());
    Assert.assertTrue(result.getColumns().size() == 4);

    // Columns id & type assertion.
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_ID, ColumnType.NUMBER);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_EMPLOYEE, ColumnType.TEXT);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_CITY, ColumnType.TEXT);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_AMOUNT, ColumnType.NUMBER);
  }
  /**
   * Test retrieving all columns from index mapping (no columns defined in def and allColumns flag
   * is set to true)
   */
  @Test
  public void testAllColumns() throws Exception {
    DataSet result =
        dataSetManager.lookupDataSet(
            DataSetFactory.newDataSetLookupBuilder()
                .dataset(EL_DATASET_ALL_COLUMNS_UUID)
                .buildLookup());

    // Columns size assertion.
    Assert.assertNotNull(result.getColumns());
    Assert.assertTrue(result.getColumns().size() == 6);

    // Columns id & type assertion.
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_AMOUNT, ColumnType.NUMBER);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_CITY, ColumnType.LABEL);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_DATE, ColumnType.DATE);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_DEPARTMENT, ColumnType.LABEL);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_EMPLOYEE, ColumnType.TEXT);
    assertColumnIdAndType(result, ExpenseReportsData.COLUMN_ID, ColumnType.NUMBER);
  }
  protected void registerDataSetDefinitions() {

    DataSetDef humanTasksDef =
        DataSetFactory.newSQLDataSetDef()
            .uuid(HUMAN_TASKS_DATASET)
            .name("Human tasks")
            .dataSource(jbpmDatasource)
            .dbTable(HUMAN_TASKS_TABLE, false)
            .date(DataSetTasksListGridViewImpl.COLUMN_ACTIVATIONTIME)
            .label(DataSetTasksListGridViewImpl.COLUMN_ACTUALOWNER)
            .label(DataSetTasksListGridViewImpl.COLUMN_CREATEDBY)
            .date(DataSetTasksListGridViewImpl.COLUMN_CREATEDON)
            .label(DataSetTasksListGridViewImpl.COLUMN_DEPLOYMENTID)
            .text(DataSetTasksListGridViewImpl.COLUMN_DESCRIPTION)
            .date(DataSetTasksListGridViewImpl.COLUMN_DUEDATE)
            .label(DataSetTasksListGridViewImpl.COLUMN_NAME)
            .number(DataSetTasksListGridViewImpl.COLUMN_PARENTID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PRIORITY)
            .label(DataSetTasksListGridViewImpl.COLUMN_PROCESSID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PROCESSINSTANCEID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PROCESSSESSIONID)
            .label(DataSetTasksListGridViewImpl.COLUMN_STATUS)
            .number(DataSetTasksListGridViewImpl.COLUMN_TASKID)
            .number(DataSetTasksListGridViewImpl.COLUMN_WORKITEMID)
            .buildDef();

    DataSetDef humanTasksWithUserDef =
        DataSetFactory.newSQLDataSetDef()
            .uuid(HUMAN_TASKS_WITH_USER_DATASET)
            .name("Human tasks and users")
            .dataSource(jbpmDatasource)
            .dbSQL(
                "select  t.activationtime, t.actualowner, t.createdby, "
                    + "t.createdon, t.deploymentid, t.description, t.duedate, "
                    + "t.name, t.parentid, t.priority, t.processid, t.processinstanceid, "
                    + "t.processsessionid, t.status, t.taskid, t.workitemid, oe.id oeid "
                    + "from AuditTaskImpl t, "
                    + "peopleassignments_potowners po, "
                    + "organizationalentity oe "
                    + "where t.id = po.task_id and po.entity_id = oe.id",
                false)
            .date(DataSetTasksListGridViewImpl.COLUMN_ACTIVATIONTIME)
            .label(DataSetTasksListGridViewImpl.COLUMN_ACTUALOWNER)
            .label(DataSetTasksListGridViewImpl.COLUMN_CREATEDBY)
            .date(DataSetTasksListGridViewImpl.COLUMN_CREATEDON)
            .label(DataSetTasksListGridViewImpl.COLUMN_DEPLOYMENTID)
            .text(DataSetTasksListGridViewImpl.COLUMN_DESCRIPTION)
            .date(DataSetTasksListGridViewImpl.COLUMN_DUEDATE)
            .label(DataSetTasksListGridViewImpl.COLUMN_NAME)
            .number(DataSetTasksListGridViewImpl.COLUMN_PARENTID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PRIORITY)
            .label(DataSetTasksListGridViewImpl.COLUMN_PROCESSID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PROCESSINSTANCEID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PROCESSSESSIONID)
            .label(DataSetTasksListGridViewImpl.COLUMN_STATUS)
            .label(
                DataSetTasksListGridViewImpl
                    .COLUMN_TASKID) // declaring as label(even though it's numeric) because needs
                                    // apply groupby and  Group by number not supported
            .number(DataSetTasksListGridViewImpl.COLUMN_WORKITEMID)
            .label(DataSetTasksListGridViewImpl.COLUMN_ORGANIZATIONAL_ENTITY)
            .buildDef();

    DataSetDef humanTaskWithAdminDef =
        DataSetFactory.newSQLDataSetDef()
            .uuid(HUMAN_TASKS_WITH_ADMIN_DATASET)
            .name("Human tasks and admins")
            .dataSource(jbpmDatasource)
            .dbSQL(
                "select t.activationtime, t.actualowner, t.createdby, "
                    + "t.createdon, t.deploymentid, t.description, t.duedate, "
                    + "t.name, t.parentid, t.priority, t.processid, t.processinstanceid, "
                    + "t.processsessionid, t.status, t.taskid, t.workitemid, oe.id oeid "
                    + "from AuditTaskImpl t, "
                    + "peopleassignments_bas bas, "
                    + "organizationalentity oe "
                    + "where t.id = bas.task_id and bas.entity_id = oe.id",
                false)
            .date(DataSetTasksListGridViewImpl.COLUMN_ACTIVATIONTIME)
            .label(DataSetTasksListGridViewImpl.COLUMN_ACTUALOWNER)
            .label(DataSetTasksListGridViewImpl.COLUMN_CREATEDBY)
            .date(DataSetTasksListGridViewImpl.COLUMN_CREATEDON)
            .label(DataSetTasksListGridViewImpl.COLUMN_DEPLOYMENTID)
            .text(DataSetTasksListGridViewImpl.COLUMN_DESCRIPTION)
            .date(DataSetTasksListGridViewImpl.COLUMN_DUEDATE)
            .label(DataSetTasksListGridViewImpl.COLUMN_NAME)
            .number(DataSetTasksListGridViewImpl.COLUMN_PARENTID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PRIORITY)
            .label(DataSetTasksListGridViewImpl.COLUMN_PROCESSID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PROCESSINSTANCEID)
            .number(DataSetTasksListGridViewImpl.COLUMN_PROCESSSESSIONID)
            .label(DataSetTasksListGridViewImpl.COLUMN_STATUS)
            .label(
                DataSetTasksListGridViewImpl
                    .COLUMN_TASKID) // declaring as label(even though it's numeric) because needs
                                    // apply groupby and  Group by number not supported
            .number(DataSetTasksListGridViewImpl.COLUMN_WORKITEMID)
            .label(DataSetTasksListGridViewImpl.COLUMN_ORGANIZATIONAL_ENTITY)
            .buildDef();

    DataSetDef processesWithVariablesDef =
        DataSetFactory.newSQLDataSetDef()
            .uuid(PROCESS_INSTANCE_WITH_VARIABLES_DATASET)
            .name("Domain Specific Process Instances")
            .dataSource(jbpmDatasource)
            .dbSQL(
                "select pil.processInstanceId pid,\n"
                    + "       pil.processId pname,\n"
                    + "       v.id varid,\n"
                    + "       v.variableId varname,\n"
                    + "       v.value varvalue\n"
                    + "from ProcessInstancelog pil\n"
                    + "  inner join (select vil.processInstanceId ,vil.variableId, MAX(vil.ID) maxvilid  FROM VariableInstanceLog vil\n"
                    + "  GROUP BY vil.processInstanceId, vil.variableId)  x\n"
                    + "    on (x.processInstanceId =pil.processInstanceId)\n"
                    + "  INNER JOIN VariableInstanceLog v\n"
                    + "    ON (v.variableId = x.variableId  AND v.id = x.maxvilid )",
                false)
            .number("pid")
            .label("pname")
            .number("varid")
            .label("varname")
            .label("varvalue")
            .buildDef();

    // Hide all these internal data set from end user view
    humanTasksDef.setPublic(false);
    humanTasksWithUserDef.setPublic(false);
    humanTaskWithAdminDef.setPublic(false);
    processesWithVariablesDef.setPublic(false);

    // Register the data set definitions
    dataSetDefRegistry.registerDataSetDef(humanTasksDef);
    dataSetDefRegistry.registerDataSetDef(humanTasksWithUserDef);
    dataSetDefRegistry.registerDataSetDef(humanTaskWithAdminDef);
    dataSetDefRegistry.registerDataSetDef(processesWithVariablesDef);
  }