Skip to content
Snippets Groups Projects
Commit bc1d055e authored by srosse's avatar srosse
Browse files

OO-687,OPENOLAT-131: make the search in repository case insensitive for the...

OO-687,OPENOLAT-131: make the search in repository case insensitive for the fields author, name and description for Oracle and PostgreSQL
parent 977e54ba
No related branches found
No related tags found
No related merge requests found
......@@ -123,6 +123,41 @@ public class PersistenceHelper {
}
return appended;
}
public static final void appendFuzzyLike(StringBuilder sb, String var, String key, String dbVendor) {
if(dbVendor.equals("mysql")) {
sb.append(" ").append(var).append(" like :").append(key);
} else {
sb.append(" lower(").append(var).append(") like :").append(key);
}
if(dbVendor.equals("oracle")) {
sb.append(" escape '\\'");
}
}
/**
* Helper method that replaces * with % and appends and
* prepends % to the string to make fuzzy SQL match when using like
* @param email
* @return fuzzized string
*/
public static final String makeFuzzyQueryString(String string) {
// By default only fuzzyfy at the end. Usually it makes no sense to do a
// fuzzy search with % at the beginning, but it makes the query very very
// slow since it can not use any index and must perform a fulltext search.
// User can always use * to make it a really fuzzy search query
// fxdiff FXOLAT-252: use "" to disable this feature and use exact match
if (string.length() > 1 && string.startsWith("\"") && string.endsWith("\"")) {
string = string.substring(1, string.length()-1);
} else {
string = string + "%";
string = string.replace('*', '%');
}
// with 'LIKE' the character '_' is a wildcard which matches exactly one character.
// To test for literal instances of '_', we have to escape it.
string = string.replace("_", "\\_");
return string.toLowerCase();
}
/**
*
......
......@@ -1716,22 +1716,44 @@ public class RepositoryManager extends BasicManager {
}
if (var_author) { // fuzzy author search
/*
author = '%' + author.replace('*', '%') + '%';
query.append(" and ownerGroup in (select msauth.securityGroup from ").append(SecurityGroupMembershipImpl.class.getName()).append(" msauth, ")
.append(" org.olat.basesecurity.IdentityImpl msauthid,")
.append(" org.olat.user.UserImpl msauthuser ")
.append(" where msauth.identity = msauthid and msauthid.user = msauthuser and ")
.append(" (msauthuser.properties['firstName'] like :author or msauthuser.properties['lastName'] like :author or msauthid.name like :author))");
*/
author = PersistenceHelper.makeFuzzyQueryString(author);
query.append(" and ownerGroup in (select msauth.securityGroup from ").append(SecurityGroupMembershipImpl.class.getName()).append(" msauth, ")
.append(" org.olat.basesecurity.IdentityImpl msauthid,")
.append(" org.olat.user.UserImpl msauthuser ")
.append(" where msauth.identity = msauthid and msauthid.user = msauthuser and ")
.append(" (");
PersistenceHelper.appendFuzzyLike(query, "msauthuser.properties['firstName']", "author", dbInstance.getDbVendor());
query.append(" or ");
PersistenceHelper.appendFuzzyLike(query, "msauthuser.properties['lastName']", "author", dbInstance.getDbVendor());
query.append(" or ");
PersistenceHelper.appendFuzzyLike(query, "msauthid.name", "author", dbInstance.getDbVendor());
query.append("))");
}
if (var_displayname) {
displayName = '%' + displayName.replace('*', '%') + '%';
query.append(" and v.displayname like :displayname");
//displayName = '%' + displayName.replace('*', '%') + '%';
//query.append(" and v.displayname like :displayname");
displayName = PersistenceHelper.makeFuzzyQueryString(displayName);
query.append(" and ");
PersistenceHelper.appendFuzzyLike(query, "v.displayname", "displayname", dbInstance.getDbVendor());
}
if (var_desc) {
desc = '%' + desc.replace('*', '%') + '%';
query.append(" and v.description like :desc");
//desc = '%' + desc.replace('*', '%') + '%';
//query.append(" and v.description like :desc");
desc = PersistenceHelper.makeFuzzyQueryString(desc);
query.append(" and ");
PersistenceHelper.appendFuzzyLike(query, "v.description", "desc", dbInstance.getDbVendor());
}
if (var_resourcetypes) {
......@@ -1769,6 +1791,8 @@ public class RepositoryManager extends BasicManager {
if(!count && orderBy) {
query.append(" order by v.displayname, v.key ASC");
}
System.out.println(query.toString());
DBQuery dbQuery = dbInstance.createQuery(query.toString());
if(institut) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment