org.opentestsystem.shared.progman.client.domain.TenantChain.java Source code

Java tutorial

Introduction

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

import java.io.Serializable;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.opentestsystem.shared.progman.client.domain.Tenant.TenantTypeComparator;

import com.google.common.collect.Sets;

/**
 * A TenantChain contains the search parameters used to find the chain and then an ordered Set that is the chain. The
 * chain is in reverse hierarchical order according to the TenantType ranking. Therefore, the most specific tenancy is
 * first and the most general is last.
 */
public class TenantChain implements Serializable {

    private static final long serialVersionUID = -2808024459843360214L;

    private TenantChainSearch searchInput;

    private Set<Tenant> tenants;

    public TenantChainSearch getSearchInput() {
        return this.searchInput;
    }

    public void addAllSearchInput(final Map<TenantType, String> inTypeAndValues) {
        this.searchInput = new TenantChainSearch(inTypeAndValues);
    }

    public Set<Tenant> getTenants() {
        return this.tenants;
    }

    public void setTenants(final Set<Tenant> inTenants) {
        // To ensure resting state of tenants are in order.
        final TreeSet<Tenant> setWithComparator = Sets.newTreeSet(new ReverseTenantComparator());
        if (inTenants != null) {
            setWithComparator.addAll(inTenants);
        }
        this.tenants = setWithComparator;
    }

    public void addTenantToChain(final Tenant inTenant) {
        if (this.tenants == null) {
            this.tenants = Sets.newTreeSet(new ReverseTenantComparator());
        }

        this.tenants.add(inTenant);
    }

    public static class ReverseTenantComparator implements Comparator<Tenant>, Serializable {

        private static final long serialVersionUID = 7634041221784656374L;
        private final TenantTypeComparator typeComparator = new TenantTypeComparator();

        @Override
        public int compare(final Tenant ten1, final Tenant ten2) {
            return -1 * typeComparator.compare(ten1, ten2);
        }
    }

    @Override
    public boolean equals(final Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    public Tenant getTenantById(final String inTenantId) {
        if (getTenants() != null) {
            for (Tenant tenant : getTenants()) {
                if (tenant.getId().equals(inTenantId)) {
                    return tenant;
                }
            }
        }
        return null;
    }
}