Golem.de API

Golem  alpha

Golem.de API > Golem > Search.js (source view)
Search:
 
Filters
/*
Copyright (c) 2009, Klass und Ihlenfeld Verlag GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Klass und Ihlenfeld Verlag GmbH nor the names of its
      contributors may be used to endorse or promote products derived from 
      this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

$Id: Search.js 1587 2009-03-26 14:46:09Z am $
*/

if(!Golem.Api.Article) {
  
    Golem.Api.Article = {};

}

/**
 * Search for articles via Golem web interface
 *
 * @class Search
 * @namespace Golem.Api.Article
 * @extends Golem.Api
 * @constructor
 */
Golem.Api.Article.Search = function() {
}

/**
 * The URL of the used web service
 *
 * @property URL
 * @type {String}
 * @protected
 */
Golem.Api.Article.Search.URL = 'http://api.golem.de/api/article/search/';

/**
 * The type of access method for the web service
 *
 * @property TYPE
 * @type {String}
 * @protected
 */
Golem.Api.Article.Search.TYPE = 'GET';

Golem.Api.Article.Search.prototype = {

    /**
     * The query to use the search
     * 
     * @property query
     * @type {String}
     * @protected
     */
    query : '',

    /**
     * Search page to fetch
     *
     * @property startIndex
     * @type {Integer}
     * @protected
     */
    startIndex : 1,

    /**
     * Number of items to list per search page
     *
     * @property itemsPerPage
     * @type {Integer}
     * @protected
     */
    itemsPerPage : 10,

    /**
     * Number of total results for a search query
     *
     * @property totalResults
     * @type {Integer}
     * @protected
     */
    totalResults : 0,

    /**
     * Sets the search query for the request
     *
     * @method setQuery
     *
     * @param {String} query - the search query
     */
    setQuery : function(query) {

        this.query = query;

    },

    /**
     * Returns the page of the result.
     *
     * <p>
     * The value may be changed after request.
     * </p>
     *
     * @method getStartIndex
     *
     * @return {Integer}
     */
    getStartIndex : function() {

        return this.startIndex;

    },

    /**
     * Sets the page to fetch.
     *
     * @method setStartIndex
     *
     * @param {Integer} startIndex - the page number
     */
    setStartIndex : function(startIndex) {

        this.startIndex = startIndex;

    },

    /**
     * Returns the number of result items per page.
     *
     * <p>
     * The value may be changed after request.
     * </p>
     *
     * @method getItemsPerPage
     *
     * @return {Integer}
     */
    getItemsPerPage : function() {

        return this.itemsPerPage;

    },

    /**
     * Sets the number of result items to fetch per request.
     * The valid value range may depend on the server settings.
     *
     * @method setItemsPerPage
     *
     * @param {Integer} itemsPerPage - the number of items to fetch
     */
    setItemsPerPage : function(itemsPerPage) {

        this.itemsPerPage = itemsPerPage;

    },

    /**
     * Returns the total number of results for the search query
     *
     * @method getTotalResults
     *
     * @return {Integer} - the total number of search results
     */
    getTotalResults : function() {

        return this.totalResults;

    },

    /**
     * Shortcut method for traversing the search result.
     *
     * <p>
     * Fetches the articles via <i>fetch()</i> first
     * and then calls <i>fnCallback</i> for every
     * article record like <i>each()</i> does.
     * </p>
     * <p>
     * There is no way to handle errors with this method!
     * </p>
     *
     * @method fetchEach
     *
     * @param {Function} fnCallback - Function to call for every article record
     * @param {Mixed} params - additional data to pass to the callback function
     */
    fetchEach : function(fnCallback, params) {

        var oSelf = this;

        this.fetch(
            {

                fnSuccess : function() {

                    if(fnCallback) {
    
                        this.each(fnCallback, params);
                    
                    }
                    
                }

            }

        );

    },

    /**
     * Traverses through the search list.
     *
     * <p>
     * After a succesful request you can traverse
     * through the result list. You pass a callback
     * function to the method, and the method is
     * called for each article
     * </p>
     * <p>
     * The first argument to this callback function is
     * an <i>article</i> object with the properties:
     * <ul>
     * <li><i>articleid</i> - {Integer} - The article identifier</li>
     * <li><i>headline</i> - {String(60)} - The article headline</li>
     * <li><i>abstracttext</i> - {String} - The article abstract</li>
     * <li><i>url</i> - {String} - The article URL</li>
     * <li><i>date</i> - {Integer} - The article publishing date in microseconds</li>
     * </ul>
     * The second argument will be the data provided by <i>params</i>.
     * </p>
     *
     * @method each
     *
     * @param {Function} fnCallback - the callback function to call for each article
     * @param {Mixed} params - additional data passed to the callback function
     */
    each : function(fnCallback, params) {

        if(null == this.data) {

            return false;

        }

        if(!this.data.length) {
            return true;
        }

        var records = this.data;

        for(var i = 0; i < records.length; i++) {

            var record = records[i];

            if(fnCallback) {

                fnCallback.call(this,
                    {
                        articleid    : record.articleid,
                        headline     : record.headline,
                        abstracttext : record.abstracttext,
                        url          : record.url,
                        date         : record.date
                    },
                    params
                );
                
            }

        }

        return true;

    },

    /**
     * Fetches the search results via web service
     *
     * <p>
     * In case of a successful request, the <i>obj.fnSuccess</i>-callback function
     * will be called. The first argument for the callback method is the result
     * data of the request. The second argument will be the data provided by <i>obj.params</i>.
     * </p>
     * <p>
     * In case of a failed request, the <i>obj.fnError</i> callback function
     * will be called. The first argument to the callback is the error code,
     * the second ist the error message.
     * </p>
     *
     * @method fetch
     *
     * @param {Object} obj - Object literal specifing the callback methods
     */
    fetch : function(obj) {

        var oSelf = this;

        if('' == this.query) {

            return false;

        }

        return Golem.Request.doRequest(Golem.Api.Article.Search.URL +
                                        encodeURIComponent(this.startIndex) + '/' +
                                        encodeURIComponent(this.itemsPerPage) + '/' +
                                        encodeURIComponent(this.query) + '/',
            '',
            function(result) {

                oSelf.data      = result.data.records;

                oSelf.itemsPerPage = result.data.itemsPerPage;
                oSelf.totalResults = result.data.totalResults;
                oSelf.startIndex   = result.data.startIndex;
                
                if(obj.fnSuccess) {

                    var p = undefined;

                    if(obj.params) {

                        p = obj.params;

                    }

                    obj.fnSuccess.call(oSelf, result.data, p);

                }

            },
            function(errCode, errMsg) {

                oSelf.errorMessage = errMsg;
                oSelf.errorCode    = errCode;

                if(obj.fnError) {

                    var p = undefined;

                    if(obj.params) {

                        p = obj.params;

                    }

                    obj.fnError.call(oSelf, params);

                }

            }, this.timeout
        );

    }

}

Golem.Api.Article.Search.prototype.data    = Golem.Api.prototype.data;
Golem.Api.Article.Search.prototype.getData = Golem.Api.prototype.getData;

Golem.Api.Article.Search.prototype.errorMessage    = Golem.Api.prototype.errorMessage;
Golem.Api.Article.Search.prototype.errorCode       = Golem.Api.prototype.errorCode;
Golem.Api.Article.Search.prototype.getErrorMessage = Golem.Api.prototype.getErrorMessage;
Golem.Api.Article.Search.prototype.getErrorCode    = Golem.Api.prototype.getErrorCode;

Golem.Api.Article.Search.prototype.useTimeout = Golem.Api.prototype.useTimeout;
Golem.Api.Article.Search.prototype.timeout    = Golem.Api.prototype.timeout;

/**
 * Indicates that a word in the search query is too short
 *
 * @property ERROR_WORD_TOO_SHORT
 * @type {Integer}
 */
Golem.Api.Article.Search.ERROR_WORD_TOO_SHORT = 12;

/**
 * Indicates that a word in the search query is too long
 *
 * @property ERROR_WORD_TOO_LONG
 * @type {Integer}
 */
Golem.Api.Article.Search.ERROR_WORD_TOO_LONG = 13;

/**
 * Indicates that the whole search query is too long
 *
 * @property ERROR_QUERY_TOO_LONG
 * @type {Integer}
 */
Golem.Api.Article.Search.ERROR_QUERY_TOO_LONG = 14;

/**
 * Indicates that the search query contains too much words
 *
 * @property ERROR_TOO_MUCH_WORDS
 * @type {Integer}
 */
Golem.Api.Article.Search.ERROR_TOO_MUCH_WORDS = 15;

Copyright © 2010 Golem.de.