View Javadoc
1   /*
2    * Kuali Coeus, a comprehensive research administration system for higher education.
3    * 
4    * Copyright 2005-2015 Kuali, Inc.
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.coeus.s2sgen.impl.generate;
20  
21  import org.apache.xmlbeans.XmlOptions;
22  import org.kuali.coeus.s2sgen.api.core.S2SException;
23  import org.kuali.coeus.s2sgen.api.generate.FormMappingInfo;
24  import org.kuali.coeus.s2sgen.api.generate.FormMappingService;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.beans.BeansException;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.beans.factory.annotation.Qualifier;
30  import org.springframework.context.ApplicationContext;
31  import org.springframework.stereotype.Component;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * 
38   * This class is used as a service implementation that is used to create instances of opportunity form generator classes. It
39   * provides an abstraction level over the different generator class implementations.
40   * 
41   * @author Kuali Research Administration Team (kualidev@oncourse.iu.edu)
42   */
43  @Component("s2SFormGeneratorRetrievalService")
44  public class S2SFormGeneratorRetrievalServiceImpl implements S2SFormGeneratorRetrievalService {
45      private static final Logger LOG = LoggerFactory.getLogger(S2SFormGeneratorRetrievalServiceImpl.class);
46  
47      @Autowired
48      @Qualifier("formMappingService")
49      private FormMappingService formMappingService;
50  
51      @Autowired
52      private ApplicationContext applicationContext;
53  
54      /**
55       *
56       * This method is used to create and return a form generator instance. Based on the namespace provided as parameter, it
57       * instantiates the respective generator class and returns it.
58       * 
59       * @param namespace name space of the generator.
60       * @return S2SFormGenerator form generator instances corresponding to the name space.
61       * @throws S2SException if generator could not be loaded
62       * @throws org.kuali.coeus.s2sgen.api.core.S2SException if form generator for given namespace is not available
63       */
64      public final S2SFormGenerator getS2SGenerator(String proposalNumber,String namespace) throws S2SException {
65          FormMappingInfo formInfo = formMappingService.getFormInfo(namespace,proposalNumber);
66          S2SFormGenerator formGenerator = (S2SFormGenerator) applicationContext.getBean(formInfo.getGeneratorName());
67  
68          if (formGenerator == null) {
69              throw new S2SException("Generator not found with name: " + formInfo.getGeneratorName());
70          }
71  
72          if (formGenerator instanceof DynamicNamespace) {
73              ((DynamicNamespace) formGenerator).setNamespace(namespace);
74          }
75  
76          return formGenerator;
77      }
78  
79  
80      public XmlOptions getXmlOptionsPrefixes() {
81          XmlOptions xmlOptions = new XmlOptions();
82          Map<String, String> prefixMap = new HashMap<String, String>();
83          prefixMap.put(
84                  "http://apply.grants.gov/system/MetaGrantApplication", "");
85          prefixMap.put(
86                  "http://apply.grants.gov/forms/RR_SubawardBudget-V1.0",
87                  "RR_SubawardBudget");
88          prefixMap.put(
89                  "http://apply.grants.gov/forms/RR_SubawardBudget-V1.1",
90                  "RR_SubawardBudget");
91          prefixMap.put(
92                  "http://apply.grants.gov/forms/RR_SubawardBudget-V1.2",
93                  "RR_SubawardBudget");
94          
95  
96          xmlOptions.setSaveSuggestedPrefixes(prefixMap);
97          return xmlOptions;
98      }
99  
100     public FormMappingService getFormMappingService() {
101         return formMappingService;
102     }
103 
104     public void setFormMappingService(FormMappingService formMappingService) {
105         this.formMappingService = formMappingService;
106     }
107 
108     public ApplicationContext getApplicationContext() {
109         return applicationContext;
110     }
111 
112     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
113         this.applicationContext = applicationContext;
114     }
115 }