src/main/webapp/js/singlepage.js
/**
	 * Attempt to parse the query pattern and update the state of the query builder
	 * to match it as much as possible.
	 *
	 * @param {string} pattern - cql query
	 * @returns True or false indicating success or failure respectively
	 */
Attempt to parse the query pattern and update the state of the query builder
to match it as much as possible.

@param {string} pattern - cql query
@returns True or false indicating success or failure respectively
Attempt to parse the query pattern and update the state of the query builder to match it as much as possible . @param { string } pattern - cql query @returns True or false indicating success or failure respectively
function populateQueryBuilder(pattern) {
		if (!pattern)
			return false;
		try {
			var parsedCql = SINGLEPAGE.CQLPARSER.parse(pattern);
			var tokens = parsedCql.tokens;
			var within = parsedCql.within;
			if (tokens === null) {
				return false;
			}
			var queryBuilder = $('#querybuilder').data('builder');
			queryBuilder.reset();
			if (tokens.length > 0) {
				$.each(queryBuilder.getTokens(), function(i, e) {
					e.element.remove();
				});
			}
			if (within)
				queryBuilder.set('within', within);
			$.each(tokens, function(index, token) {
				var tokenInstance = queryBuilder.createToken();
				$.each(tokenInstance.rootAttributeGroup.getAttributes(), function(i, el) {
					el.element.remove();
				});
				$.each(tokenInstance.rootAttributeGroup.getAttributeGroups(), function(i, el) {
					el.element.remove();
				});
				tokenInstance.set('beginOfSentence', !!token.leadingXmlTag && token.leadingXmlTag.name === 's');
				tokenInstance.set('endOfSentence', !!token.trailingXmlTag && token.trailingXmlTag.name === 's');
				tokenInstance.set('optional', token.optional || false);
				if (token.repeats) {
					tokenInstance.set('minRepeats', token.repeats.min);
					tokenInstance.set('maxRepeats', token.repeats.max);
				}
				function doOp(op, parentAttributeGroup, level) {
					if (op == null)
						return;
					if (op.type === 'binaryOp') {
						var label = op.operator === '&' ? 'AND' : 'OR'; 
						if (op.operator != parentAttributeGroup.operator) {
							if (level === 0) {
								parentAttributeGroup.operator = op.operator;
								parentAttributeGroup.label = label;
							} else if (parentAttributeGroup.operator !== op.operator) {
								parentAttributeGroup = parentAttributeGroup.createAttributeGroup(op.operator, label);
							}
						}
						doOp(op.right, parentAttributeGroup, level + 1);
						doOp(op.left, parentAttributeGroup, level + 1);
					} else if (op.type === 'attribute') {
						var attributeInstance = parentAttributeGroup.createAttribute();
						if (op.value.indexOf('(?-i)') === 0) {
							attributeInstance.set('case', true, op.name);
							op.value = op.value.substr(5);
						} else if (op.value.indexOf('(?c)') === 0) {
							attributeInstance.set('case', true, op.name);
							op.value = op.value.substr(4);
						}
						if (op.operator === '=' && op.value.length >= 2 && op.value.indexOf('|') === -1) {
							if (op.value.indexOf('.*') === 0) {
								op.operator = 'ends with';
								op.value = op.value.substr(2);
							}
							else if (op.value.indexOf('.*') === op.value.length -2) {
								op.operator = 'starts with';
								op.value = op.value.substr(0, op.value.length-2);
							}
						}
						attributeInstance.set('operator', op.operator);
						attributeInstance.set('type', op.name);
						attributeInstance.set('val', op.value);
					}
				}
				doOp(token.expression, tokenInstance.rootAttributeGroup, 0);
				tokenInstance.element.trigger('cql:modified');
			});
		} catch (e) {
			if (SINGLEPAGE.DEBUG) {
				console.log('Cql parser could not decode query pattern');
				console.log(e);
				console.log(pattern);
			}
			return false;
		}
		return true;
	}
function populateQueryBuilder ( pattern ) { if ( ! pattern ) return false ; try { var parsedCql = SINGLEPAGE . CQLPARSER . parse ( pattern ) ; var tokens = parsedCql . tokens ; var within = parsedCql . within ; if ( tokens === null ) { return false ; } var queryBuilder = $ ( '#querybuilder' ) . data ( 'builder' ) ; queryBuilder . reset ( ) ; if ( tokens . length > 0 ) { $ . each ( queryBuilder . getTokens ( ) , function ( i , e ) { e . element . remove ( ) ; } ) ; } if ( within ) queryBuilder . set ( 'within' , within ) ; $ . each ( tokens , function ( index , token ) { var tokenInstance = queryBuilder . createToken ( ) ; $ . each ( tokenInstance . rootAttributeGroup . getAttributes ( ) , function ( i , el ) { el . element . remove ( ) ; } ) ; $ . each ( tokenInstance . rootAttributeGroup . getAttributeGroups ( ) , function ( i , el ) { el . element . remove ( ) ; } ) ; tokenInstance . set ( 'beginOfSentence' , ! ! token . leadingXmlTag && token . leadingXmlTag . name === 's' ) ; tokenInstance . set ( 'endOfSentence' , ! ! token . trailingXmlTag && token . trailingXmlTag . name === 's' ) ; tokenInstance . set ( 'optional' , token . optional || false ) ; if ( token . repeats ) { tokenInstance . set ( 'minRepeats' , token . repeats . min ) ; tokenInstance . set ( 'maxRepeats' , token . repeats . max ) ; } function doOp ( op , parentAttributeGroup , level ) { if ( op == null ) return ; if ( op . type === 'binaryOp' ) { var label = op . operator === '&' ? 'AND' : 'OR' ; if ( op . operator != parentAttributeGroup . operator ) { if ( level === 0 ) { parentAttributeGroup . operator = op . operator ; parentAttributeGroup . label = label ; } else if ( parentAttributeGroup . operator !== op . operator ) { parentAttributeGroup = parentAttributeGroup . createAttributeGroup ( op . operator , label ) ; } } doOp ( op . right , parentAttributeGroup , level + 1 ) ; doOp ( op . left , parentAttributeGroup , level + 1 ) ; } else if ( op . type === 'attribute' ) { var attributeInstance = parentAttributeGroup . createAttribute ( ) ; if ( op . value . indexOf ( '(?-i)' ) === 0 ) { attributeInstance . set ( 'case' , true , op . name ) ; op . value = op . value . substr ( 5 ) ; } else if ( op . value . indexOf ( '(?c)' ) === 0 ) { attributeInstance . set ( 'case' , true , op . name ) ; op . value = op . value . substr ( 4 ) ; } if ( op . operator === '=' && op . value . length >= 2 && op . value . indexOf ( '|' ) === - 1 ) { if ( op . value . indexOf ( '.*' ) === 0 ) { op . operator = 'ends with' ; op . value = op . value . substr ( 2 ) ; } else if ( op . value . indexOf ( '.*' ) === op . value . length - 2 ) { op . operator = 'starts with' ; op . value = op . value . substr ( 0 , op . value . length - 2 ) ; } } attributeInstance . set ( 'operator' , op . operator ) ; attributeInstance . set ( 'type' , op . name ) ; attributeInstance . set ( 'val' , op . value ) ; } } doOp ( token . expression , tokenInstance . rootAttributeGroup , 0 ) ; tokenInstance . element . trigger ( 'cql:modified' ) ; } ) ; } catch ( e ) { if ( SINGLEPAGE . DEBUG ) { console . log ( 'Cql parser could not decode query pattern' ) ; console . log ( e ) ; console . log ( pattern ) ; } return false ; } return true ; }
Attempt to parse the query pattern and update the state of the query builder
 to match it as much as possible.
Attempt to parse the query pattern and update the state of the query builder to match it as much as possible .
// only clear the querybuilder when we're putting something back in // TODO: try and repopulate the "simple" tab //clean the root group of all contents // TODO get label internally in builder //inverse order, since new elements are inserted at top.. // case flag is always at the front, so check for that before checking // for starts with/ends with flags // couldn't decode query