@Override
  public List<? extends Operation> getOperationsByDeviceAndStatus(
      int enrolmentId, Operation.Status status) throws OperationManagementDAOException {
    PreparedStatement stmt = null;
    ResultSet rs = null;

    CommandOperation commandOperation;
    List<CommandOperation> commandOperations = new ArrayList<>();
    try {
      Connection conn = OperationManagementDAOFactory.getConnection();
      String sql =
          "SELECT o.ID, co1.ENABLED, co1.STATUS, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, "
              + "o.OPERATION_CODE FROM (SELECT co.OPERATION_ID, co.ENABLED, dm.STATUS "
              + "FROM DM_COMMAND_OPERATION co INNER JOIN (SELECT ENROLMENT_ID, OPERATION_ID, STATUS "
              + "FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? AND STATUS = ?) dm "
              + "ON dm.OPERATION_ID = co.OPERATION_ID) co1 INNER JOIN DM_OPERATION o ON co1.OPERATION_ID = o.ID";

      stmt = conn.prepareStatement(sql);
      stmt.setInt(1, enrolmentId);
      stmt.setString(2, status.toString());

      rs = stmt.executeQuery();
      while (rs.next()) {
        commandOperation = new CommandOperation();
        commandOperation.setId(rs.getInt("ID"));
        // commandOperation.setEnabled(rs.getInt("ENABLED") != 0);
        commandOperation.setEnabled(rs.getBoolean("ENABLED") != false);
        commandOperation.setStatus(Operation.Status.valueOf(rs.getString("STATUS")));
        commandOperation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
        commandOperation.setCreatedTimeStamp(rs.getString("CREATED_TIMESTAMP"));
        commandOperation.setReceivedTimeStamp(rs.getString("RECEIVED_TIMESTAMP"));
        commandOperation.setCode(rs.getString("OPERATION_CODE"));
        commandOperations.add(commandOperation);
      }
    } catch (SQLException e) {
      throw new OperationManagementDAOException(
          "SQL error occurred while retrieving the operation available "
              + "for the device'"
              + enrolmentId
              + "' with status '"
              + status.toString(),
          e);
    } finally {
      OperationManagementDAOUtil.cleanupResources(stmt, rs);
    }
    return commandOperations;
  }
  @Override
  public List<? extends Operation> getOperationsByDeviceAndStatus(
      int enrolmentId, Operation.Status status) throws OperationManagementDAOException {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    PolicyOperation policyOperation;
    List<Operation> operations = new ArrayList<>();

    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
      Connection conn = OperationManagementDAOFactory.getConnection();
      String sql =
          "SELECT po.OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION po "
              + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? "
              + "AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID";

      stmt = conn.prepareStatement(sql);
      stmt.setInt(1, enrolmentId);
      stmt.setString(2, status.toString());
      rs = stmt.executeQuery();

      while (rs.next()) {
        byte[] operationDetails = rs.getBytes("OPERATION_DETAILS");
        bais = new ByteArrayInputStream(operationDetails);
        ois = new ObjectInputStream(bais);
        policyOperation = (PolicyOperation) ois.readObject();
        policyOperation.setStatus(status);
        operations.add(policyOperation);
      }
    } catch (IOException e) {
      throw new OperationManagementDAOException(
          "IO Error occurred while de serialize the profile " + "operation object", e);
    } catch (ClassNotFoundException e) {
      throw new OperationManagementDAOException(
          "Class not found error occurred while de serialize the " + "profile operation object", e);
    } catch (SQLException e) {
      throw new OperationManagementDAOException(
          "SQL error occurred while retrieving the operation "
              + "available for the device'"
              + enrolmentId
              + "' with status '"
              + status.toString(),
          e);
    } finally {
      if (bais != null) {
        try {
          bais.close();
        } catch (IOException e) {
          log.warn("Error occurred while closing ByteArrayOutputStream", e);
        }
      }
      if (ois != null) {
        try {
          ois.close();
        } catch (IOException e) {
          log.warn("Error occurred while closing ObjectOutputStream", e);
        }
      }
      OperationManagementDAOUtil.cleanupResources(stmt, rs);
    }
    return operations;
  }