/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.dao;

import foam.core.ClassInfo;
import foam.core.FObject;
import foam.core.X;
import foam.mlang.order.Comparator;
import foam.mlang.predicate.Predicate;
import java.io.IOException;

public class JDAO
  extends ProxyDAO
{
  protected FileJournal journal;

  public JDAO(ClassInfo classInfo, String filename) throws IOException {
    journal = new FileJournal(filename);
    setDelegate(new MapDAO().setOf(classInfo));
    journal.replay(getDelegate());
  }

  public JDAO(DAO delegate, String filename) throws IOException {
    journal = new FileJournal(filename);
    setDelegate(delegate);
    journal.replay(delegate);
  }

  /**
   * persists data into FileJournal then calls the delegated DAO.
   *
   * @param obj
   * @returns FObject
   */
  @Override
  public FObject put_(X x, FObject obj) {
    journal.put(obj, null);
    return getDelegate().put_(x, obj);
  }

  @Override
  public FObject remove_(X x, FObject obj) {
    Object id = ((AbstractDAO) getDelegate()).getPrimaryKey().get(obj);
    journal.remove(id, null);
    return getDelegate().remove_(x, obj);
  }

  @Override
  public void removeAll_(X x, long skip, long limit, Comparator order, Predicate predicate) {
    // TODO: this is wrong, should only call journal.removeAll() if neither limit nor predicate
    // are set.
    journal.removeAll();
    getDelegate().removeAll_(x, skip, limit, order, predicate);
  }
}

@@ -181,23 +181,26 @@
 am.core.
-FObject
+Detachable
 ;%0Aimport
@@ -210,17 +210,23 @@
 am.core.
-X
+FObject
 ;%0Aimport
@@ -235,30 +235,14 @@
 oam.
-mlang.order.Comparator
+core.X
 ;%0Aim
@@ -305,16 +305,52 @@
 ception;
+%0Aimport foam.mlang.order.Comparator;
 %0A%0Apublic
@@ -1277,16 +1277,22 @@
 oveAll_(
+final 
 X x, lon
@@ -1298,16 +1298,22 @@
 ng skip,
+ final
  long li
@@ -1474,33 +1474,201 @@
 et.%0A
+%0A
     
-journal.removeAll(
+getDelegate().select_(x, new AbstractSink() %7B%0A      @Override%0A      public void put(FObject obj, Detachable sub) %7B%0A        remove_(x, obj);%0A      %7D%0A    %7D, skip, limit, order, predicate
 );%0A
+%0A
     

make jdao removeAll Sink aware