/// <summary>
		/// Updates properties of current object with properties of source object.
		/// </summary>
		/// <param name="source"></param>
		public void ApplyChanges(ExpressionViewModel source)
		{
			CopyAllSettings(source);
		}
		/// <summary>
		/// Copy constructor for the ExpressionViewModel object.
		/// </summary>
		/// <param name="source"></param>
		public ExpressionViewModel(ExpressionViewModel source)
		{			
			CopyAllSettings(source);
		}
        private void initializeExpression(ExpressionViewModel expression)
        {
            expression.ServiceUrl = ServiceUrl;
            expression.UseProxy = UseProxy;
            expression.ProxyUrl = ProxyUrl;
            expression.Fields = Fields;

            expression.PropertyChanged += Expression_PropertyChanged;

            // Creates a view with the new expression as its DataContext.

            // Create a copy of the old expression because updating CurrentExpression somehow changes properties
            // on it via binding
            ExpressionViewModel oldExpressionClone = CurrentExpression != null ? CurrentExpression.Clone() : null;
            ExpressionViewModel oldExpression = CurrentExpression;

            CurrentExpression = expression;

            // Re-apply the settings from before updating CurrentExpression to the previous CurrentExpression
            if (oldExpression != null)
                oldExpression.ApplyChanges(oldExpressionClone);

            expression.CancelAction = () =>
            {
                IsEditingExpression = false;
            };

            expression.SaveAction = () =>
            {
                var position = QueryExpressions != null ? QueryExpressions.Count : 0;
                var groupID = 0;

                // Performs an add where new expression is added
                // to the end of the collection.
                var last = QueryExpressions != null ? QueryExpressions.LastOrDefault() : null;
                if (last != null)
                    groupID = last.GroupID + 1;

                expression.GroupID = groupID;
                expression.Position = position;
                QueryExpressions.Insert(position, expression);

                IsEditingExpression = false;
            };

            expression.EditAction = () =>
            {
                // Creates a clone of the selected item.
                var selected = expression;
                var clone = new ExpressionViewModel(selected);
                CurrentExpression = clone;

                // Default value should be allowed to be empty while editing the expression
                if (CurrentExpression.ChoiceValues != null && CurrentExpression.ChoiceValues.DefaultValue != null)
                    CurrentExpression.ChoiceValues.DefaultValue.AllowEmptyValues = true;

                // Creates a view with the clone as DataContext

                clone.CancelAction = () =>
                {
                    // Hides the view without updating the expression.
                    IsEditingExpression = false;
                };

                clone.SaveAction = () =>
                {
                    selected.ApplyChanges(clone);
                    // Hides the view and updates the expression.
                    IsEditingExpression = false;
                };

                // Displays the Expression Builder.
                IsEditingExpression = true;
                //if (EditExpression != null)
                //    EditExpression(clone);
            };

            expression.RemoveAction = () =>
            {
                // Updates the collection by removing expression and updating the groupIDs
                var selected = expression;
                int groupID = selected.GroupID;
                QueryExpressions.Remove(selected);
                updateExpressionGrouping(groupID);
                selected.PropertyChanged -= Expression_PropertyChanged;
            };
        }
		/// <summary>
		/// Updates serializable properties with values from another ExpressionViewModel.
		/// </summary>
		/// <param name="source">The source ExpressionViewModel</param>
		private void CopyAllSettings(ExpressionViewModel source)
		{
			if (source == null) return;
			IsVisible = source.IsVisible;
			ServiceUrl = source.ServiceUrl;						
			UseProxy = source.UseProxy;
			Fields = source.Fields;
			OutField = source.OutField;
			ExpressionLabel = source.ExpressionLabel;
			if (OutField != null && Fields != null)
			{
				ignoreFieldUpdate = true;
				Field = Fields.FirstOrDefault(f => f.Name == OutField.Name);
				ignoreFieldUpdate = false;
			}
            Position = source.Position;
			JoinedBy = source.JoinedBy;
			ignoreComparisonUpdate = true;
			ComparisonOperator = source.ComparisonOperator;
			ignoreComparisonUpdate = false;
			ChoiceValues = source.ChoiceValues != null ? new ChoiceValues(source.ChoiceValues) : null;
			UseDefaultValue = source.UseDefaultValue;
			IsNullValue = source.IsNullValue;
			UseMultipleValues = source.UseMultipleValues;
            GroupID = source.GroupID;
            FieldName = source.FieldName;
            FieldAlias = source.FieldAlias;
            FirstInGroup = source.firstInGroup;
            InMiddleOfGroup = source.InMiddleOfGroup;
            LastInGroup = source.LastInGroup;
		}
		/// <summary>
		/// Adds new expression to the collection of expressions that form the query.
		/// </summary>
		/// <param name="commandParameter"></param>
		private void OnAdd(object commandParameter)
		{
			if (!CanAdd(commandParameter)) return;


            ExpressionViewModel newExpression = new ExpressionViewModel();
			// Create and initialize new expression.
            initializeExpression(newExpression);

			// Displays the Expression Builder.
            IsEditingExpression = true;
            //if (EditExpression != null)
            //    EditExpression(newExpression);
		}