Snippets

Piotr Szrajber Geospatial Portal SDK - Experimental gp.queries.find replacement that takes wildcard and returns a collection

Created by Piotr Szrajber
/**
 * Experimental implementation of gp.queries.find replacement
 * To be "pasted" next to gp.queries.find :-)
 * 2017-03-03 Piotr Szrajber <piotr.szrajber@hexagongeospatial.com>
 *
 *   $GP.queries.find2({
 *       name: /.*$/
 *   }, function(ret) {
 *       console.log(ret.analyses)
 *   }, function(err) {
 *       console.error(err)
 *   });
 *
 * @method find2
 * @param {Object} config
 * @param {String} [config.id] Analysis ID
 * @param {String/RegEx} [config.name] Analysis name. it can be regex
 * @param {Function} [callback] callback executed if operation succeeds
 * @param {Object} callback.ret returning object
 * @param {Analysis[]} callback.ret.analyses Analyses collection
 * @param {Analysis} callback.ret.analysis first found Analysis (for compatibility)
 * @param {Function} [errback] callback executed if operation fails or doesn't find any analysis
 * @return {void}
 */
find2: function(config, callback, errback) {
    var findBy = makeFindByPredicate({
            key: "id",
            value: config.id
        }, {
            key: "name",
            value: config.name
        }),
        predicate = config.predicate || ((isSet(config.id) || isSet(config.name)) ? findBy : alwaysTrue),
        a = getPortalObj(P_ANALYSIS_MANAGER)._analyses,
        result = Object.keys(a).map(function(analysisId) {
            return {
                id: analysisId,
                name: a[analysisId].get_name()
            };
        }).filter(predicate).map(function(obj) {
            return new Analysis({
                config: {
                    mapStateId: config.mapStateId,
                    analysisId: obj.id
                },
                portalAnalysis: a[obj.id]
            });
        });
    if (result.length > 0) {
        fire(F_SUCCESS, callback, {
            success: true,
            analyses: result,
            analysis: result[0] // compatibility
        });
    } else {
        fire(F_FAILURE, errback, {
            success: false,
            msg: "Analysis not found."
        });
    }
}

Comments (0)