/**
  * @throws Exception
  * @throws NotSupportedException
  */
 public function beforeSave()
 {
     if ($this->node !== null && !$this->node->getIsNewRecord()) {
         $this->node->refresh();
     }
     switch ($this->operation) {
         case self::OPERATION_MAKE_ROOT:
             $this->owner->setAttribute($this->parentAttribute, null);
             if ($this->sortable !== false) {
                 $this->owner->setAttribute($this->behavior->sortAttribute, 0);
             }
             break;
         case self::OPERATION_PREPEND_TO:
             $this->insertIntoInternal(false);
             break;
         case self::OPERATION_APPEND_TO:
             $this->insertIntoInternal(true);
             break;
         case self::OPERATION_INSERT_BEFORE:
             $this->insertNearInternal(false);
             break;
         case self::OPERATION_INSERT_AFTER:
             $this->insertNearInternal(true);
             break;
         default:
             if ($this->owner->getIsNewRecord()) {
                 throw new NotSupportedException('Method "' . $this->owner->className() . '::insert" is not supported for inserting new nodes.');
             }
     }
 }