diff --git a/src/files.js b/src/files.js index a38b4a05ce3..c564f1cc14c 100755 --- a/src/files.js +++ b/src/files.js @@ -284,6 +284,7 @@ FOAM_FILES([ { name: "foam/dao/SequenceNumberDAO" }, { name: "foam/dao/SequenceNumberDAOTest" }, { name: "foam/dao/ContextualizingDAO" }, + { name: "foam/dao/PredicateContextualizingDAO" }, { name: "foam/dao/VersionNoDAO" }, { name: "foam/dao/sync/SyncRecord" }, { name: "foam/dao/SyncDAO" }, diff --git a/src/foam/dao/EasyDAO.js b/src/foam/dao/EasyDAO.js index 6db172f71f4..02741ba6114 100644 --- a/src/foam/dao/EasyDAO.js +++ b/src/foam/dao/EasyDAO.js @@ -45,6 +45,7 @@ foam.CLASS({ 'foam.dao.ClientDAO', 'foam.dao.CompoundDAODecorator', 'foam.dao.ContextualizingDAO', + 'foam.dao.PredicateContextualizingDAO', 'foam.dao.DeDupDAO', 'foam.dao.InterceptedDAO', 'foam.dao.GUIDDAO', @@ -290,6 +291,12 @@ foam.CLASS({ build(); } + if ( getContextualizePredicates() ) { + delegate = new foam.dao.PredicateContextualizingDAO.Builder(getX()). + setDelegate(delegate). + build(); + } + if ( getOrder() != null && getOrder().length > 0 ) { // TODO: CompositeDAO or thenBy for ( foam.mlang.order.Comparator comp : getOrder() ) @@ -503,6 +510,12 @@ foam.CLASS({ name: 'contextualize', value: false }, + { + documentation: 'Contextualize passed predicates with the context passed to select().', + class: 'Boolean', + name: 'contextualizePredicates', + value: false + }, { class: 'Boolean', name: 'ruler', diff --git a/src/foam/dao/PredicateContextualizingDAO.js b/src/foam/dao/PredicateContextualizingDAO.js new file mode 100644 index 00000000000..8f909d5bd99 --- /dev/null +++ b/src/foam/dao/PredicateContextualizingDAO.js @@ -0,0 +1,43 @@ +/** + * @license + * Copyright 2020 The FOAM Authors. All Rights Reserved. + * http://www.apache.org/licenses/LICENSE-2.0 + */ + +foam.CLASS({ + package: 'foam.dao', + name: 'PredicateContextualizingDAO', + extends: 'foam.dao.ProxyDAO', + + javaImports: [ + 'foam.core.ContextAware', + 'foam.core.X' + ], + + axioms: [ + { + name: 'javaExtras', + buildJavaClass: function(cls) { + cls.extras.push(` + public PredicateContextualizingDAO(X x, DAO delegate) { + setX(x); + setDelegate(delegate); + } + `); + } + } + ], + + methods: [ + { + name: 'select_', + javaCode: ` + if ( predicate != null && predicate instanceof ContextAware ) { + ((ContextAware) predicate).setX(x); + } + return getDelegate().select_(x, sink, skip, limit, order, predicate); + ` + } + ], + +}); diff --git a/src/foam/mlang/mlang.js b/src/foam/mlang/mlang.js index feed05349ad..d747ae95b2a 100644 --- a/src/foam/mlang/mlang.js +++ b/src/foam/mlang/mlang.js @@ -555,6 +555,10 @@ foam.CLASS({ extends: 'foam.mlang.predicate.AbstractPredicate', abstract: true, + javaImports: [ + 'foam.core.ContextAware' + ], + documentation: 'Abstract Unary (single-argument) Predicate base-class.', properties: [ @@ -582,6 +586,20 @@ foam.CLASS({ javaCode: ` getArg1().authorize(x); ` + }, + { + name: 'setX', + args: [ + { + name: 'x', + type: 'Context' + } + ], + javaCode: ` + x_ = x; + if ( getArg1() != null && getArg1() instanceof ContextAware ) + ((ContextAware) getArg1()).setX(x); + ` } ] }); @@ -694,6 +712,10 @@ foam.CLASS({ extends: 'foam.mlang.predicate.AbstractPredicate', abstract: true, + javaImports: [ + 'foam.core.ContextAware' + ], + documentation: 'Abstract Binary (two-argument) Predicate base-class.', properties: [ @@ -764,6 +786,22 @@ getArg2().prepareStatement(stmt);` getArg1().authorize(x); getArg2().authorize(x); ` + }, + { + name: 'setX', + args: [ + { + name: 'x', + type: 'Context' + } + ], + javaCode: ` + x_ = x; + if ( getArg1() != null && getArg1() instanceof ContextAware ) + ((ContextAware) getArg1()).setX(x); + if ( getArg2() != null && getArg2() instanceof ContextAware ) + ((ContextAware) getArg2()).setX(x); + ` } ] }); @@ -775,6 +813,10 @@ foam.CLASS({ extends: 'foam.mlang.predicate.AbstractPredicate', abstract: true, + javaImports: [ + 'foam.core.ContextAware' + ], + documentation: 'Abstract n-ary (many-argument) Predicate base-class.', properties: [ @@ -836,6 +878,22 @@ foam.CLASS({ predicate.authorize(x); } ` + }, + { + name: 'setX', + args: [ + { + name: 'x', + type: 'Context' + } + ], + javaCode: ` + x_ = x; + for ( Object obj : getArgs() ) { + if ( obj != null && obj instanceof ContextAware ) + ((ContextAware) obj).setX(x); + } + ` } ] }); diff --git a/tools/classes.js b/tools/classes.js index 3e26bad8b5c..01e2aa002f6 100644 --- a/tools/classes.js +++ b/tools/classes.js @@ -184,6 +184,7 @@ var classes = [ 'foam.dao.index.PersistedIndexTest', 'foam.dao.SequenceNumberDAO', 'foam.dao.SequenceNumberDAOTest', + 'foam.dao.PredicateContextualizingDAO', 'foam.dao.PipelinePMDAO', 'foam.dao.PMDAO', 'foam.nanos.pm.PMFactory',