Example usage for org.hibernate.criterion Restrictions ne

List of usage examples for org.hibernate.criterion Restrictions ne

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions ne.

Prototype

public static SimpleExpression ne(String propertyName, Object value) 

Source Link

Document

Apply a "not equal" constraint to the named property

Usage

From source file:org.apache.ode.daohib.bpel.CriteriaBuilder.java

License:Apache License

/**
 * Build a Hibernate {@link Criteria} from an instance filter.
 * @param crit target (destination) criteria
 * @param filter filter/* w ww.  ja va 2s  . c  o  m*/
 */
void buildCriteria(Criteria crit, InstanceFilter filter) {
    Criteria processCrit = crit.createCriteria("process");

    // Filtering on PID
    List<String> pids = filter.getPidFilter();
    if (pids != null && pids.size() > 0) {
        Disjunction disj = Restrictions.disjunction();
        for (String pid : pids) {
            if (!filter.arePidsNegative()) {
                disj.add(Restrictions.eq("processId", pid));
            } else {
                disj.add(Restrictions.ne("processId", pid));
            }
        }
        processCrit.add(disj);
    }

    List<String> iids = filter.getIidFilter();
    if (iids != null && iids.size() > 0) {
        Disjunction disj = Restrictions.disjunction();
        for (String iid : iids) {
            disj.add(Restrictions.eq("id", new Long(iid)));
        }
        crit.add(disj);
    }

    // Filtering on name and namespace
    if (filter.getNameFilter() != null) {
        processCrit.add(Restrictions.like("typeName", filter.getNameFilter().replaceAll("\\*", "%")));
    }
    if (filter.getNamespaceFilter() != null) {
        processCrit.add(Restrictions.like("typeNamespace", filter.getNamespaceFilter().replaceAll("\\*", "%")));
    }

    // Specific filter for status (using a disjunction between possible statuses)
    if (filter.getStatusFilter() != null) {
        List<Short> statuses = filter.convertFilterState();
        Disjunction disj = Restrictions.disjunction();
        for (short status : statuses) {
            disj.add(Restrictions.eq("state", status));
        }
        crit.add(disj);
    }

    // Specific filter for started and last active dates.
    if (filter.getStartedDateFilter() != null) {
        for (String sdf : filter.getStartedDateFilter()) {
            addFilterOnPrefixedDate(crit, sdf, "created");
        }
    }
    if (filter.getLastActiveDateFilter() != null) {
        for (String ladf : filter.getLastActiveDateFilter()) {
            addFilterOnPrefixedDate(crit, ladf, "lastActiveTime");
        }
    }

    // Specific filter for correlation properties
    if (filter.getPropertyValuesFilter() != null) {
        Criteria propCrit = crit.createCriteria("correlationSets").createCriteria("properties");
        for (Map.Entry<String, String> corValue : filter.getPropertyValuesFilter().entrySet()) {
            String propName = (String) corValue.getKey();
            if (propName.startsWith("{")) {
                String namespace = propName.substring(1, propName.lastIndexOf("}"));
                propName = propName.substring(propName.lastIndexOf("}") + 1, propName.length());
                propCrit.add(Restrictions.eq("name", propName)).add(Restrictions.eq("namespace", namespace))
                        .add(Restrictions.eq("value", corValue.getValue()));
            } else {
                propCrit.add(Restrictions.eq("name", corValue.getKey()))
                        .add(Restrictions.eq("value", corValue.getValue()));
            }
        }
    }

    // Ordering
    if (filter.orders != null) {
        for (String key : filter.orders) {
            boolean ascending = true;
            String orderKey = key;
            if (key.startsWith("+") || key.startsWith("-")) {
                orderKey = key.substring(1, key.length());
                if (key.startsWith("-"))
                    ascending = false;
            }

            if ("name".equals(orderKey)) {
                if (ascending)
                    processCrit.addOrder(Property.forName("typeName").asc());
                else
                    processCrit.addOrder(Property.forName("typeName").desc());
            } else if ("namespace".equals(orderKey)) {
                if (ascending)
                    processCrit.addOrder(Property.forName("typeNamespace").asc());
                else
                    processCrit.addOrder(Property.forName("typeNamespace").desc());
            } else if ("pid".equals(orderKey)) {
                if (ascending)
                    processCrit.addOrder(Property.forName("processId").asc());
                else
                    processCrit.addOrder(Property.forName("processId").desc());
            } else if ("version".equals(orderKey)) {
                if (ascending)
                    processCrit.addOrder(Property.forName("version").asc());
                else
                    processCrit.addOrder(Property.forName("version").desc());
            } else if ("status".equals(orderKey)) {
                if (ascending)
                    crit.addOrder(Property.forName("state").asc());
                else
                    crit.addOrder(Property.forName("state").desc());
            } else if ("started".equals(orderKey)) {
                if (ascending)
                    crit.addOrder(Property.forName("created").asc());
                else
                    crit.addOrder(Property.forName("created").desc());
            } else if ("last-active".equals(orderKey)) {
                if (ascending)
                    crit.addOrder(Property.forName("lastActiveTime").asc());
                else
                    crit.addOrder(Property.forName("lastActiveTime").desc());
            }
        }
    }

    if (filter.getLimit() > 0)
        crit.setMaxResults(filter.getLimit());
}

From source file:org.apache.usergrid.apm.service.charts.service.LogChartUtil.java

License:Apache License

public static List<Criterion> getRawLogCriteriaList(LogRawCriteria logRawCriteria) {
    LogChartCriteria cq = logRawCriteria.getChartCriteria();
    List<Criterion> filters = new ArrayList<Criterion>();

    //Narrow down by app id first
    if (cq.getAppId() != null)
        filters.add(new AppsFilter(cq.getAppId()).getCriteria());
    //Then by time range
    filters.add(new TimeRangeFilter(cq).getCriteria());

    if (cq.getDeviceId() != null)
        filters.add(Restrictions.like("deviceId", cq.getDeviceId().trim(), MatchMode.START));

    if (logRawCriteria.getLogLevel() != null)
        filters.add(Restrictions.eq("logLevel", logRawCriteria.getLogLevel()));
    if (logRawCriteria.getTag() != null)
        filters.add(Restrictions.like("tag", logRawCriteria.getTag().trim(), MatchMode.START));
    else {// w  w  w . java2 s  .c o m
        if (logRawCriteria.isExcludeCrash())
            filters.add(Restrictions.ne("tag", "CRASH"));
    }
    if (cq.getAppVersion() != null)
        filters.add(Restrictions.like("applicationVersion", cq.getAppVersion().trim(), MatchMode.START));
    if (cq.getAppConfigType() != null)
        filters.add(Restrictions.like("appConfigType", cq.getAppConfigType(), MatchMode.START));
    if (cq.getDevicePlatform() != null)
        filters.add(Restrictions.like("devicePlatform", cq.getDevicePlatform().trim(), MatchMode.START));
    if (cq.getDeviceModel() != null)
        filters.add(Restrictions.like("deviceModel", cq.getDeviceModel().trim(), MatchMode.START));
    if (cq.getDeviceOS() != null)
        filters.add(Restrictions.like("deviceOperatingSystem", cq.getDeviceOS().trim(), MatchMode.START));

    if (cq.getNetworkCarrier() != null)
        filters.add(Restrictions.like("networkCarrier", cq.getNetworkCarrier().trim(), MatchMode.START));
    if (cq.getNetworkType() != null)
        filters.add(Restrictions.like("networkType", cq.getNetworkType().trim(), MatchMode.START));

    if (logRawCriteria.getLogMessage() != null)
        filters.add(Restrictions.like("logMessage", logRawCriteria.getLogMessage().trim(), MatchMode.ANYWHERE));
    return filters;
}

From source file:org.atomsphere.management.dao.authentication.HibernateUserDao.java

License:LGPL

@Override
public boolean isLastAdmin(Long id) {
    Criteria criteria = getCurrentSession().createCriteria(User.class);
    criteria.add(Restrictions.ne(User.FIELD_ID, id));
    criteria.add(Restrictions.eq(User.FIELD_ADMINISTRATOR, true));
    criteria.add(Restrictions.eq(User.FIELD_ENABLED, true));
    criteria.add(Restrictions.eq(User.FIELD_ACCOUNT_NON_EXPIRED, true));
    criteria.add(Restrictions.eq(User.FIELD_ACCOUNT_NON_LOCKED, true));
    criteria.add(Restrictions.eq(User.FIELD_CREDENTIALS_NON_EXPIRED, true));
    criteria.setProjection(Projections.rowCount());

    return getIntegerValue(criteria.list().get(0)).intValue() == 0;
}

From source file:org.balisunrise.daa.hibernate.HCriteria.java

License:Open Source License

private org.hibernate.criterion.Criterion makeCriterion(String propertyName, FilterType type, Object value,
        Object otherValue) {//from   ww w  . j  a v  a2  s .c  o  m

    switch (type) {
    case EQUALS:
        return Restrictions.eq(propertyName, value);
    case EQUALS_OR_NULL:
        return Restrictions.eqOrIsNull(propertyName, value);
    case DIFFERENT:
        return Restrictions.ne(propertyName, value);
    case DIFFERENT_OR_NOT_NULL:
        return Restrictions.neOrIsNotNull(propertyName, value);
    case GREATHER:
        return Restrictions.gt(propertyName, value);
    case GREATHER_EQUALS:
        return Restrictions.ge(propertyName, value);
    case LESS:
        return Restrictions.lt(propertyName, value);
    case LESS_EQUALS:
        return Restrictions.le(propertyName, value);
    case LIKE:
        return Restrictions.like(propertyName, value + "%");
    case ILIKE:
        return Restrictions.ilike(propertyName, "%" + value + "%");
    case BETWEEN:
        return Restrictions.between(propertyName, value, otherValue);
    case IN:
        if (value instanceof Collection)
            return Restrictions.in(propertyName, (Collection) value);
        else if (value instanceof Object[])
            return Restrictions.in(propertyName, (Object[]) value);
        else
            return Restrictions.in(propertyName, new Object[] { value });
    case NULL:
        return Restrictions.isNull(propertyName);
    case NOT_NULL:
        return Restrictions.isNotNull(propertyName);
    default:
        return null;
    }
}

From source file:org.candlepin.auth.permissions.CheckJobStatusPermission.java

License:Open Source License

@Override
@SuppressWarnings("checkstyle:indentation")
public Criterion getCriteriaRestrictions(Class entityClass) {
    if (!entityClass.equals(JobStatus.class)) {
        return null;
    }/*from  w  w w .ja  v a 2 s  .com*/

    Conjunction conjunction = Restrictions.conjunction();
    // Org has to match.
    conjunction.add(Restrictions.in("ownerId", allowedOrgKeys));

    conjunction.add(Restrictions.or(Restrictions.ne("targetType", JobStatus.TargetType.OWNER),
            Restrictions.and(Restrictions.eq("targetType", JobStatus.TargetType.OWNER),
                    Restrictions.eqProperty("ownerId", "targetId"))));

    // If the principal is not a user, make sure to enforce a principalName match.
    if (!"user".equalsIgnoreCase(principalType)) {
        conjunction.add(Restrictions.eq("principalName", principalName));
    }
    return conjunction;
}

From source file:org.candlepin.auth.permissions.JobStatusPermission.java

License:Open Source License

@Override
@SuppressWarnings("checkstyle:indentation")
public Criterion getCriteriaRestrictions(Class entityClass) {
    if (!entityClass.equals(JobStatus.class)) {
        return null;
    }//from w  w  w . j  a  v a2 s  .  c  o  m

    Conjunction conjunction = Restrictions.conjunction();
    // Org has to match.
    conjunction.add(Restrictions.in("ownerId", allowedOrgIds));

    conjunction.add(Restrictions.or(Restrictions.ne("targetType", JobStatus.TargetType.OWNER),
            Restrictions.and(Restrictions.eq("targetType", JobStatus.TargetType.OWNER),
                    Restrictions.eqProperty("ownerId", "targetId"))));

    // If the principal is not a user, make sure to enforce a principalName match.
    if (!"user".equalsIgnoreCase(principalType)) {
        conjunction.add(Restrictions.eq("principalName", principalName));
    }
    return conjunction;
}

From source file:org.candlepin.gutterball.curator.EventCurator.java

License:Open Source License

public boolean hasEventForMessage(String messageId) {
    // Do not include UNKNOWN since they were Events that
    // existed pre-update and there is no way to recover the
    // message ID.
    Criteria criteria = currentSession().createCriteria(Event.class)
            .add(Restrictions.eq("messageId", messageId)).add(Restrictions.ne("messageId", "UNKNOWN"))
            .setProjection(Projections.count("id"));
    return ((Long) criteria.uniqueResult()) > 0;
}

From source file:org.candlepin.model.JobCurator.java

License:Open Source License

public JobStatus getByClassAndOwner(String ownerKey, Class<? extends KingpinJob> jobClass) {

    return (JobStatus) this.currentSession().createCriteria(JobStatus.class).addOrder(Order.desc("created"))
            .add(Restrictions.ge("updated", getBlockingCutoff()))
            .add(Restrictions.ne("state", JobState.FINISHED)).add(Restrictions.ne("state", JobState.FAILED))
            .add(Restrictions.ne("state", JobState.CANCELED)).add(Restrictions.eq("targetId", ownerKey))
            .add(Restrictions.eq("jobClass", jobClass)).setMaxResults(1).uniqueResult();
}

From source file:org.candlepin.model.PoolCurator.java

License:Open Source License

/**
 * List entitlement pools./*from w w w .j a  v  a  2s  .c  om*/
 *
 * Pools will be refreshed from the underlying subscription service.
 *
 * @param c Consumer being entitled.
 * @param o Owner whose subscriptions should be inspected.
 * @param productId only entitlements which provide this product are included.
 * @param activeOn Indicates to return only pools valid on this date.
 *        Set to null for no date filtering.
 * @param activeOnly if true, only active entitlements are included.
 * @param filters filter builder with set filters to apply to the criteria.
 * @param pageRequest used to specify paging criteria.
 * @param postFilter if you plan on filtering the list in java
 * @return List of entitlement pools.
 */
@SuppressWarnings("unchecked")
@Transactional
public Page<List<Pool>> listAvailableEntitlementPools(Consumer c, Owner o, String productId, Date activeOn,
        boolean activeOnly, PoolFilterBuilder filters, PageRequest pageRequest, boolean postFilter) {
    if (o == null && c != null) {
        o = c.getOwner();
    }

    if (log.isDebugEnabled()) {
        log.debug("Listing available pools for:");
        log.debug("   consumer: " + c);
        log.debug("   owner: " + o);
        log.debug("   product: " + productId);
    }

    Criteria crit = createSecureCriteria();
    if (activeOnly) {
        crit.add(Restrictions.eq("activeSubscription", Boolean.TRUE));
    }
    if (c != null) {
        crit.add(Restrictions.eq("owner", c.getOwner()));

        // Ask the rules for any business logic criteria to filter with for
        // this consumer
        List<Criterion> filterCriteria = poolCriteria.availableEntitlementCriteria(c);

        if (log.isDebugEnabled()) {
            log.debug("Got " + filterCriteria.size() + "  query filters from database.");
        }

        for (Criterion rulesCriteria : filterCriteria) {
            crit.add(rulesCriteria);
        }
    }
    if (o != null) {
        crit.add(Restrictions.eq("owner", o));
        crit.add(Restrictions.ne("productName", Product.ueberProductNameForOwner(o)));
    }
    if (activeOn != null) {
        crit.add(Restrictions.le("startDate", activeOn));
        crit.add(Restrictions.ge("endDate", activeOn));
    }

    if (productId != null) {
        crit.createAlias("providedProducts", "providedProduct", CriteriaSpecification.LEFT_JOIN);

        crit.add(Restrictions.or(Restrictions.eq("productId", productId),
                Restrictions.eq("providedProduct.productId", productId)));
    }

    // Append any specified filters
    if (filters != null) {
        filters.applyTo(crit);
    }

    return listByCriteria(crit, pageRequest, postFilter);
}

From source file:org.candlepin.model.SubscriptionCurator.java

License:Open Source License

/**
 * Return a list of subscriptions filtered by owner.
 * @param o Owner of the subscription./*from  www . ja  v a2  s .c  o m*/
 * @return a list of subscriptions filtered by owner.
 */
@SuppressWarnings("unchecked")
public List<Subscription> listByOwner(Owner o) {
    List<Subscription> subs = currentSession().createCriteria(Subscription.class)
            .add(Restrictions.eq("owner", o)).createCriteria("product")
            .add(Restrictions.ne("name", Product.ueberProductNameForOwner(o))).list();
    if (subs == null) {
        return new LinkedList<Subscription>();
    }
    log.debug("Found subs: " + subs.size());
    return subs;
}