/**
  * Executor
  *
  * @throws UpdateDomainContactTask_Exception
  */
 public function Run($userid = null)
 {
     try {
         $Factory = RegistryModuleFactory::GetInstance();
         $Registry = $Factory->GetRegistryByExtension($this->Domain->Extension);
         $Manifest = $Registry->GetManifest();
         if ($userid && $this->Domain->UserID != $userid) {
             throw new UpdateDomainContactAction_Exception(_("You don't have permissions for manage this domain"), UpdateDomainContactAction_Exception::DOMAIN_NOT_BELONGS_TO_USER);
         }
         $OldContact = $this->Domain->GetContact($this->contact_type);
         if ($this->NewContact && $this->NewContact->UserID != $this->Domain->UserID) {
             throw new UpdateDomainContactAction_Exception(_("You don't have permissions for using this contact"), UpdateDomainContactAction_Exception::CONTACT_NOT_BELONGS_TO_USER);
         }
         $trade = $Manifest->GetRegistryOptions()->ability->trade == '1';
         if ($this->contact_type != CONTACT_TYPE::REGISTRANT || !$trade) {
             try {
                 $Registry->UpdateDomainContact($this->Domain, $this->contact_type, $OldContact, $this->NewContact);
                 return $this->Domain->HasPendingOperation(Registry::OP_UPDATE) ? UpdateDomainContactAction_Result::PENDING : UpdateDomainContactAction_Result::OK;
             } catch (Exception $e) {
                 throw new UpdateDomainContactAction_Exception(sprintf(_("Cannot change contact. Reason: %s"), $e->getMessage()));
             }
         } else {
             // Execute trade when:
             // - Trade invoice is paid
             // - Previous trade failed
             if ($this->Domain->IncompleteOrderOperation == INCOMPLETE_OPERATION::DOMAIN_TRADE || $this->Invoice && $this->Invoice->Status == INVOICE_STATUS::PAID) {
                 $this->Domain->SetContact($this->NewContact, CONTACT_TYPE::REGISTRANT);
                 $this->Domain->IncompleteOrderOperation = null;
                 $extra["requesttype"] = "ownerChange";
                 try {
                     $trade = $Registry->ChangeDomainOwner($this->Domain, 1, $extra);
                     return $this->Domain->HasPendingOperation(Registry::OP_TRADE) ? UpdateDomainContactAction_Result::PENDING : UpdateDomainContactAction_Result::OK;
                 } catch (Exception $e) {
                     $this->Domain->IncompleteOrderOperation = INCOMPLETE_OPERATION::DOMAIN_TRADE;
                     if ($this->Invoice) {
                         $this->Invoice->ActionStatus = INVOICE_ACTION_STATUS::FAILED;
                         $this->Invoice->ActionFailReason = $e->getMessage();
                     }
                     throw new UpdateDomainContactAction_Exception(sprintf(_("Cannot change contact. Reason: %s"), $e->getMessage()));
                 }
             } else {
                 // Issue an invoice for trade operation
                 $invoiceid = $this->Db->GetOne("SELECT * FROM invoices WHERE status=? AND itemid=? AND purpose=?", array(INVOICE_STATUS::PENDING, $this->Domain->ID, INVOICE_PURPOSE::DOMAIN_TRADE));
                 if (!$invoiceid) {
                     $this->Domain->SetExtraField("NewRegistrantCLID", $this->NewContact->CLID);
                     DBDomain::GetInstance()->Save($this->Domain);
                     $Invoice = new Invoice(INVOICE_PURPOSE::DOMAIN_TRADE, $this->Domain->ID, $userid);
                     $Invoice->Description = sprintf(_("%s domain name trade"), $this->Domain->GetHostName());
                     $Invoice->Save();
                     $this->Invoice = $Invoice;
                     return UpdateDomainContactAction_Result::INVOICE_GENERATED;
                 } else {
                     throw new UpdateDomainContactAction_Exception(_("Another domain trade is already initiated"));
                 }
             }
         }
     } catch (Exception $e) {
         throw new UpdateDomainContactAction_Exception($e->getMessage());
     }
 }
		public function UpdateDomainContact(Domain $domain, $contactType, Contact $oldContact, Contact $newContact)
		{
			if (!$newContact && !$oldContact)
				throw new Exception("At leat one contact (\$newContact or \$oldContact) must be passed into UpdateDomainContact");
			
			if ($contactType == CONTACT_TYPE::TECH && 
				preg_match('/^\d+/', $domain->GetContact(CONTACT_TYPE::REGISTRANT)->CLID))
			{
				throw new Exception("Domain transfer from SWITCH was buggy. You must update registrant contact first");
			}
				
			$params = array(
				"name" 		=> $this->MakeNameIDNCompatible($domain->GetHostName()),
				"change"	=> "",
				"add"		=> "",
				"rem"		=> ""
			);
			
			if ($contactType == CONTACT_TYPE::REGISTRANT)
				$params["change"] = "<domain:chg><domain:registrant>{$newContact->CLID}</domain:registrant></domain:chg>";
			else
			{
				if ($newContact)
					$params['add'] = '<domain:add><domain:contact type="'.$contactType.'">'.$newContact->CLID.'</domain:contact></domain:add>';
				
				if ($oldContact && !preg_match('/^\d+/', $oldContact->CLID)) 
					$params['rem'] = '<domain:rem><domain:contact type="'.$contactType.'">'.$oldContact->CLID.'</domain:contact></domain:rem>';
			}
			
			$response = $this->Request("domain-update-contact", $params);
			
			$status = ($response->Succeed) ? REGISTRY_RESPONSE_STATUS::SUCCESS : REGISTRY_RESPONSE_STATUS::FAILED;
			
			// Hack for bad transferred from SWITCH	
			if ($response->Succeed && preg_match('/^\d+/', $oldContact->CLID) && $contactType == CONTACT_TYPE::REGISTRANT)
			{
				$domain->SetContact(null, CONTACT_TYPE::TECH);
			}
			
			return new UpdateDomainContactResponse($status, $response->ErrMsg, $response->Code);
		}