org.opentestsystem.authoring.testauth.publish.RegistrationPublisherHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.authoring.testauth.publish.RegistrationPublisherHelper.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.publish;

import static org.opentestsystem.authoring.testauth.config.TestAuthUtil.DEFAULT_VERSION;
import static org.opentestsystem.authoring.testauth.publish.PublisherUtil.FIXEDFORM_SEGMENT_FILTER;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.opentestsystem.authoring.testauth.config.TestAuthUtil;
import org.opentestsystem.authoring.testauth.domain.AffinityGroup;
import org.opentestsystem.authoring.testauth.domain.Assessment;
import org.opentestsystem.authoring.testauth.domain.Form;
import org.opentestsystem.authoring.testauth.domain.Item;
import org.opentestsystem.authoring.testauth.domain.ItemSelectionAlgorithmType;
import org.opentestsystem.authoring.testauth.domain.Segment;
import org.opentestsystem.authoring.testauth.publish.domain.Identifier;
import org.opentestsystem.authoring.testauth.publish.domain.ItemPool;
import org.opentestsystem.authoring.testauth.publish.domain.PoolProperty;
import org.opentestsystem.authoring.testauth.publish.domain.Purpose;
import org.opentestsystem.authoring.testauth.publish.domain.PurposeBaseContent;
import org.opentestsystem.authoring.testauth.publish.domain.Registration;
import org.opentestsystem.authoring.testauth.publish.domain.RegistrationForm;
import org.opentestsystem.authoring.testauth.publish.domain.RegistrationSegment;
import org.opentestsystem.authoring.testauth.publish.domain.TestItem;
import org.opentestsystem.authoring.testauth.publish.domain.TestSpecification;
import org.opentestsystem.authoring.testauth.service.FormService;
import org.opentestsystem.shared.search.domain.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component("registrationPublisherHelper")
public class RegistrationPublisherHelper extends BasePublisherHelper implements PublisherHelper {
    private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationPublisherHelper.class);

    @Autowired
    private transient FormService formService;

    @Override
    public TestSpecification<Registration> createTestSpec(final Assessment assessment, final DateTime publishDate,
            final String version, final Purpose purpose,
            final TestSpecification<? extends PurposeBaseContent> seedingTestSpec) {// ignore/disregard seedingTestSpec as not really usable for Registration
        final long start = System.currentTimeMillis();
        final TestSpecification<Registration> testSpec = doGeneralTestSpecificationSetup(assessment, publishDate,
                version, purpose, Registration.class);

        final Registration specContent = new Registration();

        final String assessmentId = assessment.getId();
        final List<Segment> segmentList = retrieveSegmentList(assessmentId);
        final List<AffinityGroup> affinityGroupList = getActiveAffinityGroups(assessmentId);
        final List<Item> itemList = retrieveItemsForAssessment(assessmentId);
        final long dataRetrieval = System.currentTimeMillis();

        final ItemPool itemPool = setupItemPoolData(assessment, itemList, segmentList, affinityGroupList, version);
        final Map<String, TestItem> testItemMap = buildTestItemMap(itemPool.getTestItemList());
        final List<List<PoolProperty>> poolPropertyLists = Lists.newArrayList();
        final long itemPoolCreation = System.currentTimeMillis();

        // SEGMENT
        final List<RegistrationSegment> registrationSegmentList = Lists.newArrayList();
        for (final Segment segment : segmentList) {
            final ItemSelectionAlgorithmType segmentType = segment.getItemSelectionAlgorithm()
                    .getItemSelectionAlgorithmType();

            final List<Item> filteredItemList = Lists
                    .newArrayList(Iterables.concat(retrieveScopedItems(assessment.getId(), itemList,
                            segment.getId(), segmentType.equals(ItemSelectionAlgorithmType.FIXEDFORM)).values()));

            final RegistrationSegment registrationSegment = new RegistrationSegment(segment.getPosition(),
                    segmentType.name(),
                    new Identifier(segment.getId(), segment.getLabel(), segment.getLabel(), DEFAULT_VERSION), null,
                    null, segment.getMinOpItems() + segment.getMinFtItems(),
                    segment.getMaxOpItems() + segment.getMaxFtItems(),
                    buildPoolPropertyListFromItemList(testItemMap, filteredItemList));

            registrationSegmentList.add(registrationSegment);
            poolPropertyLists.add(registrationSegment.getPoolPropertyList());
        }

        specContent.setRegistrationSegmentList(registrationSegmentList);
        final long segmentCreation = System.currentTimeMillis();

        // FORM
        long formCreation = segmentCreation;
        if (Iterables.any(segmentList, FIXEDFORM_SEGMENT_FILTER)) {
            final List<RegistrationForm> formList = Lists.newArrayList();
            final SearchResponse<Form> formSearchResponse = this.formService
                    .searchForms(TestAuthUtil.searchParamsByAssessmentIdLarge(assessment.getId()));
            for (final Form form : formSearchResponse.getSearchResults()) {
                final List<Item> filteredItemList = Lists.newArrayList(
                        Iterables.concat(retrieveScopedItems(assessment.getId(), itemList, null, true).values()));
                final RegistrationForm registrationForm = doGeneralFormSetup(RegistrationForm.class, form,
                        filteredItemList, testItemMap, assessment.getVersion());
                formList.add(registrationForm);
            }
            specContent.setRegistrationFormList(formList);
            formCreation = System.currentTimeMillis();
        }

        // top-level pool property
        specContent
                .setPoolPropertyList(sumPoolPropertyLists(Lists.newArrayList(Iterables.concat(poolPropertyLists))));
        final long poolCreation = System.currentTimeMillis();

        testSpec.setContent(specContent);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("data retrieval: " + (dataRetrieval - start) + "ms\n" + "item pool: "
                    + (itemPoolCreation - dataRetrieval) + "ms\n" + "registration segment: "
                    + (segmentCreation - itemPoolCreation) + "ms\n" + "form: " + (formCreation - segmentCreation)
                    + "ms\n" + "test poolproperty: " + (poolCreation - formCreation) + "ms\n" + "total time: "
                    + (System.currentTimeMillis() - start) + "ms");
        }
        return testSpec;
    }
}