/** * Given a set of {@code missingConsents missing consents}, returns the union of all {@link Consent#getFields() * fields}, which the provided set of {@code missingConsents} prevents from being used. Returns an empty set if no * fields are affected. * * @param missingConsents a set of not granted {@link Consent} consents. * @return {@code null} if mandatory consents are missing, an empty list of no fields are missing consents, or a * list of field names that are missing a required consent */ private Set<String> getNonConsentedFieldSet(@Nonnull Set<Consent> missingConsents) { Set<String> notConsentedFields = new HashSet<>(); for (Consent consent : missingConsents) { if (consent.affectsAllFields() || consent.isRequired()) { // if at least one of the consents affects all fields no point to examine other consents // since all fields are affected anyway return null; } if (!consent.affectsSomeFields()) { continue; } notConsentedFields.addAll(consent.getFields()); } return notConsentedFields; } }
private static List<String> convertToIds(List<Consent> consents) { List<String> ids = new LinkedList<>(); for (Consent consent : consents) { ids.add(consent.getId()); } return ids; }
@Override public JSONArray toJSON(Collection<Consent> consents) { if (consents == null) { return null; } JSONArray result = new JSONArray(); for (Consent consent : consents) { result.put(consent.toJSON()); } return result; }
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; }
/** * Copies an instance of {@link Consent} to a new instance. * * @param status the consent whose state is to be copied * @return an instance which is identical to the `from` instance */ @Override public Consent copy(ConsentStatus status) { Consent copy = new DefaultConsent( this.getId(), this.getLabel(), this.getDescription(), this.isRequired(), this.getFields()); copy.setStatus(status); return copy; } }
/** * Given a set of missing consents for a {@link Patient} object, returns true iff none of the * {@code missingConsents} is required ({@link Consent#isRequired()}), false otherwise. * * @param missingConsents a set of missing {@link Consent} objects * @return true iff none of the {@code missingConsents} are required, false otherwise */ private boolean containsRequiredConsents(@Nonnull Set<Consent> missingConsents) { for (Consent consent : missingConsents) { if (consent.isRequired()) { return false; } } return true; }
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; }
/** * Copies an instance of {@link Consent} to a new instance. * * @param status the consent whose state is to be copied * @return an instance which is identical to the `from` instance */ @Override public Consent copy(ConsentStatus status) { Consent copy = new DefaultConsent( this.getId(), this.getLabel(), this.getDescription(), this.isRequired(), this.getDataFields(), this.getFields()); copy.setStatus(status); return copy; } }
/** * Given a set of missing consents for a {@link Patient} object, returns true iff none of the * {@code missingConsents} is required ({@link Consent#isRequired()}), false otherwise. * * @param missingConsents a set of missing {@link Consent} objects * @return true iff none of the {@code missingConsents} are required, false otherwise */ private boolean containsRequiredConsents(@Nonnull Set<Consent> missingConsents) { for (Consent consent : missingConsents) { if (consent.isRequired()) { return false; } } return true; }
/** * Given a set of {@code missingConsents missing consents}, returns the union of all {@link Consent#getFields() * fields}, which the provided set of {@code missingConsents} prevents from being used. Returns an empty set if no * fields are affected. * * @param missingConsents a set of not granted {@link Consent} consents. * @return {@code null} if mandatory consents are missing, an empty list of no fields are missing consents, or a * list of field names that are missing a required consent */ private Set<String> getNonConsentedFieldSet(@Nonnull Set<Consent> missingConsents) { Set<String> notConsentedFields = new HashSet<>(); for (Consent consent : missingConsents) { if (consent.affectsAllFields() || consent.isRequired()) { // if at least one of the consents affects all fields no point to examine other consents // since all fields are affected anyway return null; } if (!consent.affectsSomeFields()) { continue; } notConsentedFields.addAll(consent.getFields()); } return notConsentedFields; } }
private static List<String> convertToIds(List<Consent> consents) { List<String> ids = new LinkedList<>(); for (Consent consent : consents) { ids.add(consent.getId()); } return ids; }
@Override public JSONArray toJSON(Collection<Consent> consents) { if (consents == null) { return null; } JSONArray result = new JSONArray(); for (Consent consent : consents) { result.put(consent.toJSON()); } return result; }
@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 boolean isValidConsentId(String consentId) { Set<Consent> systemConsents = getSystemConsents(); for (Consent consent : systemConsents) { if (StringUtils.equals(consentId, consent.getId())) { return true; } } return false; }
/** @return consents that exist in the system and correspond to the given ids */ private List<Consent> selectFromSystem(Iterable<String> ids) { List<Consent> existingConsents = new LinkedList<>(); for (String id : ids) { for (Consent consent : this.getSystemConsents()) { if (StringUtils.equals(consent.getId(), id)) { existingConsents.add(consent); break; } } } return existingConsents; }
/** * @return consents that exist in the system and correspond to the given ids */ private List<Consent> selectFromSystem(Iterable<String> ids) { List<Consent> existingConsents = new LinkedList<>(); for (String id : ids) { for (Consent consent : this.getSystemConsents()) { if (StringUtils.equals(consent.getId(), id)) { existingConsents.add(consent); break; } } } return existingConsents; }
@Override public boolean authorizeInteraction(Set<String> grantedConsents) { Set<Consent> systemConsents = this.consentManager.getSystemConsents(); if (CollectionUtils.isEmpty(systemConsents)) { return true; } if (CollectionUtils.isEmpty(grantedConsents)) { return containsRequiredConsents(systemConsents); } Set<Consent> missingConsents = new HashSet<>(); for (Consent consent : systemConsents) { if (!grantedConsents.contains(consent.getId())) { missingConsents.add(consent); } } return containsRequiredConsents(missingConsents); }
@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; }
@Override public boolean authorizeInteraction(Set<String> grantedConsents) { Set<Consent> systemConsents = this.consentManager.getSystemConsents(); if (CollectionUtils.isEmpty(systemConsents)) { return true; } if (CollectionUtils.isEmpty(grantedConsents)) { return containsRequiredConsents(systemConsents); } Set<Consent> missingConsents = new HashSet<>(); for (Consent consent : systemConsents) { if (!grantedConsents.contains(consent.getId())) { missingConsents.add(consent); } } return containsRequiredConsents(missingConsents); }
@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; }