001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.data.metadata.impl; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.kuali.rice.krad.data.metadata.DataObjectMetadata; 022import org.kuali.rice.krad.data.metadata.MetadataRepository; 023import org.kuali.rice.krad.data.provider.MetadataProvider; 024import org.kuali.rice.krad.data.provider.ProviderRegistry; 025import org.springframework.beans.factory.annotation.Required; 026 027/** 028 * MetadataRepository implementation backed by the ProviderRegistry 029 * 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 */ 032public class MetadataRepositoryImpl implements MetadataRepository { 033 034 private ProviderRegistry registry; 035 036 @Required 037 public void setProviderRegistry(ProviderRegistry registry) { 038 this.registry = registry; 039 } 040 041 @Override 042 public synchronized DataObjectMetadata getMetadata(Class<?> type) { 043 // don't compose, just return first provider result 044 for (MetadataProvider provider: registry.getMetadataProviders()) { 045 if (provider.handles(type)) { 046 return provider.getMetadataForType(type); 047 } 048 } 049 return null; 050 } 051 052 @Override 053 public Map<Class<?>, DataObjectMetadata> getAllMetadata() { 054 Map<Class<?>, DataObjectMetadata> allMetaData = new HashMap<Class<?>, DataObjectMetadata>(); 055 // accumulate all metadata 056 // subsequent mappings for given data object type override previous mappings 057 // so provider order matters - last one wins 058 for (MetadataProvider provider: registry.getMetadataProviders()) { 059 allMetaData.putAll(provider.provideMetadata()); 060 } 061 return allMetaData; 062 } 063 064 @Override 065 public boolean contains(Class<?> type) { 066 return getMetadata(type) != null; 067 } 068 069}