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.commons.lang3.StringUtils;
19  import org.kuali.coeus.propdev.api.s2s.UserAttachedFormService;
20  import org.kuali.coeus.s2sgen.api.generate.FormMappingInfo;
21  import org.kuali.coeus.s2sgen.api.generate.FormMappingService;
22  import org.springframework.beans.factory.annotation.Autowired;
23  import org.springframework.beans.factory.annotation.Qualifier;
24  import org.springframework.core.io.ClassPathResource;
25  import org.springframework.stereotype.Component;
26  
27  import java.util.*;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  
31  @Component("formMappingService")
32  public class FormMappingServiceImpl implements FormMappingService {
33  
34      private static final String BASE_PACKAGE_PATH = "org/kuali/coeus/s2sgen/impl/generate";
35  
36      private Map<String, FormMappingInfo> bindings = new ConcurrentHashMap<>();
37      private Map<Integer, Set<String>> sortedNameSpaces = new ConcurrentHashMap<>();
38  
39      @Autowired
40      @Qualifier("userAttachedFormService")
41      private UserAttachedFormService userAttachedFormService;
42  
43      /**
44       * 
45       * This method is used to get the Form Information based on the given schema
46       * 
47       * @param namespace {@link String} namespace URL of the form
48       * @return {@link FormMappingInfo}containing the namespace information
49       */
50      @Override
51      public FormMappingInfo getFormInfo(String namespace) {
52  
53          if (StringUtils.isBlank(namespace)) {
54              throw new IllegalArgumentException("namespace is blank");
55          }
56  
57          return bindings.get(namespace);
58      }
59  
60      @Override
61      public FormMappingInfo getFormInfo(String namespace, String proposalNumber) {
62  
63          if (StringUtils.isBlank(namespace)) {
64              throw new IllegalArgumentException("proposalNumber is blank");
65          }
66  
67          if (StringUtils.isBlank(namespace)) {
68              throw new IllegalArgumentException("namespace is blank");
69          }
70  
71          FormMappingInfo formMappingInfo = bindings.get(namespace);
72          if (formMappingInfo != null) {
73              return formMappingInfo;
74          } else if (proposalNumber != null) {
75              return getUserAttachedForm(namespace, proposalNumber);
76          }
77  
78          return null;
79      }
80  
81      protected FormMappingInfo getUserAttachedForm(String namespace, String proposalNumber) {
82          final String formName = getUserAttachedFormService().findFormNameByProposalNumberAndNamespace(proposalNumber, namespace);
83  
84          if (formName != null) {
85              FormMappingInfo mappingInfo = new FormMappingInfo();
86              mappingInfo.setFormName(formName);
87              mappingInfo.setGeneratorName("UserAttachedFormGenerator");
88              mappingInfo.setNameSpace(namespace);
89              mappingInfo.setSortIndex(999);
90              mappingInfo.setStyleSheet(createStylesheetName(namespace));
91              mappingInfo.setUserAttachedForm(true);
92              return mappingInfo;
93          } else {
94              return null;
95          }
96          
97      }
98  
99      protected String createStylesheetName(String namespace) {
100         String[] tokens = namespace.split("/");
101         String formname = tokens[tokens.length-1];
102         try {
103             return new ClassPathResource(BASE_PACKAGE_PATH + "/support/stylesheet/"+formname+".xsl").getURL().toString();
104         } catch (Exception e) {
105             throw new RuntimeException("No resource found at " + BASE_PACKAGE_PATH + "/support/stylesheet/"+formname+".xsl",e);
106         }
107     }
108 
109     @Override
110     public Map<String, FormMappingInfo> getBindings() {
111         return bindings == null ? Collections.<String, FormMappingInfo>emptyMap() : new HashMap<>(bindings);
112     }
113 
114     @Override
115     public Map<Integer, Set<String>> getSortedNameSpaces() {
116         return sortedNameSpaces == null ? Collections.<Integer, Set<String>>emptyMap() : new HashMap<>(sortedNameSpaces);
117     }
118 
119     @Override
120     public void registerForm(FormMappingInfo info) {
121         bindings.put(info.getNameSpace(), info);
122 
123         Set<String> list = sortedNameSpaces.get(info.getSortIndex());
124         if (list == null) {
125             list = new HashSet<>();
126             sortedNameSpaces.put(info.getSortIndex(), list);
127         }
128 
129         list.add(info.getNameSpace());
130     }
131 
132     public UserAttachedFormService getUserAttachedFormService() {
133         return userAttachedFormService;
134     }
135 
136     public void setUserAttachedFormService(UserAttachedFormService userAttachedFormService) {
137         this.userAttachedFormService = userAttachedFormService;
138     }
139 }