Find by principal on or after date.
@param principal Principal to search for
@param onOrAfterDate start date for search
@return records for principal after date.
972947
@View(name = "by_principal_date",
        map = "function(doc) { if (doc.recordKey && doc.principal && doc.deviceFingerprint && doc.recordDate) { emit([doc.principal, doc.recordDate], doc) } }")
    public List<CouchDbMultifactorAuthenticationTrustRecord> findByPrincipalAfterDate(final String principal, final LocalDateTime onOrAfterDate) {
        val view = createQuery("by_principal_date")
            .startKey(ComplexKey.of(principal, onOrAfterDate)).endKey(ComplexKey.of(principal, "999999"));

        return db.queryView(view, CouchDbMultifactorAuthenticationTrustRecord.class);
    }
1121678
@View(name = "by_type_for_principal_id_since", map = "function(doc) { emit([doc.type, doc.principal, doc.creationTime], doc) }")
    public Collection<CouchDbCasEvent> findByTypeForPrincipalSince(final String type, final String principalId, final LocalDateTime localDeateTime) {
        val view = createQuery("by_type_for_principal_id_since").startKey(ComplexKey.of(type, principalId, localDeateTime))
            .endKey(ComplexKey.of(type, principalId, LocalDateTime.now()));
        return db.queryView(view, CouchDbCasEvent.class);
    }
1160774
@JsonIgnore
    protected Map<String, List<Object>> getPrincipalAttributes(final Principal principal) {
        if (ignoreResolvedAttributes) {
            return new HashMap<>(0);
        }
        return convertPrincipalAttributesToPersonAttributes(principal.getAttributes());
    }
1240799
private static boolean doesNameMatchPattern(final Principal principal, final Pattern pattern) {
        if (pattern != null) {
            val name = principal.getName();
            val result = pattern.matcher(name).matches();
            LOGGER.debug("[{}] matches [{}] == [{}]", pattern.pattern(), name, result);
            return result;
        }
        return true;
    }
124742
private Principal createPrincipal(String schema, JsonNode principalNode) {
        if (schema.equalsIgnoreCase(PRINCIPAL_SCHEMA_USER)) {
            return new Principal(PRINCIPAL_SCHEMA_USER, principalNode.asText(), options.isStripAwsPrincipalIdHyphensEnabled());
        } else if (schema.equalsIgnoreCase(PRINCIPAL_SCHEMA_SERVICE)) {
            return new Principal(schema, principalNode.asText());
        } else if (schema.equalsIgnoreCase(PRINCIPAL_SCHEMA_FEDERATED)) {
            if (WebIdentityProviders.fromString(principalNode.asText()) != null) {
                return new Principal(WebIdentityProviders.fromString(principalNode.asText()));
            } else {
                return new Principal(PRINCIPAL_SCHEMA_FEDERATED, principalNode.asText());
            }
        }
        throw new SdkClientException("Schema " + schema + " is not a valid value for the principal.");
    }
1199949
@ReadOperation
    public Collection<Map<String, Object>> consentDecisions(@Selector final String principal) {
        val result = new HashSet<Map<String, Object>>();
        LOGGER.debug("Fetching consent decisions for principal [{}]", principal);
        val consentDecisions = this.consentRepository.findConsentDecisions(principal);
        LOGGER.debug("Resolved consent decisions for principal [{}]: [{}]", principal, consentDecisions);

        consentDecisions.forEach(d -> {
            val map = new HashMap<String, Object>();
            map.put("decision", d);
            map.put("attributes", this.consentEngine.resolveConsentableAttributesFrom(d));
            result.add(map);
        });
        return result;
    }
1101617
public Iterable<DContact> queryByUpdatedDate(Object parent, java.util.Date updatedDate) {
    return queryByField(parent, DContactMapper.Field.UPDATEDDATE.getFieldName(), updatedDate);
  }
367822
@View(name = "by_surrogate_principal", map = "function(doc) { if(doc.surrogate && doc.principal) { emit([doc.principal, doc.surrogate], doc) } }")
    public List<CouchDbSurrogateAuthorization> findBySurrogatePrincipal(final String surrogate, final String principal) {
        return queryView("by_surrogate_principal", ComplexKey.of(principal, surrogate));
    }
561671
private Map<String, List<String>> groupPrincipalByScheme(
            List<Principal> principals) {
        Map<String, List<String>> principalsByScheme = new LinkedHashMap<String, List<String>>();

        String provider;
        List<String> principalValues;
        for (Principal principal : principals) {
            provider = principal.getProvider();
            if (!principalsByScheme.containsKey(provider)) {
                principalsByScheme.put(provider, new ArrayList<String>());
            }
            principalValues = principalsByScheme.get(provider);
            principalValues.add(principal.getId());
        }

        return principalsByScheme;
    }
912113
public static Principal getSubjectUserPrincipal(Subject s) {
      if (s != null) {
         for (Principal p : s.getPrincipals()) {
            if (!(p instanceof Group)) {
               return p;
            }
         }
      }
      return null;
   }
599483
private List<Principal> principalOf(JsonNode principalNodes) {
        List<Principal> principals = new LinkedList<Principal>();

        if (principalNodes.asText().equals("*")) {
            principals.add(Principal.All);
            return principals;
        }

        Iterator<Map.Entry<String, JsonNode>> mapOfPrincipals = principalNodes
                .fields();
        String schema;
        JsonNode principalNode;
        Entry<String, JsonNode> principal;
        Iterator<JsonNode> elements;
        while (mapOfPrincipals.hasNext()) {
            principal = mapOfPrincipals.next();
            schema = principal.getKey();
            principalNode = principal.getValue();

            if (principalNode.isArray()) {
                elements = principalNode.elements();
                while (elements.hasNext()) {
                    principals.add(createPrincipal(schema, elements.next()));
                }
            } else {
                principals.add(createPrincipal(schema, principalNode));
            }
        }

        return principals;
    }
1353453
private List<O> findByDate(String dateField, Date start, Date end, int numObjects, boolean asc) {
        if (start == null)
            start = new Date(0);
        if (end == null)
            end = new Date();
        return getDatastore().find(clazz).filter(dateField + " >", start).filter(dateField + " <", end).order(asc ? dateField : '-' + dateField).limit(numObjects).asList();
    }
356257
@View(name = "by_principal",
        map = "function(doc) { if (doc.principal && doc.deviceFingerprint && doc.recordDate) { emit(doc.principal, doc) } }")
    public List<CouchDbMultifactorAuthenticationTrustRecord> findByPrincipal(final String principal) {
        val view = createQuery("by_principal").key(principal);
        return db.queryView(view, CouchDbMultifactorAuthenticationTrustRecord.class);
    }
502272
private DateList getCandidates(final Calendar rootSeed, final Date date, final Value value) {
        DateList dates = new DateList(value);
        if (date instanceof DateTime) {
            if (((DateTime) date).isUtc()) {
                dates.setUtc(true);
            } else {
                dates.setTimeZone(((DateTime) date).getTimeZone());
            }
        }
        dates.add(date);
        if (transformers.get(BYMONTH) != null) {
            dates = transformers.get(BYMONTH).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYMONTH processing: " + dates);
            }
        }

        if (transformers.get(BYWEEKNO) != null) {
            dates = transformers.get(BYWEEKNO).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYWEEKNO processing: " + dates);
            }
        }

        if (transformers.get(BYYEARDAY) != null) {
            dates = transformers.get(BYYEARDAY).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYYEARDAY processing: " + dates);
            }
        }

        if (transformers.get(BYMONTHDAY) != null) {
            dates = transformers.get(BYMONTHDAY).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYMONTHDAY processing: " + dates);
            }
        } else if (frequency == Frequency.MONTHLY || (frequency == Frequency.YEARLY && yearDayList.isEmpty()
                && weekNoList.isEmpty() && dayList.isEmpty())) {

            NumberList implicitMonthDayList = new NumberList();
            implicitMonthDayList.add(rootSeed.get(Calendar.DAY_OF_MONTH));
            ByMonthDayRule implicitRule = new ByMonthDayRule(implicitMonthDayList, frequency, Optional.ofNullable(weekStartDay));
            dates = implicitRule.transform(dates);
        }

        if (transformers.get(BYDAY) != null) {
            dates = transformers.get(BYDAY).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYDAY processing: " + dates);
            }
        } else if (frequency == Frequency.WEEKLY || (frequency == Frequency.YEARLY && yearDayList.isEmpty()
                && !weekNoList.isEmpty() && monthDayList.isEmpty())) {

            ByDayRule implicitRule = new ByDayRule(new WeekDayList(WeekDay.getWeekDay(rootSeed)),
                    deriveFilterType(),  Optional.ofNullable(weekStartDay));
            dates = implicitRule.transform(dates);
        }

        if (transformers.get(BYHOUR) != null) {
            dates = transformers.get(BYHOUR).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYHOUR processing: " + dates);
            }
        }

        if (transformers.get(BYMINUTE) != null) {
            dates = transformers.get(BYMINUTE).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYMINUTE processing: " + dates);
            }
        }

        if (transformers.get(BYSECOND) != null) {
            dates = transformers.get(BYSECOND).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after BYSECOND processing: " + dates);
            }
        }

        if (transformers.get(BYSETPOS) != null) {
            dates = transformers.get(BYSETPOS).transform(dates);
            // debugging..
            if (log.isDebugEnabled()) {
                log.debug("Dates after SETPOS processing: " + dates);
            }
        }
        return dates;
    }
1226945
public Iterable<DContact> queryByPrimaryCustomIndex(Object parent, java.lang.String primaryCustomIndex) {
    return queryByField(parent, DContactMapper.Field.PRIMARYCUSTOMINDEX.getFieldName(), primaryCustomIndex);
  }
625616
public List<O> createdInPeriod(Date start, Date end, int numObjects) {
        return findByDate("creationDate", start, end, numObjects, true);
    }
881157
public static Date getEndOfWeek(Date date) {

        if (date == null)
            return null;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);

        return calendar.getTime();
    }