agave-tags/tags-core/src/main/java/org/iplantc/service/tags/dao/TagDao.java
/**
     * Searches for software by the given user who matches the given set of 
     * parameters. Permissions are honored in this query as in pagination
     *
     * @param username the user for which the search runs
     * @param searchCriteria the criteria of the search
     * @param offset the results to skip
     * @param limit the max results
     * @return a list of {@link Tag} matching the search
     * @throws TagException if unable to query
     */
Searches for software by the given user who matches the given set of
parameters. Permissions are honored in this query as in pagination


Searches for software by the given user who matches the given set of parameters . Permissions are honored in this query as in pagination
@SuppressWarnings("unchecked")
    public List<Tag> findMatching(String username, Map<SearchTerm, Object> searchCriteria, int offset, int limit) 
    throws TagException
    {
        try
        {
            Session session = getSession();
            session.clear();
            String hql = "SELECT t FROM Tag t left join t.taggedResources taggedResource \n";
            SearchTerm publicSearchTerm = null;
            for (SearchTerm searchTerm: searchCriteria.keySet()) {
                if (searchTerm.getMappedField().startsWith("public")) {
                    publicSearchTerm = searchTerm;
                }
            }
            if (!AuthorizationHelper.isTenantAdmin(username)) {
                if (publicSearchTerm == null) {
                    hql +=  " WHERE ( \n" +
                            "       t.owner = :owner OR \n" +
                            "       t.id in ( \n" + 
                            "               SELECT tp.entityId FROM TagPermission tp \n" +
                            "               WHERE tp.username = :owner AND tp.permission <> :none \n" +
                            "              ) \n" +
                            "      ) AND \n";
                } 
                else if ((publicSearchTerm.getOperator() == SearchTerm.Operator.EQ && 
                            !(Boolean)searchCriteria.get(publicSearchTerm)) || 
                        (publicSearchTerm.getOperator() == SearchTerm.Operator.NEQ && 
                            (Boolean)searchCriteria.get(publicSearchTerm))) {
                    hql +=  " WHERE ( \n" +
                            "       t.owner = :owner OR \n" +
                            "       t.id in ( \n" + 
                            "               SELECT tp.entityId FROM TagPermission tp \n" +
                            "               WHERE tp.username = :owner AND tp.permission <> :none \n" +
                            "              ) \n" +
                            "      ) AND \n";
                }
            }
            else {
                hql += " WHERE ";
            }
            hql +=  "        t.tenantId = :tenantid "; 
            for (SearchTerm searchTerm: searchCriteria.keySet()) 
            {
                if (searchCriteria.get(searchTerm) == null 
                        || StringUtils.equalsIgnoreCase(searchCriteria.get(searchTerm).toString(), "null")) 
                {
                    if (searchTerm.getOperator() == SearchTerm.Operator.NEQ ) {
                        hql += "\n       AND       " + String.format(searchTerm.getMappedField(), searchTerm.getPrefix()) + " is not null ";
                    } else if (searchTerm.getOperator() == SearchTerm.Operator.EQ ) {
                        hql += "\n       AND       " + String.format(searchTerm.getMappedField(), searchTerm.getPrefix()) + " is null ";
                    } else {
                        hql += "\n       AND       " + searchTerm.getExpression();
                    }
                } else {
                    hql += "\n       AND       " + searchTerm.getExpression();
                }
            }
            hql +=  " ORDER BY t.name ASC\n";
            String q = hql;
            Query query = session.createQuery(hql)
                                 .setString("tenantid", TenancyHelper.getCurrentTenantId());
            q = q.replaceAll(":tenantid", "'" + TenancyHelper.getCurrentTenantId() + "'");
            if (!AuthorizationHelper.isTenantAdmin(username)) {
                query.setString("owner",username)
                    .setString("none",PermissionType.NONE.name());
                q = q.replaceAll(":owner", "'" + username + "'")
                    .replaceAll(":none", "'NONE'");
            }
            for (SearchTerm searchTerm: searchCriteria.keySet()) 
            {
                if (searchTerm.getOperator() == SearchTerm.Operator.BETWEEN || searchTerm.getOperator() == SearchTerm.Operator.ON) {
                    List<String> formattedDates = (List<String>)searchTerm.getOperator().applyWildcards(searchCriteria.get(searchTerm));
                    for(int i=0;i<formattedDates.size(); i++) {
                        query.setString(searchTerm.getSafeSearchField()+i, formattedDates.get(i));
                        q = q.replaceAll(":" + searchTerm.getSafeSearchField(), "'" + formattedDates.get(i) + "'");
                    }
                }
                else if (searchTerm.getOperator().isSetOperator()) 
                {
                    query.setParameterList(searchTerm.getSafeSearchField(), (List<Object>)searchCriteria.get(searchTerm));
                    q = q.replaceAll(":" + searchTerm.getSafeSearchField(), "('" + StringUtils.join((List<String>)searchTerm.getOperator().applyWildcards(searchCriteria.get(searchTerm)), "','") + "')" );
                }
                else if (searchCriteria.get(searchTerm) == null 
                        || StringUtils.equalsIgnoreCase(searchCriteria.get(searchTerm).toString(), "null")
                        && (searchTerm.getOperator() == SearchTerm.Operator.NEQ || searchTerm.getOperator() == SearchTerm.Operator.EQ )) {
                }
                else 
                {
                    query.setParameter(searchTerm.getSafeSearchField(), 
                            searchTerm.getOperator().applyWildcards(searchCriteria.get(searchTerm)));
                    q = q.replaceAll(":" + searchTerm.getSafeSearchField(), "'" + searchTerm.getOperator().applyWildcards(searchCriteria.get(searchTerm)) + "'");
                }
            }
            List<Tag> tags = query
                    .setFirstResult(offset)
                    .setMaxResults(limit)
                    .list();
            session.flush();
            return tags;
        }
        catch (Throwable ex)
        {
            throw new TagException(ex);
        }
        finally {
            try { HibernateUtil.commitTransaction();} catch (Exception ignored) {}
        }
    }
@ SuppressWarnings ( "unchecked" ) public List < Tag > findMatching ( String username , Map < SearchTerm , Object > searchCriteria , int offset , int limit ) throws TagException { try { Session session = getSession ( ) ; session . clear ( ) ; String hql = "SELECT t FROM Tag t left join t.taggedResources taggedResource \n" ; SearchTerm publicSearchTerm = null ; for ( SearchTerm searchTerm : searchCriteria . keySet ( ) ) { if ( searchTerm . getMappedField ( ) . startsWith ( "public" ) ) { publicSearchTerm = searchTerm ; } } if ( ! AuthorizationHelper . isTenantAdmin ( username ) ) { if ( publicSearchTerm == null ) { hql += " WHERE ( \n" + "       t.owner = :owner OR \n" + "       t.id in ( \n" + "               SELECT tp.entityId FROM TagPermission tp \n" + "               WHERE tp.username = :owner AND tp.permission <> :none \n" + "              ) \n" + "      ) AND \n" ; } else if ( ( publicSearchTerm . getOperator ( ) == SearchTerm . Operator . EQ && ! ( Boolean ) searchCriteria . get ( publicSearchTerm ) ) || ( publicSearchTerm . getOperator ( ) == SearchTerm . Operator . NEQ && ( Boolean ) searchCriteria . get ( publicSearchTerm ) ) ) { hql += " WHERE ( \n" + "       t.owner = :owner OR \n" + "       t.id in ( \n" + "               SELECT tp.entityId FROM TagPermission tp \n" + "               WHERE tp.username = :owner AND tp.permission <> :none \n" + "              ) \n" + "      ) AND \n" ; } } else { hql += " WHERE " ; } hql += "        t.tenantId = :tenantid " ; for ( SearchTerm searchTerm : searchCriteria . keySet ( ) ) { if ( searchCriteria . get ( searchTerm ) == null || StringUtils . equalsIgnoreCase ( searchCriteria . get ( searchTerm ) . toString ( ) , "null" ) ) { if ( searchTerm . getOperator ( ) == SearchTerm . Operator . NEQ ) { hql += "\n       AND       " + String . format ( searchTerm . getMappedField ( ) , searchTerm . getPrefix ( ) ) + " is not null " ; } else if ( searchTerm . getOperator ( ) == SearchTerm . Operator . EQ ) { hql += "\n       AND       " + String . format ( searchTerm . getMappedField ( ) , searchTerm . getPrefix ( ) ) + " is null " ; } else { hql += "\n       AND       " + searchTerm . getExpression ( ) ; } } else { hql += "\n       AND       " + searchTerm . getExpression ( ) ; } } hql += " ORDER BY t.name ASC\n" ; String q = hql ; Query query = session . createQuery ( hql ) . setString ( "tenantid" , TenancyHelper . getCurrentTenantId ( ) ) ; q = q . replaceAll ( ":tenantid" , "'" + TenancyHelper . getCurrentTenantId ( ) + "'" ) ; if ( ! AuthorizationHelper . isTenantAdmin ( username ) ) { query . setString ( "owner" , username ) . setString ( "none" , PermissionType . NONE . name ( ) ) ; q = q . replaceAll ( ":owner" , "'" + username + "'" ) . replaceAll ( ":none" , "'NONE'" ) ; } for ( SearchTerm searchTerm : searchCriteria . keySet ( ) ) { if ( searchTerm . getOperator ( ) == SearchTerm . Operator . BETWEEN || searchTerm . getOperator ( ) == SearchTerm . Operator . ON ) { List < String > formattedDates = ( List < String > ) searchTerm . getOperator ( ) . applyWildcards ( searchCriteria . get ( searchTerm ) ) ; for ( int i = 0 ; i < formattedDates . size ( ) ; i ++ ) { query . setString ( searchTerm . getSafeSearchField ( ) + i , formattedDates . get ( i ) ) ; q = q . replaceAll ( ":" + searchTerm . getSafeSearchField ( ) , "'" + formattedDates . get ( i ) + "'" ) ; } } else if ( searchTerm . getOperator ( ) . isSetOperator ( ) ) { query . setParameterList ( searchTerm . getSafeSearchField ( ) , ( List < Object > ) searchCriteria . get ( searchTerm ) ) ; q = q . replaceAll ( ":" + searchTerm . getSafeSearchField ( ) , "('" + StringUtils . join ( ( List < String > ) searchTerm . getOperator ( ) . applyWildcards ( searchCriteria . get ( searchTerm ) ) , "','" ) + "')" ) ; } else if ( searchCriteria . get ( searchTerm ) == null || StringUtils . equalsIgnoreCase ( searchCriteria . get ( searchTerm ) . toString ( ) , "null" ) && ( searchTerm . getOperator ( ) == SearchTerm . Operator . NEQ || searchTerm . getOperator ( ) == SearchTerm . Operator . EQ ) ) { } else { query . setParameter ( searchTerm . getSafeSearchField ( ) , searchTerm . getOperator ( ) . applyWildcards ( searchCriteria . get ( searchTerm ) ) ) ; q = q . replaceAll ( ":" + searchTerm . getSafeSearchField ( ) , "'" + searchTerm . getOperator ( ) . applyWildcards ( searchCriteria . get ( searchTerm ) ) + "'" ) ; } } List < Tag > tags = query . setFirstResult ( offset ) . setMaxResults ( limit ) . list ( ) ; session . flush ( ) ; return tags ; } catch ( Throwable ex ) { throw new TagException ( ex ) ; } finally { try { HibernateUtil . commitTransaction ( ) ; } catch ( Exception ignored ) { } } }
Searches for software by the given user who matches the given set of
 parameters.
Searches for software by the given user who matches the given set of parameters .
// no public search term specified. return public and private // otherwise, all public apps will be included in the general where clause // public = false || public.eq = false || public.neq = true, return only private // this was explicitly set to 'is null' or 'is not null'