Search in sources :

Example 1 with SimpleTokenLdap

use of io.jans.orm.ldap.model.SimpleTokenLdap in project jans by JanssenProject.

the class LdapBatchJobSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    LdapEntryManagerSample ldapEntryManagerSample = new LdapEntryManagerSample();
    // Create LDAP entry manager
    final LdapEntryManager ldapEntryManager = ldapEntryManagerSample.createLdapEntryManager();
    BatchOperation<SimpleTokenLdap> tokenLdapBatchOperation = new ProcessBatchOperation<SimpleTokenLdap>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleTokenLdap> objects) {
            for (SimpleTokenLdap simpleTokenLdap : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(ldapEntryManager, simpleTokenLdap.getDn(), "exp", simpleTokenLdap.getAttribute("exp"));
                    simpleTokenLdap.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleTokenLdap);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter1 = Filter.createPresenceFilter("exp");
    ldapEntryManager.findEntries("o=jans", SimpleTokenLdap.class, filter1, SearchScope.SUB, new String[] { "exp" }, tokenLdapBatchOperation, 0, 0, 100);
    BatchOperation<SimpleSession> sessionBatchOperation = new ProcessBatchOperation<SimpleSession>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleSession> objects) {
            for (SimpleSession simpleSession : objects) {
                try {
                    CustomAttribute customAttribute = getUpdatedAttribute(ldapEntryManager, simpleSession.getDn(), "jansLastAccessTime", simpleSession.getAttribute("jansLastAccessTime"));
                    simpleSession.setCustomAttributes(Arrays.asList(new CustomAttribute[] { customAttribute }));
                    ldapEntryManager.merge(simpleSession);
                    processedCount++;
                } catch (EntryPersistenceException ex) {
                    LOG.error("Failed to update entry", ex);
                }
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter2 = Filter.createPresenceFilter("jansLastAccessTime");
    ldapEntryManager.findEntries("o=jans", SimpleSession.class, filter2, SearchScope.SUB, new String[] { "jansLastAccessTime" }, sessionBatchOperation, 0, 0, 100);
    BatchOperation<SimpleClient> clientBatchOperation = new ProcessBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter3 = Filter.createPresenceFilter("exp");
    List<SimpleClient> result3 = ldapEntryManager.findEntries("o=jans", SimpleClient.class, filter3, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation, 0, 0, 1000);
    LOG.info("Result count (without collecting results): " + result3.size());
    BatchOperation<SimpleClient> clientBatchOperation2 = new DefaultBatchOperation<SimpleClient>() {

        private int processedCount = 0;

        @Override
        public void performAction(List<SimpleClient> objects) {
            for (SimpleClient simpleClient : objects) {
                processedCount++;
            }
            LOG.info("Total processed: " + processedCount);
        }
    };
    final Filter filter4 = Filter.createPresenceFilter("exp");
    List<SimpleClient> result4 = ldapEntryManager.findEntries("o=jans", SimpleClient.class, filter4, SearchScope.SUB, new String[] { "exp" }, clientBatchOperation2, 0, 0, 1000);
    LOG.info("Result count (with collecting results): " + result4.size());
}
Also used : SimpleTokenLdap(io.jans.orm.ldap.model.SimpleTokenLdap) CustomAttribute(io.jans.orm.model.base.CustomAttribute) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) DefaultBatchOperation(io.jans.orm.model.DefaultBatchOperation) LdapEntryManager(io.jans.orm.ldap.impl.LdapEntryManager) Filter(io.jans.orm.search.filter.Filter) ProcessBatchOperation(io.jans.orm.model.ProcessBatchOperation) List(java.util.List) SimpleClient(io.jans.orm.ldap.model.SimpleClient) SimpleSession(io.jans.orm.ldap.model.SimpleSession)

Aggregations

EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)1 LdapEntryManager (io.jans.orm.ldap.impl.LdapEntryManager)1 SimpleClient (io.jans.orm.ldap.model.SimpleClient)1 SimpleSession (io.jans.orm.ldap.model.SimpleSession)1 SimpleTokenLdap (io.jans.orm.ldap.model.SimpleTokenLdap)1 DefaultBatchOperation (io.jans.orm.model.DefaultBatchOperation)1 ProcessBatchOperation (io.jans.orm.model.ProcessBatchOperation)1 CustomAttribute (io.jans.orm.model.base.CustomAttribute)1 Filter (io.jans.orm.search.filter.Filter)1 List (java.util.List)1