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.shared.progman.service.impl; import java.util.List; import java.util.Map; import java.util.Set; import org.opentestsystem.shared.exception.LocalizedException; import org.opentestsystem.shared.progman.domain.Asset; import org.opentestsystem.shared.progman.domain.Asset.AssetType; import org.opentestsystem.shared.progman.domain.AssetPool; import org.opentestsystem.shared.progman.domain.search.AssetPoolSearchRequest; import org.opentestsystem.shared.progman.persistence.AssetGridFsRepository; import org.opentestsystem.shared.progman.persistence.AssetPoolRepository; import org.opentestsystem.shared.progman.persistence.TenantRepository; import org.opentestsystem.shared.progman.service.AssetPoolService; import org.opentestsystem.shared.search.domain.SearchResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.mongodb.DBObject; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSFile; @Service public class AssetPoolServiceImpl implements AssetPoolService { private static final Map<String, AssetType> SUPPORTED_FILES = Maps.newHashMap(); private static final int ONE = 1; @Autowired private transient AssetGridFsRepository assetGridFsRepository; @Autowired private transient AssetPoolRepository assetPoolRepository; @Autowired private transient TenantRepository tenantRepository; @Value("${pm.rest.service.endpoint}") private transient String pmBaseUrl; static { SUPPORTED_FILES.put(MediaType.IMAGE_JPEG_VALUE, AssetType.IMAGE); SUPPORTED_FILES.put(MediaType.IMAGE_GIF_VALUE, AssetType.IMAGE); SUPPORTED_FILES.put(MediaType.IMAGE_PNG_VALUE, AssetType.IMAGE); SUPPORTED_FILES.put("image/x-png", AssetType.IMAGE); SUPPORTED_FILES.put("image/pjpeg", AssetType.IMAGE); SUPPORTED_FILES.put("audio/mpeg", AssetType.FILE); } @Override public AssetPool getAssetPool(final String inAssetPoolId) { return this.assetPoolRepository.findOne(inAssetPoolId); } @Override public AssetPool getAssetPoolByTenantId(final String tenantId) { final Map<String, String[]> params = ImmutableMap.of(AssetPoolSearchRequest.SEARCH_KEY_TENANT_ID, new String[] { tenantId }); final AssetPoolSearchRequest searchReq = new AssetPoolSearchRequest(params); final SearchResponse<AssetPool> searchResp = this.assetPoolRepository.search(searchReq); AssetPool pool = null; if (searchResp.getTotalCount() == ONE) { pool = searchResp.getSearchResults().get(0); } else if (searchResp.getTotalCount() > ONE) { throw new LocalizedException("assetpool.multiple.found", new String[] { tenantId }); } return pool; } @Override public AssetPool saveAssetPool(final AssetPool inAssetPool) { // validate tenant if (this.tenantRepository.findOne(inAssetPool.getTenantId()) == null) { throw new LocalizedException("assetGroup.tenant.invalid", new String[] { inAssetPool.getId(), inAssetPool.getTenantId() }); } return this.assetPoolRepository.save(inAssetPool); } @Override public GridFSDBFile getAssetFile(final String inGridFSId) { return this.assetGridFsRepository.getById(inGridFSId); } @Override public void deleteAssetFile(final String inAssetPoolId, final String inGridFSId) { final AssetPool assetPool = getAssetPool(inAssetPoolId); if (assetPool == null) { throw createAssetPoolNotFoundException(inAssetPoolId); } this.assetGridFsRepository.delete(inGridFSId); // remove file reference from assetPool assetPool.deleteAssetByGridId(inGridFSId); saveAssetPool(assetPool); } @Override public Asset saveAssetFile(final String assetPoolId, final String fileName, final byte[] inBytes, final String fileContentType) { final AssetType assetType = determineAssetType(fileContentType); if (assetType == null) { throw new LocalizedException("upload.type.not.supported", new String[] { fileContentType }); } final AssetPool assetPool = getAssetPool(assetPoolId); if (assetPool == null) { throw createAssetPoolNotFoundException(assetPoolId); } final DBObject metadata = null; final GridFSFile gridFile = this.assetGridFsRepository.save(inBytes, fileName, metadata); final Asset asset = new Asset(); asset.setType(assetType); asset.setAssetFileGridId(gridFile.getId().toString()); asset.setAssetFileName(gridFile.getFilename()); asset.setBasePath(this.pmBaseUrl); asset.setFileContentType(fileContentType); asset.setProperty("/assetPool" + asset.calculatePropertyValueForFile()); assetPool.addAsset(asset); final Set<String> duplicateFiles = findDuplicateFileNames(assetPool.getAssets()); if (!duplicateFiles.isEmpty()) { throw new LocalizedException("assetPool.duplicate.assets", new String[] { duplicateFiles.toString() }); } saveAssetPool(assetPool); return asset; } private AssetType determineAssetType(final String fileContentType) { return SUPPORTED_FILES.get(fileContentType); } private Set<String> findDuplicateFileNames(final List<Asset> assets) { final Set<String> duplicateFiles = Sets.newHashSet(); if (!CollectionUtils.isEmpty(assets)) { final Set<String> fileNames = Sets.newHashSet(); for (final Asset asset : assets) { if ((asset.getType() == Asset.AssetType.FILE || asset.getType() == Asset.AssetType.IMAGE) && !fileNames.add(asset.getAssetFileName())) { duplicateFiles.add(asset.getAssetFileName()); } } } return duplicateFiles; } private LocalizedException createAssetPoolNotFoundException(final String inAssetPoolId) { return new LocalizedException("assetpool.not.found", new String[] { inAssetPoolId }); } }