org.opentestsystem.authoring.testauth.service.impl.ItemServiceImpl.java Source code

Java tutorial

Introduction

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

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

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.authoring.testauth.domain.AffinityGroup;
import org.opentestsystem.authoring.testauth.domain.Assessment;
import org.opentestsystem.authoring.testauth.domain.BlueprintElement;
import org.opentestsystem.authoring.testauth.domain.FormPartition;
import org.opentestsystem.authoring.testauth.domain.Item;
import org.opentestsystem.authoring.testauth.domain.ItemCountSummary;
import org.opentestsystem.authoring.testauth.domain.ItemGroup;
import org.opentestsystem.authoring.testauth.domain.ItemLocation;
import org.opentestsystem.authoring.testauth.domain.ItemMoveRequest;
import org.opentestsystem.authoring.testauth.domain.ItemMoveResponse;
import org.opentestsystem.authoring.testauth.domain.Segment;
import org.opentestsystem.authoring.testauth.domain.TibImportResult;
import org.opentestsystem.authoring.testauth.domain.ValidationResult;
import org.opentestsystem.authoring.testauth.domain.search.ItemSearchRequest;
import org.opentestsystem.authoring.testauth.persistence.ItemRepository;
import org.opentestsystem.authoring.testauth.persistence.SegmentRepository;
import org.opentestsystem.authoring.testauth.service.AffinityGroupService;
import org.opentestsystem.authoring.testauth.service.BlueprintElementService;
import org.opentestsystem.authoring.testauth.service.FormPartitionService;
import org.opentestsystem.authoring.testauth.service.ItemGroupService;
import org.opentestsystem.authoring.testauth.service.ItemService;
import org.opentestsystem.authoring.testauth.service.SegmentService;
import org.opentestsystem.authoring.testauth.validation.ItemValidationHelper;
import org.opentestsystem.shared.exception.LocalizedException;
import org.opentestsystem.shared.exception.RestException;
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.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class ItemServiceImpl extends AssessmentChildHelper implements ItemService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private transient ItemRepository itemRepository;

    @Autowired
    private SegmentRepository segmentRepository;

    @Autowired
    private SegmentService segmentService;

    @Autowired
    private FormPartitionService formPartitionService;

    @Autowired
    private ItemGroupService itemGroupService;

    @Autowired
    private AffinityGroupService affinityGroupService;

    @Autowired
    private BlueprintElementService blueprintElementService;

    @Autowired
    private ItemImportHelper itemImportHelper;

    @Autowired
    private ItemOrderingHelper itemOrderingHelper;

    @Autowired
    private ItemRemovalHelper itemRemovalHelper;

    @Autowired
    private ItemCountHelper itemCountHelper;

    @Autowired
    private ItemValidationHelper itemValidationHelper;

    @Override
    public Item getItem(final String itemId) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Finding item for Id: " + itemId);
        }
        return this.itemRepository.findOne(itemId);
    }

    @Override
    public List<Item> getItems() {
        return this.itemRepository.findAll();
    }

    @Override
    public List<Item> getItemsByAssessmentId(final String assessmentId) {
        return this.itemRepository.findAllByAssessmentId(assessmentId);
    }

    @Override
    public Item getItemByAssessmentIdAndTibIdentifier(final String assessmentId, final String tibIdentifier) {
        return this.itemRepository.findByAssessmentIdAndTibIdentifier(assessmentId, tibIdentifier);
    }

    @Override
    public List<Item> getItemsBySegmentId(final String segmentId) {
        return this.itemRepository.findAllBySegmentId(segmentId);
    }

    @Override
    public List<Item> getItemsByItemGroupId(final String itemGroupId) {
        return this.itemRepository.findAllByItemGroupId(itemGroupId);
    }

    @Override
    public List<Item> getItemsByFormPartitionId(final String partitionId) {
        return this.itemRepository.findAllByFormPartitionId(partitionId);
    }

    @Override
    public List<Item> getItemsByAffinityGroupId(final String affinityGroupId) {
        return this.itemRepository.findAllByAffinityGroupId(affinityGroupId);
    }

    @Override
    public List<Item> getFirst20DistinctItemsByAssessmentIdAndSearchVal(final String assessmentId,
            final String searchVal) {
        return this.itemRepository.findFirst20DistinctItemsByAssessmentIdAndSearchVal(assessmentId, searchVal);
    }

    @Override
    public Map<String, Map<String, ItemCountSummary>> getSegmentItemCounts(final String segmentId) {
        return this.itemCountHelper.getSegmentItemCounts(segmentId);
    }

    @Override
    public Map<String, Map<BlueprintElement, ItemCountSummary>> getAdaptiveSegmentStandardItemCounts(
            final String assessmentId) {
        return this.itemCountHelper.getAdaptiveSegmentStandardItemCounts(assessmentId);
    }

    @Override
    public Map<String, Map<BlueprintElement, ItemCountSummary>> getFormPartitionStandardItemCounts(
            final String assessmentId) {
        return this.itemCountHelper.getFormPartitionStandardItemCounts(assessmentId);
    }

    @Override
    public Map<String, ItemCountSummary> getAffinityGroupItemCounts(final String affinityGroupId) {
        return this.itemCountHelper.getAffinityGroupItemCounts(affinityGroupId);
    }

    @Override
    public Map<String, Map<String, ItemCountSummary>> getAffinityGroupItemCountsByAssessment(
            final String assessmentId) {
        final Map<String, Map<String, ItemCountSummary>> assessmentItemCounts = Maps.newHashMap();

        final List<AffinityGroup> affinityGroups = this.affinityGroupService
                .getAffinityGroupsByAssessment(assessmentId);
        for (final AffinityGroup affinityGroup : affinityGroups) {
            assessmentItemCounts.put(affinityGroup.getId(), getAffinityGroupItemCounts(affinityGroup.getId()));
        }

        return assessmentItemCounts;
    }

    @Override
    public SearchResponse<Item> searchItems(final ItemSearchRequest searchRequest) {
        return this.itemCountHelper.searchItems(searchRequest);
    }

    @Override
    public long searchItemCounts(final Map<String, String[]> parameterMap) {
        final ItemSearchRequest searchRequest = new ItemSearchRequest(parameterMap);
        if (searchRequest.isValid()) {
            return this.itemRepository.searchForItemCounts(searchRequest);
        }
        throw new RestException("item.search.invalidsearchcriteria");
    }

    @Override
    public Item saveItem(final Item item) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Saving item");
        }
        if (item == null) {
            throw new LocalizedException("item.invalid.id");
        }
        checkForLockedAssessment(item.getAssessmentId());
        return this.itemImportHelper.saveItem(item);
    }

    @Override
    public List<Item> saveItemList(final List<Item> items) {
        final List<Item> savedItems = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(items)) {
            checkForLockedAssessment(items.get(0).getAssessmentId());
            final List<Item> itemsToRemove = Lists
                    .newArrayList(Iterables.filter(items, ItemHelper.ITEM_ELEMENT_FILTER_REMOVE));
            final List<Item> itemsToSave = Lists
                    .newArrayList(Iterables.filter(items, Predicates.not(ItemHelper.ITEM_ELEMENT_FILTER_REMOVE)));
            try {
                if (!CollectionUtils.isEmpty(itemsToRemove)) {
                    this.itemRepository.delete(itemsToRemove);
                }
                if (!CollectionUtils.isEmpty(itemsToSave)) {
                    savedItems.addAll(this.itemRepository.save(itemsToSave));
                }
            } catch (final DuplicateKeyException dke) {
                throw new LocalizedException("item.listhasduplicates", dke);
            }
        }

        return savedItems;
    }

    @Override
    public void removeItem(final String itemId) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Removing item with Id:" + itemId);
        }
        final Item item = this.itemRepository.findOne(itemId);
        if (item != null) {
            checkForLockedAssessment(item.getAssessmentId());
        }
        this.itemRemovalHelper.removeEnemiesByItem(item);
        this.itemRepository.delete(itemId);
    }

    @Override
    public void removeByAssessmentId(final String assessmentId) {
        checkForLockedAssessment(assessmentId);
        this.itemRepository.delete(this.itemRepository.findAllByAssessmentId(assessmentId));
    }

    @Override
    public void removeByItemGroupId(final String itemGroupId) {
        final ItemGroup grp = this.itemGroupService.getItemGroup(itemGroupId);
        if (grp == null) {
            throw new LocalizedException("itemgroup.not.found");
        }
        checkForLockedAssessment(grp.getAssessmentId());
        final List<Item> items = this.itemRepository.findAllByItemGroupId(itemGroupId);
        this.itemRemovalHelper.removeByFilter(items, ItemLocation.ITEM_GROUP_ID_FILTER.getInstance(itemGroupId));
    }

    @Override
    public void removeByAffinityGroupId(final String affinityGroupId) {
        final AffinityGroup grp = this.affinityGroupService.getAffinityGroup(affinityGroupId);
        if (grp == null) {
            throw new LocalizedException("affinityGroup.id.invalid");
        }
        checkForLockedAssessment(grp.getAssessmentId());
        final List<Item> items = this.itemRepository.findAllByAffinityGroupId(affinityGroupId);
        this.itemRemovalHelper.removeByFilter(items,
                ItemLocation.AFFINITY_GROUP_ID_FILTER.getInstance(affinityGroupId));
    }

    @Override
    public void removeBySegmentId(final String segmentId) {
        final List<Item> itemList = this.itemRepository.findAllBySegmentId(segmentId);
        if (!CollectionUtils.isEmpty(itemList)) {
            checkForLockedAssessment(itemList.get(0).getAssessmentId());
            this.itemRemovalHelper.removeByFilter(itemList, ItemLocation.SEGMENT_ID_FILTER.getInstance(segmentId));
            final List<FormPartition> formPartitionList = this.formPartitionService
                    .getFormPartitionsBySegmentId(segmentId);
            for (final FormPartition formPartition : formPartitionList) {
                final List<Item> ffItems = this.itemRepository.findAllByFormPartitionId(formPartition.getId());
                this.itemRemovalHelper.removeByFilter(ffItems,
                        ItemLocation.FORM_PARTITION_ID_FILTER.getInstance(formPartition.getId()));
            }
        }
    }

    @Override
    public void removeByFormPartitionId(final String formPartitionId) {
        final List<Item> itemList = this.itemRepository.findAllByFormPartitionId(formPartitionId);
        if (!CollectionUtils.isEmpty(itemList)) {
            checkForLockedAssessment(itemList.get(0).getAssessmentId());
            this.itemRemovalHelper.removeByFilter(itemList,
                    ItemLocation.FORM_PARTITION_ID_FILTER.getInstance(formPartitionId));
        }
    }

    @Override
    public boolean hasLocation(final Item item, final ItemLocation location) {
        return this.itemOrderingHelper.hasLocation(item, location);
    }

    @Override
    public TibImportResult importItemsFromTib(final String targetSegmentId, final String targetformPartitionId,
            final String targetItemGroupId, final Map<String, String[]> params) {
        String assessmentId = null;
        if (StringUtils.isNotBlank(targetSegmentId)) {
            final Segment segment = this.segmentRepository.findOne(targetSegmentId);
            assessmentId = segment.getAssessmentId();
        } else if (StringUtils.isNotBlank(targetformPartitionId)) {
            final FormPartition partition = this.formPartitionService.getFormPartition(targetformPartitionId);
            assessmentId = partition.getAssessmentId();
        }
        final Assessment assessment = StringUtils.isNotBlank(assessmentId) ? checkForLockedAssessment(assessmentId)
                : null;
        return this.itemImportHelper.importItemsFromTib(assessment, targetSegmentId, targetformPartitionId,
                targetItemGroupId, params);
    }

    @Override
    public void autoSortFormPartitionItems(final String assessmentId, final String formPartitionId) {
        this.itemOrderingHelper.autoSortFormPartitionItems(assessmentId, formPartitionId);
    }

    @Override
    public List<ItemLocation> getItemLocationsOnPartition(final String formPartitionId, final List<Item> items) {
        return this.itemOrderingHelper.getItemLocationsOnPartition(formPartitionId, items);
    }

    @Override
    public void updateItemLocationForSegment(final String segmentId, final List<ItemLocation> itemLocationList) {
        final Segment segment = this.segmentService.getSegment(segmentId);
        for (final ItemLocation location : itemLocationList) {
            if (!StringUtils.equals(segment.getId(), location.getSegmentId())) {
                throw new LocalizedException("item.not.on.segment");
            }
        }
        final List<Item> items = this.itemRepository.findAllBySegmentId(segmentId);
        this.itemOrderingHelper.updateItemLocationSortingInformation(segment.getAssessmentId(), items,
                itemLocationList);
    }

    @Override
    public void updateItemLocationForPartition(final String formPartitionId,
            final List<ItemLocation> itemLocationList) {
        final FormPartition partition = this.formPartitionService.getFormPartition(formPartitionId);
        for (final ItemLocation location : itemLocationList) {
            if (!StringUtils.equals(partition.getId(), location.getFormPartitionId())) {
                throw new LocalizedException("item.not.on.partition");
            }
        }
        final List<Item> items = this.itemRepository.findAllByFormPartitionId(formPartitionId);
        this.itemOrderingHelper.updateItemLocationSortingInformation(partition.getAssessmentId(), items,
                itemLocationList);
    }

    @Override
    public void updateItemLocationSortingInformation(final String assessmentId, final List<Item> itemsOnPartition,
            final List<ItemLocation> itemLocationList) {
        this.itemOrderingHelper.updateItemLocationSortingInformation(assessmentId, itemsOnPartition,
                itemLocationList);
    }

    @Override
    public ItemMoveResponse removeFormPartitionItems(final String assessmentId, final ItemMoveRequest moveRequest) {
        return this.itemRemovalHelper.removeFormPartitionItems(assessmentId, moveRequest);
    }

    @Override
    public ItemMoveResponse removeSegmentPoolItems(final String assessmentId, final ItemMoveRequest moveRequest) {
        return this.itemRemovalHelper.removeSegmentPoolItems(assessmentId, moveRequest);
    }

    @Override
    public ItemMoveResponse removeItemsFromLocation(final String assessmentId, final ItemLocation removeMe,
            final List<String> itemIdsToRemove) {
        checkForLockedAssessment(assessmentId);
        return this.itemRemovalHelper.removeItemsFromLocation(assessmentId, removeMe, itemIdsToRemove);
    }

    @Override
    public ItemMoveResponse moveSegmentPoolItems(final String assessmentId, final ItemMoveRequest moveRequest) {
        return this.itemOrderingHelper.moveSegmentPoolItems(assessmentId, moveRequest);
    }

    @Override
    public ItemMoveResponse moveFormPartitionItems(final String assessmentId, final ItemMoveRequest moveRequest) {
        return this.itemOrderingHelper.moveFormPartitionItems(assessmentId, moveRequest);
    }

    @Override
    public ItemMoveResponse moveItems(final String assessmentId, final ItemLocation source,
            final ItemLocation target, final List<String> itemIdsToMove) {
        checkForLockedAssessment(assessmentId);
        return this.itemOrderingHelper.moveItems(assessmentId, source, target, itemIdsToMove);
    }

    @Override
    public List<ValidationResult<Segment>> validateItemPools(final String assessmentId) {
        return this.itemValidationHelper.validateItemPools(assessmentId,
                getAdaptiveSegmentStandardItemCounts(assessmentId));
    }

}