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