private List<String> runQuery(String queryString, String input) { String formattedInput = String.format(PhenotipsFamilyExport.INPUT_FORMAT, input.toLowerCase()); // Query patients Query query = null; List<String> queryResults = null; try { query = this.qm.createQuery(queryString, Query.XWQL); query.bindValue(PhenotipsFamilyExport.INPUT_PARAMETER, formattedInput); queryResults = query.execute(); } catch (QueryException e) { this.logger.error("Error while performing patients/families query: [{}] ", e.getMessage()); return Collections.emptyList(); } return queryResults; }
/** * For a given label and its corresponding id value (fields on {@code LabeledIdentifierClass} objects), query * patient documents. * * @param label the name of the label * @param id the id value for the label * @return an empty list if no patients have the label identifier and its corresponding id value, else a list of * patient internal identifiers * @throws QueryException if there is any error during querying */ private List<String> queryPatientsByLabelAndEid(String label, String id) throws QueryException { Query q = null; try { q = this.qm.createQuery( "select doc.name from Document doc, doc.object(PhenoTips.LabeledIdentifierClass) obj " + "where obj.label = :label and obj.value = :value", Query.XWQL); q.bindValue(KEY_LABEL, label); q.bindValue(KEY_VALUE, id); return q.execute(); } catch (QueryException ex) { this.logger.warn("Failed to query patient documents with label [{}] and corresponding external ID [{}]: {}", label, id, ex.getMessage(), ex); throw new QueryException(ex.getMessage(), q, ex); } }
public Response toResponse(QueryException exception) { return Response.serverError().entity( String.format("%s\n%s\n", exception.getMessage(), exception.getCause().getMessage())).type( MediaType.TEXT_PLAIN).build(); }
@Override public Query createQuery(String statement, String language) throws QueryException { if (hasLanguage(language)) { return new DefaultQuery(statement, language, getQueryExecutorManager()); } else { throw new QueryException("Language [" + language + "] is not supported", null, null); } }
@Override public <T> List<T> execute(Query query) throws QueryException { if (query instanceof SecureQuery) { SecureQuery secureQuery = (SecureQuery) query; // Force checking current author rights secureQuery.checkCurrentAuthor(true); } else if (!this.authorization.hasAccess(Right.PROGRAM)) { throw new QueryException("Unsecure query require programming right", query, null); } return this.defaultQueryExecutorManager.execute(query); }
/** * Checks whether the global configuration allows for other eids to be set arbitrarily by the user for each patient, * by looking if there is a {@code PhenoTips.LabeledIdentifierGlobalSettings} object with {@code 0} set for its * {@code allowOtherEids} property. * * @return {@code false} if custom labels are explicitly denied, {@code true} otherwise */ private boolean allowOtherEids() { Query q = null; try { q = this.qm.createQuery("select obj.allowOtherEids from Document doc, doc.object(" + "PhenoTips.LabeledIdentifierGlobalSettings) obj where doc.fullName = 'XWiki.XWikiPreferences'", Query.XWQL); List<Integer> results = q.execute(); if (results == null || results.isEmpty()) { this.logger.debug("There should be one LabeledIdentifierGlobalSettings object. None were found."); return true; } return results.get(0) != 0; } catch (QueryException ex) { this.logger.warn("Failed to query LabeledIdentifierGlobalSettings object: {}", ex.getMessage(), ex); return true; } }
@Override public <T> List<T> execute(Query query) throws QueryException { if (query.isNamed()) { return this.namedQueryExecutorProvider.get().execute(query); } else { try { return this.componentManagerProvider.get() .<QueryExecutor>getInstance(QueryExecutor.class, query.getLanguage()).execute(query); } catch (ComponentLookupException e) { throw new QueryException("Fail to lookup query executor", query, e); } } }
@Override protected long getLastUsedId() { this.logger.debug("getLastUsedId()"); long crtMaxID = 0; try { Query q = this.qm.createQuery("select family.identifier " + "from Document doc, " + " doc.object(PhenoTips.FamilyClass) as family " + "where family.identifier is not null " + "order by family.identifier desc", Query.XWQL).setLimit(1); List<Long> crtMaxIDList = q.execute(); if (crtMaxIDList.size() > 0 && crtMaxIDList.get(0) != null) { crtMaxID = crtMaxIDList.get(0); } crtMaxID = Math.max(crtMaxID, 0); } catch (QueryException ex) { this.logger.warn("Failed to get the last used identifier: {}", ex.getMessage()); } return crtMaxID; }
/** * {@inheritDoc} */ public <T> List<T> execute(final Query query) throws QueryException { String olddatabase = getContext().getDatabase(); try { if (query.getWiki() != null) { getContext().setDatabase(query.getWiki()); } return getStore().executeRead(getContext(), true, new HibernateCallback<List<T>>() { @SuppressWarnings("unchecked") public List<T> doInHibernate(Session session) { org.hibernate.Query hquery = createHibernateQuery(session, query); populateParameters(hquery, query); return hquery.list(); } }); } catch (XWikiException e) { throw new QueryException("Exception while execute query", query, e); } finally { getContext().setDatabase(olddatabase); } }
/** * Gets a list of patient internal IDs. * * @param label the name of the label * @param id the id value for the label * @return null if the label is invalid, an empty list if no patients have the label identifier and its * corresponding id value, else a list of patient internal IDs */ private List<String> getPatientInternalIdentifiersByLabelAndEid(String label, String id) { try { // Check global configs first before querying all labeled identifier objects on all patients, much cheaper if (isLabelAllowed(label)) { return queryPatientsByLabelAndEid(label, id); } else { return Collections.emptyList(); } } catch (QueryException ex) { this.logger.warn("Failed to query patient with label [{}] and corresponding external ID [{}]: {}", label, id, ex.getMessage(), ex); throw new WebApplicationException(Response.Status.NOT_FOUND); } }
@Override public Response fetchPatients() { final Request request = this.container.getRequest(); // Get the internal and external IDs, if provided. final List<Object> eids = request.getProperties("eid"); final List<Object> ids = request.getProperties("id"); this.slf4Jlogger.debug("Retrieving patient records with external IDs [{}] and internal IDs [{}]", eids, ids); // Build a set of patients from the provided external and/or internal ID data. final ImmutableSet.Builder<PrimaryEntity> patientsBuilder = ImmutableSet.builder(); try { addEids(patientsBuilder, eids); addIds(patientsBuilder, ids); // Generate JSON for all retrieved patients. final String json = this.objectMapper.writeValueAsString(patientsBuilder.build()); return Response.ok(json, MediaType.APPLICATION_JSON_TYPE).build(); } catch (final JsonProcessingException ex) { this.slf4Jlogger.error("Failed to serialize patients [{}] to JSON: {}", eids, ex.getMessage()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (final QueryException ex) { this.slf4Jlogger.error("Failed to retrieve patients with external ids [{}]: {}", eids, ex.getMessage()); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } }
@Override protected long getLastUsedId() { long crtMaxID = 0; try { Query q = this.qm.createQuery( "select patient.identifier from Document doc, doc.object(PhenoTips.PatientClass) as patient" + " where patient.identifier is not null order by patient.identifier desc", Query.XWQL).setLimit(1); List<Long> crtMaxIDList = q.execute(); if (!crtMaxIDList.isEmpty() && crtMaxIDList.get(0) != null) { crtMaxID = crtMaxIDList.get(0); } crtMaxID = Math.max(crtMaxID, 0); } catch (QueryException ex) { this.logger.warn("Failed to get the last used identifier: {}", ex.getMessage()); } return crtMaxID; } }
private Response checkForMultipleRecords(Patient patient, String eid) { try { Query q = this.qm.createQuery("where doc.object(PhenoTips.PatientClass).external_id = :eid", Query.XWQL); q.bindValue("eid", eid); List<String> results = q.execute(); if (results.size() > 1) { this.slf4Jlogger.debug("Multiple patient records ({}) with external ID [{}]: {}", results.size(), eid, results); Alternatives response = this.factory.createAlternatives(results, this.uriInfo); return Response.status(300).entity(response).build(); } } catch (QueryException ex) { this.slf4Jlogger.warn("Failed to retrieve patient with external id [{}]: {}", eid, ex.getMessage()); } if (patient == null) { this.slf4Jlogger.debug("No patient record with external ID [{}] exists yet", eid); return Response.status(Status.NOT_FOUND).build(); } return null; }
@Override protected long getLastUsedId() { long crtMaxID = 0; try { Query q = this.qm.createQuery( "select patient.identifier from Document doc, doc.object(PhenoTips.PatientClass) as patient" + " where patient.identifier is not null order by patient.identifier desc", Query.XWQL).setLimit(1); List<Long> crtMaxIDList = q.execute(); if (!crtMaxIDList.isEmpty() && crtMaxIDList.get(0) != null) { crtMaxID = crtMaxIDList.get(0); } crtMaxID = Math.max(crtMaxID, 0); } catch (QueryException ex) { this.logger.warn("Failed to get the last used identifier: {}", ex.getMessage()); } return crtMaxID; } }
this.logger.error("Error while performing patients query: [{}] ", e.getMessage()); return Collections.emptyList();
@Override public void deleteMessage(String id) { Query q; try { q = this.qm.createQuery("where event.id = :id", Query.XWQL); q.bindValue("id", id); List<Event> events = this.stream.searchEvents(q); if (events == null || events.isEmpty()) { throw new IllegalArgumentException("This message does not exist"); } else if (events.get(0).getUser().equals(this.bridge.getCurrentUserReference())) { this.stream.deleteEvent(events.get(0)); } else { throw new IllegalArgumentException("You are not authorized to delete this message"); } } catch (QueryException ex) { LOG.warn("Failed to delete message: {}", ex.getMessage()); } }
private List<String> runQuery(String queryString, String input, int resultsLimit) { String formattedInput = String.format(PhenotipsFamilyExport.INPUT_FORMAT, input); // Query patients Query query = null; List<String> queryResults = null; try { query = this.qm.createQuery(queryString, Query.XWQL); query.setLimit(resultsLimit); query.bindValue(PhenotipsFamilyExport.INPUT_PARAMETER, formattedInput); queryResults = query.execute(); } catch (QueryException e) { this.logger.error("Error while performing patiets query: [{}] ", e.getMessage()); return Collections.emptyList(); } return queryResults; }
/** * Checks if the label in question is configured at the administrative level, which means that the label is * available globally for all patients. If it is, then it will be the {@code label} value of a * {@code PhenoTips.LabeledIdentifierSettings} object. * * @param label the name of the label * @return {@code true} if the label is configured, {@code false} otherwise */ private boolean isLabelConfiguredByAdmin(String label) { Query q = null; try { q = this.qm.createQuery( "select obj.label from Document doc, doc.object(PhenoTips.LabeledIdentifierSettings) obj " + "where doc.fullName = 'XWiki.XWikiPreferences' and obj.label = :label", Query.XWQL); q.bindValue(KEY_LABEL, label); List<String> results = q.execute(); return !results.isEmpty(); } catch (QueryException ex) { this.logger.warn("Failed to query LabeledIdentifierSettings object: {}", ex.getMessage(), ex); return false; } }
protected long getLastUsedId() { long crtMaxID = 0; try { Query q = this.qm.createQuery( "select doc.name from Document doc, doc.object(" + this.localSerializer.serialize(getEntityXClassReference()) + ") as entity where doc.space = :space order by doc.name desc", Query.XWQL).bindValue("space", this.getDataSpace().getName()).setLimit(1); List<String> crtMaxIDList = q.execute(); if (!crtMaxIDList.isEmpty() && crtMaxIDList.get(0) != null) { crtMaxID = Integer.parseInt(crtMaxIDList.get(0).replaceAll("\\D++", "")); } crtMaxID = Math.max(crtMaxID, 0); } catch (QueryException ex) { this.logger.warn("Failed to get the last used identifier: {}", ex.getMessage()); } return crtMaxID; }
protected long getLastUsedId() { long crtMaxID = 0; try { Query q = this.qm.createQuery( "select doc.name from Document doc, doc.object(" + this.localSerializer.serialize(getEntityXClassReference()) + ") as entity where doc.space = :space order by doc.name desc", Query.XWQL).bindValue("space", this.getDataSpace().getName()).setLimit(1); List<String> crtMaxIDList = q.execute(); if (!crtMaxIDList.isEmpty() && crtMaxIDList.get(0) != null) { crtMaxID = Integer.parseInt(crtMaxIDList.get(0).replaceAll("\\D++", "")); } crtMaxID = Math.max(crtMaxID, 0); } catch (QueryException ex) { this.logger.warn("Failed to get the last used identifier: {}", ex.getMessage()); } return crtMaxID; }