View Javadoc

1   /**
2    * Copyright 2004-2013 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.student.contract.writer.service;
17  
18  import java.util.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.kuali.student.contract.model.MessageStructure;
23  import org.kuali.student.contract.model.ServiceContractModel;
24  import org.kuali.student.contract.model.ServiceMethod;
25  import org.kuali.student.contract.model.ServiceMethodParameter;
26  import org.kuali.student.contract.model.ServiceMethodReturnValue;
27  import org.kuali.student.contract.model.XmlType;
28  import org.kuali.student.contract.model.impl.ServiceContractModelPescXsdLoader;
29  import org.kuali.student.contract.model.util.ModelFinder;
30  import org.kuali.student.contract.model.validation.DictionaryValidationException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   *
36   * @author nwright
37   */
38  public class PureJavaInfcWriterForOneService {
39      
40      private static Logger log = LoggerFactory.getLogger(PureJavaInfcWriterForOneService.class);
41  
42      private ServiceContractModel model;
43      private ModelFinder finder;
44      private String directory;
45      private String rootPackage;
46      private String servKey;
47  
48      public PureJavaInfcWriterForOneService(ServiceContractModel model,
49              String directory,
50              String rootPackage,
51              String servKey) {
52          this.model = model;
53          this.finder = new ModelFinder(model);
54          this.directory = directory;
55          this.rootPackage = rootPackage;
56          this.servKey = servKey;
57      }
58  
59      /**
60       * Write out the entire file
61       * @param out
62       */
63      public void write() {
64          List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
65          if (methods.size() == 0) {
66              log.warn("No methods defined for servKey: " + servKey);
67              return;
68          }
69  
70          // the main servKey
71          log.info("Generating servKeys API's for " + servKey);
72          new PureJavaInfcServiceWriter(model, directory, rootPackage, servKey, methods).write();
73  
74          // the beans's
75          log.info("Generating info interfaces");
76          for (XmlType xmlType : getXmlTypesUsedJustByService()) {
77              log.info("Generating Beans for " + xmlType.getName());
78              new PureJavaInfcBeanWriter(model, directory, rootPackage, servKey, xmlType).write();
79          }
80  
81          // the Info interfaces's
82          log.info("Generating Info interfaces");
83          for (XmlType xmlType : getXmlTypesUsedJustByService()) {
84              log.info("Generating info interface for " + xmlType.getName());
85              new PureJavaInfcInfcWriter(model, directory, rootPackage, servKey, xmlType).write();
86          }
87      }
88  
89      private Set<XmlType> getXmlTypesUsedJustByService() {
90          Set<XmlType> set = new HashSet();
91          for (XmlType type : model.getXmlTypes()) {
92              if (type.getService().equalsIgnoreCase(servKey)) {
93                  if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
94                      set.add(type);
95                  }
96              }
97          }
98          return set;
99      }
100 
101     private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
102         Set<XmlType> set = new HashSet();
103         for (ServiceMethod method : methods) {
104             if (method.getReturnValue() != null) {
105                 ServiceMethodReturnValue ret = method.getReturnValue();
106                 XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
107                 if (xmlType == null) {
108                     throw new DictionaryValidationException("Method " + method.getService()
109                             + "." + method.getName()
110                             + "returns an unknown type, "
111                             + ret.getType());
112                 }
113                 addTypeAndAllSubTypes(set, xmlType);
114             }
115             for (ServiceMethodParameter param : method.getParameters()) {
116                 XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
117                 if (xmlType == null) {
118                     throw new DictionaryValidationException("Parameter "
119                             + method.getService() + "."
120                             + method.getName() + "."
121                             + param.getName()
122                             + "has an unknown type, "
123                             + param.getType());
124                 }
125                 addTypeAndAllSubTypes(set, xmlType);
126             }
127         }
128         return set;
129     }
130 
131     private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
132         if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
133             if (set.add(xmlType)) {
134                 addXmlTypesUsedByMessageStructure(set, xmlType);
135             }
136         }
137     }
138 
139     private String stripListFromType(String type) {
140         if (type.endsWith("List")) {
141             type = type.substring(0, type.length() - "List".length());
142         }
143         return type;
144     }
145 
146     private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
147             XmlType xmlType) {
148         ModelFinder finder = new ModelFinder(model);
149         for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
150             XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
151             if (subType == null) {
152                 throw new DictionaryValidationException("MessageStructure field "
153                         + ms.getId()
154                         + " has an unknown type, "
155                         + ms.getType());
156             }
157             addTypeAndAllSubTypes(set, subType);
158         }
159     }
160 }