/**
     * @param Pap_Db_VisitorAffiliate
     * @return Pap_Db_VisitorAffiliate
     */
    public function recognizeAffiliate(Pap_Db_VisitorAffiliate $visitorAffiliate) {
        $user = $this->getUserById($visitorAffiliate->getUserId());

        if ($user == null) {
            $this->isValid = false;
            return;
        }
        
        $this->context->setUserObject($user);

        $campaign = $this->getCampaignById($visitorAffiliate->getCampaignId());
        if ($campaign != null && $this->context->getCampaignObject() == null) {
            $this->context->setCampaignObject($campaign);
        }

        $banner = $this->getBannerById($visitorAffiliate->getBannerId());
        if ($banner != null && $this->context->getBannerObject() == null) {
            $this->context->setBannerObject($banner);
        }

        $channel = $this->getChannelById($visitorAffiliate->getChannelId());
        if ($channel != null) {
            $this->context->setChannelObject($channel);
        }
        $this->context->setVisitorAffiliate($visitorAffiliate);

        $this->isValid = true;
    }
    public function recognize(Pap_Contexts_Action $context) {
        if ($context->isVisitorAffiliateRecognized()) {
            return;
        }
        
        if(Gpf_Settings::get(Pap_Settings::TRACK_BY_IP_SETTING_NAME) != Gpf::YES) {
            return;
        }
        
        $ip = $context->getIp();
        $context->debug('Trying to get visitor affiliate from IP address '. $ip);

        $visitorAffiliate = $this->visitorAffiliateCache->getLatestVisitorAffiliateFromIp($ip, $context->getAccountId());
        if ($visitorAffiliate == null) {
            $context->debug("No visitor affiliate from IP '$ip'");
            return;
        }
        
        try {
            $periodInSeconds = $this->getValidityInSeconds();
        } catch (Gpf_Exception $e) {
            $context->debug($e->getMessage());
            return;
        }
        
        
        $dateFrom = new Gpf_DateTime($context->getVisitDateTime());
        $dateFrom->addSecond(-1*$periodInSeconds);
        $dateVisit = new Gpf_DateTime($visitorAffiliate->getDateVisit());

        if ($dateFrom->compare($dateVisit) > 0) {
            $context->debug("    No click from IP '$ip' found within ip validity period");
            return null;
        }

        if (!$context->isTrackingMethodSet()) {
            $context->setTrackingMethod(Pap_Common_Transaction::TRACKING_METHOD_IP_ADDRESS);
        }
        $context->debug('Visitor affiliate recognized from IP, id: '.$visitorAffiliate->getId(). ', accountId: '. $visitorAffiliate->getAccountId());
        $context->setVisitorAffiliate($visitorAffiliate);
    }