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.remote.impl.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 RemoteImplWriterForOneService {
39      
40      private static Logger log = LoggerFactory.getLogger(RemoteImplWriterForOneService.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 RemoteImplWriterForOneService(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 remote impl and search unit test for " + servKey + " directory = " + directory);
72          new RemoteImplServiceWriter(model, directory, rootPackage, servKey, methods).write();
73  //        new RemoteImplServiceTestWriter(model, directory, rootPackage, servKey, methods).write();
74          
75      }
76  
77      private Set<XmlType> getXmlTypesUsedJustByService() {
78          Set<XmlType> set = new HashSet();
79          for (XmlType type : model.getXmlTypes()) {
80              if (type.getService().equalsIgnoreCase(servKey)) {
81                  if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
82                      set.add(type);
83                  }
84              }
85          }
86          return set;
87      }
88  
89      private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
90          Set<XmlType> set = new HashSet();
91          for (ServiceMethod method : methods) {
92              if (method.getReturnValue() != null) {
93                  ServiceMethodReturnValue ret = method.getReturnValue();
94                  XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
95                  if (xmlType == null) {
96                      throw new DictionaryValidationException("Method " + method.getService()
97                              + "." + method.getName()
98                              + "returns an unknown type, "
99                              + ret.getType());
100                 }
101                 addTypeAndAllSubTypes(set, xmlType);
102             }
103             for (ServiceMethodParameter param : method.getParameters()) {
104                 XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
105                 if (xmlType == null) {
106                     throw new DictionaryValidationException("Parameter "
107                             + method.getService() + "."
108                             + method.getName() + "."
109                             + param.getName()
110                             + "has an unknown type, "
111                             + param.getType());
112                 }
113                 addTypeAndAllSubTypes(set, xmlType);
114             }
115         }
116         return set;
117     }
118 
119     private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
120         if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
121             if (set.add(xmlType)) {
122                 addXmlTypesUsedByMessageStructure(set, xmlType);
123             }
124         }
125     }
126 
127     private String stripListFromType(String type) {
128         if (type.endsWith("List")) {
129             type = type.substring(0, type.length() - "List".length());
130         }
131         return type;
132     }
133 
134     private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
135             XmlType xmlType) {
136         ModelFinder finder = new ModelFinder(model);
137         for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
138             XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
139             if (subType == null) {
140                 throw new DictionaryValidationException("MessageStructure field "
141                         + ms.getId()
142                         + " has an unknown type, "
143                         + ms.getType());
144             }
145             addTypeAndAllSubTypes(set, subType);
146         }
147     }
148 }