use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.
the class SqlCustomObjectAttributesSample method main.
public static void main(String[] args) {
// Prepare sample connection details
SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
// Create SQL entry manager
SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
// Add dummy user
SimpleUser newUser = new SimpleUser();
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 CustomObjectAttribute("address", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansGuid", "test_value"));
newUser.getCustomAttributes().add(new CustomObjectAttribute("birthdate", new Date()));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansActive", false));
// Require cusom attribute in table with age: INT type
newUser.getCustomAttributes().add(new CustomObjectAttribute("scimCustomThird", 18));
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
SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
for (CustomObjectAttribute customAttribute : foundUser.getCustomAttributes()) {
if (customAttribute.getValue() instanceof Date) {
LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Integer) {
LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Boolean) {
LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValues().size() > 1) {
LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
}
}
for (Iterator<CustomObjectAttribute> it = foundUser.getCustomAttributes().iterator(); it.hasNext(); ) {
CustomObjectAttribute attr = (CustomObjectAttribute) it.next();
if (StringHelper.equalsIgnoreCase(attr.getName(), "jansGuid")) {
attr.setValue("");
break;
}
}
sqlEntryManager.merge(foundUser);
// Find updated dummy user
SimpleUser foundUser2 = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser2, foundUser2.getUserId(), foundUser2.getDn());
LOG.info("Custom attributes after merge '{}'", foundUser2.getCustomAttributes());
for (CustomObjectAttribute customAttribute : foundUser2.getCustomAttributes()) {
if (customAttribute.getValue() instanceof Date) {
LOG.info("Found date custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Integer) {
LOG.info("Found integer custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValue() instanceof Boolean) {
LOG.info("Found boolean custom attribute '{}' with value '{}'", customAttribute.getName(), customAttribute.getValue());
} else if (customAttribute.getValues().size() > 1) {
LOG.info("Found list custom attribute '{}' with value '{}', multiValued: {}", customAttribute.getName(), customAttribute.getValues(), customAttribute.isMultiValued());
}
}
// Find added dummy user by numeric attribute
Filter filter = Filter.createGreaterOrEqualFilter("scimCustomThird", 16);
List<SimpleUser> foundUsers = sqlEntryManager.findEntries("ou=people,o=jans", SimpleUser.class, filter);
if (foundUsers.size() > 0) {
foundUser = foundUsers.get(0);
LOG.info("Found User '{}' by filter '{}' with uid '{}' and key '{}'", foundUser, filter, foundUser, foundUser);
} else {
LOG.error("Can't find User by filter '{}'", filter);
}
}
use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.
the class SqlDeleteSample 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 baseDn = "ou=people,o=jans";
Filter filter = Filter.createANDFilter(Filter.createEqualityFilter("del", true), Filter.createLessOrEqualFilter("exp", sqlEntryManager.encodeTime(baseDn, new Date())));
int result = sqlEntryManager.remove(baseDn, SimpleSession.class, filter, 5);
System.out.println(result);
}
use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.
the class SqlCustomMultiValuedTypesSample method main.
public static void main(String[] args) {
// Prepare sample connection details
SqlEntryManagerSample sqlEntryManagerSample = new SqlEntryManagerSample();
// Create SQL entry manager
SqlEntryManager sqlEntryManager = sqlEntryManagerSample.createSqlEntryManager();
// Add dummy user
SimpleUser newUser = new SimpleUser();
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 CustomObjectAttribute("jansOptOuts", Arrays.asList("London", "Texas", "Kiev")));
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansExtUid", "test_value").multiValued());
newUser.getCustomAttributes().add(new CustomObjectAttribute("jansPPID", "test_value").multiValued());
newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
newUser.setAttributeValue("givenName", "john");
sqlEntryManager.persist(newUser);
LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
LOG.info("Persisted custom attributes '{}'", newUser.getCustomAttributes());
// Find added dummy user
SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
// Dump custom attributes
for (CustomObjectAttribute attr : foundUser.getCustomAttributes()) {
System.out.println(attr.getName() + " - " + attr.getValues());
}
// Update custom attributes
foundUser.setAttributeValues("jansOptOuts", Arrays.asList("London", "Texas", "Kiev", "Dublin"));
foundUser.setAttributeValues("jansExtUid", Arrays.asList("test_value_11", "test_value_22", "test_value_33", "test_value_44"));
foundUser.setAttributeValues("jansExtUid", Arrays.asList(11, 22, 33, 44));
foundUser.setAttributeValues("jansPPID", Arrays.asList("fuzzy_value_1", "fuzzy_value_2"));
foundUser.setAttributeValue("jansGuid", "simple");
CustomObjectAttribute multiValuedSingleValue = new CustomObjectAttribute("jansAssociatedClnt", "multivalued_single_valued");
multiValuedSingleValue.setMultiValued(true);
foundUser.getCustomAttributes().add(multiValuedSingleValue);
sqlEntryManager.merge(foundUser);
LOG.info("Updated custom attributes '{}'", foundUser.getCustomAttributes());
// Find updated dummy user
SimpleUser foundUpdatedUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUpdatedUser, foundUpdatedUser.getUserId(), foundUpdatedUser.getDn());
LOG.info("Cusom attributes '{}'", foundUpdatedUser.getCustomAttributes());
// Dump custom attributes
for (CustomObjectAttribute attr : foundUser.getCustomAttributes()) {
System.out.println(attr.getName() + " - " + attr.getValues());
}
Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("givenName"), StringHelper.toLowerCase("john"));
List<SimpleUser> foundUpdatedUsers = sqlEntryManager.findEntries("o=jans", SimpleUser.class, filter);
System.out.println(foundUpdatedUsers);
}
use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.
the class SqlSimpleGroupSample 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 personDN = "inum=ae8c7ff9-def7-4b42-8147-de8380617a37,ou=people,o=gluu";
Filter ownerFilter = Filter.createEqualityFilter("owner", personDN);
Filter memberFilter = Filter.createEqualityFilter("member", personDN);
Filter searchFilter = Filter.createORFilter(ownerFilter, memberFilter);
List<SimpleGroup> result = sqlEntryManager.findEntries("ou=groups,o=gluu", SimpleGroup.class, searchFilter, 1);
System.out.println(result);
}
use of io.jans.orm.sql.persistence.SqlEntryManagerSample in project jans by JanssenProject.
the class SqlUpateMissingEntrySample 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 sessionId = UUID.randomUUID().toString();
final String sessionDn = "uniqueIdentifier=" + sessionId + ",ou=session,o=jans";
final SimpleSessionState simpleSessionState = new SimpleSessionState();
simpleSessionState.setDn(sessionDn);
simpleSessionState.setId(sessionId);
simpleSessionState.setLastUsedAt(new Date());
try {
sqlEntryManager.merge(simpleSessionState);
System.out.println("Updated");
} catch (EntryPersistenceException ex) {
LOG.info("Failed to update, root case exception: {}", ex.getCause().getClass(), ex);
}
}
Aggregations