agave-notifications/notifications-core/src/main/java/org/iplantc/service/notification/dao/NotificationDao.java
/**
     * Searches for an entity by search criteria. Permissions are honored in this query as is pagination.
     *
     * @param username
     * @param searchCriteria
     * @param offset
     * @param limit
     * @return
     * @throws NotificationException
     */
Searches for an entity by search criteria. Permissions are honored in this query as is pagination.
Searches for an entity by search criteria . Permissions are honored in this query as is pagination .
@SuppressWarnings("unchecked")
    public List<Notification> findMatching(String username, Map<SearchTerm, Object> searchCriteria, int offset, int limit) 
    throws NotificationException
    {
        try
        {
            Session session = getSession();
            session.clear();
if (!AuthorizationHelper.isTenantAdmin(username)) {
    // no public search term specified. return public and private
    hql +=  " WHERE ( \n" +
            "       c.owner = :owner OR \n" +
            "       c.id in ( \n" + 
            "               SELECT cp.resourceId FROM RealtimeChannelPermission cp \n" +
            "               WHERE cp.username = :owner AND cp.permission <> :none \n" +
            "              ) \n" +
            "      ) AND \n";
} else {
    hql += " WHERE ";
}
            String hql = "FROM Notification c "; 
            hql += "\n WHERE c.tenantId = :tenantid ";
            if (!AuthorizationHelper.isTenantAdmin(username)) {
				hql +=   "    AND c.owner = :owner \n";
			}
            boolean hasVisibleSearchTerm = false;
            for (SearchTerm searchTerm: searchCriteria.keySet()) 
            {
            	if (StringUtils.equalsIgnoreCase(searchTerm.getSearchField(), "visible")) {
            		hasVisibleSearchTerm = true;
            	}
                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();
                }
            }
            if (!hasVisibleSearchTerm) {
            	hql += "\n       AND       c.visible = :visiblebydefault";
            }
            hql +=  " ORDER BY c.created DESC\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);
                q = q.replaceAll(":owner", "'" + username + "'");
            }
            for (SearchTerm searchTerm: searchCriteria.keySet()) 
            {
                if (searchTerm.getOperator() == SearchTerm.Operator.BETWEEN) {
                    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)) + "'");
                }
            }
            if (!hasVisibleSearchTerm) {
                query.setBoolean("visiblebydefault", Boolean.TRUE);
                q = q.replaceAll(":visiblebydefault", "1");
            }
log.debug(q);
            List<Notification> attempts = query
                    .setFirstResult(offset)
                    .setMaxResults(limit)
                    .list();
            session.flush();
            return attempts;
        }
        catch (Throwable ex)
        {
            throw new NotificationException(ex);
        }
        finally {
            try { HibernateUtil.commitTransaction();} catch (Exception ignored) {}
        }
    }
@ SuppressWarnings ( "unchecked" ) public List < Notification > findMatching ( String username , Map < SearchTerm , Object > searchCriteria , int offset , int limit ) throws NotificationException { try { Session session = getSession ( ) ; session . clear ( ) ; String hql = "FROM Notification c " ; hql += "\n WHERE c.tenantId = :tenantid " ; if ( ! AuthorizationHelper . isTenantAdmin ( username ) ) { hql += "    AND c.owner = :owner \n" ; } boolean hasVisibleSearchTerm = false ; for ( SearchTerm searchTerm : searchCriteria . keySet ( ) ) { if ( StringUtils . equalsIgnoreCase ( searchTerm . getSearchField ( ) , "visible" ) ) { hasVisibleSearchTerm = true ; } 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 ( ) ; } } if ( ! hasVisibleSearchTerm ) { hql += "\n       AND       c.visible = :visiblebydefault" ; } hql += " ORDER BY c.created DESC\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 ) ; q = q . replaceAll ( ":owner" , "'" + username + "'" ) ; } for ( SearchTerm searchTerm : searchCriteria . keySet ( ) ) { if ( searchTerm . getOperator ( ) == SearchTerm . Operator . BETWEEN ) { 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 ) ) + "'" ) ; } } if ( ! hasVisibleSearchTerm ) { query . setBoolean ( "visiblebydefault" , Boolean . TRUE ) ; q = q . replaceAll ( ":visiblebydefault" , "1" ) ; } List < Notification > attempts = query . setFirstResult ( offset ) . setMaxResults ( limit ) . list ( ) ; session . flush ( ) ; return attempts ; } catch ( Throwable ex ) { throw new NotificationException ( ex ) ; } finally { try { HibernateUtil . commitTransaction ( ) ; } catch ( Exception ignored ) { } } }
Searches for an entity by search criteria.
Searches for an entity by search criteria .
//            String hql = "FROM NotificationAttempt c left join c.observableEvents observableEvents \n"; //             //            if (!AuthorizationHelper.isTenantAdmin(username)) { //                // no public search term specified. return public and private //                hql +=  " WHERE ( \n" + //                        "       c.owner = :owner OR \n" + //                        "       c.id in ( \n" +  //                        "               SELECT cp.resourceId FROM RealtimeChannelPermission cp \n" + //                        "               WHERE cp.username = :owner AND cp.permission <> :none \n" + //                        "              ) \n" + //                        "      ) AND \n"; //            } else { //                hql += " WHERE "; //            } // hide deleted notifications by default // this was explicitly set to 'is null' or 'is not null' // hide deleted notifications by default //            log.debug(q);