@Test
  public void testGetworkflowByName() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    Workflow w = repo.getWorkflowByName("Test Workflow");

    assertNotNull(w);

    assertThat("Test Workflow", equalTo(w.getName()));
  }
  @BeforeClass
  public static void setup() throws MalformedURLException {

    testWrkInst = new WorkflowInstance();
    testWrkFlw = new Workflow();
    testTask = new WorkflowTask();
    testCond = new WorkflowCondition();
    Metadata sharedContext = new Metadata();

    // to check if the path already exists and to delete if it does exist
    if (new File(catalogPath).exists()) {
      try {
        FileUtils.deleteDirectory(new File(catalogPath));
      } catch (IOException e) {
        fail(e.getMessage());
      }
    }
    repo = new LuceneWorkflowInstanceRepository(catalogPath, stdPgSz);

    testWrkFlw.setName("test.getMetadataWorkflow");
    testWrkFlw.setId("test.id");
    List tasks = new Vector();
    List conds = new Vector();

    testCond.setConditionId("test.cond.id");
    testCond.setConditionInstanceClassName("test.class");
    testCond.setConditionName("test.cond.name");
    testCond.setOrder(1);
    conds.add(testCond);

    testTask.setTaskConfig(new WorkflowTaskConfiguration());
    testTask.setTaskId("test.task.id");
    testTask.setConditions(conds);
    testTask.setOrder(1);
    testTask.setTaskInstanceClassName("test.class");
    testTask.setTaskName("test.task.name");
    tasks.add(testTask);
    testWrkFlw.setTasks(tasks);

    testWrkInst.setCurrentTaskId("test.task");
    testWrkInst.setStatus("STARTED");
    testWrkInst.setWorkflow(testWrkFlw);

    sharedContext.addMetadata("key1", "val1");
    sharedContext.addMetadata("key1", "val2");
    sharedContext.addMetadata("key1", "val3");
    sharedContext.addMetadata("key2", "val4");
    sharedContext.addMetadata("key2", "val5");
    testWrkInst.setSharedContext(sharedContext);
    startXmlRpcWorkflowManager();
    startWorkflow();
    fmc = new XmlRpcWorkflowManagerClient(new URL("http://localhost:" + WM_PORT));
  }
  /** @since OODT-205 */
  @Test
  public void testWorkflowConditions() {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    Workflow w = null;
    try {
      w = repo.getWorkflowById("1");
    } catch (Exception e) {
      fail(e.getMessage());
    }

    assertNotNull(w);
    assertNotNull(w.getConditions());
    assertTrue(w.getConditions().size() > 0);
    assertEquals(w.getConditions().size(), 1);
  }
  /*
   * (non-Javadoc)
   *
   * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#startWorkflow(org.apache.oodt.cas.workflow.structs.Workflow,
   *      org.apache.oodt.cas.metadata.Metadata)
   */
  public synchronized WorkflowInstance startWorkflow(Workflow workflow, Metadata metadata)
      throws EngineException {
    // to start the workflow, we create a default workflow instance
    // populate it
    // persist it
    // add it to the worker map
    // start it

    WorkflowInstance wInst = new WorkflowInstance();
    wInst.setWorkflow(workflow);
    wInst.setCurrentTaskId(((WorkflowTask) workflow.getTasks().get(0)).getTaskId());
    wInst.setSharedContext(metadata);
    wInst.setStatus(CREATED);
    persistWorkflowInstance(wInst);

    IterativeWorkflowProcessorThread worker =
        new IterativeWorkflowProcessorThread(wInst, instRep, this.wmgrUrl);
    worker.setRClient(rClient);
    workerMap.put(wInst.getId(), worker);

    wInst.setStatus(QUEUED);
    persistWorkflowInstance(wInst);

    try {
      pool.execute(worker);
    } catch (InterruptedException e) {
      throw new EngineException(e);
    }

    return wInst;
  }
  @Test
  public void testAddWorkflow() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    Workflow w = new Workflow();
    w.setId("50");
    w.setName("Manual");
    WorkflowTask t = new WorkflowTask();
    t.setTaskId("1");
    List<WorkflowTask> l = new ArrayList<WorkflowTask>();
    l.add(t);
    w.setTasks(l);
    String workflow = repo.addWorkflow(w);

    assertThat(workflow, notNullValue());
    assertThat(workflow, equalTo("50"));

    // TODO GET WORKFLOW

  }
  public Workflow getWorkflowByName(String workflowName, boolean getTasks)
      throws RepositoryException {
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;

    Workflow workflow = null;

    try {
      conn = dataSource.getConnection();
      statement = conn.createStatement();

      String getWorkflowSql =
          "SELECT * from workflows WHERE workflow_name = '" + workflowName + "'";

      LOG.log(Level.FINE, "getWorkflowByName: Executing: " + getWorkflowSql);
      rs = statement.executeQuery(getWorkflowSql);

      while (rs.next()) {
        workflow = DbStructFactory.getWorkflow(rs);

        if (getTasks) {
          workflow.setTasks(getTasksByWorkflowId(workflow.getId()));
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      LOG.log(Level.WARNING, "Exception getting workflow. Message: " + e.getMessage());
      try {
        conn.rollback();
      } catch (SQLException e2) {
        LOG.log(
            Level.SEVERE,
            "Unable to rollback getWorkflowByName transaction. Message: " + e2.getMessage());
      }
      throw new RepositoryException(e.getMessage());
    } finally {

      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException ignore) {
        }

        rs = null;
      }

      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException ignore) {
        }

        statement = null;
      }

      if (conn != null) {
        try {
          conn.close();

        } catch (SQLException ignore) {
        }

        conn = null;
      }
    }

    return workflow;
  }
  public List getWorkflowsForEvent(String eventName, boolean getTasks) throws RepositoryException {
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;

    List workflows = null;

    try {
      conn = dataSource.getConnection();
      statement = conn.createStatement();

      String getWorkflowSql =
          "SELECT * from workflows, event_workflow_map WHERE event_workflow_map.workflow_id = workflows.workflow_id  "
              + "AND event_workflow_map.event_name = '"
              + eventName
              + "'";

      LOG.log(Level.FINE, "getWorkflowsForEvent: Executing: " + getWorkflowSql);
      rs = statement.executeQuery(getWorkflowSql);
      workflows = new Vector();

      while (rs.next()) {
        Workflow workflow = DbStructFactory.getWorkflow(rs);

        if (getTasks) {
          workflow.setTasks(getTasksByWorkflowId(workflow.getId()));
        }
        workflows.add(workflow);
      }

      if (workflows.size() == 0) {
        workflows = null;
      }

    } catch (Exception e) {
      e.printStackTrace();
      LOG.log(Level.WARNING, "Exception getting workflows for event. Message: " + e.getMessage());
      try {
        conn.rollback();
      } catch (SQLException e2) {
        LOG.log(
            Level.SEVERE,
            "Unable to rollback getWorkflowsForEvent transaction. Message: " + e2.getMessage());
      }
      throw new RepositoryException(e.getMessage());
    } finally {

      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException ignore) {
        }

        rs = null;
      }

      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException ignore) {
        }

        statement = null;
      }

      if (conn != null) {
        try {
          conn.close();

        } catch (SQLException ignore) {
        }

        conn = null;
      }
    }

    return workflows;
  }