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

package foam.dao;

import foam.core.*;
import foam.mlang.order.*;
import foam.mlang.predicate.*;
import java.util.HashSet;
import java.util.List;

public abstract class AbstractDAO
  extends    ContextAwareSupport
  implements DAO
{
  public final static long MAX_SAFE_INTEGER = 9007199254740991l;

  protected ClassInfo    of_          = null;
  protected PropertyInfo primaryKey_  = null;

  public DAO where(Predicate predicate) {
    return new FilteredDAO(predicate, this);
  }

  public DAO orderBy(Comparator comparator) {
    return new OrderedDAO(comparator, this);
  }

  public DAO skip(long count) {
    return new SkipDAO(count, this);
  }

  public DAO limit(long count) {
    return new LimitedDAO(count, this);
  }

  public void pipe_(X x, foam.dao.Sink sink) {
    throw new UnsupportedOperationException();
  }

  protected Sink decorateListener_(Sink sink, Predicate predicate) {
    if ( predicate != null ) {
      sink = new PredicatedSink(predicate, sink);
    }

    return sink;
  }

  public static Sink decorateSink_(Sink sink, long skip, long limit, Comparator order, Predicate predicate) {
    if ( ( limit > 0 ) && ( limit < AbstractDAO.MAX_SAFE_INTEGER ) ) {
      sink = new LimitedSink(limit, 0, sink);
    }

    if ( ( skip > 0 ) && ( skip < AbstractDAO.MAX_SAFE_INTEGER ) ) {
      sink = new SkipSink(skip, 0, sink);
    }

    if ( order != null ) {
      sink = new OrderedSink(order, null, sink);
    }

    if ( predicate != null ) {
      sink = new PredicatedSink(predicate, sink);
    }

    return sink;
  }

  public static Sink decorateDedupSink_(Sink sink) {
    sink = new DedupSink(new HashSet(), sink);
    return sink;
  }

  public ClassInfo getOf() {
    return of_;
  }

  public void setOf(ClassInfo of) {
    of_ = of;
    primaryKey_ = (PropertyInfo) of.getAxiomByName("id");
  }

  public PropertyInfo getPrimaryKey() {
    return primaryKey_;
  }

  public Object getPK(FObject obj) {
    return getPrimaryKey().get(obj);
  }

  protected class DAOListener implements foam.core.Detachable {
    protected Sink sink;
    protected java.util.Collection listeners;

    public DAOListener(Sink sink, java.util.Collection listeners) {
      this.sink = sink;
      this.listeners = listeners;
    }<commit_msg>Make getPrimaryKey() work for ProxyDAO.<commit_after>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.dao;

import foam.core.*;
import foam.mlang.order.*;
import foam.mlang.predicate.*;
import java.util.HashSet;
import java.util.List;

public abstract class AbstractDAO
  extends    ContextAwareSupport
  implements DAO
{
  public final static long MAX_SAFE_INTEGER = 9007199254740991l;

  protected ClassInfo    of_          = null;
  protected PropertyInfo primaryKey_  = null;

  public DAO where(Predicate predicate) {
    return new FilteredDAO(predicate, this);
  }

  public DAO orderBy(Comparator comparator) {
    return new OrderedDAO(comparator, this);
  }

  public DAO skip(long count) {
    return new SkipDAO(count, this);
  }

  public DAO limit(long count) {
    return new LimitedDAO(count, this);
  }

  public void pipe_(X x, foam.dao.Sink sink) {
    throw new UnsupportedOperationException();
  }

  protected Sink decorateListener_(Sink sink, Predicate predicate) {
    if ( predicate != null ) {
      sink = new PredicatedSink(predicate, sink);
    }

    return sink;
  }

  public static Sink decorateSink_(Sink sink, long skip, long limit, Comparator order, Predicate predicate) {
    if ( ( limit > 0 ) && ( limit < AbstractDAO.MAX_SAFE_INTEGER ) ) {
      sink = new LimitedSink(limit, 0, sink);
    }

    if ( ( skip > 0 ) && ( skip < AbstractDAO.MAX_SAFE_INTEGER ) ) {
      sink = new SkipSink(skip, 0, sink);
    }

    if ( order != null ) {
      sink = new OrderedSink(order, null, sink);
    }

    if ( predicate != null ) {
      sink = new PredicatedSink(predicate, sink);
    }

    return sink;
  }

  public static Sink decorateDedupSink_(Sink sink) {
    sink = new DedupSink(new HashSet(), sink);
    return sink;
  }

  public ClassInfo getOf() {
    return of_;
  }

  public void setOf(ClassInfo of) {
    of_ = of;
  }

  public PropertyInfo getPrimaryKey() {
    if ( primaryKey_ == null ) {
      primaryKey_ = (PropertyInfo) getOf().getAxiomByName("id");
    }

    return primaryKey_;
  }

  public Object getPK(FObject obj) {
    return getPrimaryKey().get(obj);
  }

  protected class DAOListener implements foam.core.Detachable {
    protected Sink sink;
    protected java.util.Collection listeners;

    public DAOListener(Sink sink, java.util.Collection listeners) {
      this.sink = sink;
      this.listeners = listeners;
    }
{'fraction_non_alphanumeric': 0.10378710337768679, 'fraction_numerical': 0.010644831115660185, 'mean_word_length': 3.0887029288702927, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7195209', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1572, 'n_words': 423}