/*
 * Copyright (c) 2013-2014 Josef Hardi <josef.hardi@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.obidea.semantika.mapping.base.sql;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import com.obidea.semantika.database.sql.base.ISqlExpression;
import com.obidea.semantika.expression.base.AbstractProlog;
import com.obidea.semantika.expression.base.BuiltInPredicate;
import com.obidea.semantika.expression.base.IAtom;
import com.obidea.semantika.expression.base.IFunction;
import com.obidea.semantika.expression.base.IQueryExt;
import com.obidea.semantika.expression.base.IQueryExtVisitor;
import com.obidea.semantika.expression.base.ITerm;

public abstract class PrologMediator extends AbstractProlog implements IQueryExt
{
   private static final long serialVersionUID = 629451L;

   private Set<IFunction> mFilters = new HashSet<IFunction>();

   public PrologMediator()
   {
      super(BuiltInPredicate.Answer);
   }

   protected void notifySelectItemChanged(ISqlExpression expression)
   {
      ITerm term = (ITerm) expression;
      super.addDistTerm(term);
   }

   protected void notifyFromExpressionChanged(ISqlExpression expression)
   {
      IAtom atom = (IAtom) expression;
      super.addAtom(atom);
   }

   protected void notifyWhereExpressionChanged(ISqlExpression expression)
   {
      IFunction filter = (IFunction) expression;
      mFilters.add(filter);
   }

   protected void notifyQueryConstraintAllRemoved()
   {
      mFilters.clear();
   }

   @Override
   public void setFilter(IFunction filter)
   {
      if (filter != null) {
         mFilters.add(filter);
      }
   }

   @Override
   public Set<IFunction> getFilters()
   {
      return Collections.unmodifiableSet(mFilters);
   }

   @Override
   public boolean hasFilter()
   {
      return (getFilters().size() > 0) ? true : false;
   }

   @Override
   public void accept(IQueryExtVisitor visitor)
   {
      visitor.visit(this);
   }
}

@@ -1206,16 +1206,71 @@
 e.ITerm;
+%0Aimport com.obidea.semantika.expression.base.TermUtils;
 %0A%0Apublic
@@ -2547,11 +2547,944 @@
 s);%0A   %7D
+%0A%0A   @Override%0A   public int hashCode()%0A   %7B%0A      final int prime = 31;%0A      int result = 1;%0A      result = prime * result + getHead().hashCode();%0A      result = prime * result + getBody().hashCode();%0A      result = prime * result + getFilters().hashCode();%0A      return result;%0A   %7D%0A%0A   /**%0A    * Internal use only for debugging%0A    */%0A   @Override%0A   public String toString()%0A   %7B%0A      final StringBuilder sb = new StringBuilder();%0A      sb.append(super.toString());%0A      %0A      if (mFilters.size() %3E 0) %7B%0A         sb.append(%22FILTER:%22); //$NON-NLS-1$%0A         boolean needComma = false;%0A         for (final IFunction filter : mFilters) %7B%0A            if (needComma) %7B%0A               sb.append(%22,%22); //$NON-NLS-1$%0A            %7D%0A            sb.append(%22%5Cn   %22);%0A            sb.append(TermUtils.toString(filter));%0A            needComma = true;%0A         %7D%0A      %7D%0A      sb.append(%22%5Cn%22); //$NON-NLS-1$%0A      return sb.toString();%0A   %7D
 %0A%7D%0A

Update toString() method to add filters.