org.opentestsystem.authoring.testauth.persistence.BlueprintElementRepositoryImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.authoring.testauth.persistence.BlueprintElementRepositoryImpl.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.authoring.testauth.persistence;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.authoring.testauth.domain.BlueprintElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * Interface for BlueprintElement mongoDb repository.
 */
public class BlueprintElementRepositoryImpl implements BlueprintElementRepositoryCustom {

    private static final Map<String, String> REGEX_REPLACE_MAP = ImmutableMap.of("\\|", "\\\\|");

    @Autowired
    private transient MongoOperations mongoOperations;

    @Override
    public List<BlueprintElement> findAllChildrenByAssessmentIdAndGradeAndParentKey(final String assessmentId,
            final String grade, final String inParentKey) {
        final Query query = new Query();

        // search for blueprint elements within the given assessment and grade
        query.addCriteria(Criteria.where("assessmentId").is(assessmentId));
        query.addCriteria(Criteria.where("grade").is(grade));

        // if parentKey provided, filter to include only elements under the given parentKey tree
        // e.g. parentKey == inParentKey || parentKey starts with inParentKey|
        if (StringUtils.isNotBlank(inParentKey)) {
            final Criteria searchValCriteria = new Criteria().orOperator(
                    Criteria.where("parentKey").is(inParentKey), Criteria.where("standardKey").is(inParentKey),
                    Criteria.where("parentKey").regex("^" + escapeString(inParentKey) + "\\|"));

            query.addCriteria(searchValCriteria);
        }

        return this.mongoOperations.find(query, BlueprintElement.class);
    }

    private String escapeString(String val) {
        for (final Entry<String, String> entry : REGEX_REPLACE_MAP.entrySet()) {
            val = val.replaceAll(entry.getKey(), entry.getValue());
        }

        return val;
    }

    @SuppressWarnings({ "unchecked" })
    @Override
    public String[] findDistinctActiveLevelsByAssessmentId(final String assessmentId) {
        final Query query = new Query();
        query.addCriteria(Criteria.where("assessmentId").is(assessmentId));
        query.addCriteria(Criteria.where("active").is(true));

        return Iterables.toArray(Lists.newArrayList(
                this.mongoOperations.getCollection("blueprintElement").distinct("level", query.getQueryObject())),
                String.class);
    }
}