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

package foam.dao;

import foam.core.FObject;
import foam.core.X;
import foam.mlang.order.Comparator;
import foam.mlang.predicate.Predicate;
import foam.nanos.pm.PM;

public class PipelinePMDAO
  extends ProxyDAO
{
  protected String putName_;
  protected String findName_;
  protected String removeName_;
  protected String removeAllName_;

  public PipelinePMDAO(X x, DAO delegate) {
    super(x, delegate);
    init();
  }

  void init() {
    createPipeline();
    putName_       = getDelegate().getClass().getName() + ":pipePut";
    findName_      = getDelegate().getClass().getName() + ":pipeFind";
    removeName_    = getDelegate().getClass().getName() + ":pipeRemove";
    removeAllName_ = getDelegate().getClass().getName() + ":pipeRemoveAll";
  }

  private void createPipeline() {
    DAO delegate = getDelegate();
    DAO secondaryDelegate;
    if ( delegate instanceof ProxyDAO ) {
      secondaryDelegate = ((ProxyDAO) delegate).getDelegate();
      ((ProxyDAO) delegate).setDelegate(new EndPipelinePMDAO(getX(), secondaryDelegate));
      delegate = ((ProxyDAO) delegate).getDelegate();
      if ( secondaryDelegate instanceof ProxyDAO ) {
        ((ProxyDAO) delegate).setDelegate(new PipelinePMDAO(getX(), secondaryDelegate));
      }
    }
  }

  private void createPM(String name) {
    PM pm = new PM();
    pm.setClassType(PipelinePMDAO.getOwnClassInfo());
    pm.setName(name);
    X pipeX = getX().put("pipePmStart", pm);
  }

  @Override
  public FObject put_(X x, FObject obj) {
    createPM(putName_);
    return super.put_(pipeX, obj);
  }

  @Override
  public FObject find_(X x, Object id) {
    createPM(findName_);
    return super.find_(pipeX, id);
  }

  @Override
  public FObject remove_(X x, FObject obj) {
    createPM(removeName_);
    return super.remove_(pipeX, obj);
  }

  @Override
  public void removeAll_(X x, long skip, long limit, Comparator order, Predicate predicate) {
    createPM(removeAllName_);
    super.removeAll_(pipeX, skip, limit, order, predicate);
  }

  public class EndPipelinePMDAO extends ProxyDAO {
    public EndPipelinePMDAO(X x, DAO delegate) {
      super(x, delegate);
    }

    @Override
    public FObject put_(X x, FObject obj) {
      ((PM) getX().get("pipePmStart")).log(x);
      return super.put_(x, obj);
    }

    @Override
    public FObject find_(X x, Object id) {
      ((PM) getX().get("pipePmStart")).log(x);
      return super.find_(x, id);
    } 

    @Override
    public FObject remove_(X x, FObject obj) {
      ((PM) getX().get("pipePmStart")).log(x);
      return super.remove_(x, obj);
    }

    @Override
    public void removeAll_(X x, long skip, long limit, Comparator order, Predicate predicate) {
      ((PM) getX().get("pipePmStart")).log(x);
      super.removeAll_(x, skip, limit, order, predicate);
    }
  }
}
@@ -456,16 +456,50 @@
 llName_;
+%0A  protected String delegateName_;
 %0A%0A  publ
@@ -556,24 +556,80 @@
  delegate);%0A
+    delegateName_ = getDelegate().getClass().getName();%0A
     init();%0A
@@ -696,42 +696,21 @@
   = 
-getD
+d
 elegate
-().getClass().get
 Name
-()
+_
  + %22
@@ -745,42 +745,21 @@
   = 
-getD
+d
 elegate
-().getClass().get
 Name
-()
+_
  + %22
@@ -795,42 +795,21 @@
   = 
-getD
+d
 elegate
-().getClass().get
 Name
-()
+_
  + %22
@@ -847,42 +847,21 @@
 _ = 
-getD
+d
 elegate
-().getClass().get
 Name
-()
+_
  + %22

refactored the name of the delagate