@Override public boolean setPatientConsents(Patient patient, Iterable<String> consents) { try { List<Consent> existingConsents = this.selectFromSystem(consents); SaveablePatientConsentHolder holder = this.getPatientConsentHolder(patient); holder.setConsents(convertToIds(existingConsents)); holder.save(); return true; } catch (Exception ex) { this.logger.error("Could not update consents in patient record {}. {}", patient, ex.getMessage()); } return false; }
@Override public Set<Consent> getMissingConsentsForPatient(Patient patient) { return this.getConsentsForPatient(patient, false); }
@Override public boolean hasConsent(Patient patient, String consentId) { if (patient == null || !isValidConsentId(consentId)) { return false; } Set<Consent> missingPatientConsents = getMissingConsentsForPatient(patient); if (missingPatientConsents == null) { return false; } for (Consent consent : missingPatientConsents) { if (consent.getId().equals(consentId)) { return false; } } return true; }
@SuppressWarnings("unchecked") private Consent fromXWikiConsentConfiguration(BaseObject xwikiConsent, XWikiDocument configDoc) { try { String id = xwikiConsent.getStringValue("id"); String label = cleanDescription( configDoc.display("label", RENDERING_MODE, xwikiConsent, this.contextProvider.get()), true); if (label == null || label.length() == 0) { label = id + " " + this.translationManager.translate("phenotips.consents.label.empty"); } String description = cleanDescription( configDoc.display("description", RENDERING_MODE, xwikiConsent, this.contextProvider.get()), false); boolean required = intToBool(xwikiConsent.getIntValue("required")); boolean affectsFields = intToBool(xwikiConsent.getIntValue("affectsFields")); List<String> formFields = null; List<String> dataFields = null; if (affectsFields) { dataFields = new LinkedList<>(); formFields = xwikiConsent.getListValue(FIELDS); fetchConsentFields(formFields, dataFields); } return new DefaultConsent(id, label, description, required, dataFields, formFields); } catch (Exception ex) { this.logger.error("A patient consent is improperly configured: {}", ex.getMessage()); } return null; }
private Set<Consent> getConsentsForPatient(Patient patient, boolean includeGranted) { if (patient == null) { return null; } // List of consent ids a patient has agreed to, read from the database Set<String> xwikiPatientConsents = readConsentIdsFromPatientDoc(patient); Set<Consent> returnedConsents = new LinkedHashSet<>(); // Using system consents to ignore consents set for the patient but no longer configured in the system // (it is faster to check contains() in a set, so iterating through the list and checking the set) Set<Consent> systemConsents = getSystemConsents(); for (Consent systemConsent : systemConsents) { if (xwikiPatientConsents.contains(systemConsent.getId())) { if (includeGranted) { Consent copy = systemConsent.copy(ConsentStatus.YES); returnedConsents.add(copy); } } else { Consent copy = systemConsent.copy(ConsentStatus.NO); returnedConsents.add(copy); } } return returnedConsents; }
/** * @param grant if true will grant the consent, otherwise will revoke * @return if operation was successful */ private boolean manageConsent(Patient patient, String consentId, boolean grant) { if (!this.isValidConsentId(consentId)) { this.logger.error("Invalid consent id ({}) was supplied", consentId); return false; } try { SaveablePatientConsentHolder consentHolder = this.getPatientConsentHolder(patient); List<String> currentConsents = consentHolder.getConsents(); if (grant) { if (!currentConsents.contains(consentId)) { currentConsents.add(consentId); } } else { currentConsents.remove(consentId); } consentHolder.setConsents(currentConsents); consentHolder.save(); return true; } catch (Exception ex) { this.logger.error("Could not update consent {} in patient record {}. {}", consentId, patient.getId(), ex.getMessage()); return false; } }
@SuppressWarnings("unchecked") private Consent fromXWikiConsentConfiguration(BaseObject xwikiConsent, XWikiDocument configDoc) { try { String id = xwikiConsent.getStringValue("id"); String label = cleanDescription( configDoc.display("label", RENDERING_MODE, xwikiConsent, this.contextProvider.get()), true); if (label == null || label.length() == 0) { label = id + " " + this.translationManager.translate("PhenoTips.PatientConsentManager_emptyLabelPostfix"); } String description = cleanDescription( configDoc.display("description", RENDERING_MODE, xwikiConsent, this.contextProvider.get()), false); boolean required = intToBool(xwikiConsent.getIntValue("required")); boolean affectsFields = intToBool(xwikiConsent.getIntValue("affectsFields")); List<String> formFields = null; if (affectsFields) { formFields = xwikiConsent.getListValue("fields"); } return new DefaultConsent(id, label, description, required, formFields); } catch (Exception ex) { this.logger.error("A patient consent is improperly configured: {}", ex.getMessage()); } return null; }
@Override public boolean isValidConsentId(String consentId) { Set<Consent> systemConsents = getSystemConsents(); for (Consent consent : systemConsents) { if (StringUtils.equals(consentId, consent.getId())) { return true; } } return false; }
@Override public Set<Consent> getAllConsentsForPatient(String patientId) { return this.getAllConsentsForPatient(this.repository.get(patientId)); }
private SaveablePatientConsentHolder getPatientConsentHolder(Patient patient) throws Exception { XWikiDocument patientDoc = patient.getXDocument(); return new SaveablePatientConsentHolder(getXWikiConsentHolder(patientDoc), patientDoc, this.contextProvider.get()); }
@Override public Set<Consent> getMissingConsentsForPatient(String patientId) { return this.getMissingConsentsForPatient(this.repository.get(patientId)); }
private synchronized void updateSystemConsentCache(XWikiDocument configDoc) { try { Set<Consent> consents = new LinkedHashSet<>(); List<BaseObject> consentObjects = configDoc.getXObjects(this.consentReference); if (consentObjects != null) { for (BaseObject consentObject : consentObjects) { Consent nextConsent = fromXWikiConsentConfiguration(consentObject, configDoc); if (nextConsent != null) { consents.add(nextConsent); } } } this.cachedSystemConsents = Collections.unmodifiableSet(consents); this.lastSystemConsentLoadTime = configDoc.getDate(); } catch (Exception ex) { this.logger.error("Could not load system consents from preferences document: {}", ex.getMessage()); } }
private Set<Consent> getConsentsForPatient(Patient patient, boolean includeGranted) { if (patient == null) { return null; } // List of consent ids a patient has agreed to, read from the database Set<String> xwikiPatientConsents = readConsentIdsFromPatientDoc(patient); Set<Consent> returnedConsents = new LinkedHashSet<>(); // Using system consents to ignore consents set for the patient but no longer configured in the system // (it is faster to check contains() in a set, so iterating through the list and checking the set) Set<Consent> systemConsents = getSystemConsents(); for (Consent systemConsent : systemConsents) { if (xwikiPatientConsents.contains(systemConsent.getId())) { if (includeGranted) { Consent copy = systemConsent.copy(ConsentStatus.YES); returnedConsents.add(copy); } } else { Consent copy = systemConsent.copy(ConsentStatus.NO); returnedConsents.add(copy); } } return returnedConsents; }
/** * @param grant if true will grant the consent, otherwise will revoke * @return if operation was successful */ private boolean manageConsent(Patient patient, String consentId, boolean grant) { if (!this.isValidConsentId(consentId)) { this.logger.error("Invalid consent id ({}) was supplied", consentId); return false; } try { SaveablePatientConsentHolder consentHolder = this.getPatientConsentHolder(patient); List<String> currentConsents = consentHolder.getConsents(); if (grant) { if (!currentConsents.contains(consentId)) { currentConsents.add(consentId); } } else { currentConsents.remove(consentId); } consentHolder.setConsents(currentConsents); consentHolder.save(); return true; } catch (Exception ex) { this.logger.error("Could not update consent {} in patient record {}. {}", consentId, patient.getId(), ex.getMessage()); return false; } }
@Override public boolean isValidConsentId(String consentId) { Set<Consent> systemConsents = getSystemConsents(); for (Consent consent : systemConsents) { if (StringUtils.equals(consentId, consent.getId())) { return true; } } return false; }
@Override public Set<Consent> getAllConsentsForPatient(String patientId) { return this.getAllConsentsForPatient(this.repository.get(patientId)); }
private SaveablePatientConsentHolder getPatientConsentHolder(Patient patient) throws Exception { XWikiDocument patientDoc = patient.getXDocument(); return new SaveablePatientConsentHolder(getXWikiConsentHolder(patientDoc), patientDoc, this.contextProvider.get()); }
@Override public Set<Consent> getMissingConsentsForPatient(String patientId) { return this.getMissingConsentsForPatient(this.repository.get(patientId)); }
private synchronized void updateSystemConsentCache(XWikiDocument configDoc) { try { Set<Consent> consents = new LinkedHashSet<>(); List<BaseObject> consentObjects = configDoc.getXObjects(this.consentReference); if (consentObjects != null) { for (BaseObject consentObject : consentObjects) { Consent nextConsent = fromXWikiConsentConfiguration(consentObject, configDoc); if (nextConsent != null) { consents.add(nextConsent); } } } this.cachedSystemConsents = Collections.unmodifiableSet(consents); this.lastSystemConsentLoadTime = configDoc.getDate(); } catch (Exception ex) { this.logger.error("Could not load system consents from preferences document: {}", ex.getMessage()); } }
@Override public boolean setPatientConsents(Patient patient, Iterable<String> consents) { try { List<Consent> existingConsents = this.selectFromSystem(consents); SaveablePatientConsentHolder holder = this.getPatientConsentHolder(patient); holder.setConsents(convertToIds(existingConsents)); holder.save(); return true; } catch (Exception ex) { this.logger.error("Could not update consents in patient record {}. {}", patient, ex.getMessage()); } return false; }