org.opentestsystem.shared.progman.service.impl.AssetGroupServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.progman.service.impl.AssetGroupServiceImpl.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.shared.progman.service.impl;

import java.util.List;

import org.bson.types.ObjectId;
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.AssetGroup;
import org.opentestsystem.shared.progman.domain.Component;
import org.opentestsystem.shared.progman.domain.Tenant;
import org.opentestsystem.shared.progman.domain.search.AssetGroupSearchRequest;
import org.opentestsystem.shared.progman.persistence.AssetGroupRepository;
import org.opentestsystem.shared.progman.service.AssetGroupService;
import org.opentestsystem.shared.progman.transformer.AssetGroupComponentTransformer;
import org.opentestsystem.shared.progman.transformer.AssetGroupTenantTransformer;
import org.opentestsystem.shared.search.domain.SearchResponse;
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.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

@Service
public class AssetGroupServiceImpl implements AssetGroupService {

    @Autowired
    private transient AssetGroupRepository assetGroupRepository;

    public void setAssetGroupRepository(final AssetGroupRepository inAssetRepository) {
        this.assetGroupRepository = inAssetRepository;
    }

    @Override
    public AssetGroup getAssetGroup(final String inId) {
        return this.assetGroupRepository.findOne(inId);
    }

    @Override
    public SearchResponse<AssetGroup> searchAssetGroups(final AssetGroupSearchRequest inSearchRequest) {
        return this.assetGroupRepository.search(inSearchRequest);
    }

    @Override
    public AssetGroup saveAssetGroup(final AssetGroup inAssetGroup) {
        AssetGroup savedAssetGroup;
        try {
            // delete asset file if asset group exists and type is property
            if (inAssetGroup != null && inAssetGroup.getId() != null
                    && !CollectionUtils.isEmpty(inAssetGroup.getAssets())) {
                for (final Asset asset : inAssetGroup.getAssets()) {
                    if (asset.getType() == AssetType.PROPERTY && asset.getAssetFileGridId() != null) {
                        asset.setAssetFileGridId(null);
                    }
                }
            }
            savedAssetGroup = this.assetGroupRepository.save(inAssetGroup);
        } catch (final DuplicateKeyException ex) {
            throw new LocalizedException("assetGroup.exists", new String[] { inAssetGroup.getId() }, ex);
        }

        return savedAssetGroup;
    }

    @Override
    public void deleteAssetGroup(final String inAssetGroupId) {
        this.assetGroupRepository.delete(inAssetGroupId);
    }

    @Override
    public void cascadeComponentChanges(final Component component, final boolean retainThisComponent) {
        final List<AssetGroup> foundGroups = this.assetGroupRepository
                .findByComponentId(new ObjectId(component.getId()));
        final List<AssetGroup> updatedGroups = Lists.newArrayList(Iterables.transform(foundGroups,
                new AssetGroupComponentTransformer(component, retainThisComponent)));
        if (!CollectionUtils.isEmpty(updatedGroups)) {
            final List<AssetGroup> groupsToSave = Lists
                    .newArrayList(Iterables.filter(updatedGroups, COMPONENT_SAVE_FILTER));
            if (!CollectionUtils.isEmpty(groupsToSave)) {
                this.assetGroupRepository.save(groupsToSave);
            }
            final List<AssetGroup> groupsToRemove = Lists
                    .newArrayList(Iterables.filter(updatedGroups, Predicates.not(COMPONENT_SAVE_FILTER)));
            if (!CollectionUtils.isEmpty(groupsToRemove)) {
                this.assetGroupRepository.delete(groupsToRemove);
            }
        }
    }

    private static final Predicate<AssetGroup> COMPONENT_SAVE_FILTER = new Predicate<AssetGroup>() {
        @Override
        public boolean apply(final AssetGroup assetGroup) {
            return assetGroup != null && assetGroup.getComponent() != null;
        }
    };

    @Override
    public void cascadeTenantChanges(final Tenant tenant) {
        final List<AssetGroup> foundGroups = this.assetGroupRepository.findByTenantId(new ObjectId(tenant.getId()));
        final List<AssetGroup> updatedGroups = Lists
                .newArrayList(Iterables.transform(foundGroups, new AssetGroupTenantTransformer(tenant)));
        if (!CollectionUtils.isEmpty(updatedGroups)) {
            this.assetGroupRepository.save(updatedGroups);
        }
    }
}