public Filter getFilterFromExample(Object example, ExampleOptions options) { if (example == null) return null; if (options == null) options = defaultExampleOptions; List<Filter> filters = new ArrayList<Filter>(); LinkedList<String> path = new LinkedList<String>(); Metadata metadata = metadataUtil.get(example.getClass()); getFilterFromExampleRecursive(example, metadata, options, path, filters); if (filters.size() == 0) { return null; } else if (filters.size() == 1) { return filters.get(0); } else { return new Filter("AND", filters, Filter.OP_AND); } }
/** * Return the all the persistent instances of the given entity class with * the given identifiers. An array of entities is returned that matches the * same order of the ids listed in the call. For each entity that is not * found in the datastore, a null will be inserted in its place in the * return array. */ protected <T> T[] _find(Class<T> type, Serializable... ids) { Object[] retList = (Object[]) Array.newInstance(type, ids.length); for (Object entity : pullByIds("select _it_", type, ids)) { Serializable id = getMetadataUtil().getId(entity); for (int i = 0; i < ids.length; i++) { if (id.equals(ids[i])) { retList[i] = entity; // don't break. the same id could be in the list twice. } } } return (T[]) retList; }
/** * Returns the number of instances of this entity in the datastore. */ protected int _count(Class<?> type) { return ((Number) em().createQuery("select count(_it_) from " + getMetadataUtil().get(type).getEntityName() + " _it_").getSingleResult()).intValue(); }
/** * Return the all the persistent instances of the given entity class with * the given identifiers. An array of entities is returned that matches the * same order of the ids listed in the call. For each entity that is not * found in the datastore, a null will be inserted in its place in the * return array. */ protected <T> T[] _find(Class<T> type, Serializable... ids) { Object[] retList = (Object[]) Array.newInstance(type, ids.length); for (Object entity : pullByIds("select _it_", type, ids)) { Serializable id = getMetadataUtil().getId(entity); for (int i = 0; i < ids.length; i++) { if (id.equals(ids[i])) { retList[i] = entity; // don't break. the same id could be in the list twice. } } } return (T[]) retList; }
/** * Returns the number of instances of this entity in the datastore. */ protected int _count(Class<?> type) { return ((Number) em().createQuery("select count(_it_) from " + getMetadataUtil().get(type).getEntityName() + " _it_").getSingleResult()).intValue(); }
protected boolean _exists(Object entity) { if (entity == null) return false; if (em().contains(entity)) return true; return _exists(entity.getClass(), getMetadataUtil().getId(entity)); }
protected boolean _exists(Class<?> type, Serializable id) { if (type == null) throw new NullPointerException("Type is null."); if (!validId(id)) return false; Query query = em().createQuery("select _it_.id from " + getMetadataUtil().get(type).getEntityName() + " _it_ where _it_.id = :id"); query.setParameter("id", id); return query.getResultList().size() == 1; }
protected boolean _exists(Object entity) { if (entity == null) return false; if (em().contains(entity)) return true; return _exists(entity.getClass(), getMetadataUtil().getId(entity)); }
protected boolean _exists(Class<?> type, Serializable id) { if (type == null) throw new NullPointerException("Type is null."); if (!validId(id)) return false; Query query = em().createQuery("select _it_.id from " + getMetadataUtil().get(type).getEntityName() + " _it_ where _it_.id = :id"); query.setParameter("id", id); return query.getResultList().size() == 1; }
/** * Remove the specified entity from the datastore. * * @return <code>true</code> if the object is found in the datastore and * removed, <code>false</code> if the item is not found. */ protected boolean _removeEntity(Object entity) { if (entity != null) { if (em().contains(entity)) { em().remove(entity); return true; } else { Serializable id = getMetadataUtil().getId(entity); return _removeById(entity.getClass(), id); } } return false; }
/** * Get a list of all the entities of the specified class. */ protected <T> List<T> _all(Class<T> type) { return em().createQuery("select _it_ from " + getMetadataUtil().get(type).getEntityName() + " _it_").getResultList(); }
/** * Remove the specified entity from the datastore. * * @return <code>true</code> if the object is found in the datastore and * removed, <code>false</code> if the item is not found. */ protected boolean _removeEntity(Object entity) { if (entity != null) { if (em().contains(entity)) { em().remove(entity); return true; } else { Serializable id = getMetadataUtil().getId(entity); return _removeById(entity.getClass(), id); } } return false; }
private List<?> pullByIds(String select, Class<?> type, Serializable[] ids) { List<Serializable> nonNulls = new LinkedList<Serializable>(); StringBuilder sb = new StringBuilder(select); sb.append(" from "); sb.append(getMetadataUtil().get(type).getEntityName()); sb.append(" _it_ where "); for (Serializable id : ids) { if (id != null) { if (nonNulls.size() == 0) sb.append("_it_.id = ?1"); else sb.append(" or _it_.id = ?").append(nonNulls.size() + 1); nonNulls.add(id); } } if (nonNulls.size() == 0) return new ArrayList<Object>(0); Query query = em().createQuery(sb.toString()); int idx = 1; for (Serializable id : nonNulls) { query.setParameter(idx++, id); } return query.getResultList(); }
/** * If an entity with the same ID already exists in the database, merge the * changes into that entity. If not persist the given entity. In either * case, a managed entity with the changed values is returned. It may or may * not be the same object as was passed in. */ protected <T> T _persistOrMerge(T entity) { if (entity == null) return null; if (em().contains(entity)) return entity; Serializable id = getMetadataUtil().getId(entity); if (!validId(id)) { _persist(entity); return entity; } T prev = em().find((Class<T>) entity.getClass(), id); if (prev == null) { _persist(entity); return entity; } else { return _merge(entity); } }
private List<?> pullByIds(String select, Class<?> type, Serializable[] ids) { List<Serializable> nonNulls = new LinkedList<Serializable>(); StringBuilder sb = new StringBuilder(select); sb.append(" from "); sb.append(getMetadataUtil().get(type).getEntityName()); sb.append(" _it_ where "); for (Serializable id : ids) { if (id != null) { if (nonNulls.size() == 0) sb.append("_it_.id = ?1"); else sb.append(" or _it_.id = ?").append(nonNulls.size() + 1); nonNulls.add(id); } } if (nonNulls.size() == 0) return new ArrayList<Object>(0); Query query = em().createQuery(sb.toString()); int idx = 1; for (Serializable id : nonNulls) { query.setParameter(idx++, id); } return query.getResultList(); }
/** * If an entity with the same ID already exists in the database, merge the * changes into that entity. If not persist the given entity. In either * case, a managed entity with the changed values is returned. It may or may * not be the same object as was passed in. */ protected <T> T _persistOrMerge(T entity) { if (entity == null) return null; if (em().contains(entity)) return entity; Serializable id = getMetadataUtil().getId(entity); if (!validId(id)) { _persist(entity); return entity; } T prev = em().find((Class<T>) entity.getClass(), id); if (prev == null) { _persist(entity); return entity; } else { return _merge(entity); } }
/** * Get a list of all the entities of the specified class. */ protected <T> List<T> _all(Class<T> type) { return em().createQuery("select _it_ from " + getMetadataUtil().get(type).getEntityName() + " _it_").getResultList(); }
/** * Internal method for generating from clause. This method should be called * after generating other clauses because it relies on the aliases they * create. This method takes every path that is called for in the other * clauses and makes it available as an alias using left joins. It also adds * join fetching for properties specified by <code>fetches</code> if * <code>doEagerFetching</code> is <code>true</code>. <b>NOTE:</b> When * using eager fetching, <code>applyFetches()</code> must be executed first. */ protected String generateFromClause(SearchContext ctx, boolean doEagerFetching) { StringBuilder sb = new StringBuilder(" from "); sb.append(getMetadataUtil().get(ctx.rootClass).getEntityName()); sb.append(" "); sb.append(ctx.getRootAlias()); sb.append(generateJoins(ctx, doEagerFetching)); return sb.toString(); }
/** * Remove the entity of the specified class with the specified id from the * datastore. * * @return <code>true</code> if the object is found in the datastore and * deleted, <code>false</code> if the item is not found. */ protected boolean _removeById(Class<?> type, Serializable id) { if (id != null) { Query query = em().createQuery("select _it_.id from " + getMetadataUtil().get(type).getEntityName() + " _it_ where _it_.id = ?1").setParameter(1, id); if (query.getResultList().size() != 0) { em().remove(em().getReference(type, id)); return true; } } return false; }