View Javadoc
1   /*
2    * Copyright 2005-2014 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.coeus.s2sgen.impl.generate;
17  
18  import org.apache.xmlbeans.XmlOptions;
19  import org.kuali.coeus.s2sgen.api.core.S2SException;
20  import org.kuali.coeus.s2sgen.api.generate.FormMappingInfo;
21  import org.kuali.coeus.s2sgen.api.generate.FormMappingService;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springframework.beans.BeansException;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.beans.factory.annotation.Qualifier;
27  import org.springframework.context.ApplicationContext;
28  import org.springframework.stereotype.Component;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /**
34   * 
35   * This class is used as a service implementation that is used to create instances of opportunity form generator classes. It
36   * provides an abstraction level over the different generator class implementations.
37   * 
38   * @author Kuali Research Administration Team (kualidev@oncourse.iu.edu)
39   */
40  @Component("s2SFormGeneratorRetrievalService")
41  public class S2SFormGeneratorRetrievalServiceImpl implements S2SFormGeneratorRetrievalService {
42      private static final Logger LOG = LoggerFactory.getLogger(S2SFormGeneratorRetrievalServiceImpl.class);
43  
44      @Autowired
45      @Qualifier("formMappingService")
46      private FormMappingService formMappingService;
47  
48      @Autowired
49      private ApplicationContext applicationContext;
50  
51      /**
52       *
53       * This method is used to create and return a form generator instance. Based on the namespace provided as parameter, it
54       * instantiates the respective generator class and returns it.
55       * 
56       * @param namespace name space of the generator.
57       * @return S2SFormGenerator form generator instances corresponding to the name space.
58       * @throws S2SException if generator could not be loaded
59       * @throws org.kuali.coeus.s2sgen.api.core.S2SException if form generator for given namespace is not available
60       */
61      public final S2SFormGenerator getS2SGenerator(String proposalNumber,String namespace) throws S2SException {
62          FormMappingInfo formInfo = formMappingService.getFormInfo(namespace,proposalNumber);
63          S2SFormGenerator formGenerator = (S2SFormGenerator) applicationContext.getBean(formInfo.getGeneratorName());
64  
65          if (formGenerator == null) {
66              throw new S2SException("Generator not found with name: " + formInfo.getGeneratorName());
67          }
68  
69          if (formGenerator instanceof DynamicNamespace) {
70              ((DynamicNamespace) formGenerator).setNamespace(namespace);
71          }
72  
73          return formGenerator;
74      }
75  
76  
77      public XmlOptions getXmlOptionsPrefixes() {
78          XmlOptions xmlOptions = new XmlOptions();
79          Map<String, String> prefixMap = new HashMap<String, String>();
80          prefixMap.put(
81                  "http://apply.grants.gov/system/MetaGrantApplication", "");
82          prefixMap.put(
83                  "http://apply.grants.gov/forms/RR_SubawardBudget-V1.0",
84                  "RR_SubawardBudget");
85          prefixMap.put(
86                  "http://apply.grants.gov/forms/RR_SubawardBudget-V1.1",
87                  "RR_SubawardBudget");
88          prefixMap.put(
89                  "http://apply.grants.gov/forms/RR_SubawardBudget-V1.2",
90                  "RR_SubawardBudget");
91          
92  
93          xmlOptions.setSaveSuggestedPrefixes(prefixMap);
94          return xmlOptions;
95      }
96  
97      public FormMappingService getFormMappingService() {
98          return formMappingService;
99      }
100 
101     public void setFormMappingService(FormMappingService formMappingService) {
102         this.formMappingService = formMappingService;
103     }
104 
105     public ApplicationContext getApplicationContext() {
106         return applicationContext;
107     }
108 
109     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
110         this.applicationContext = applicationContext;
111     }
112 }