var Policy = require('ULocks').Policy;
var Context = require('ULocks').Context;

function valid(o) {
    return ((o !== undefined) && (o !== null));
}

function checkRead(subject, subjectPolicy, object, objectPolicy) {
    try {
        if(!(subjectPolicy instanceof Policy))
            subjectPolicy = new Policy(subjectPolicy);

        if(!(objectPolicy instanceof Policy))
            objectPolicy = new Policy(objectPolicy);
    }
    catch(e) {
        return Promise.reject(e);
    }

    // TODO: check whether this type exists in Entity
    if(!valid(subject) || !valid(subject.type))
        return Promise.reject(new Error("PDP ERROR: Subject must specify a valid Entity type."));
    
    if(!valid(object) || !valid(object.type))
        return Promise.reject(new Error("PDP ERROR: Object must specify a valid Entity type."));
    
    var subjectInfo = {
        type : subject.type,
        data : subject
    }

    var objectInfo = {
        type : object.type,
        data : object
    }
    
    return new Promise(function (resolve, reject) {
        var context = new Context(subjectInfo, objectInfo);
        resolve(objectPolicy.checkRead(subjectPolicy, context));
    });
}

function checkWrite(subject, subjectPolicy, object, objectPolicy) {
    try {
        if(!(subjectPolicy instanceof Policy))
            subjectPolicy = new Policy(subjectPolicy);

        if(!(objectPolicy instanceof Policy))
            objectPolicy = new Policy(objectPolicy);
    }
    catch(e) {
        return Promise.reject(e);
    }

    // TODO: check whether this type exists in Entity
    if(!valid(subject) || !valid(subject.type))
        return Promise.reject(new Error("PDP ERROR: Subject must specify a valid Entity type."));
    
    if(!valid(object) || !valid(object.type))
        return Promise.reject(new Error("PDP ERROR: Object must specify a valid Entity type."));
    
    var subjectInfo = {
        type : subject.type,
        data : subject
    }

    var objectInfo = {
        type : object.type,
        data : object
    }
    
    return new Promise(function (resolve, reject) {
        var context = new Context(subjectInfo, objectInfo);
        resolve(objectPolicy.checkWrite(subjectPolicy, context));
    });
}

module.exports = {
    checkRead : checkRead,
    checkWrite : checkWrite
}

@@ -1,18 +1,15 @@
 var 
-Policy
+api
  = requi
@@ -16,483 +16,47 @@
 re('
-ULocks').Policy;%0Avar Context = require('ULocks').Context;%0A%0Afunction valid(o) %7B%0A    return ((o !== undefined) && (o !== null));%0A%7D%0A%0Afunction checkRead(subject, subjectPolicy, object, objectPolicy) %7B%0A    try %7B%0A        if(!(subjectPolicy instanceof Policy))%0A            subjectPolicy = new Policy(subjectPolicy);%0A%0A        if(!(objectPolicy instanceof Policy))%0A            objectPolicy = new Policy(objectPolicy);%0A    %7D%0A    catch(e) %7B%0A        return Promise.reject(e);%0A    %7D%0A%0A    
+./api');%0Avar pap = require('../pap');%0A%0A
 // T
@@ -64,514 +64,65 @@
 DO: 
-c
+C
 heck 
-whether this type exists in Entity%0A    if(!valid(subject) %7C%7C !valid(subject.type))%0A        return Promise.reject(new Error(%22PDP ERROR: Subject must specify a valid Entity type.%22));%0A    %0A    if(!valid(object) %7C%7C !valid(object.type))%0A        return Promise.reject(new Error(%22PDP ERROR: Object must specify a valid Entity type.%22));%0A    %0A    var subjectInfo = %7B%0A        type : subject.type,%0A        data : subject%0A    %7D%0A%0A    var objectInfo = %7B%0A        type : object.type,%0A        data : object%0A    %7D%0A    
+how to share app with PAP%0Afunction init(settings) %7B
 %0A   
@@ -141,33 +141,32 @@
 Promise(function
- 
 (resolve, reject
@@ -181,208 +181,53 @@
     
-var context = new Context(subjectInfo, objectInfo);%0A        resolve(objectPolicy.checkRead(subjectPolicy, context));%0A    %7D);%0A%7D%0A%0Afunction checkWrite(subject, subjectPolicy, object, objectPolicy
+pap.init(settings.pap).then(function(
 ) %7B%0A    
 try 
@@ -226,22 +226,16 @@
     
-try %7B%0A
         
 if(!
@@ -234,959 +234,210 @@
     
-if(!(subjectPolicy instanceof Policy))%0A            subjectPolicy = new Policy(subjectPolicy);%0A%0A        if(!(objectPolicy instanceof Policy))%0A            objectPolicy = new Policy(objectPolicy);%0A    %7D%0A    catch(e) %7B%0A        return Promise.reject(e);%0A    %7D%0A%0A    // TODO: check whether this type exists in Entity%0A    if(!valid(subject) %7C%7C !valid(subject.type))%0A        return Promise.reject(new Error(%22PDP ERROR: Subject must specify a valid Entity type.%22)
+api.init(settings.pdp, pap).then(function() %7B%0A                resolve(
 );%0A    
-%0A
     
-if(!valid(object) %7C%7C !valid(object.type))%0A        return Promise.reject(new Error(%22PDP ERROR: Object must specify a valid Entity type.%22));%0A    %0A    var subjectInfo = %7B%0A        type : subject.type,%0A        data : subject%0A    %7D%0A%0A    var objectInfo = %7B%0A        type : object.type,%0A        data : object%0A
+    %7D, function(e) %7B%0A                reject(e);%0A        
     %7D
+);
 %0A    
-%0A
     
-return new Promise(function (resolve, reject) %7B%0A        var context = new Context(subjectInfo, objectInfo);%0A        resolve(objectPolicy.checkWrite(subjectPolicy, context)
+%7D, function(e) %7B%0A            reject(e);%0A        %7D
 );%0A 
@@ -465,16 +465,32 @@
 rts = %7B%0A
+    init: init,%0A
     chec
@@ -497,16 +497,20 @@
 kRead : 
+api.
 checkRea
@@ -529,16 +529,20 @@
 Write : 
+api.
 checkWri
@@ -543,11 +543,25 @@
 eckWrite
+,%0A    pap: pap
 %0A%7D%0A

Move code to api