private void ids(Criteria criteria, QueryBuilder qb, String path) { if (values.onlyIds != null) { if (criteria != null) { criteria.add(Restrictions.in("id", values.onlyIds)); } if (qb != null) { String unique = qb.unique_alias("ids"); qb.and(String.format("%sid in (:%s) ", path, unique)); qb.paramList(unique, values.onlyIds); } } }
/** * Adds a single clause of the form "AND (A OR B ...)" where each * {@link ScriptFileType} A, B, etc. is given a chance to define * its own clause. */ public void buildQuery(QueryBuilder qb) { boolean first = true; qb.and(" ("); // will prepend "AND" if not first clause. for (String mimetype : mimetypes) { if (first) { first = false; } else { qb.append(" OR "); } qb.append("o.mimetype = '" + mimetype + "'"); } qb.append(") "); }
private void applyOwnerGroup(final Parameters p, QueryBuilder qb, String ownerPath, String groupPath) { if (p != null && p.theFilter != null) { Filter f = p.theFilter; if (f.ownerId != null) { qb.and(ownerPath + " = :owner_id "); qb.param("owner_id", f.ownerId.getValue()); } if (f.groupId != null) { qb.and(groupPath + " = :group_id "); qb.param("group_id", f.groupId.getValue()); } } }
public static void notNullOrLikeOrEqual(QueryBuilder qb, String path, Class type, Object value, boolean useLike, boolean caseSensitive) { if (null == value) { qb.and(path + " is null "); } else { String operator; if (useLike && String.class.isAssignableFrom(type)) { if (caseSensitive) { operator = "like"; } else { operator = "ilike"; } } else { operator = "="; } String alias = qb.unique_alias("main"); qb.and(path); qb.append(operator); qb.append(":"); qb.append(alias); qb.appendSpace(); qb.param(alias, value); } }
@RolesAllowed("user") public List<Long> getLeaderOfGroupIds(final Experimenter e) { Assert.notNull(e); Assert.notNull(e.getId()); final QueryBuilder qb = new QueryBuilder(); qb.select("g.id").from("ExperimenterGroup", "g"); qb.join("g.groupExperimenterMap", "m", false, false); qb.where(); qb.and("m.owner = true"); qb.and("m.parent.id = g.id"); qb.and("m.child.id = :id"); qb.param("id", e.getId()); List<Long> groupIds = iQuery.execute(new HibernateCallback<List<Long>>() { public List<Long> doInHibernate(Session session) throws HibernateException, SQLException { org.hibernate.Query q = qb.query(session); return (List<Long>) q.list(); } }); return groupIds; }
/** * Build a query string based on the current {@link #excludes} {@link List}. * The query expects a single :id parameter to be set on execution. The * {@link #excludes} list is used to filter out unwanted {@link EventLog} * instances. */ private void initQueryString() { List<String> copy = excludes; // Instead of synchronizing QueryBuilder qb = new QueryBuilder(); qb.select("el"); qb.from("EventLog", "el"); qb.where(); qb.and("el.id > :id"); if (copy != null) { for (String exclude : copy) { qb.and("el.entityType != '" + exclude + "'"); } } qb.order("id", true); query = qb.queryString(); }
private void annotatedBetween(AnnotationCriteria ann, QueryBuilder qb, String path) { if (values.annotatedStart != null) { if (ann != null) { ann.getCreate().add( Restrictions .gt("anncreate.time", values.annotatedStart)); } if (qb != null) { String astart = qb.unique_alias("astart"); qb.and(path + "details.creationEvent.time > :" + astart); qb.param(astart, values.annotatedStart); } } if (values.annotatedStop != null) { if (ann != null) { ann.getCreate() .add( Restrictions.lt("anncreate.time", values.annotatedStop)); } if (qb != null) { String astop = qb.unique_alias("astop"); qb.and(path + "details.creationEvent.time < :" + astop); qb.param(astop, values.annotatedStop); } } }
@Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { QueryBuilder qb = new QueryBuilder(); qb.select("distinct fa"); qb.from("Image", "i"); qb.append(", Roi roi "); qb.join("roi.annotationLinks", "rlinks", false, false); qb.join("rlinks.child", "rfa", false, false); qb.join("i.wellSamples", "ws", false, false); qb.join("ws.well", "well", false, false); qb.join("well.plate", "plate", false, false); qb.join("plate.annotationLinks", "links", false, false); qb.join("links.child", "fa", false, false); qb.where(); qb.and("fa.ns = '" + NSMEASUREMENT.value + "'"); qb.and("rfa.id = fa.id"); qb.and("i.id = :id"); qb.and("i.id = roi.image"); qb.param("id", imageId); qb.filter("fa", filter(opts)); return qb.query(session).list(); } }));
private boolean parseAcceptsList(final QueryBuilder qb, final List<IObject> acceptsList) { qb.where(); scripts.buildQuery(qb); if (acceptsList != null && acceptsList.size() > 0) { for (IObject object : acceptsList) { if (object instanceof Experimenter) { qb.and("o.details.owner.id = :oid"); qb.param("oid", object.getId().getValue()); } else if (object instanceof ExperimenterGroup) { qb.and("o.details.group.id = :gid"); qb.param("gid", object.getId().getValue()); } else { throw new ome.conditions.ValidationException( "Unsupported accept-type: " + object); } } return true; } return false; }
public Object call() throws Exception { final QueryBuilder qb = new QueryBuilder(); qb.select("o").from("OriginalFile", "o"); if (!parseAcceptsList(qb, acceptsList)) { long gid = factory.sessionManager .getEventContext(factory.principal) .getCurrentGroupId(); qb.and("o.details.group.id = " + gid); } List<Long> officialIds = scripts.idsInDb(); if (officialIds != null && officialIds.size() > 0) { qb.and("o.id not in (:ids) "); qb.paramList("ids", officialIds); } List<OriginalFile> files = (List<OriginalFile>) factory.executor.execute(__current.ctx, factory.principal, new Executor.SimpleWork(this, "getUserScripts") { @Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { return qb.query(session).list(); } }); IceMapper mapper = new IceMapper(); return mapper.map(files); } });
@Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { final Filter f = filter(opts); final QueryBuilder qb = new QueryBuilder(); qb.select("distinct r").from("Roi", "r"); qb.join("r.shapes", "s", false, true); // fetch qb.join("r.folderLinks", "folderLinks", true, true); // fetch qb.join("folderLinks.parent", "folder", true, true); // fetch qb.join("r.image", "i", false, false); qb.where(); qb.and("i.id = :id"); qb.and(" ( s.theZ is null or s.theZ = :z ) "); qb.and(" ( s.theT is null or s.theT = :t ) "); qb.filter("r", f); qb.filterNow(); qb.order("r.id", true); // ascending qb.param("id", imageId); qb.param("z", z); qb.param("t", t); return qb.queryWithoutFilter(session).list(); } }));
@Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { QueryBuilder qb = new QueryBuilder(); qb.select("f"); qb.from("FileAnnotation", "fa"); qb.join("fa.file", "f", false, false); qb.where(); qb.and("fa.id = :id"); qb.param("id", annotationId); OriginalFile file = (OriginalFile) qb.query(session) .uniqueResult(); if (file == null) { throw new ome.conditions.ApiUsageException("No such file annotation: " + annotationId); } return file.getId(); } }) {
protected IObject get(Session session, Class type, long id) { QueryBuilder qb = new QueryBuilder(); qb.select("this").from(type.getName(), "this"); if (IAnnotated.class.isAssignableFrom(type)) { qb.join("this.annotationLinks", "l1", true, true); qb.join("l1.child", "a1", true, true); qb.join("a1.annotationLinks", "l2", true, true); qb.join("l2.child", "a2", true, true); } if (!IGlobal.class.isAssignableFrom(type)) { if (IMutable.class.isAssignableFrom(type)) { qb.join("this.details.updateEvent", "update", false, true); } qb.join("this.details.creationEvent", "create", false, true); qb.join("this.details.owner", "owner", false, true); qb.join("this.details.group", "group", false, true); } qb.where().and("this.id = :id"); qb.param("id", id); return (IObject) qb.query(session).uniqueResult(); } }
void on(QueryBuilder qb, boolean equals) { check(); String op = equals ? "=" : "!="; String unique = qb.unique_alias("owner"); qb.and(path); qb.append(" "); qb.append(op); qb.append(" :"); qb.append(unique); qb.appendSpace(); qb.param(unique, id); } }
@Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { final Filter f = filter(opts); final QueryBuilder qb = new QueryBuilder(); qb.select("distinct r").from("Roi", "r"); qb.join("r.image", "i", false, false); qb.join("r.shapes", "shapes", false, true); // fetch qb.join("r.folderLinks", "folderLinks", true, true); // fetch qb.join("folderLinks.parent", "folder", true, true); // fetch qb.where(); qb.and("i.id = :id"); qb.filter("r", f); qb.filterNow(); qb.order("r.id", true); // ascending qb.param("id", imageId); return qb.queryWithoutFilter(session).list(); } }));
private OriginalFile getScriptId(final Job job, final Ice.Current current) throws omero.ValidationException { final QueryBuilder qb = new QueryBuilder(); qb.select("o").from("Job", "j"); qb.join("j.originalFileLinks", "links", false, false); qb.join("links.child", "o", false, false); qb.where(); qb.and("j.id = :id").param("id", job.getId().getValue()); scriptRepoHelper.buildQuery(qb); final Map<String, String> ctx = new HashMap<String, String>(); ctx.putAll(current.ctx); ctx.put("omero.group", "-1"); final OriginalFile f = (OriginalFile) this.ex.execute(ctx, this.principal, new Executor.SimpleWork(this, "getScriptId") { @Transactional(readOnly = true) public Object doWork(org.hibernate.Session session, ServiceFactory sf) { return qb.query(session).uniqueResult(); } }); if (f == null) { throw new omero.ValidationException(null, null, "No script for job :" + job.getId().getValue()); } return f; }
/** * In order to support the order() method in addition * to a filter, we allow applying the filter and nulling * the instance eagerly before the user calls order. */ public QueryBuilder filterNow() { if (filter != null && filterTarget != null) { if (filter.owner() >= 0) { this.and(filterTarget+".details.owner.id = "); String alias = this.unique_alias("owner"); this.append(":"); this.append(alias); this.param(alias, filter.owner()); appendSpace(); } if (filter.group() >= 0) { this.and(filterTarget+".details.group.id = "); String alias = this.unique_alias("group"); this.append(":"); this.append(alias); this.param(alias, filter.group()); appendSpace(); } } return this; }
/** * Method to get the original file of the script with id. This method will * not throw an exception, but instead will return null. * * @param name * See above. * @return original file or null if script does not exist or more than one * script with name exists. */ private OriginalFile getOriginalFileOrNull(long id, final Ice.Current current) { try { final QueryBuilder qb = new QueryBuilder(); qb.select("o").from("OriginalFile", "o"); qb.where(); scripts.buildQuery(qb); qb.and("o.id = :id"); qb.param("id", id); OriginalFile file = (OriginalFile) factory.executor.execute( current.ctx, factory.principal, new Executor.SimpleWork(this, "getOriginalFileOrNull", id) { @Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { return qb.query(session).uniqueResult(); } }); return file; } catch (RuntimeException re) { return null; } }
/** * If the current user is not an admin, then this methods adds a subclause * to the HQL: * * AND ( share.owner.id = :userId or user.id = :userId ) * * {@link QueryBuilder#where()} should already have been called. */ protected void applyIfShareAccessible(QueryBuilder qb) { EventContext ec = getSecuritySystem().getEventContext(); if ( ! ec.isCurrentUserAdmin()) { qb.param("userId", ec.getCurrentUserId()); qb.and("("); qb.append("share.owner.id = :userId" ); qb.append(" OR "); qb.append("user.id = :userId" ); qb.append(" ) "); } }
public Object call() throws Exception { final boolean official = acceptsList != null && acceptsList.size() == 0; final QueryBuilder qb = new QueryBuilder(); qb.select("o").from("Job", "j"); qb.join("j.originalFileLinks", "links", false, false); qb.join("links.child", "o", false, false); parseAcceptsList(qb, acceptsList); qb.and("j.id = :id"); qb.param("id", j.getId().getValue()); OriginalFile file = (OriginalFile) factory.executor.execute(__current.ctx, factory.principal, new Executor.SimpleWork(this, "validateScript", j.getId().getValue(), acceptsList) { @Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { List<OriginalFile> files = (List<OriginalFile>) qb.query(session).list(); if (files.size() != 1) { throw new ome.conditions.ValidationException("Found wrong number of files: " + files); } Long id = files.get(0).getId(); if (official) { return scripts.load(id, session,getSqlAction(), true); } else { return sf.getQueryService() .get(OriginalFile.class, id); } } }); return new IceMapper().map(file); } });