Java Code Examples for org.springframework.data.mongodb.core.MongoTemplate#findById()

The following examples show how to use org.springframework.data.mongodb.core.MongoTemplate#findById() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AssessmentConverter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
/**
 * On update, AssessmentFamilyReference will not exist on the entity. Fetch it from
 * the db and re-add it to the entity.
 */
private void fixAssessmentFamilyReference(Entity entity, SuperdocConverter.Option option) {
    //assessmentFamilyHierarchy is ignored on create/update
    Object assessmentFamilyHierarchy = entity.getBody().remove(ASSESSMENT_FAMILY_HIERARCHY);

    // There is a requirement that assessment.assessmentFamilyReference be READONLY via the API
    // so only add assessmentFamilyReference back if the delete option is NOT set
    if (option == null || Option.DELETE_ASSESSMENT_FAMILY_REFERENCE != option) {
        MongoTemplate mongo = ((MongoEntityRepository) repo).getTemplate();
        Entity existingAssessment = mongo.findById(entity.getEntityId(), Entity.class, ASSESSMENT);
        if (existingAssessment == null) {
            return;
        }
        if (existingAssessment.getBody().containsKey(ASSESSMENT_FAMILY_REFERENCE)) {
            String assessmentFamilyRef = (String) existingAssessment.getBody().get(ASSESSMENT_FAMILY_REFERENCE);
            entity.getBody().put(ASSESSMENT_FAMILY_REFERENCE, assessmentFamilyRef);
        }
    }
}
 
Example 2
Source File: AssessmentConverter.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Construct the assessmentFamilyHierarchy string from the assessment family reference
 */
private void addFamilyHierarchy(Entity entity) {
    Object object = entity.getBody().remove(ASSESSMENT_FAMILY_REFERENCE);
    if (object == null || !(object instanceof String)) {
        // we don't validate assessmentFamilyHierarchy any more, so someone could have passed in
        // an object, array, number, etc.
        return;
    }
    String familyRef = (String) object;
    MongoTemplate mongo = ((MongoEntityRepository) repo).getTemplate();
    Entity family = mongo.findById(familyRef, Entity.class, ASSESSMENT_FAMILY);

    List<String> familyTitles = new LinkedList<String>();
    Set<String> seenFamilyRefs = new HashSet<String>();
    seenFamilyRefs.add(familyRef);
    while (family != null) {
        String familyTitle = (String)family.getBody().get(ASSESSMENT_FAMILY_TITLE);
        if (familyTitle == null) {
            LOG.error("Required assessmentFamilyTitle is null for assessmentFamily with _id : {}", new Object[] {family.getEntityId()});
            break;
        }
        familyTitles.add(familyTitle);
        String parentRef = (String)family.getBody().get(ASSESSMENT_FAMILY_ASSESSMENT_FAMILY_REFERENCE);
        family = null;

        if (parentRef != null) {
            if (!seenFamilyRefs.contains(parentRef)) {
                family = mongo.findById(parentRef, Entity.class, ASSESSMENT_FAMILY);
                seenFamilyRefs.add(parentRef);
            } else {
                LOG.error("Circular reference detected in assessment family hierarchy. _id : {} occurs twice in hierarchy.", new Object[] { parentRef });
            }
        }
    }

    String familyHierarchyString = Joiner.on(".").join(Lists.reverse(familyTitles));
    entity.getBody().put(ASSESSMENT_FAMILY_HIERARCHY, familyHierarchyString);
}