Search in sources :

Example 51 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class CouchbaseSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create Couchbase entry manager
    CouchbaseEntryManager couchbaseEntryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    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("streetAddress", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("test", "test_value"));
    couchbaseEntryManager.persist(newUser);
    // SimpleUser dummyUser = couchbaseEntryManager.find(SimpleUser.class, "inum=test,o=test,o=jans");
    // LOG.info("Dummy User '{}'", dummyUser);
    // Find all users which have specified object classes defined in SimpleUser
    List<SimpleUser> users = couchbaseEntryManager.findEntries("o=@!5304.5F36.0E64.E1AC!0001!179C.62D7,o=jans", SimpleUser.class, null);
    for (SimpleUser user : users) {
        LOG.info("User with uid: '{}' with DN: '{}'", user.getUserId(), user.getDn());
    }
    if (users.size() > 0) {
        // Add attribute "streetAddress" to first user
        SimpleUser user = users.get(3);
        LOG.info("Updating: " + user.getUserId());
        String[] values = new String[] { "Somewhere: " + System.currentTimeMillis(), "Somewhere2: " + System.currentTimeMillis() };
        user.getCustomAttributes().add(new CustomObjectAttribute("streetAddress", Arrays.asList(values)));
        user.getCustomAttributes().add(new CustomObjectAttribute("test", "test_value"));
        user.getCustomAttributes().add(new CustomObjectAttribute("test2", "test_value2"));
        user.getCustomAttributes().add(new CustomObjectAttribute("test3", "test_value3"));
        user.setUserId("user1");
        user.setUserPassword("test");
        couchbaseEntryManager.merge(user);
    }
    for (SimpleUser user : users) {
        boolean result1 = couchbaseEntryManager.authenticate(user.getDn(), "test");
        boolean result2 = couchbaseEntryManager.authenticate("ou=people,o=jans", SimpleUser.class, user.getUserId(), "test");
        System.out.println("authetication result: " + result1 + ", " + result2);
    }
    Filter filter = Filter.createEqualityFilter("status", "active");
    List<SimpleAttribute> attributes = couchbaseEntryManager.findEntries("o=jans", SimpleAttribute.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
    for (SimpleAttribute attribute : attributes) {
        LOG.info("Attribute with displayName: " + attribute.getCustomAttributes().get(1));
    }
    List<SimpleSession> sessions = couchbaseEntryManager.findEntries("o=jans", SimpleSession.class, filter, SearchScope.SUB, null, null, 10, 0, 0);
    LOG.info("Found sessions: " + sessions.size());
    List<SimpleGrant> grants = couchbaseEntryManager.findEntries("o=jans", SimpleGrant.class, null, SearchScope.SUB, new String[] { "grtId" }, null, 1, 0, 0);
    LOG.info("Found grants: " + grants.size());
    try {
        PagedResult<SimpleUser> listViewResponse = couchbaseEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "status" }, "uid", SortOrder.ASCENDING, 0, 6, 4);
        LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
        for (SimpleUser user : listViewResponse.getEntries()) {
            System.out.println(user.getUserId());
        }
    } catch (Exception ex) {
        LOG.info("Failed to search", ex);
    }
    try {
        PagedResult<SimpleUser> listViewResponse = couchbaseEntryManager.findPagedEntries("o=jans", SimpleUser.class, null, new String[] { "uid", "displayName", "status" }, "uid", SortOrder.DESCENDING, 0, 6, 4);
        LOG.info("Found persons: " + listViewResponse.getEntriesCount() + ", total persons: " + listViewResponse.getTotalEntriesCount());
        for (SimpleUser user : listViewResponse.getEntries()) {
            System.out.println(user.getUserId());
        }
    } catch (Exception ex) {
        LOG.info("Failed to search", ex);
    }
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.couchbase.model.SimpleUser) SimpleAttribute(io.jans.orm.couchbase.model.SimpleAttribute) SimpleGrant(io.jans.orm.couchbase.model.SimpleGrant) Filter(io.jans.orm.search.filter.Filter) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) SimpleSession(io.jans.orm.couchbase.model.SimpleSession)

Example 52 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class UmaResourceSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    CouchbaseEntryManagerSample couchbaseEntryManagerSample = new CouchbaseEntryManagerSample();
    // Create Couchbase entry manager
    CouchbaseEntryManager entryManager = couchbaseEntryManagerSample.createCouchbaseEntryManager();
    final Filter filter = Filter.createEqualityFilter("jansAssociatedClnt", "inum=AB77-1A2B,ou=clients,o=jans");
    List<UmaResource> umaResource = entryManager.findEntries("ou=resources,ou=uma,o=jans", UmaResource.class, filter);
    LOG.info("Found umaResources: " + umaResource);
}
Also used : Filter(io.jans.orm.search.filter.Filter) CouchbaseEntryManager(io.jans.orm.couchbase.impl.CouchbaseEntryManager) UmaResource(io.jans.orm.couchbase.model.UmaResource)

Example 53 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SqlFilterConverterTest method checkMultivaluedGeFilters.

@Test
public void checkMultivaluedGeFilters() throws SearchException {
    // GE -- String
    Filter filterGe1 = Filter.createGreaterOrEqualFilter("uid", "test").multiValued();
    ConvertedExpression expressionGe1 = simpleConverter.convertToSqlFilter(filterGe1, null, null);
    String queryGe1 = toSelectSQL(expressionGe1);
    assertEquals(queryGe1, "select doc.`*` from `table` as doc where doc.uid->'$.v[0]' >= '[\"test\"]'");
    // GE -- Integer
    Filter filterGe2 = Filter.createGreaterOrEqualFilter("age", 23).multiValued();
    ConvertedExpression expressionGe2 = simpleConverter.convertToSqlFilter(filterGe2, null, null);
    String queryGe2 = toSelectSQL(expressionGe2);
    assertEquals(queryGe2, "select doc.`*` from `table` as doc where doc.age->'$.v[0]' >= '[23]'");
    // GE -- Long
    Filter filterGe3 = Filter.createGreaterOrEqualFilter("age", 23L).multiValued();
    ConvertedExpression expressionGe3 = simpleConverter.convertToSqlFilter(filterGe3, null, null);
    String queryGe3 = toSelectSQL(expressionGe3);
    assertEquals(queryGe3, "select doc.`*` from `table` as doc where doc.age->'$.v[0]' >= '[23]'");
    // GE -- Date
    Filter filterGe4 = Filter.createGreaterOrEqualFilter("added", getUtcDateFromMillis(1608130698398L)).multiValued();
    ConvertedExpression expressionGe4 = simpleConverter.convertToSqlFilter(filterGe4, null, null);
    String queryGe4 = toSelectSQL(expressionGe4);
    assertEquals(queryGe4, "select doc.`*` from `table` as doc where doc.added->'$.v[0]' >= '[\"2020-12-16T14:58:18.398\"]'");
    // GE -- Date
    Filter filterGe5 = Filter.createGreaterOrEqualFilter("added", getUtcDateFromMillis(1608130698398L)).multiValued(3);
    ConvertedExpression expressionGe5 = simpleConverter.convertToSqlFilter(filterGe5, null, null);
    String queryGe5 = toSelectSQL(expressionGe5);
    assertEquals(queryGe5, "select doc.`*` from `table` as doc where doc.added->'$.v[0]' >= '[\"2020-12-16T14:58:18.398\"]' or doc.added->'$.v[1]' >= '[\"2020-12-16T14:58:18.398\"]' or doc.added->'$.v[2]' >= '[\"2020-12-16T14:58:18.398\"]'");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Example 54 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SqlFilterConverterTest method checkAndFilters.

@Test
public void checkAndFilters() throws SearchException {
    Filter filterEq1 = Filter.createEqualityFilter("uid", "test");
    Filter filterPresence1 = Filter.createPresenceFilter("mail");
    Filter filterLe1 = Filter.createLessOrEqualFilter("age", 23);
    Filter filterAnd1 = Filter.createANDFilter(filterPresence1, filterEq1, filterLe1);
    ConvertedExpression expressionAnd1 = simpleConverter.convertToSqlFilter(filterAnd1, null, null);
    String queryAnd1 = toSelectSQL(expressionAnd1);
    assertEquals(queryAnd1, "select doc.`*` from `table` as doc where doc.mail is not null and doc.uid = 'test' and doc.age <= 23");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Example 55 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SqlFilterConverterTest method checkMultivaluedSubFilters.

@Test
public void checkMultivaluedSubFilters() throws SearchException {
    Filter filterSub1 = Filter.createSubstringFilter("uid", null, new String[] { "test" }, null).multiValued();
    ConvertedExpression expressionSub1 = simpleConverter.convertToSqlFilter(filterSub1, null, null);
    String querySub1 = toSelectSQL(expressionSub1);
    assertEquals(querySub1, "select doc.`*` from `table` as doc where doc.uid->'$.v[0]' like '%test%'");
    Filter filterSub2 = Filter.createSubstringFilter("uid", "a", new String[] { "test" }, null).multiValued();
    ConvertedExpression expressionSub2 = simpleConverter.convertToSqlFilter(filterSub2, null, null);
    String querySub2 = toSelectSQL(expressionSub2);
    assertEquals(querySub2, "select doc.`*` from `table` as doc where doc.uid->'$.v[0]' like 'a%test%'");
    Filter filterSub3 = Filter.createSubstringFilter("uid", null, new String[] { "test" }, "z").multiValued();
    ConvertedExpression expressionSub3 = simpleConverter.convertToSqlFilter(filterSub3, null, null);
    String querySub3 = toSelectSQL(expressionSub3);
    assertEquals(querySub3, "select doc.`*` from `table` as doc where doc.uid->'$.v[0]' like '%test%z'");
    Filter filterSub4 = Filter.createSubstringFilter("uid", null, new String[] { "test" }, "z").multiValued(3);
    ConvertedExpression expressionSub4 = simpleConverter.convertToSqlFilter(filterSub4, null, null);
    String querySub4 = toSelectSQL(expressionSub4);
    assertEquals(querySub4, "select doc.`*` from `table` as doc where doc.uid->'$.v[0]' like '%test%z' or doc.uid->'$.v[1]' like '%test%z' or doc.uid->'$.v[2]' like '%test%z'");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Aggregations

Filter (io.jans.orm.search.filter.Filter)188 Test (org.testng.annotations.Test)50 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)32 ConvertedExpression (io.jans.orm.couchbase.model.ConvertedExpression)28 SearchException (io.jans.orm.exception.operation.SearchException)25 ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)24 MappingException (io.jans.orm.exception.MappingException)22 ArrayList (java.util.ArrayList)21 AuthenticationException (io.jans.orm.exception.AuthenticationException)20 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)19 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)18 Date (java.util.Date)14 List (java.util.List)14 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)11 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)11 CustomAttribute (io.jans.orm.model.base.CustomAttribute)9 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)9 DateTimeParseException (java.time.format.DateTimeParseException)9 SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)8 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)8