/**
 * @license
 * Copyright 2017 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 PMDAO
  extends ProxyDAO
{

  protected String putName_;
  protected String findName_;
  protected String removeName_;
  protected String removeAllName_;

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

  void init() {
    putName_       = getOf().getId() + ":put";
    findName_      = getOf().getId() + ":find";
    removeName_    = getOf().getId() + ":remove";
    removeAllName_ = getOf().getId() + ":removeAll";
  }

  @Override
  public FObject put_(X x, FObject obj) {
    PM pm = new PM();
    pm.setClassType(PMDAO.getOwnClassInfo());
    pm.setName(putName_);

    try {
      return super.put_(x, obj);
    } finally {
      pm.log(x);
    }
  }

  @Override
  public FObject find_(X x, Object id) {
    PM pm = new PM();
    pm.setClassType(PMDAO.getOwnClassInfo());
    pm.setName(findName_);

    try {
      return super.find_(x, id);
    } finally {
      pm.log(x);
    }
  }

  @Override
  public FObject remove_(X x, FObject obj) {
    PM pm = new PM();
    pm.setClassType(PMDAO.getOwnClassInfo());
    pm.setName(removeName_);

    try {
      return super.remove_(x, obj);
    } finally {
      pm.log(x);
    }
  }

  @Override
  public void removeAll_(X x, long skip, long limit, Comparator order, Predicate predicate) {
    PM pm = new PM();
    pm.setClassType(PMDAO.getOwnClassInfo());
    pm.setName(removeAllName_);

    try {
      super.removeAll_(x, skip, limit, order, predicate);
    } finally {
      pm.log(x);
    }
  }
}

@@ -753,154 +753,282 @@
 %0A%0A  
-@Override%0A  public FObject put_(X x, FObject obj) %7B%0A    PM pm = new PM();%0A    pm.setClassType(PMDAO.getOwnClassInfo());%0A    pm.setName(putName_);%0A
+PM createPM(String name) %7B%0A    PM pm = new PM();%0A    // TODO: should be modelled%0A    pm.setClassType(PMDAO.getOwnClassInfo());%0A    pm.setName(putName_);%0A    pm.init_();%0A    return pm;%0A  %7D%0A%0A  @Override%0A  public FObject put_(X x, FObject obj) %7B%0A    PM pm = createPM(putName_);
 %0A   
@@ -1180,78 +1180,16 @@
 m = 
-new PM();%0A    pm.setClassType(PMDAO.getOwnClassInfo());%0A    pm.setName
+createPM
 (fin
@@ -1358,78 +1358,16 @@
 m = 
-new PM();%0A    pm.setClassType(PMDAO.getOwnClassInfo());%0A    pm.setName
+createPM
 (rem
@@ -1590,78 +1590,16 @@
 m = 
-new PM();%0A    pm.setClassType(PMDAO.getOwnClassInfo());%0A    pm.setName
+createPM
 (rem

Fix for PMDAO.