Search in sources :

Example 1 with SimpleCustomStringUser

use of io.jans.orm.sql.model.SimpleCustomStringUser in project jans by JanssenProject.

the class SqlCustomStringAttributesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
    // Create SQL entry manager
    SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
    String randomExternalUid = "" + System.currentTimeMillis();
    // String randomExternalUid = "otp:" + System.currentTimeMillis();
    // Add dummy user
    SimpleCustomStringUser newUser = new SimpleCustomStringUser();
    newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
    newUser.setUserId("sample_user_" + System.currentTimeMillis());
    newUser.setUserPassword("test");
    newUser.getCustomAttributes().add(new CustomAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add((new CustomAttribute("jansExtUid", randomExternalUid)).multiValued());
    newUser.setUserRole(UserRole.ADMIN);
    newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
    sqlEntryManager.persist(newUser);
    LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
    // Find added dummy user but use custom class with String values
    SimpleCustomStringUser foundUser = sqlEntryManager.find(SimpleCustomStringUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
    LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
    for (CustomAttribute customAttribute : foundUser.getCustomAttributes()) {
        LOG.info("Found custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
    }
    // Find by jsExternalUid
    Filter jsExternalUidFilter = Filter.createEqualityFilter("jansExtUid", randomExternalUid).multiValued();
    List<SimpleCustomStringUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleCustomStringUser.class, jsExternalUidFilter);
    for (SimpleCustomStringUser foundUser2 : foundUsers) {
        LOG.info("Found User '{}' by jansExtUid with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) CustomAttribute(io.jans.orm.model.base.CustomAttribute) SimpleCustomStringUser(io.jans.orm.sql.model.SimpleCustomStringUser) SqlEntryManagerSample(io.jans.orm.sql.persistence.SqlEntryManagerSample) SqlEntryManager(io.jans.orm.sql.impl.SqlEntryManager)

Aggregations

CustomAttribute (io.jans.orm.model.base.CustomAttribute)1 Filter (io.jans.orm.search.filter.Filter)1 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)1 SimpleCustomStringUser (io.jans.orm.sql.model.SimpleCustomStringUser)1 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)1