View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.metadata.impl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
22  import org.kuali.rice.krad.data.metadata.MetadataRepository;
23  import org.kuali.rice.krad.data.provider.MetadataProvider;
24  import org.kuali.rice.krad.data.provider.ProviderRegistry;
25  import org.springframework.beans.factory.annotation.Required;
26  
27  /**
28   * MetadataRepository implementation backed by the ProviderRegistry
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class MetadataRepositoryImpl implements MetadataRepository {
33  
34      private ProviderRegistry registry;
35  
36      @Required
37      public void setProviderRegistry(ProviderRegistry registry) {
38          this.registry = registry;
39      }
40  
41      @Override
42  	public synchronized DataObjectMetadata getMetadata(Class<?> type) {
43          // don't compose, just return first provider result
44          for (MetadataProvider provider: registry.getMetadataProviders()) {
45              if (provider.handles(type)) {
46                  return provider.getMetadataForType(type);
47              }
48          }
49          return null;
50      }
51  
52      @Override
53  	public Map<Class<?>, DataObjectMetadata> getAllMetadata() {
54  		Map<Class<?>, DataObjectMetadata> allMetaData = new HashMap<Class<?>, DataObjectMetadata>();
55          // accumulate all metadata
56          // subsequent mappings for given data object type override previous mappings
57          // so provider order matters - last one wins
58          for (MetadataProvider provider: registry.getMetadataProviders()) {
59              allMetaData.putAll(provider.provideMetadata());
60          }
61          return allMetaData;
62      }
63  
64      @Override
65      public boolean contains(Class<?> type) {
66          return getMetadata(type) != null;
67      }
68  
69  }