org.opentestsystem.shared.progman.transformer.TenantComponentTransformer.java Source code

Java tutorial

Introduction

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

import java.util.List;

import org.opentestsystem.shared.progman.domain.Component;
import org.opentestsystem.shared.progman.domain.Tenant;
import org.opentestsystem.shared.progman.domain.TenantSubscription;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * use function & predicate to filter & transform list matching on underlying components
 */
public final class TenantComponentTransformer implements Function<Tenant, Tenant> {
    private final Component component;
    private final boolean retain;

    private final Predicate<TenantSubscription> matchingTenantSub = new Predicate<TenantSubscription>() {
        @Override
        public boolean apply(final TenantSubscription tenantSubscription) {
            return tenantSubscription.getComponent().getId()
                    .equals(TenantComponentTransformer.this.component.getId());
        }
    };

    private final Function<TenantSubscription, TenantSubscription> tenantSubscriptionTransformer = new Function<TenantSubscription, TenantSubscription>() {
        @Override
        public TenantSubscription apply(final TenantSubscription tenantSubscription) {
            tenantSubscription.setComponent(TenantComponentTransformer.this.component);
            return tenantSubscription;
        }
    };

    public TenantComponentTransformer(final Component aComponent, final boolean retainThisComponent) {
        this.retain = retainThisComponent;
        this.component = aComponent;
    }

    @Override
    public Tenant apply(final Tenant tenant) {
        if (tenant != null && tenant.getTenantSubscriptions() != null) {
            final Iterable<TenantSubscription> matchingTenantSubscriptionList = Iterables.transform(
                    Iterables.filter(tenant.getTenantSubscriptions(), this.matchingTenantSub),
                    this.tenantSubscriptionTransformer);
            final List<TenantSubscription> tenantSubsToPreserve = Lists.newArrayList(
                    Iterables.filter(tenant.getTenantSubscriptions(), Predicates.not(this.matchingTenantSub)));
            if (this.retain) {
                Iterables.addAll(tenantSubsToPreserve, matchingTenantSubscriptionList);
            }
            tenant.setTenantSubscriptions(tenantSubsToPreserve);
        }
        return tenant;
    }
}