View Javadoc

1   /*
2    * Copyright 2010 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.osedu.org/licenses/ECL-2.0
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.util.ModelFinder;
29  import org.kuali.student.contract.model.validation.DictionaryValidationException;
30  
31  /**
32   *
33   * @author nwright
34   */
35  public class MockImplWriterForOneService {
36  
37      private ServiceContractModel model;
38      private ModelFinder finder;
39      private String directory;
40      private String rootPackage;
41      private String servKey;
42      private boolean isR1;
43  
44      public MockImplWriterForOneService(ServiceContractModel model,
45              String directory,
46              String rootPackage,
47              String servKey,
48              boolean isR1) {
49          this.model = model;
50          this.finder = new ModelFinder(model);
51          this.directory = directory;
52          this.rootPackage = rootPackage;
53          this.servKey = servKey;
54          this.isR1 = isR1;
55      }
56  
57      /**
58       * Write out the entire file
59       * @param out
60       */
61      public void write() {
62          List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
63          if (methods.size() == 0) {
64              System.out.println("No methods defined for servKey: " + servKey);
65              return;
66          }
67  
68          // the main servKey
69          System.out.println("Generating mock impls for " + servKey);
70          new MockImplServiceWriter(model, directory, rootPackage, servKey, methods, isR1).write();
71  
72      }
73  
74      private Set<XmlType> getXmlTypesUsedJustByService() {
75          Set<XmlType> set = new HashSet();
76          for (XmlType type : model.getXmlTypes()) {
77              if (type.getService().equalsIgnoreCase(servKey)) {
78                  if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
79                      set.add(type);
80                  }
81              }
82          }
83          return set;
84      }
85  
86      private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
87          Set<XmlType> set = new HashSet();
88          for (ServiceMethod method : methods) {
89              if (method.getReturnValue() != null) {
90                  ServiceMethodReturnValue ret = method.getReturnValue();
91                  XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
92                  if (xmlType == null) {
93                      throw new DictionaryValidationException("Method " + method.getService()
94                              + "." + method.getName()
95                              + "returns an unknown type, "
96                              + ret.getType());
97                  }
98                  addTypeAndAllSubTypes(set, xmlType);
99              }
100             for (ServiceMethodParameter param : method.getParameters()) {
101                 XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
102                 if (xmlType == null) {
103                     throw new DictionaryValidationException("Parameter "
104                             + method.getService() + "."
105                             + method.getName() + "."
106                             + param.getName()
107                             + "has an unknown type, "
108                             + param.getType());
109                 }
110                 addTypeAndAllSubTypes(set, xmlType);
111             }
112         }
113         return set;
114     }
115 
116     private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
117         if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
118             if (set.add(xmlType)) {
119                 addXmlTypesUsedByMessageStructure(set, xmlType);
120             }
121         }
122     }
123 
124     private String stripListFromType(String type) {
125         if (type.endsWith("List")) {
126             type = type.substring(0, type.length() - "List".length());
127         }
128         return type;
129     }
130 
131     private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
132             XmlType xmlType) {
133         ModelFinder finder = new ModelFinder(model);
134         for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
135             XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
136             if (subType == null) {
137                 throw new DictionaryValidationException("MessageStructure field "
138                         + ms.getId()
139                         + " has an unknown type, "
140                         + ms.getType());
141             }
142             addTypeAndAllSubTypes(set, subType);
143         }
144     }
145 }