public CommandOperation getOperation(int id) throws OperationManagementDAOException {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    CommandOperation commandOperation = null;
    try {
      Connection conn = OperationManagementDAOFactory.getConnection();
      String sql = "SELECT OPERATION_ID, ENABLED FROM DM_COMMAND_OPERATION WHERE OPERATION_ID = ?";
      stmt = conn.prepareStatement(sql);
      stmt.setInt(1, id);
      rs = stmt.executeQuery();

      if (rs.next()) {
        commandOperation = new CommandOperation();
        commandOperation.setEnabled(rs.getInt("ENABLED") != 0);
      }
    } catch (SQLException e) {
      throw new OperationManagementDAOException(
          "SQL Error occurred while retrieving the command operation "
              + "object available for the id '"
              + id,
          e);
    } finally {
      OperationManagementDAOUtil.cleanupResources(stmt, rs);
    }
    return commandOperation;
  }
 @Override
 public void deleteOperation(int operationId) throws OperationManagementDAOException {
   PreparedStatement stmt = null;
   try {
     super.deleteOperation(operationId);
     Connection connection = OperationManagementDAOFactory.getConnection();
     stmt = connection.prepareStatement("DELETE DM_POLICY_OPERATION WHERE OPERATION_ID=?");
     stmt.setInt(1, operationId);
     stmt.executeUpdate();
   } catch (SQLException e) {
     throw new OperationManagementDAOException(
         "Error occurred while deleting operation metadata", e);
   } finally {
     OperationManagementDAOUtil.cleanupResources(stmt);
   }
 }
  @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 void updateOperation(Operation operation) throws OperationManagementDAOException {
   PreparedStatement stmt = null;
   try {
     Connection connection = OperationManagementDAOFactory.getConnection();
     stmt =
         connection.prepareStatement(
             "UPDATE DM_COMMAND_OPERATION SET ENABLED = ? WHERE OPERATION_ID = ?");
     stmt.setBoolean(1, operation.isEnabled());
     stmt.setInt(2, operation.getId());
     stmt.executeUpdate();
   } catch (SQLException e) {
     throw new OperationManagementDAOException(
         "Error occurred while adding operation metadata", e);
   } finally {
     OperationManagementDAOUtil.cleanupResources(stmt);
   }
 }
  @Override
  public void updateOperation(Operation operation) throws OperationManagementDAOException {
    PreparedStatement stmt = null;
    ByteArrayOutputStream bao = null;
    ObjectOutputStream oos = null;
    try {
      super.updateOperation(operation);
      Connection connection = OperationManagementDAOFactory.getConnection();
      stmt =
          connection.prepareStatement(
              "UPDATE DM_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " + "WHERE O.OPERATION_ID=?");
      bao = new ByteArrayOutputStream();
      oos = new ObjectOutputStream(bao);
      oos.writeObject(operation);

      stmt.setBytes(1, bao.toByteArray());
      stmt.setInt(2, operation.getId());
      stmt.executeUpdate();
    } catch (SQLException e) {
      throw new OperationManagementDAOException(
          "Error occurred while update policy operation metadata", e);
    } catch (IOException e) {
      throw new OperationManagementDAOException(
          "Error occurred while serializing policy operation object", e);
    } finally {
      if (bao != null) {
        try {
          bao.close();
        } catch (IOException e) {
          log.warn("Error occurred while closing ByteArrayOutputStream", e);
        }
      }
      if (oos != null) {
        try {
          oos.close();
        } catch (IOException e) {
          log.warn("Error occurred while closing ObjectOutputStream", e);
        }
      }
      OperationManagementDAOUtil.cleanupResources(stmt);
    }
  }
 @Override
 public int addOperation(Operation operation) throws OperationManagementDAOException {
   int operationId;
   CommandOperation commandOp = (CommandOperation) operation;
   PreparedStatement stmt = null;
   try {
     operationId = super.addOperation(operation);
     Connection conn = OperationManagementDAOFactory.getConnection();
     stmt =
         conn.prepareStatement(
             "INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)");
     stmt.setInt(1, operationId);
     stmt.setBoolean(2, commandOp.isEnabled());
     stmt.executeUpdate();
   } catch (SQLException e) {
     throw new OperationManagementDAOException("Error occurred while adding command operation", e);
   } finally {
     OperationManagementDAOUtil.cleanupResources(stmt);
   }
   return operationId;
 }
  @Override
  public Operation getOperation(int operationId) throws OperationManagementDAOException {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    PolicyOperation policyOperation = null;

    ByteArrayInputStream bais;
    ObjectInputStream ois;
    try {
      Connection conn = OperationManagementDAOFactory.getConnection();
      String sql =
          "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?";
      stmt = conn.prepareStatement(sql);
      stmt.setInt(1, operationId);
      rs = stmt.executeQuery();

      if (rs.next()) {
        byte[] operationDetails = rs.getBytes("OPERATION_DETAILS");
        bais = new ByteArrayInputStream(operationDetails);
        ois = new ObjectInputStream(bais);
        policyOperation = (PolicyOperation) ois.readObject();
      }
    } catch (IOException e) {
      throw new OperationManagementDAOException(
          "IO Error occurred while de serialize the policy operation " + "object", e);
    } catch (ClassNotFoundException e) {
      throw new OperationManagementDAOException(
          "Class not found error occurred while de serialize the " + "policy operation object", e);
    } catch (SQLException e) {
      throw new OperationManagementDAOException(
          "SQL Error occurred while retrieving the policy operation "
              + "object available for the id '"
              + operationId
              + "'",
          e);
    } finally {
      OperationManagementDAOUtil.cleanupResources(stmt, rs);
    }
    return policyOperation;
  }
 @Override
 public int addOperation(Operation operation) throws OperationManagementDAOException {
   int operationId;
   PreparedStatement stmt = null;
   try {
     operationId = super.addOperation(operation);
     operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
     operation.setId(operationId);
     operation.setEnabled(true);
     PolicyOperation policyOperation = (PolicyOperation) operation;
     Connection conn = OperationManagementDAOFactory.getConnection();
     stmt =
         conn.prepareStatement(
             "INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)");
     stmt.setInt(1, operationId);
     stmt.setObject(2, policyOperation);
     stmt.executeUpdate();
   } catch (SQLException e) {
     throw new OperationManagementDAOException("Error occurred while adding policy operation", e);
   } finally {
     OperationManagementDAOUtil.cleanupResources(stmt);
   }
   return operationId;
 }
  @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;
  }