/**
 * @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(DAO delegate) {
    setDelegate(delegate);
    init();
    System.err.println("*************************** WARNING: FAILURE TO SET PMDAO CONTEXT IN CONSTRUCTOR!");
  }
*/
  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.Builder(x).setClassType(PMDAO.getOwnClassInfo()).setName(putName_).build();

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

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

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

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

    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.Builder(x).setClassType(PMDAO.getOwnClassInfo()).setName(removeAllName_).build();

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

@@ -455,197 +455,8 @@
 _;%0A%0A
-/*%0A  public PMDAO(DAO delegate) %7B%0A    setDelegate(delegate);%0A    init();%0A    System.err.println(%22*************************** WARNING: FAILURE TO SET PMDAO CONTEXT IN CONSTRUCTOR!%22);%0A  %7D%0A*/%0A
   pu
@@ -815,35 +815,34 @@
  pm = new PM
-.Builder(x)
+();%0A    pm
 .setClassTyp
@@ -859,32 +859,40 @@
 tOwnClassInfo())
+;%0A    pm
 .setName(putName
@@ -889,32 +889,24 @@
 me(putName_)
-.build()
 ;%0A%0A    try %7B
@@ -1050,35 +1050,34 @@
  pm = new PM
-.Builder(x)
+();%0A    pm
 .setClassTyp
@@ -1094,32 +1094,40 @@
 tOwnClassInfo())
+;%0A    pm
 .setName(findNam
@@ -1125,32 +1125,24 @@
 e(findName_)
-.build()
 ;%0A%0A    try %7B
@@ -1290,35 +1290,34 @@
  pm = new PM
-.Builder(x)
+();%0A    pm
 .setClassTyp
@@ -1334,32 +1334,40 @@
 tOwnClassInfo())
+;%0A    pm
 .setName(removeN
@@ -1367,32 +1367,24 @@
 removeName_)
-.build()
 ;%0A%0A    try %7B
@@ -1592,19 +1592,18 @@
 w PM
-.Builder(x)
+();%0A    pm
 .set
@@ -1636,16 +1636,24 @@
 sInfo())
+;%0A    pm
 .setName
@@ -1672,16 +1672,8 @@
 me_)
-.build()
 ;%0A%0A 

Remove use of Builder in PMDAO to reduce number of objects created.