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 java.util.Set; import org.opentestsystem.authoring.testauth.domain.ItemMetadataKeys; import org.opentestsystem.authoring.testauth.persistence.ItemMetadataKeysRepository; import org.opentestsystem.authoring.testauth.service.ItemMetadataKeysService; import org.opentestsystem.authoring.testitembank.client.TibItem; import org.opentestsystem.shared.exception.LocalizedException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import com.google.common.base.Function; import com.google.common.collect.Iterables; import com.google.common.collect.Multimaps; import com.google.common.collect.Sets; @Service public class ItemMetadataKeysServiceImpl extends AssessmentChildHelper implements ItemMetadataKeysService { private static final Function<TibItem, Set<String>> TIBITEM_METADATA_KEYS = new Function<TibItem, Set<String>>() { @Override public Set<String> apply(final TibItem input) { return input.getAllIncludedMetatdata().keySet(); } }; @Autowired private transient ItemMetadataKeysRepository itemMetadataKeysRepository; @Override public ItemMetadataKeys getItemMetadataKeysByAssessment(final String assessmentId) { return this.itemMetadataKeysRepository.findByAssessmentId(assessmentId); } @Override public void loadTibItemImportIntoItemMetadataKeys(final String assessmentId, final List<TibItem> tibItemList) { checkForLockedAssessment(assessmentId); final Set<String> metadataKeys = Sets .newHashSet(Iterables.concat(Multimaps.index(tibItemList, TIBITEM_METADATA_KEYS).asMap().keySet())); ItemMetadataKeys itemMetadataKeys = getItemMetadataKeysByAssessment(assessmentId); if (itemMetadataKeys == null) { itemMetadataKeys = new ItemMetadataKeys(); itemMetadataKeys.setAssessmentId(assessmentId); itemMetadataKeys.setItemMetadataKeys(metadataKeys); } else { itemMetadataKeys.getItemMetadataKeys().addAll(metadataKeys); } try { this.itemMetadataKeysRepository.save(itemMetadataKeys); } catch (final DuplicateKeyException dke) { throw new LocalizedException("itemMetadataKeys.already.exists", new String[] {}, dke); } } @Override public void removeItemMetadataKeysByAssessment(final String assessmentId) { checkForLockedAssessment(assessmentId); final ItemMetadataKeys itemMetadataKeys = this.itemMetadataKeysRepository.findByAssessmentId(assessmentId); if (itemMetadataKeys != null) { this.itemMetadataKeysRepository.delete(itemMetadataKeys); } } }