View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.student.r2.common.datadictionary;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  import org.kuali.rice.krad.datadictionary.DataObjectEntry;
24  import org.kuali.student.r2.common.constants.CommonServiceConstants;
25  import org.kuali.student.r2.common.datadictionary.dto.DictionaryEntryInfo;
26  import org.kuali.student.r2.common.datadictionary.service.DataDictionaryService;
27  import org.kuali.student.r2.common.dto.ContextInfo;
28  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
29  import org.kuali.student.r2.common.exceptions.MissingParameterException;
30  import org.kuali.student.r2.common.exceptions.OperationFailedException;
31  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
32  import org.springframework.context.ConfigurableApplicationContext;
33  import org.springframework.context.support.ClassPathXmlApplicationContext;
34  
35  /**
36   * Dictionary Service implementation that reads the dictionary from the spring beans and feeds
37   * both the Student dictionary and the RICE dictionary
38   *
39   * @author nwright
40   */
41  public class DataDictionaryServiceImpl implements DataDictionaryService, RiceDataDictionaryServiceInfc {
42  
43      private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DataDictionaryServiceImpl.class);
44      private String serviceNamespaceSuffix;
45      private Map<String, DictionaryEntryInfo> studMap;
46      private Map<String, DataObjectEntry> riceMap;
47  
48      public DataDictionaryServiceImpl() {
49      }
50  
51      public String getServiceNamespaceSuffix() {
52          return serviceNamespaceSuffix;
53      }
54  
55      public void setServiceNamespaceSuffix(String serviceNamespaceSuffix) {
56          this.serviceNamespaceSuffix = serviceNamespaceSuffix;
57      }
58  
59  
60      public void setDictionaryLocations(List<String> locations) throws IOException {
61          studMap = new LinkedHashMap<String, DictionaryEntryInfo>();
62          riceMap = new LinkedHashMap<String, DataObjectEntry>();
63          ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext(locations.toArray(new String[locations.size()]));
64          Map<String, DataObjectEntry> beansOfType = ac.getBeansOfType(DataObjectEntry.class);
65          ac.close();
66          for (DataObjectEntry entry : beansOfType.values()) {
67              log.debug(entry.getDataObjectClass());
68              riceMap.put(entry.getFullClassName(), entry);
69              studMap.put(calcRefObjectURI (entry.getDataObjectClass()), new Rice2StudentDictionaryEntryConverter().convert(entry));
70          }
71      }
72  
73      private String calcRefObjectURI (Class<?> objectClass) {
74       return CommonServiceConstants.REF_OBJECT_URI_GLOBAL_PREFIX + this.serviceNamespaceSuffix + "/" + objectClass.getSimpleName();
75      }
76  
77      @Override
78      public DictionaryEntryInfo getDataDictionaryEntry(String entryKey, ContextInfo context)
79              throws OperationFailedException,
80              MissingParameterException,
81              PermissionDeniedException,
82              DoesNotExistException {
83          DictionaryEntryInfo entry = studMap.get(entryKey);
84          if (entry == null) {
85              throw new DoesNotExistException(entryKey);
86          }
87          return entry;
88      }
89  
90      @Override
91      public List<String> getDataDictionaryEntryKeys(ContextInfo context)
92              throws OperationFailedException,
93              MissingParameterException,
94              PermissionDeniedException {
95          return new ArrayList(studMap.keySet());
96      }
97  
98      @Override
99      public DataObjectEntry getDataObjectEntry(String entryKey) {
100         return riceMap.get(entryKey);
101     }
102 }