/**
  * Creates or updates the action and returns it.
  *
  * @param WiseChatAction $action
  *
  * @return WiseChatAction
  * @throws Exception On validation error
  */
 public function save($action)
 {
     global $wpdb;
     // low-level validation:
     if ($action->getCommand() === null) {
         throw new Exception('Command of the action cannot equal null');
     }
     // prepare action data:
     $columns = array('time' => $action->getTime(), 'command' => json_encode($action->getCommand()));
     // update or insert:
     if ($action->getId() !== null) {
         $columns['user_id'] = $action->getUserId();
         $wpdb->update($this->table, $columns, array('id' => $action->getId()), '%s', '%d');
     } else {
         if ($action->getUserId() !== null) {
             $columns['user_id'] = $action->getUserId();
         }
         $wpdb->insert($this->table, $columns);
         $action->setId($wpdb->insert_id);
     }
     return $action;
 }