All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cass.rollup.coprocessor.ims.OpenBadgeCoprocessor Maven / Gradle / Ivy

There is a newer version: 3.1.8
Show newest version
package cass.rollup.coprocessor.ims;

import cass.rollup.InquiryPacket;
import cass.rollup.coprocessor.AssertionCoprocessor;
import com.eduworks.ec.array.EcAsyncHelper;
import com.eduworks.ec.crypto.EcCrypto;
import com.eduworks.ec.crypto.EcPk;
import org.cass.profile.EcAssertion;
import org.cassproject.ebac.repository.EcRepository;
import org.cassproject.schema.cass.profile.Assertion;
import org.cassproject.schema.general.EcRemoteLinkedData;
import org.stjs.javascript.Array;
import org.stjs.javascript.Date;
import org.stjs.javascript.JSObjectAdapter;
import org.stjs.javascript.functions.Callback0;
import org.stjs.javascript.functions.Callback1;
import org.stjs.javascript.functions.Callback2;

public class OpenBadgeCoprocessor extends AssertionCoprocessor {

    public String email = null;
    public EcPk badgePluginIdentifier = null;
    public Double confidenceOfBadges = 1.0;

    @Override
    public void collectAssertions(final InquiryPacket ip, final Array listOfCompetencies, final Callback1> success) {
        if (listOfCompetencies.$length() == 0) {
            super.collectAssertions(ip, listOfCompetencies, success);
            return;
        }
        final Array assertions = new Array<>();
        final OpenBadgeCoprocessor me = this;
        EcAsyncHelper eah = new EcAsyncHelper();
        eah.each(me.assertionProcessor.repositories, new Callback2() {
            @Override
            public void $invoke(final EcRepository currentRepository, final Callback0 callback0) {
                String searchQuery = "@type:\"BadgeClass\"";
                for (int i = 0; i < listOfCompetencies.$length(); i++) {
                    if (i == 0)
                        searchQuery += " AND (";
                    if (i > 0)
                        searchQuery += " OR ";
                    searchQuery += "alignment.targetUrl:\"" + listOfCompetencies.$get(i) + "\"";
                }
                if (listOfCompetencies.$length() > 0)
                    searchQuery += ")";

                me.assertionProcessor.log(ip, "Querying repositories for badges regarding " + listOfCompetencies.$length() + " query: " + searchQuery);
                Object params = new Object();
                JSObjectAdapter.$put(params, "size", 5000);
                currentRepository.searchWithParams(searchQuery, params, null, new Callback1>() {
                    public void $invoke(final Array p1) {
                        me.assertionProcessor.log(ip, p1.$length() + " badgeClasses found.");
                        EcAsyncHelper badgeClassHelper = new EcAsyncHelper<>();
                        badgeClassHelper.each(p1, new Callback2() {
                            @Override
                            public void $invoke(final EcRemoteLinkedData badgeClass, final Callback0 badgeClassDone) {
                                currentRepository.search("@context:\"https://w3id.org/openbadges/v2\" AND @type:Assertion AND badge:\"" + badgeClass.id + "\"", null, new Callback1>() {
                                    @Override
                                    public void $invoke(Array badgeAssertions) {
                                        for (int j = 0; j < badgeAssertions.$length(); j++) {
                                            String hash = EcCrypto.sha256(me.email + JSObjectAdapter.$get(JSObjectAdapter.$get(badgeAssertions.$get(j), "recipient"), "salt"));
                                            if ("sha256$" + hash.toLowerCase() != JSObjectAdapter.$get(JSObjectAdapter.$get(badgeAssertions.$get(j), "recipient"), "identity")) {
                                                me.assertionProcessor.log(ip,me.email + " hashed with salt != " + JSObjectAdapter.$get(JSObjectAdapter.$get(badgeAssertions.$get(j), "recipient"), "identity"));
                                                badgeAssertions.splice(j--, 1);
                                            }
                                            else
                                                me.assertionProcessor.log(ip,me.email + " hashed with salt == " + JSObjectAdapter.$get(JSObjectAdapter.$get(badgeAssertions.$get(j), "recipient"), "identity"));
                                        }
                                        for (int j = 0;j < badgeAssertions.$length();j++)
                                        {
                                            Array alignments = (Array) JSObjectAdapter.$get(badgeClass,"alignment");
                                            if (alignments != null)
                                                for (int k = 0;k < alignments.$length();k++)
                                                {
                                                    Object alignment = alignments.$get(k);
                                                    EcAssertion a = (EcAssertion)new Assertion();
                                                    a.addOwner(ip.subject.$get(0));
                                                    a.setSubject(ip.subject.$get(0));
                                                    a.setAgent(me.badgePluginIdentifier);
                                                    a.competency = (String) JSObjectAdapter.$get(alignment,"targetUrl");
                                                    me.assertionProcessor.log(ip,"Generating Assertion for competency: " + (String) JSObjectAdapter.$get(alignment,"targetUrl"));
                                                    a.framework = (String) JSObjectAdapter.$get(alignment,"targetFramework");
                                                    a.confidence = me.confidenceOfBadges;
                                                    Array evidence = new Array<>();
                                                    evidence.push(badgeAssertions.$get(j).id);
                                                    a.setEvidence(evidence);
                                                    a.setAssertionDate((Long) (Object) new Date((String) JSObjectAdapter.$get(badgeAssertions.$get(j),"issuedOn")).getTime());
                                                    assertions.push(a);
                                                }
                                        }
                                        badgeClassDone.$invoke();
                                    }
                                }, ip.failure);
                            }
                        }, new Callback1>() {
                            @Override
                            public void $invoke(Array strings) {
                                callback0.$invoke();
                            }
                        });
                    }
                }, ip.failure);
            }
        }, new Callback1>() {
            @Override
            public void $invoke(Array strings) {
                success.$invoke(assertions);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy