Exemple #1
0
 /**
  * Perform a search and return record collection.
  *
  * @param AbstractQuery $query  Search query
  * @param integer       $offset Search offset
  * @param integer       $limit  Search limit
  * @param ParamBag      $params Search backend parameters
  *
  *@return \VuFindSearch\Response\RecordCollectionInterface
  **/
 public function search(AbstractQuery $query, $offset, $limit, ParamBag $params = null)
 {
     // process EDS API communication tokens.
     $authenticationToken = $this->getAuthenticationToken();
     $sessionToken = $this->getSessionToken();
     $this->debugPrint("Authentication Token: {$authenticationToken}, SessionToken: {$sessionToken}");
     // check to see if there is a parameter to only process this call as a setup
     if (null !== $params && true == $params->get('setuponly')) {
         return false;
     }
     // create query parameters from VuFind data
     $queryString = !empty($query) ? $query->getAllTerms() : '';
     $paramsStr = implode('&', null !== $params ? $params->request() : []);
     $this->debugPrint("Query: {$queryString}, Limit: {$limit}, Offset: {$offset}, " . "Params: {$paramsStr}");
     $baseParams = $this->getQueryBuilder()->build($query);
     $paramsStr = implode('&', $baseParams->request());
     $this->debugPrint("BaseParams: {$paramsStr} ");
     if (null !== $params) {
         $baseParams->mergeWith($params);
     }
     $baseParams->set('resultsPerPage', $limit);
     $page = $limit > 0 ? floor($offset / $limit) + 1 : 1;
     $baseParams->set('pageNumber', $page);
     $searchModel = $this->paramBagToEBSCOSearchModel($baseParams);
     $qs = $searchModel->convertToQueryString();
     $this->debugPrint("Search Model query string: {$qs}");
     try {
         $response = $this->client->search($searchModel, $authenticationToken, $sessionToken);
     } catch (\EbscoEdsApiException $e) {
         // if the auth or session token was invalid, try once more
         switch ($e->getApiErrorCode()) {
             case 104:
             case 108:
             case 109:
                 try {
                     // For error 104, retry auth token; for 108/9, retry sess token:
                     if ($e->getApiErrorCode() == 104) {
                         $authenticationToken = $this->getAuthenticationToken(true);
                     } else {
                         $sessionToken = $this->getSessionToken(true);
                     }
                     $response = $this->client->search($searchModel, $authenticationToken, $sessionToken);
                 } catch (Exception $e) {
                     throw new BackendException($e->getMessage(), $e->getCode(), $e);
                 }
                 break;
             default:
                 $response = [];
                 break;
         }
     } catch (Exception $e) {
         $this->debugPrint("Exception found: " . $e->getMessage());
         throw new BackendException($e->getMessage(), $e->getCode(), $e);
     }
     $collection = $this->createRecordCollection($response);
     $this->injectSourceIdentifier($collection);
     return $collection;
 }