Search in sources :

Example 6 with BusinessGroupOrder

use of org.olat.group.BusinessGroupOrder in project openolat by klemens.

the class BusinessGroupDAO method createFindDBQuery.

private <T> TypedQuery<T> createFindDBQuery(SearchBusinessGroupParams params, RepositoryEntryRef resource, Class<T> resultClass, BusinessGroupOrder... ordering) {
    StringBuilder query = new StringBuilder();
    if (BusinessGroup.class.equals(resultClass)) {
        query.append("select distinct(bgi) from ");
    } else {
        query.append("select count(bgi.key) from ");
    }
    query.append("businessgroup as bgi ");
    // inner joins
    if (BusinessGroup.class.equals(resultClass)) {
        query.append("inner join fetch bgi.resource bgResource ");
        query.append("inner join fetch bgi.baseGroup as baseGroup ");
    } else {
        query.append("inner join bgi.resource bgResource ");
        query.append("inner join bgi.baseGroup as baseGroup ");
    }
    boolean where = false;
    if (StringHelper.containsNonWhitespace(params.getExternalId())) {
        where = where(query, where);
        query.append("bgi.externalId=:externalId");
    }
    Long id = null;
    if (StringHelper.containsNonWhitespace(params.getIdRef())) {
        if (StringHelper.isLong(params.getIdRef())) {
            try {
                id = new Long(params.getIdRef());
            } catch (NumberFormatException e) {
            // not a real number, can be a very long numerical external id
            }
        }
        where = where(query, where);
        query.append("(bgi.externalId=:idRefString");
        if (id != null) {
            query.append(" or bgi.key=:idRefLong");
        }
        query.append(")");
    }
    if (params.getManaged() != null) {
        where = where(query, where);
        if (params.getManaged().booleanValue()) {
            query.append("bgi.managedFlagsString is not null");
        } else {
            query.append("bgi.managedFlagsString is null");
        }
    }
    if (params.getGroupKeys() != null && !params.getGroupKeys().isEmpty()) {
        where = where(query, where);
        query.append("bgi.key in (:groupKeys)");
    }
    if (resource != null) {
        where = where(query, where);
        query.append(" bgi.baseGroup.key in (").append("   select relation.group.key from repoentrytogroup as relation where relation.entry.key=:resourceKey").append(" )");
    }
    if (StringHelper.containsNonWhitespace(params.getCourseTitle())) {
        where = where(query, where);
        query.append(" bgi.key in (").append("   select bgRel.relationId.groupKey from repoentryrelationview bgRel ").append("     where ");
        searchLikeAttribute(query, "bgRel", "repositoryEntryDisplayName", "displayName");
        query.append(" )");
    }
    List<String> roles = null;
    if (params.isOwner() || params.isAttendee() || params.isWaiting()) {
        where = where(query, where);
        roles = new ArrayList<>();
        query.append(" bgi.baseGroup.key in (select bmember.group.key from bgroupmember as bmember").append("   where bmember.identity.key=:identId and bmember.role in (:roles)").append(" )");
        if (params.isOwner()) {
            roles.add(GroupRoles.coach.name());
        }
        if (params.isAttendee()) {
            roles.add(GroupRoles.participant.name());
        }
        if (params.isWaiting()) {
            roles.add(GroupRoles.waiting.name());
        }
    }
    if (StringHelper.containsNonWhitespace(params.getNameOrDesc())) {
        where = where(query, where);
        query.append("(");
        searchLikeAttribute(query, "bgi", "name", "search");
        query.append(" or ");
        searchLikeAttribute(query, "bgi", "description", "search");
        query.append(")");
    } else {
        if (StringHelper.containsNonWhitespace(params.getExactName())) {
            where = where(query, where);
            query.append("bgi.name=:exactName");
        }
        if (StringHelper.containsNonWhitespace(params.getName())) {
            where = where(query, where);
            searchLikeAttribute(query, "bgi", "name", "name");
        }
        if (StringHelper.containsNonWhitespace(params.getDescription())) {
            where = where(query, where);
            searchLikeAttribute(query, "bgi", "description", "description");
        }
    }
    if (params.getTools() != null && !params.getTools().isEmpty()) {
        where = where(query, where);
        query.append("bgi.key in (select prop.resourceTypeId from ").append(Property.class.getName()).append(" prop").append(" where prop.category='").append(CollaborationTools.PROP_CAT_BG_COLLABTOOLS).append("'").append(" and prop.name in (:tools) and prop.stringValue='true' and prop.resourceTypeName='BusinessGroup')");
    }
    // order by (not for count)
    if (BusinessGroup.class.equals(resultClass)) {
        query.append(" order by ");
        if (ordering != null && ordering.length > 0) {
            for (BusinessGroupOrder o : ordering) {
                switch(o) {
                    case nameAsc:
                        query.append("bgi.name,");
                        break;
                    case nameDesc:
                        query.append("bgi.name desc,");
                        break;
                    case creationDateAsc:
                        query.append("bgi.creationDate,");
                        break;
                    case creationDateDesc:
                        query.append("bgi.creationDate desc,");
                        break;
                }
            }
        } else {
            query.append("bgi.name,");
        }
        query.append("bgi.key");
    }
    TypedQuery<T> dbq = dbInstance.getCurrentEntityManager().createQuery(query.toString(), resultClass);
    // add parameters
    if (params.isOwner() || params.isAttendee() || params.isWaiting()) {
        dbq.setParameter("identId", params.getIdentity().getKey());
    }
    if (params.getGroupKeys() != null && !params.getGroupKeys().isEmpty()) {
        dbq.setParameter("groupKeys", params.getGroupKeys());
    }
    if (StringHelper.containsNonWhitespace(params.getExternalId())) {
        dbq.setParameter("externalId", params.getExternalId());
    }
    if (StringHelper.containsNonWhitespace(params.getIdRef())) {
        dbq.setParameter("idRefString", params.getIdRef());
        if (id != null) {
            dbq.setParameter("idRefLong", id);
        }
    }
    if (resource != null) {
        dbq.setParameter("resourceKey", resource.getKey());
    }
    if (params.getTools() != null && !params.getTools().isEmpty()) {
        dbq.setParameter("tools", params.getTools());
    }
    if (roles != null) {
        dbq.setParameter("roles", roles);
    }
    if (StringHelper.containsNonWhitespace(params.getNameOrDesc())) {
        dbq.setParameter("search", PersistenceHelper.makeFuzzyQueryString(params.getNameOrDesc()));
    } else {
        if (StringHelper.containsNonWhitespace(params.getExactName())) {
            dbq.setParameter("exactName", params.getExactName());
        }
        if (StringHelper.containsNonWhitespace(params.getName())) {
            dbq.setParameter("name", PersistenceHelper.makeFuzzyQueryString(params.getName()));
        }
        if (StringHelper.containsNonWhitespace(params.getDescription())) {
            dbq.setParameter("description", PersistenceHelper.makeFuzzyQueryString(params.getDescription()));
        }
    }
    if (StringHelper.containsNonWhitespace(params.getCourseTitle())) {
        dbq.setParameter("displayName", PersistenceHelper.makeFuzzyQueryString(params.getCourseTitle()));
    }
    return dbq;
}
Also used : BusinessGroupOrder(org.olat.group.BusinessGroupOrder)

Aggregations

BusinessGroupOrder (org.olat.group.BusinessGroupOrder)6 BusinessGroup (org.olat.group.BusinessGroup)4 HashSet (java.util.HashSet)2 PortletEntry (org.olat.core.gui.control.generic.portal.PortletEntry)2