/*******************************************************************************
 * Mission Control Technologies, Copyright (c) 2009-2012, United States Government
 * as represented by the Administrator of the National Aeronautics and Space 
 * Administration. All rights reserved.
 *
 * The MCT platform is licensed under the Apache License, Version 2.0 (the 
 * "License"); you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations under 
 * the License.
 *
 * MCT includes source code licensed under additional open source licenses. See 
 * the MCT Open Source Licenses file included with this distribution or the About 
 * MCT Licenses dialog available at runtime from the MCT Help menu for additional 
 * information. 
 *******************************************************************************/
package gov.nasa.arc.mct.scenario.component;

import gov.nasa.arc.mct.components.AbstractComponent;
import gov.nasa.arc.mct.components.Bootstrap;

/**
 * Abstract superclass for components which serve as canonical 
 * repositories for objects of other types. (e.g. User Tags, 
 * Mission Activity Types, et cetera...)
 */
public abstract class RepositoryComponent extends AbstractComponent implements RepositoryCapability, Bootstrap {
	@Override
	public <T> T handleGetCapability(Class<T> capabilityClass) {	
		return
			capabilityClass.isAssignableFrom(getClass()) ? 
					capabilityClass.cast(this) :
			super.handleGetCapability(capabilityClass);
	}

	// TODO: Use this to support the Group capability
	@Override
	public String getUserScope() {
		return getCreator(); //model.get().scope;
	}
	
	@Override
	public boolean isGlobal() {
		return getCreator().equals("*");
	}

	@Override
	public boolean isSandbox() {
		return false;
	}

	@Override
	public int categoryIndex() {
		// Categorize by package - "somewhere in the middle",
		// but grouped with other components from the same package.
		return getClass().getPackage().getName().hashCode() & 0xFFFF;
	}

	@Override
	public int componentIndex() {
		return (getClass().getName().hashCode() << 1) + (isGlobal() ? 0 : 1);
	}
}

@@ -1348,16 +1348,223 @@
 otstrap;
+%0Aimport gov.nasa.arc.mct.platform.spi.PlatformAccess;%0A%0Aimport java.util.Collection;%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Map.Entry;%0Aimport java.util.Set;
 %0A%0A/**%0A *
@@ -1845,16 +1845,1383 @@
 strap %7B%0A
+%0A%09@Override%0A%09protected void addDelegateComponentsCallback(%0A%09%09%09Collection%3CAbstractComponent%3E childComponents) %7B%0A%09%09super.addDelegateComponentsCallback(childComponents);%0A%09%09%0A%09%09Map%3CAbstractComponent, Set%3CAbstractComponent%3E%3E otherRepositories = %0A%09%09%09%09new HashMap%3CAbstractComponent, Set%3CAbstractComponent%3E%3E();%0A%09%09%0A%09%09// Build a list of things to remove%0A%09%09for (AbstractComponent child : childComponents) %7B%0A%09%09%09for (AbstractComponent parent : child.getReferencingComponents()) %7B%0A%09%09%09%09RepositoryCapability parentRepo = parent.getCapability(RepositoryCapability.class);%09%09%09%09%0A%09%09%09%09// Is another parent the same kind of repository?%0A%09%09%09%09if (parentRepo != null && %0A%09%09%09%09%09parentRepo.getCapabilityClass().isAssignableFrom(getCapabilityClass())) %7B%0A%09%09%09%09%09// Make sure we are not just looking at ourself%0A%09%09%09%09%09if (!(getComponentId().equals(parent.getComponentId()))) %7B%0A%09%09%09%09%09%09if (!otherRepositories.containsKey(parent)) %7B%0A%09%09%09%09%09%09%09otherRepositories.put(parent, new HashSet%3CAbstractComponent%3E());%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09otherRepositories.get(parent).add(child);%0A%09%09%09%09%09%7D%09%09%09%09%09%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09// Now, remove them%0A%09%09for (Entry%3CAbstractComponent, Set%3CAbstractComponent%3E%3E otherRepo : otherRepositories.entrySet()) %7B%0A%09%09%09otherRepo.getKey().removeDelegateComponents(otherRepo.getValue());%0A%09%09%7D%0A%09%09%0A%09%09// Finally, persist%0A%09%09PlatformAccess.getPlatform().getPersistenceProvider().persist(otherRepositories.keySet());%0A%09%7D%0A%0A
 %09@Overri

Remove components from other repositories on add