Java tutorial
/******************************************************************************* * 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 org.opentestsystem.authoring.testauth.domain.AdaptiveItemLocation; import org.opentestsystem.authoring.testauth.domain.FixedFormItemLocation; import org.opentestsystem.authoring.testauth.domain.Item; import org.opentestsystem.authoring.testauth.domain.ItemLocation; import org.opentestsystem.authoring.testauth.domain.search.ItemSearchRequest; import org.opentestsystem.authoring.testauth.persistence.ItemRepository; import org.opentestsystem.authoring.testauth.service.FormPartitionService; import org.opentestsystem.shared.exception.LocalizedException; import org.opentestsystem.shared.exception.RestException; import org.opentestsystem.shared.search.domain.SearchResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Component; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @Component public abstract class ItemBaseHelper { @Autowired private transient ItemRepository itemRepository; @Autowired private FormPartitionService formPartitionService; protected Item getItemByAssessmentIdAndTibIdentifier(final String assessmentId, final String tibIdentifier) { return this.itemRepository.findByAssessmentIdAndTibIdentifier(assessmentId, tibIdentifier); } protected List<ItemLocation> getItemLocationsOnPartition(final String formPartitionId, final List<Item> items) { final List<ItemLocation> locations = Lists.newArrayList(); for (final Item item : items) { for (final ItemLocation cloc : item.getItemLocation()) { if (cloc instanceof FixedFormItemLocation && cloc.getFormPartitionId().equals(formPartitionId)) { locations.add((FixedFormItemLocation) cloc); } } } return locations; } protected List<ItemLocation> getItemLocationsOnSegment(final String segmentId, final List<Item> items) { final List<ItemLocation> locations = Lists.newArrayList(); for (final Item item : items) { for (final ItemLocation cloc : item.getItemLocation()) { if (cloc instanceof AdaptiveItemLocation && cloc.getSegmentId().equals(segmentId)) { locations.add((AdaptiveItemLocation) cloc); } } } return locations; } protected boolean hasLocation(final Item item, final ItemLocation location) { return Iterables.any(item.getItemLocation(), ItemLocation.SAME_LOCATION_FINDER.getInstance(location)); } protected Item saveItem(final Item item) { Item savedItem = null; try { savedItem = this.itemRepository.save(item); } catch (final DuplicateKeyException dke) { throw new LocalizedException("item.already.exists", new String[] { item.getId() }, dke); } return savedItem; } public SearchResponse<Item> searchItems(final ItemSearchRequest itemSearchRequest) { if (itemSearchRequest.isValid()) { return this.itemRepository.search(itemSearchRequest); } throw new RestException("item.search.invalidsearchcriteria"); } }