List of usage examples for org.hibernate.criterion Restrictions isNotNull
public static Criterion isNotNull(String propertyName)
From source file:net.firejack.platform.core.store.AbstractStore.java
License:Apache License
protected Criterion getRestrictions(SearchQuery query, Class<?> type) { Criterion criterion;// w w w . j a v a2s . c o m Object value = query.getValue(); QueryOperation operation = query.getOperation(); if (value != null && !(QueryOperation.FIELDEQUALS.equals(operation) || QueryOperation.FIELDNOTEQUALS.equals(operation) || QueryOperation.FIELDGREATERTHAN.equals(operation) || QueryOperation.FIELDLESSTHAN.equals(operation))) { if (value instanceof Collection) { Collection values = (Collection) value; if (Integer.class.equals(type)) { List<Integer> list = new ArrayList<Integer>(); for (Object item : values) { list.add(Integer.parseInt(item.toString())); } value = list; } else if (Long.class.equals(type)) { List<Long> list = new ArrayList<Long>(); for (Object item : values) { list.add(Long.parseLong(item.toString())); } value = list; } else if (java.sql.Date.class.equals(type) || Date.class.equals(type)) { List<Date> list = new ArrayList<Date>(); for (Object item : values) { Tuple<Date, QueryOperation> tuple = convertToDate(item, operation); operation = tuple.getValue(); list.add(tuple.getKey()); } value = list; } else if (Enum.class.isAssignableFrom(type)) { List<Enum> enumValues = new ArrayList<Enum>(values.size()); for (Object item : values) { Enum enumItem = prepareEnumFromSearchCriteria((Class<? extends Enum>) type, item); enumValues.add(enumItem); } value = enumValues; } } else { if (Integer.class.equals(type)) { value = Integer.parseInt(value.toString()); } else if (Long.class.equals(type)) { value = Long.parseLong(value.toString()); } else if (Double.class.equals(type)) { value = Double.parseDouble(value.toString()); } else if (java.sql.Date.class.equals(type) || Date.class.equals(type)) { Tuple<Date, QueryOperation> tuple = convertToDate(value, operation); value = tuple.getKey(); operation = tuple.getValue(); } else if (Enum.class.isAssignableFrom(type)) { value = prepareEnumFromSearchCriteria((Class<? extends Enum>) type, value); } } } if (!String.class.equals(type) && (QueryOperation.LIKECS.equals(operation) || QueryOperation.LIKECSFIRST.equals(operation) || QueryOperation.LIKE.equals(operation) || QueryOperation.LIKEFIRST.equals(operation))) { operation = QueryOperation.EQUALS; } switch (operation) { case LIKECS: criterion = Restrictions.like(query.getField(), "%" + value + "%"); break; case LIKECSFIRST: criterion = Restrictions.like(query.getField(), value + "%"); break; case LIKE: criterion = Restrictions.ilike(query.getField(), "%" + value + "%"); break; case LIKEFIRST: criterion = Restrictions.ilike(query.getField(), value + "%"); break; case EQUALS: criterion = Restrictions.eq(query.getField(), value); break; case LESSTHAN: criterion = Restrictions.lt(query.getField(), value); break; case GREATERTHAN: criterion = Restrictions.gt(query.getField(), value); break; case ISNULL: criterion = Restrictions.isNull(query.getField()); break; case ISNOTNULL: criterion = Restrictions.isNotNull(query.getField()); break; case ISEMPTY: criterion = Restrictions.isEmpty(query.getField()); break; case ISNOTEMPTY: criterion = Restrictions.isNotEmpty(query.getField()); break; case NOTEQUALS: criterion = Restrictions.ne(query.getField(), value); break; case IN: criterion = generateInRestriction(query.getField(), (Collection) value); break; case NOTIN: criterion = Restrictions.not(generateInRestriction(query.getField(), (Collection) value)); break; case FIELDEQUALS: criterion = Restrictions.eqProperty(query.getField(), value != null ? value.toString() : ""); break; case FIELDNOTEQUALS: criterion = Restrictions.neProperty(query.getField(), value != null ? value.toString() : ""); break; case FIELDGREATERTHAN: criterion = Restrictions.gtProperty(query.getField(), value != null ? value.toString() : ""); break; case FIELDLESSTHAN: criterion = Restrictions.ltProperty(query.getField(), value != null ? value.toString() : ""); break; default: throw new RuntimeException("Operation " + query.getField() + " is not a valid operation"); } return criterion; }
From source file:net.firejack.platform.core.store.registry.EntityStore.java
License:Apache License
@Override @SuppressWarnings("unchecked") @Transactional(readOnly = true)/*from ww w . j a va2 s. c o m*/ public List<Object[]> findAllIdAndExtendedId() { return getHibernateTemplate().executeFind(new HibernateCallback<List<Object[]>>() { @Override public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(EntityModel.class); criteria.add(Restrictions.isNotNull("extendedEntity.id")); ProjectionList proList = Projections.projectionList(); proList.add(Projections.property("id")); proList.add(Projections.property("extendedEntity.id")); criteria.setProjection(proList); return criteria.list(); } }); }
From source file:net.firejack.platform.core.store.user.BasicUserRoleStore.java
License:Apache License
@Override @Transactional(readOnly = true)//from w w w. jav a 2 s. c om public Map<SecuredRecordModel, List<UserRoleModel>> findAllContextRolesBySecuredRecords() { final UserRoleModel example = this.instantiate(); @SuppressWarnings("unchecked") List<UserRoleModel> securedRecordUserRoles = getHibernateTemplate() .execute(new HibernateCallback<List<UserRoleModel>>() { @Override public List<UserRoleModel> doInHibernate(Session session) throws HibernateException, SQLException { Criteria criteria = session.createCriteria(example.getClass()); criteria.setFetchMode("role", FetchMode.JOIN); criteria.add(Restrictions.isNotNull("securedRecord")); return (List<UserRoleModel>) criteria.list(); } }); Map<SecuredRecordModel, List<UserRoleModel>> result = new HashMap<SecuredRecordModel, List<UserRoleModel>>(); if (securedRecordUserRoles != null) { for (UserRoleModel securedRecordUserRole : securedRecordUserRoles) { SecuredRecordModel securedRecord = securedRecordUserRole.getSecuredRecord(); List<UserRoleModel> contextRoles = result.get(securedRecord); if (contextRoles == null) { contextRoles = new ArrayList<UserRoleModel>(); result.put(securedRecord, contextRoles); } contextRoles.add(securedRecordUserRole); Hibernate.initialize(securedRecordUserRole.getSecuredRecord()); Hibernate.initialize(securedRecordUserRole.getRole().getPermissions()); for (PermissionModel permission : securedRecordUserRole.getRole().getPermissions()) { Hibernate.initialize(permission); } } } return result; }
From source file:net.firejack.platform.service.registry.broker.system.ReadSystemListBroker.java
License:Apache License
protected List<SystemModel> getModelList(ServiceRequest<NamedValues> request) throws BusinessFunctionException { Boolean aliases = (Boolean) request.getData().get("aliases"); if (aliases != null) { List<Criterion> criterions = new ArrayList<Criterion>(); if (aliases) { criterions.add(Restrictions.isNotNull("main")); } else {//from ww w . j a v a2 s . com criterions.add(Restrictions.isNull("main")); } return getStore().findAllWithFilter(criterions, getFilter()); } else { return getStore().findAllWithFilter(getFilter()); } }
From source file:net.lunikon.rethul.data.StringsDAO.java
License:Open Source License
/** * Loads all strings of the given file marked as "done", ignoring master * strings.//from ww w. j a v a 2s .c o m * * @param file * The file. * @return a list of {@link LocalizedString}. */ @SuppressWarnings("unchecked") public List<LocalizedString> loadCompletedStrings(File file) { return criteria() // .add(Restrictions.eq("file", file)) // .add(Restrictions.eq("pending", Boolean.FALSE)) // .add(Restrictions.isNotNull("locale")) // .addOrder(Order.asc("key")) // .list(); }
From source file:net.sasasin.sreader.commons.dao.impl.SubscriberDaoHibernateImpl.java
License:Open Source License
@Override public Subscriber getByFeedUrl(FeedUrl f) { Session ses = getSessionFactory().openSession(); // TODO ???????? // TODO ????? Subscriber sub = (Subscriber) ses.createCriteria(Subscriber.class).add(Restrictions.eq("feedUrl", f)) .add(Restrictions.isNotNull("authName")).add(Restrictions.isNotNull("authPassword")) .setMaxResults(1).uniqueResult(); return sub;// www .j av a 2 s . com }
From source file:nl.b3p.viewer.admin.stripes.ChooseApplicationActionBean.java
License:Open Source License
public Resolution getGridData() throws JSONException { JSONArray jsonData = new JSONArray(); String filterName = ""; String filterPublished = ""; String filterOwner = ""; /*//ww w. j a va 2 s. c o m * FILTERING: filter is delivered by frontend as JSON array [{property, * value}] for demo purposes the value is now returned, ofcourse here * should the DB query be built to filter the right records */ if (this.getFilter() != null) { for (int k = 0; k < this.getFilter().length(); k++) { JSONObject j = this.getFilter().getJSONObject(k); String property = j.getString("property"); String value = j.getString("value"); if (property.equals("name")) { filterName = value; } if (property.equals("published")) { filterPublished = value; } if (property.equals("owner")) { filterOwner = value; } } } Session sess = (Session) Stripersist.getEntityManager().getDelegate(); Criteria c = sess.createCriteria(Application.class); /* * Sorting is delivered by the frontend as two variables: sort which * holds the column name and dir which holds the direction (ASC, DESC). */ if (sort != null && dir != null) { Order order = null; if (sort.equals("published")) { sort = "version"; } if (dir.equals("ASC")) { order = Order.asc(sort); } else { order = Order.desc(sort); } order.ignoreCase(); c.addOrder(order); } if (filterName != null && filterName.length() > 0) { Criterion nameCrit = Restrictions.ilike("name", filterName, MatchMode.ANYWHERE); c.add(nameCrit); } if (filterPublished != null && filterPublished.length() > 0) { if (filterPublished.equalsIgnoreCase("nee")) { Criterion publishedCrit = Restrictions.isNotNull("version"); c.add(publishedCrit); } else if (filterPublished.equalsIgnoreCase("ja")) { Criterion publishedCrit = Restrictions.isNull("version"); c.add(publishedCrit); } } if (filterOwner != null && filterOwner.length() > 0) { Criterion ownerCrit = Restrictions.ilike("owner.username", filterOwner, MatchMode.ANYWHERE); c.add(ownerCrit); } int rowCount = c.list().size(); c.setMaxResults(limit); c.setFirstResult(start); List applications = c.list(); for (Iterator it = applications.iterator(); it.hasNext();) { Application app = (Application) it.next(); String appName = app.getName(); if (app.getVersion() != null) { appName += " v" + app.getVersion(); } String ownername = ""; if (app.getOwner() != null) { ownername = app.getOwner().getUsername(); } String published = "Nee"; if (app.getVersion() == null) { published = "Ja"; } JSONObject j = this.getGridRow(app.getId().intValue(), appName, published, ownername); jsonData.put(j); } final JSONObject grid = new JSONObject(); grid.put("totalCount", rowCount); grid.put("gridrows", jsonData); return new StreamingResolution("application/json") { @Override public void stream(HttpServletResponse response) throws Exception { response.getWriter().print(grid.toString()); } }; }
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) {/*ww w .ja v a 2 s . c om*/ 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.model.ConsumerCurator.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional/*from w w w .j ava2s . c o m*/ public List<Consumer> getHypervisorsForOwner(String ownerKey) { return createSecureCriteria().createAlias("owner", "o").add(Restrictions.eq("o.key", ownerKey)) .createAlias("hypervisorId", "hvsr").add(Restrictions.isNotNull("hvsr.hypervisorId")).list(); }
From source file:org.candlepin.model.EntitlementCurator.java
License:Open Source License
/** * For a given stack, find the eldest active entitlement with a subscription ID. * This is used to look up the upstream subscription certificate to use to talk to * the CDN.//from w w w . j a v a2s . c om * * @param consumer the consumer * @param stackId the ID of the stack * @return the eldest active entitlement with a subscription ID, or null if none can * be found. */ public Entitlement findUpstreamEntitlementForStack(Consumer consumer, String stackId) { Date currentDate = new Date(); Criteria activeNowQuery = currentSession().createCriteria(Entitlement.class) .add(Restrictions.eq("consumer", consumer)).createAlias("pool", "ent_pool") .createAlias("ent_pool.productAttributes", "attrs") .add(Restrictions.le("ent_pool.startDate", currentDate)) .add(Restrictions.ge("ent_pool.endDate", currentDate)) .add(Restrictions.eq("attrs.name", "stacking_id")).add(Restrictions.eq("attrs.value", stackId)) .add(Restrictions.isNull("ent_pool.sourceEntitlement")) .createAlias("ent_pool.sourceSubscription", "sourceSub").add(Restrictions.isNotNull("sourceSub.id")) .addOrder(Order.asc("created")) // eldest entitlement .setMaxResults(1); return (Entitlement) activeNowQuery.uniqueResult(); }